List of usage examples for javax.xml.transform.stream StreamSource StreamSource
public StreamSource(File f)
From source file:Main.java
public static void getWordDocument1(ByteArrayOutputStream recvstram, String filename, String xslfilename) throws TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError { try {/* w ww . j av a 2s . co m*/ byte[] myBytes = recvstram.toByteArray(); FileOutputStream out = new FileOutputStream("g:/input.txt"); try { out.write(myBytes); } finally { out.close(); } (TransformerFactory.newInstance().newTransformer(new StreamSource(new File("g:/" + xslfilename)))) .transform(new StreamSource(new File("g:/input.txt")), new StreamResult(new File("g:/channel.doc"))); } catch (Exception e) { } }
From source file:Main.java
/** * Converts an XML file to an XSL-FO file using JAXP (XSLT). * @param xml the XML file/*w ww . j ava2 s .c o m*/ * @param xslt the stylesheet file * @param fo the target XSL-FO file * @throws IOException In case of an I/O problem * @throws TransformerException In case of a XSL transformation problem */ public static void convertXML2FO(InputStream xml, File xslt, File fo) { //Setup output OutputStream out = null; try { out = new java.io.FileOutputStream(fo); //Setup XSLT TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(xslt)); //Setup input for XSLT transformation Source src = new StreamSource(xml); //Resulting SAX events (the generated FO) must be piped through to FOP Result res = new StreamResult(out); //Start XSLT transformation and FOP processing transformer.transform(src, res); } catch (Exception ex) { ex.printStackTrace(); } finally { try { out.close(); } catch (Exception e) { } } }
From source file:Main.java
public static String getFormattedXml(String xmlString) { // /////////////////////////////////////////////////////////////// // Declarations // /////////////////////////////////////////////////////////////// Source xmlInput = null;//from w w w .ja v a 2s . c om StringWriter stringWriter = null; StreamResult xmlOutput = null; TransformerFactory transformerFactory = null; Transformer transformer = null; String formattedXml = null; // /////////////////////////////////////////////////////////////// // Code // /////////////////////////////////////////////////////////////// try { xmlInput = new StreamSource(new StringReader(xmlString)); stringWriter = new StringWriter(); xmlOutput = new StreamResult(stringWriter); transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); formattedXml = xmlOutput.getWriter().toString(); } catch (Exception e) { // To Do: Handle Exception.. } return formattedXml; }
From source file:Main.java
/** * Apply the XSLT transformation (<code>xsltStream</code>) to the xml given * (<code>inputXmlStream</code>) and write the output to * <code>outputStream</code> * /*w w w .ja v a 2 s. co m*/ * @param xsltStream * @param xsltParameters * @param inputXmlStream * @param outputStream * @return the {@link OutputStream} with the transformation output. * @throws TransformerException */ public static OutputStream applyTransformation(InputStream xsltStream, Map<String, Object> xsltParameters, InputStream inputXmlStream, OutputStream outputStream) throws TransformerException { return applyTransformation(new StreamSource(xsltStream), xsltParameters, inputXmlStream, outputStream); }
From source file:Main.java
protected static Result internalTransform(InputStream doc, Templates templates, Result r, boolean trace) { StringWriter sw = new StringWriter(); try {//from w w w . j a v a 2 s . co m Transformer transformer = templates.newTransformer(); transformer.transform(new StreamSource(doc), r); sw.close(); return r; } catch (Throwable th) { th.printStackTrace(); return r; } }
From source file:Main.java
/** * Transforms an XML file using an XSL file and no parameters. * @param docFile the file containing the XML to transform. * @param xslFile the file containing the XSL transformation program. * @return the transformed DOM Document. *///from w w w . jav a 2s . c om public static Document getTransformedDocument(File docFile, File xslFile) throws Exception { String[] nullString = {}; return getTransformedDocument(new StreamSource(docFile), new StreamSource(xslFile), nullString); }
From source file:Main.java
public static <T extends Object> T unmarshalFromString(Class clz, String input) throws JAXBException { JAXBContext context = JAXBContext.newInstance(clz); Unmarshaller unmarshaller = context.createUnmarshaller(); T unobj = (T) unmarshaller.unmarshal(new StreamSource(new StringReader(input.toString()))); return unobj; }
From source file:Main.java
public static String prettyFormat(String input, int indent) { try {/* www . j ava2s .co m*/ Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); // simple exception handling, please review it } }
From source file:Util.java
/** * Applies a stylesheet to a given xml document. * /* w w w . jav a 2 s . co m*/ * @param xmlDocument * the xml document to be transformed * @param xsltFilename * the filename of the stylesheet * @return the transformed xml document * @throws Exception */ public static String transformDocumentAsString(Document xmlDocument, String xsltFilename) throws Exception { // Generate a Transformer. Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFilename)); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); // Perform the transformation. transformer.transform(new DOMSource(xmlDocument), streamResult); // Now you can get the output Node from the DOMResult. return stringWriter.toString(); }
From source file:Main.java
private static Transformer getTransformerFromText(String xsltContent) throws TransformerConfigurationException { if (xsltContent == null) { return getDefaultTransformer(); } else {/* www . java2s . c o m*/ checkTransformFactory(); return transformerFactory.newTransformer(new StreamSource(new StringReader(xsltContent))); } }