List of usage examples for javax.xml.transform Source getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:com.xpn.xwiki.pdf.impl.PDFURIResolverTest.java
@Test public void resolveWhenMapItemExists() throws Exception { DocumentReference documentReference = new DocumentReference("wiki", "space", "page"); AttachmentReference attachmentReference = new AttachmentReference("fileName", documentReference); com.xpn.xwiki.XWiki xwiki = mock(com.xpn.xwiki.XWiki.class); XWikiDocument document = mock(XWikiDocument.class); XWikiAttachment attachment = mock(XWikiAttachment.class); XWikiContext context = mock(XWikiContext.class); // Add an encoded space character to test that the URL is not decoded. The input URL (to be resolved) is // expected to be encoded because the URL factory creates encoded URLs. String url = "encoded+url"; when(context.get("pdfExportImageURLMap")).thenReturn(Collections.singletonMap(url, attachmentReference)); when(context.getWiki()).thenReturn(xwiki); when(xwiki.getDocument(attachmentReference.extractReference(EntityType.DOCUMENT), context)) .thenReturn(document);/*from w w w . ja va2s .c o m*/ when(document.getAttachment("fileName")).thenReturn(attachment); when(attachment.getContentInputStream(context)).thenReturn(new ByteArrayInputStream("content".getBytes())); PDFURIResolver resolver = new PDFURIResolver(context); Source source = resolver.resolve(url, "base"); Assert.assertEquals(StreamSource.class, source.getClass()); Assert.assertEquals("content", IOUtils.readLines(((StreamSource) source).getInputStream()).get(0)); }
From source file:be.e_contract.mycarenet.consent.ConsentClient.java
/** * Invokes a method on the eHealthBox consultation web service using the * low-level SOAP payload.// w w w . java2s. c om * * @param request * @return */ public String invoke(String request) { Source responseSource = this.dispatch.invoke(new StreamSource(new StringReader(request))); LOG.debug("response Source type: " + responseSource.getClass().getName()); return toString(responseSource); }
From source file:be.e_contract.mycarenet.ehbox.EHealthBoxPublicationClient.java
public String invoke(String request) { Source responseSource = this.publicationDispatch.invoke(new StreamSource(new StringReader(request))); LOG.debug("response Source type: " + responseSource.getClass().getName()); return toString(responseSource); }
From source file:be.e_contract.mycarenet.ehbox.EHealthBoxConsultationClient.java
/** * Invokes a method on the eHealthBox consultation web service using the * low-level SOAP payload.// w w w . jav a 2 s . c om * * @param request * @return */ public String invoke(String request) { Source responseSource = this.consultationDispatch.invoke(new StreamSource(new StringReader(request))); LOG.debug("response Source type: " + responseSource.getClass().getName()); return toString(responseSource); }
From source file:nl.nn.adapterframework.validation.xerces_2_11.XMLSchemaFactory.java
public Schema newSchema(Source[] schemas) throws SAXException { // this will let the loader store parsed Grammars into the pool. XMLGrammarPoolImplExtension pool = new XMLGrammarPoolImplExtension(); fXMLGrammarPoolWrapper.setGrammarPool(pool); XMLInputSource[] xmlInputSources = new XMLInputSource[schemas.length]; InputStream inputStream;/* w w w . ja v a2s .c o m*/ Reader reader; for (int i = 0; i < schemas.length; ++i) { Source source = schemas[i]; if (source instanceof StreamSource) { StreamSource streamSource = (StreamSource) source; String publicId = streamSource.getPublicId(); String systemId = streamSource.getSystemId(); inputStream = streamSource.getInputStream(); reader = streamSource.getReader(); XMLInputSource xmlInputSource = new XMLInputSource(publicId, systemId, null); xmlInputSource.setByteStream(inputStream); xmlInputSource.setCharacterStream(reader); xmlInputSources[i] = xmlInputSource; } else if (source instanceof SAXSource) { SAXSource saxSource = (SAXSource) source; InputSource inputSource = saxSource.getInputSource(); if (inputSource == null) { throw new SAXException(JAXPValidationMessageFormatter .formatMessage(fXMLSchemaLoader.getLocale(), "SAXSourceNullInputSource", null)); } xmlInputSources[i] = new SAXInputSource(saxSource.getXMLReader(), inputSource); } else if (source instanceof DOMSource) { DOMSource domSource = (DOMSource) source; Node node = domSource.getNode(); String systemID = domSource.getSystemId(); xmlInputSources[i] = new DOMInputSource(node, systemID); } // else if (source instanceof StAXSource) { // StAXSource staxSource = (StAXSource) source; // XMLEventReader eventReader = staxSource.getXMLEventReader(); // if (eventReader != null) { // xmlInputSources[i] = new StAXInputSource(eventReader); // } // else { // xmlInputSources[i] = new StAXInputSource(staxSource.getXMLStreamReader()); // } // } else if (source == null) { throw new NullPointerException(JAXPValidationMessageFormatter .formatMessage(fXMLSchemaLoader.getLocale(), "SchemaSourceArrayMemberNull", null)); } else { throw new IllegalArgumentException( JAXPValidationMessageFormatter.formatMessage(fXMLSchemaLoader.getLocale(), "SchemaFactorySourceUnrecognized", new Object[] { source.getClass().getName() })); } } try { fXMLSchemaLoader.loadGrammar(xmlInputSources); } catch (XNIException e) { // this should have been reported to users already. throw Util.toSAXException(e); } catch (IOException e) { // this hasn't been reported, so do so now. SAXParseException se = new SAXParseException(e.getMessage(), null, e); if (fErrorHandler != null) { fErrorHandler.error(se); } throw se; // and we must throw it. } // Clear reference to grammar pool. fXMLGrammarPoolWrapper.setGrammarPool(null); // Select Schema implementation based on grammar count. final int grammarCount = pool.getGrammarCount(); AbstractXMLSchema schema = null; if (fUseGrammarPoolOnly) { throw new NotImplementedException("fUseGrammarPoolOnly"); // if (grammarCount > 1) { // schema = new XMLSchemaHack(new ReadOnlyGrammarPool(pool)); // } // else if (grammarCount == 1) { // Grammar[] grammars = pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA); // schema = new XMLSchemaHack(grammars[0]); // } // else { // schema = new XMLSchemaHack(); // } } else { schema = new XMLSchema(new ReadOnlyGrammarPool(pool), false); } propagateFeatures(schema); return schema; }
From source file:org.apache.axis2.jaxws.message.databinding.impl.SourceBlockImpl.java
/** * Constructor called from factory/*from ww w.j a v a 2 s.c o m*/ * * @param busObject * @param qName * @param factory */ SourceBlockImpl(Source busObject, QName qName, BlockFactory factory) throws WebServiceException { super(busObject, null, qName, factory); // Check validity of Source if (busObject instanceof DOMSource || busObject instanceof SAXSource || busObject instanceof StreamSource || (busObject.getClass().equals(staxSource)) || busObject instanceof JAXBSource) { // Okay, these are supported Source objects if (log.isDebugEnabled()) { log.debug("data object is a " + busObject.getClass().getName()); } } else { throw ExceptionFactory.makeWebServiceException( Messages.getMessage("SourceNotSupported", busObject.getClass().getName())); } }
From source file:org.apache.ode.utils.DOMUtils.java
/** * Parse an XML document located using an {@link InputSource} using the * pooled document builder./*from w ww . ja v a 2s . c om*/ */ public static Document sourceToDOM(Source inputSource) throws IOException { try { /* // Requires JDK 1.6+ if (inputSource instanceof StAXSource) { StAXSource stax = (StAXSource) inputSource; //if (stax.getXMLEventReader() != null || sax.getXMLStreamReader() != null) { if (sax.getXMLStreamReader() != null) { return parse(stax.getXMLStreamReader()); } } */ if (inputSource instanceof SAXSource) { InputSource sax = ((SAXSource) inputSource).getInputSource(); if (sax.getCharacterStream() != null || sax.getByteStream() != null) { return parse(((SAXSource) inputSource).getInputSource()); } } if (inputSource instanceof DOMSource) { Node node = ((DOMSource) inputSource).getNode(); if (node != null) { return toDOMDocument(node); } } if (inputSource instanceof StreamSource) { StreamSource stream = (StreamSource) inputSource; if (stream.getReader() != null || stream.getInputStream() != null) { return toDocumentFromStream((StreamSource) inputSource); } } DOMResult domresult = new DOMResult(newDocument()); Transformer txer = getTransformer(); txer.transform(inputSource, domresult); return (Document) domresult.getNode(); } catch (SAXException e) { throwIOException(e); } catch (TransformerException e) { throwIOException(e); } throw new IllegalArgumentException("Cannot parse XML source: " + inputSource.getClass()); }
From source file:org.dhatim.delivery.dom.SmooksDOMFilter.java
protected void doFilter(Source source, Result result) { if (!(source instanceof StreamSource) && !(source instanceof DOMSource) && !(source instanceof JavaSource)) { throw new IllegalArgumentException( source.getClass().getName() + " Source types not yet supported by the DOM Filter."); }//from w w w. j a va 2s . co m if (!(result instanceof FilterResult)) { if (result != null && !(result instanceof StreamResult) && !(result instanceof DOMResult)) { throw new IllegalArgumentException( result.getClass().getName() + " Result types not yet supported by the DOM Filter."); } } try { Node resultNode; // Filter the Source.... if (source instanceof DOMSource) { Node node = ((DOMSource) source).getNode(); if ((node instanceof Document)) { resultNode = filter((Document) node); } else if ((node instanceof Element)) { resultNode = filter((Element) node); } else { throw new IllegalArgumentException( "DOMSource Source types must contain a Document or Element node."); } } else { resultNode = filter(source); } // Populate the Result if (result instanceof StreamResult) { StreamResult streamResult = ((StreamResult) result); Writer writer = getWriter(streamResult, executionContext); try { serialize(resultNode, writer); writer.flush(); } catch (IOException e) { logger.debug("Error writing result to output stream.", e); } } else if (result instanceof DOMResult) { ((DOMResult) result).setNode(resultNode); } } finally { if (closeSource) { close(source); } if (closeResult) { close(result); } } }
From source file:org.dhatim.delivery.sax.SmooksSAXFilter.java
protected void doFilter(Source source, Result result) { if (source instanceof DOMSource) { String serializedDOM = XmlUtil.serialize(((DOMSource) source).getNode(), false); source = new StringSource(serializedDOM); if (logger.isDebugEnabled()) { logger.debug("DOMSource converted to a StringSource."); }/*from w w w . j ava 2 s . c om*/ } if (!(source instanceof StreamSource) && !(source instanceof JavaSource)) { throw new IllegalArgumentException(source.getClass().getName() + " Source types not yet supported by the SAX Filter. Only supports StreamSource and JavaSource at present."); } if (!(result instanceof FilterResult)) { if (!(result instanceof StreamResult) && result != null) { throw new IllegalArgumentException(result.getClass().getName() + " Result types not yet supported by the SAX Filter. Only supports StreamResult at present."); } } try { Writer writer = parser.parse(source, result, executionContext); writer.flush(); } catch (TerminateException e) { if (logger.isDebugEnabled()) { if (e.isTerminateBefore()) { logger.debug("Terminated filtering on visitBefore of element '" + SAXUtil.getXPath(e.getElement()) + "'."); } else { logger.debug("Terminated filtering on visitAfter of element '" + SAXUtil.getXPath(e.getElement()) + "'."); } } } catch (Exception e) { throw new SmooksException("Failed to filter source.", e); } finally { if (closeSource) { close(source); } if (closeResult) { close(result); } } }
From source file:org.jboss.bpm.console.server.util.DOMUtils.java
public static Element sourceToElement(Source source) throws IOException { Element retElement = null;// ww w. j a va 2s . co m try { if (source instanceof StreamSource) { StreamSource streamSource = (StreamSource) source; InputStream ins = streamSource.getInputStream(); if (ins != null) { retElement = DOMUtils.parse(ins); } else { Reader reader = streamSource.getReader(); retElement = DOMUtils.parse(new InputSource(reader)); } } else if (source instanceof DOMSource) { DOMSource domSource = (DOMSource) source; Node node = domSource.getNode(); if (node instanceof Element) { retElement = (Element) node; } else if (node instanceof Document) { retElement = ((Document) node).getDocumentElement(); } else { throw new RuntimeException("Unsupported Node type: " + node.getClass().getName()); } } else if (source instanceof SAXSource) { // The fact that JAXBSource derives from SAXSource is an implementation detail. // Thus in general applications are strongly discouraged from accessing methods defined on SAXSource. // The XMLReader object obtained by the getXMLReader method shall be used only for parsing the InputSource object returned by the getInputSource method. TransformerFactory tf = TransformerFactory.newInstance(); ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(source, new StreamResult(baos)); retElement = DOMUtils.parse(new ByteArrayInputStream(baos.toByteArray())); } else { throw new RuntimeException("Source type not implemented: " + source.getClass().getName()); } } catch (TransformerException ex) { IOException ioex = new IOException(); ioex.initCause(ex); throw ioex; } return retElement; }