private Source getSource(Object payload) { if (payload instanceof byte[]) { return new StreamSource(new ByteArrayInputStream((byte[]) payload)); } else { return new StreamSource(new StringReader((String) payload)); } }
import javax.xml.transform.*; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; public class TestMain { public static void main(String[] args) throws IOException, URISyntaxException, TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Source xslt = new StreamSource(new File("transform.xslt")); Transformer transformer = factory.newTransformer(xslt); Source text = new StreamSource(new File("input.xml")); transformer.transform(text, new StreamResult(new File("output.xml"))); } }
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); } }
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(); }
@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 void startup() throws Exception { setWarnClient(true); // !! where should that be decided? if (stylesheet == null) { TransformerFactory tFactory = TransformerFactory.newInstance(); StreamSource stylesheetStream = new StreamSource(this.getClass().getResourceAsStream("sable-to-mary.xsl")); stylesheet = tFactory.newTemplates(stylesheetStream); } if (dbFactory == null) { dbFactory = DocumentBuilderFactory.newInstance(); } if (docBuilder == null) { docBuilder = dbFactory.newDocumentBuilder(); } super.startup(); }
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); } }
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 createNewTemplate( String xslSource, boolean isAfile ) throws Exception { FileObject file = null; InputStream xslInputStream = null; Transformer transformer = null; try { if ( isAfile ) { file = KettleVFS.getFileObject( xslSource ); xslInputStream = KettleVFS.getInputStream( file ); } else { xslInputStream = new ByteArrayInputStream( xslSource.getBytes( "UTF-8" ) ); } // Use the factory to create a template containing the xsl source transformer = factory.newTransformer( new StreamSource( xslInputStream ) ); // Add transformer to cache transformers.put( xslSource, transformer ); return transformer; } finally { try { if ( file != null ) { file.close(); } if ( xslInputStream != null ) { xslInputStream.close(); } } catch ( Exception e ) { /* Ignore */ } } }
boolean success = false; try { XMLUtils.safeTransform((Source)new StreamSource(xml), new StreamResult(configXml));
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)); }
/** * Unmarshal the given {@link TextMessage} into an object. * @param message the message * @param unmarshaller the unmarshaller to use * @return the unmarshalled object * @throws JMSException if thrown by JMS methods * @throws IOException in case of I/O errors * @throws XmlMappingException in case of OXM mapping errors * @see Unmarshaller#unmarshal(Source) */ protected Object unmarshalFromTextMessage(TextMessage message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException { Source source = new StreamSource(new StringReader(message.getText())); return unmarshaller.unmarshal(source); }
public static String generateLayoutTree(Source myList, Templates layoutTreeCreatorXslt) throws Exception { // Generate hierarchical layout tree System.out.println("Generating layout tree.."); ByteArrayOutputStream layoutBAOS = new ByteArrayOutputStream(); Result layoutResult = new StreamResult(layoutBAOS); XmlUtils.transform(myList, layoutTreeCreatorXslt, null, layoutResult); // tmpXslStr = layoutBAOS.toString("UTF-8"); // System.out.println(tmpXslStr); ByteArrayOutputStream layoutBAOS2 = new ByteArrayOutputStream(); Result layoutResult2 = new StreamResult(layoutBAOS2); XmlUtils.transform( new javax.xml.transform.stream.StreamSource( new java.io.StringReader(layoutBAOS.toString("UTF-8"))), xsltGenerateIds, null, layoutResult2); return layoutBAOS2.toString("UTF-8"); }
@Test public void eventWriterSource() throws Exception { StringWriter stringWriter = new StringWriter(); XMLEventWriter eventWriter = inputFactory.createXMLEventWriter(stringWriter); Reader reader = new StringReader(XML); Source source = new StreamSource(reader); StaxResult result = new StaxResult(eventWriter); assertEquals("Invalid eventWriter returned", eventWriter, result.getXMLEventWriter()); assertNull("StreamWriter returned", result.getXMLStreamWriter()); transformer.transform(source, result); assertThat("Invalid result", stringWriter.toString(), isSimilarTo(XML)); }
public void startup() throws Exception { setWarnClient(true); // !! where should that be decided? if (stylesheet == null) { TransformerFactory tFactory = TransformerFactory.newInstance(); StreamSource stylesheetStream = new StreamSource(this.getClass().getResourceAsStream("ssml-to-mary.xsl")); stylesheet = tFactory.newTemplates(stylesheetStream); } if (dbFactory == null) { dbFactory = DocumentBuilderFactory.newInstance(); } if (docBuilder == null) { docBuilder = dbFactory.newDocumentBuilder(); } super.startup(); }
private static Transformer getTransformer(String stylesheet) throws Exception { Transformer transformer = null; InputStream xslStream = null; try { InputStream in = XSLTransformation.class.getResourceAsStream( stylesheet ); xslStream = new BufferedInputStream( in ); StreamSource src = new StreamSource( xslStream ); src.setSystemId( stylesheet ); transformer = TransformerFactory.newInstance().newTransformer( src ); } finally { if ( xslStream != null ) xslStream.close(); } return transformer; } }
public Properties generate(File[] allTestFiles, String uploadDestPath) { FileOutputStream transformedHtml = null; File mergedResults = new File(folderToUpload.getAbsolutePath() + FileUtil.fileseparator() + TEST_RESULTS_FILE); File mergedResource = null; FileInputStream mergedFileStream = null; try { mergedResource = mergeAllTestResultToSingleFile(allTestFiles); transformedHtml = new FileOutputStream(mergedResults); try { mergedFileStream = new FileInputStream(mergedResource); Source xmlSource = new StreamSource(mergedFileStream); StreamResult result = new StreamResult(transformedHtml); templates.newTransformer().transform(xmlSource, result); } catch (Exception e) { publisher.reportErrorMessage("Unable to publish test properties. Error was " + e.getMessage(), e); } extractProperties(mergedResults); publisher.upload(mergedResults, uploadDestPath); return null; } catch (Exception e) { publisher.reportErrorMessage("Unable to publish test properties. Error was " + e.getMessage(), e); } finally { IOUtils.closeQuietly(mergedFileStream); IOUtils.closeQuietly(transformedHtml); if (mergedResource != null) { mergedResource.delete(); } } return new Properties(); }
@Override public ValidationResult validate(final String subject, final String input, final ValidationContext validationContext) { final Tuple<String, ValidationResult> lastResult = this.cachedResult; if (lastResult != null && lastResult.getKey().equals(input)) { return lastResult.getValue(); } else { String error = null; final File stylesheet = new File(input); final TransformerFactory tFactory = new net.sf.saxon.TransformerFactoryImpl(); final StreamSource styleSource = new StreamSource(stylesheet); try { tFactory.newTransformer(styleSource); } catch (final Exception e) { error = e.toString(); } this.cachedResult = new Tuple<>(input, new ValidationResult.Builder() .input(input) .subject(subject) .valid(error == null) .explanation(error) .build()); return this.cachedResult.getValue(); } } }
try { StringReader reader = new StringReader("<xml>blabla</xml>"); StringWriter writer = new StringWriter(); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer( new javax.xml.transform.stream.StreamSource("styler.xsl")); transformer.transform( new javax.xml.transform.stream.StreamSource(reader), new javax.xml.transform.stream.StreamResult(writer)); String result = writer.toString(); } catch (Exception e) { e.printStackTrace(); }
private static StreamSource asStreamSource(String xmlString) { return new StreamSource(new StringReader(xmlString)); }