Example usage for javax.xml.transform.stream StreamSource StreamSource

List of usage examples for javax.xml.transform.stream StreamSource StreamSource

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamSource StreamSource.

Prototype

public StreamSource(File f) 

Source Link

Document

Construct a StreamSource from a File.

Usage

From source file:Main.java

public static void transformDocument(Document doc, String xsl, String targetFile) {
    try {//from w  ww  .ja  va  2  s  .  c o m
        Source source = new DOMSource(doc);

        // Creation of the output file
        File file = new File(targetFile);
        Result result = new StreamResult(file);

        // configuration of the transformer
        TransformerFactory factoryT = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(xsl);
        Transformer transformer;

        transformer = factoryT.newTransformer(stylesource);
        transformer.setOutputProperty(OutputKeys.METHOD, "text");

        // Transformation
        transformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void transformDocument(final Document doc, final String xsl, final String targetFile) {
    try {//from   ww w . ja  v a  2s .  c  o m
        final Source source = new DOMSource(doc);

        // Creation of the output file
        final File file = new File(targetFile);
        final Result result = new StreamResult(file);

        // configuration of the transformer
        final TransformerFactory factoryT = TransformerFactory.newInstance();
        final StreamSource stylesource = new StreamSource(xsl);
        Transformer transformer;

        transformer = factoryT.newTransformer(stylesource);
        transformer.setOutputProperty(OutputKeys.METHOD, "text");

        // Transformation
        transformer.transform(source, result);
    } catch (final TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (final TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * Perform an xsl transformation//from   w ww .  j a v a 2 s  . co m
 */
public static void transform(InputStream xsl, InputStream xml, Writer result) throws Exception {
    transform(new StreamSource(xsl), new StreamSource(xml), new StreamResult(result));
}

From source file:Main.java

protected static Result internalTransform(Reader doc, Templates templates, Result r, boolean trace) {
    StringWriter sw = new StringWriter();

    try {//from   w  ww.j  a  v  a2s . c o 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

/**
 * Validates the given document with its schema.
 * // w w w .  j av a  2 s.co  m
 * @param document - The XML file contents
 * @param schemaFileStream - the inputStream of the schema content
 * @throws Exception
 */
public static void validateDocument(Document document, InputStream schemaFileStream) throws Exception {
    // create a SchemaFactory capable of understanding WXS schemas
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    // load a WXS schema, represented by a Schema instance
    Source schemaFile = new StreamSource(schemaFileStream);
    Schema schema = factory.newSchema(schemaFile);
    // create a Validator instance, which can be used to validate an
    // instance document
    Validator validator = schema.newValidator();
    // validate the DOM tree
    validator.validate(new DOMSource(document));
}

From source file:Main.java

public final static void applyXSLTTransform(final Reader xslt, final Reader input, final Writer output)
        throws TransformerException {
    final Transformer transformer = transformerFactory.newTransformer(new StreamSource(xslt));

    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, OUTPUT_PROPERTY_YES);
    transformer.setOutputProperty(OutputKeys.INDENT, OUTPUT_PROPERTY_NO);

    transformer.transform(new StreamSource(input), new StreamResult(output));
}

From source file:Main.java

/**
 * //from w w w.j av  a2s .com
 * @param header Just a title for the stanza for readability.  Single word no spaces since
 * it is inserted as the root element in the output.
 * @param xml The string to pretty print
 */
static public void prettyPrint(String header, String xml) {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");

        if (header != null) {
            xml = "\n<" + header + ">" + xml + "</" + header + '>';
        }
        transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(System.out));
    } catch (Exception e) {
        System.out.println("Something wrong with xml in \n---------------\n" + xml + "\n---------------");
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Deserialize the XML content of a stream into a Java object of the specified type.
 *
 * @param docClass    The class of the object to unmarshall the XML as
 * @param inputStream An input stream containing the XML to deserialize
 * @param <T>         The type of object to unmarshall the XML as
 * @return The deserialized object/*from   w  w w . j a va  2 s.c o m*/
 * @throws JAXBException if an error occurs during deserialization
 */
public static <T> T deSerialize(Class<T> docClass, InputStream inputStream) throws JAXBException {
    return deSerialize(docClass, new StreamSource(inputStream));
}

From source file:Main.java

/**
 * Transform input xml given a stylesheet.
 * /*w  w w  . j  a  va  2  s.  c om*/
 * @param styleSheet the style-sheet
 * @param xml input xml data
 * @param out output
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
public static void transform(InputStream styleSheet, InputStream xml, Writer out)
        throws TransformerConfigurationException, TransformerException {
    // Instantiate a TransformerFactory
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Use the TransformerFactory to process the  
    // stylesheet and generate a Transformer
    Transformer transformer = tFactory.newTransformer(new StreamSource(styleSheet));

    // Use the Transformer to transform an XML Source 
    // and send the output to a Result object.
    transformer.transform(new StreamSource(xml), new StreamResult(out));
}

From source file:com.sdl.odata.renderer.util.PrettyPrinter.java

/**
 * Pretty-print a given XML.//  www  . j  a  v  a  2  s  .  c  o  m
 *
 * @param xml The not-formatted XML.
 * @return The pretty-printed XML.
 */
public static String prettyPrintXml(String xml) throws TransformerException, IOException {

    Source xmlInput = new StreamSource(new StringReader(xml));
    try (StringWriter stringWriter = new StringWriter()) {
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", DEFAULT_INDENT);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
        transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);

        return xmlOutput.getWriter().toString();
    }
}