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

/**
 * Reads a file.//from w w w  .  j a  v a2s  .  co m
 * @param systemId the system id
 * @return the content
 * @throws Exception if an exception occurs
 */
public static String readFile(String systemId) throws Exception {
    /*
    String s = new String(Files.readAllBytes(Paths.get(path)),"UTF-8");
    if (s != null) s = s.trim();
    return s;
     */
    StringWriter result = new StringWriter();
    transform(new StreamSource(systemId), new StreamResult(result), true);
    return checkResult(result, true);
}

From source file:Main.java

/**
 * Transforms an XML file using an XSL file and an array of parameters.
 * The parameter array consists of a sequence of pairs of (String parametername)
 * followed by (Object parametervalue) in an Object[].
 * @param docFile the file containing the XML to transform.
 * @param xslFile the file containing the XSL transformation program.
 * @param params the array of transformation parameters.
 * @return the transformed DOM Document.
 *///from  w  ww .  j av a  2  s .  c  o m
public static Document getTransformedDocument(File docFile, File xslFile, Object[] params) throws Exception {
    return getTransformedDocument(new StreamSource(docFile), new StreamSource(xslFile), params);
}

From source file:Main.java

public static synchronized void deserialize(Source source, Result result, InputStream xsltSource)
        throws TransformerConfigurationException, JAXBException, TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer;/*from  w w w .ja  va 2 s .c o m*/
    transformer = factory.newTransformer(new StreamSource(xsltSource));
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * This method will validate a non-niem xml instance.
 * /*from  ww w.  j av a 2  s  .  co m*/
 * @param xsdPath - this is a relative path to an xsd, typically in OJB_Utilies or in the enclosing project
 * @param xml - This is the XML document as a string
 * @throws Exception - typically a validation exception or a file path exception
 */

public static void validateInstanceNonNIEMXsd(String xsdPath, String xml) throws Exception {

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(new File(xsdPath));
    Validator validator = schema.newValidator();
    validator.validate(new StreamSource(new StringReader(xml)));
}

From source file:Main.java

/**
 * Apply the XSLT transformation (<code>xslt</code>) to the XML given (
 * <code>inputXmlStream</code>) and write the output to
 * <code>outputStream</code>
 * //from w w  w .j  av  a 2 s  .  c o  m
 * @param xslt
 * @param xsltParameters
 * @param inputXmlStream
 * @param outputStream
 * 
 * @return the {@link OutputStream} with the transformation output.
 * 
 * @throws TransformerException
 */
public static OutputStream applyTransformation(String xslt, Map<String, Object> xsltParameters,
        InputStream inputXmlStream, OutputStream outputStream) throws TransformerException {

    return applyTransformation(new StreamSource(new StringReader(xslt)), xsltParameters, inputXmlStream,
            outputStream);
}

From source file:XsltDomServlet.java

public void init() throws ServletException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {/*from  w  w w .ja  v a 2  s  .c  o  m*/
        DocumentBuilder db = dbf.newDocumentBuilder();
        dom = db.getDOMImplementation();
    } catch (ParserConfigurationException pce) {
        throw new ServletException(pce);
    }

    // prepare the XSLT transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    StreamSource ss = new StreamSource("/var/www/stylesheets/paramTable.xslt");
    try {
        transformer = tf.newTransformer(ss);
    } catch (TransformerConfigurationException tce) {
        throw new ServletException(tce);
    }
}

From source file:Main.java

/**
 * This method validate XML by XML File and XSD File.
 *
 * @param xml input XML File// ww w .j a  va2  s  .  c o  m
 * @param xsd input XSD File
 *
 * @return true or false, valid or not
 */
public static boolean validateXMLByXSD(File xml, File xsd) {
    try {
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(xsd).newValidator()
                .validate(new StreamSource(xml));
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * Applies a stylesheet (that receives parameters) to a given xml document.
 * /*  w w w . java  2 s .  c  o  m*/
 * @param xmlDocument
 *            the xml document to be transformed
 * @param parameters
 *            the hashtable with the parameters to be passed to the
 *            stylesheet
 * @param xsltFilename
 *            the filename of the stylesheet
 * @return the transformed xml document
 * @throws Exception
 */
public static Document transformDocument(Document xmlDocument, Map<String, String> parameters,
        String xsltFilename) throws Exception {

    // Generate a Transformer.
    Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFilename));

    // set transformation parameters
    if (parameters != null) {
        for (Map.Entry<String, String> param : parameters.entrySet()) {
            transformer.setParameter(param.getKey(), param.getValue());
        }

    }

    // Create an empty DOMResult object for the output.
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
    dFactory.setNamespaceAware(true);
    DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
    Document dstDocument = dBuilder.newDocument();

    DOMResult domResult = new DOMResult(dstDocument);

    // Perform the transformation.
    transformer.transform(new DOMSource(xmlDocument), domResult);
    // Now you can get the output Node from the DOMResult.
    return dstDocument;
}

From source file:Main.java

/**
 * Applies a stylesheet (that receives parameters) to a given xml document.
 *
 * @param xmlDocument  the xml document to be transformed
 * @param parameters   the hashtable with the parameters to be passed to the
 *                     stylesheet/*from w  w w.j a  v  a 2  s.  c o  m*/
 * @param xsltFilename the filename of the stylesheet
 * @return the transformed xml document
 * @throws Exception
 */
public static Document transformDocument(Document xmlDocument, Map<String, String> parameters,
        String xsltFilename) throws TransformerException, ParserConfigurationException {

    File xslFile = new File(xsltFilename);
    if (xslFile.exists()) {

        // Generate a Transformer.
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer(new StreamSource(xsltFilename));

        if (transformer != null) {

            // set transformation parameters
            if (parameters != null) {
                for (Map.Entry<String, String> param : parameters.entrySet()) {
                    transformer.setParameter(param.getKey(), param.getValue());
                }
            }

            // Create an empty DOMResult object for the output.
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            dFactory.setNamespaceAware(true);
            DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
            Document dstDocument = dBuilder.newDocument();

            DOMResult domResult = new DOMResult(dstDocument);

            // Perform the transformation.
            transformer.transform(new DOMSource(xmlDocument), domResult);
            // Now you can get the output Node from the DOMResult.
            return dstDocument;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Transform an XML DOM Document using an XSL file and an array of parameters.
 * The parameter array consists of a sequence of pairs of (String parametername)
 * followed by (Object parametervalue) in an Object[].
 * @param doc the document to transform.
 * @param xsl the XSL transformation program.
 * @param params the array of transformation parameters.
 * @return the transformed DOM Document.
 *//* w  w  w.j a  va  2s . co  m*/
public static Document getTransformedDocument(Document doc, File xsl, Object[] params) throws Exception {
    return getTransformedDocument(new DOMSource(doc), new StreamSource(xsl), params);
}