public static void main(String[] args) throws Exception { File xsltFile = new File("mardownXSLT.xslt"); Source xmlSource = new StreamSource(new StringReader(theHTML)); Source xsltSource = new StreamSource(xsltFile); TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans = transFact.newTransformer(xsltSource); StringWriter result = new StringWriter(); trans.transform(xmlSource, new StreamResult(result)); }
/** * Transform summary information to HTML. * * @param htmlWriter * the Writer to write the HTML output to */ public void transformSummaryToHTML(Writer htmlWriter) throws IOException, TransformerException { ByteArrayOutputStream summaryOut = new ByteArrayOutputStream(8096); reportSummary(summaryOut); StreamSource in = new StreamSource(new ByteArrayInputStream(summaryOut.toByteArray())); StreamResult out = new StreamResult(htmlWriter); InputStream xslInputStream = this.getClass().getClassLoader().getResourceAsStream("summary.xsl"); if (xslInputStream == null) { throw new IOException("Could not load summary stylesheet"); } StreamSource xsl = new StreamSource(xslInputStream); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(xsl); transformer.transform(in, out); Reader rdr = in.getReader(); if (rdr != null) { rdr.close(); } htmlWriter.close(); InputStream is = xsl.getInputStream(); if (is != null) { is.close(); } }
private static InputSource streamSourceToInputSource( StreamSource ss ) { InputSource is = new InputSource(); is.setSystemId( ss.getSystemId() ); is.setByteStream( ss.getInputStream() ); is.setCharacterStream( ss.getReader() ); return is; }
@Override protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException { if (streamSource.getInputStream() != null) { return unmarshalInputStream(streamSource.getInputStream()); } else if (streamSource.getReader() != null) { return unmarshalReader(streamSource.getReader()); } else { throw new IllegalArgumentException("StreamSource contains neither InputStream nor Reader"); } }
/** * Attempt to obtain a SAX InputSource object from a Source * object. * * @param source Must be a non-null Source reference. * * @return An InputSource, or null if Source can not be converted. */ public static InputSource sourceToInputSource(Source source) { if (source instanceof SAXSource) { return ((SAXSource) source).getInputSource(); } else if (source instanceof StreamSource) { StreamSource ss = (StreamSource) source; InputSource isource = new InputSource(ss.getSystemId()); isource.setByteStream(ss.getInputStream()); isource.setCharacterStream(ss.getReader()); isource.setPublicId(ss.getPublicId()); return isource; } else { return null; } } }
static boolean validateAgainstXSD(InputStream xml, InputStream xsd) { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new StreamSource(xsd)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(xml)); return true; } catch(Exception ex) { return false; } }
public static String transform(String stylesheet, String srcXMLString, HashMap<String, String> params) throws Exception { StringWriter writer = new StringWriter(); StreamResult result = new StreamResult( writer ); Source src = new StreamSource( new StringReader( srcXMLString ) ); transform( stylesheet, src, result, params ); return writer.toString(); }
private Source getSource(Object payload) { if (payload instanceof byte[]) { return new StreamSource(new ByteArrayInputStream((byte[]) payload)); } else { return new StreamSource(new StringReader((String) payload)); } }
try m_tfactory = TransformerFactory.newInstance(); this.showStatus("Causing Transformer and Parser to Load and JIT..."); StringReader xmlbuf = new StringReader("<?xml version='1.0'?><foo/>"); StringReader xslbuf = new StringReader( "<?xml version='1.0'?><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'><xsl:template match='foo'><out/></xsl:template></xsl:stylesheet>"); PrintWriter pw = new PrintWriter(new StringWriter()); Templates templates = m_tfactory.newTemplates(new StreamSource(xslbuf)); Transformer transformer = templates.newTransformer(); transformer.transform(new StreamSource(xmlbuf), new StreamResult(pw));
argv[++i], ObjectFactory.findClassLoader(), true); tfactory.setURIResolver(uriResolver); stylesheet = tfactory.newTemplates(new StreamSource(xslFileName)); strResult = new StreamResult(new FileOutputStream(outFileName)); strResult.setSystemId(outFileName); strResult = new StreamResult(System.out); stf.getAssociatedStylesheet(new StreamSource(inFileName), media, null, null); transformer.setErrorListener(new DefaultErrorHandler(false)); transformer.setOutputProperty(OutputKeys.METHOD, outputType); transformer.transform(new StreamSource(inFileName), result); transformer.transform(new StreamSource(inFileName), strResult); new StringReader("<?xml version=\"1.0\"?> <doc/>"); transformer.transform(new StreamSource(reader), strResult);
private org.iso_relax.verifier.Schema compileSchema(Source s) throws IOException, VerifierConfigurationException, SAXException, TransformerException { if( s instanceof StreamSource ) { StreamSource ss = (StreamSource)s; InputSource is = new InputSource(); is.setByteStream(ss.getInputStream()); is.setCharacterStream(ss.getReader()); is.setSystemId(ss.getSystemId()); is.setPublicId(ss.getPublicId()); return core.compileSchema(is); } // we can't directly parse SAX and DOM into JARV, // so turn it into stream first. StringWriter w = new StringWriter(); Transformer t = TransformerFactory.newInstance().newTransformer(); t.transform(s,new StreamResult(w)); return compileSchema(new StreamSource(new StringReader(w.toString()))); }
data.factory = TransformerFactory.newInstance(); transformer.setOutputProperties( data.outputProperties ); transformer.setParameter( data.nameOfParams[i], row[data.indexOfParams[i]] ); Source source = new StreamSource( new StringReader( xmlValue ) ); StreamResult result = new StreamResult( new StringWriter() ); transformer.transform( source, result ); String xmlString = result.getWriter().toString(); if ( log.isDetailed() ) { logDetailed( BaseMessages.getString( PKG, "Xslt.Log.FileResult" ) );
@Test public void streamWriterSource() throws Exception { StringWriter stringWriter = new StringWriter(); XMLStreamWriter streamWriter = inputFactory.createXMLStreamWriter(stringWriter); Reader reader = new StringReader(XML); Source source = new StreamSource(reader); StaxResult result = new StaxResult(streamWriter); assertEquals("Invalid streamWriter returned", streamWriter, result.getXMLStreamWriter()); assertNull("EventWriter returned", result.getXMLEventWriter()); transformer.transform(source, result); assertThat("Invalid result", stringWriter.toString(), isSimilarTo(XML)); }
public XmlSchemaValidator(MockServerLogger mockServerLogger, String schema) { this.mockServerLogger = mockServerLogger; try { if (schema.trim().endsWith(">") || Strings.isNullOrEmpty(schema)) { this.schema = schemaFactory.newSchema(new StreamSource(new StringReader(schema))); } else if (schema.trim().endsWith(".xsd")) { this.schema = schemaFactory.newSchema(new StreamSource(FileReader.openReaderToFileFromClassPathOrPath(schema))); } else { throw new IllegalArgumentException("Schema must either be a path reference to a *.xsd file or an xml string"); } } catch (SAXException e) { throw new IllegalArgumentException("Schema is not valid", e); } catch (FileNotFoundException e) { throw new IllegalArgumentException("Schema file not found", e); } }
@Override public void writeTo(Source source, Class<?> t, Type gt, Annotation[] as, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { try { if (source instanceof StreamSource) { StreamSource stream = (StreamSource) source; InputSource inputStream = new InputSource(stream.getInputStream()); inputStream.setCharacterStream(inputStream.getCharacterStream()); inputStream.setPublicId(stream.getPublicId()); inputStream.setSystemId(source.getSystemId()); source = new SAXSource(saxParserFactory.get().newSAXParser().getXMLReader(), inputStream); } StreamResult sr = new StreamResult(entityStream); transformerFactory.get().newTransformer().transform(source, sr); } catch (SAXException ex) { throw new InternalServerErrorException(ex); } catch (ParserConfigurationException ex) { throw new InternalServerErrorException(ex); } catch (TransformerException ex) { throw new InternalServerErrorException(ex); } } }
private void transformAndMarshal(Object graph, Result result) throws IOException { try { ByteArrayOutputStream os = new ByteArrayOutputStream(1024); marshalOutputStream(graph, os); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); Transformer transformer = this.transformerFactory.newTransformer(); transformer.transform(new StreamSource(is), result); } catch (TransformerException ex) { throw new MarshallingFailureException( "Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex); } }
private String upgrade(String originalContent, URL upgradeScript) { try (InputStream xslt = upgradeScript.openStream()) { ByteArrayOutputStream convertedConfig = new ByteArrayOutputStream(); transformer(upgradeScript.getPath(), xslt) .transform(new StreamSource(new ByteArrayInputStream(originalContent.getBytes())), new StreamResult(convertedConfig)); return convertedConfig.toString(); } catch (TransformerException e) { throw bomb("Couldn't transform configuration file using upgrade script " + upgradeScript.getPath(), e); } catch (IOException e) { throw bomb("Couldn't write converted config file", e); } }
private Transformer transformer(String xsltName, InputStream xslt) { try { return TransformerFactory.newInstance().newTransformer(new StreamSource(xslt)); } catch (TransformerConfigurationException tce) { throw bomb("Couldn't parse XSL template " + xsltName, tce); } }
public StoreDefinitionsMapper() { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source source = new StreamSource(StoreDefinitionsMapper.class.getResourceAsStream("stores.xsd")); this.schema = factory.newSchema(source); } catch(SAXException e) { throw new MappingException(e); } }
@Test public void unmarshalStreamSourceInputStream() throws Exception { StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8"))); Object flights = unmarshaller.unmarshal(source); testFlight(flights); }