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

/**
 * 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 text.//from   ww w  . java  2s.c  om
 */
public static String getTransformedText(Document doc, File xsl, Object[] params) throws Exception {
    return getTransformedText(new DOMSource(doc), new StreamSource(xsl), params);
}

From source file:com.shopzilla.api.service.ProductService.java

public ProductResponse xmlInputStreamToJava(InputStream in) throws IOException, JAXBException {
    try {/*from w  ww. j a v  a2 s  . co  m*/

        ProductResponse productResponse = (ProductResponse) unmarshaller.unmarshal(new StreamSource(in));
        System.out.println("productResponse = " + productResponse);
        return productResponse;

    } catch (XmlMappingException xme) {
        xme.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Transforms a memory document with XML format into an string according an
 * XSL file, and stores it in a file/* ww  w .j  a v  a2  s.c o m*/
 * 
 * @param doc
 *            The document to read
 * @param xslFile
 *            The name of the XSL file which allows transformate XML into
 *            String according its style
 * 
 * @return String value of a DOM
 * 
 * @throws FileNotFoundException
 *             java.io.FileNotFoundException
 * @throws TransformerException
 *             javax.xml.transform.TransformerException
 */
public static String write_DOM_into_an_String(Document doc, String xslFile)
        throws FileNotFoundException, TransformerException {
    // An instance of a object transformer factory
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Creates a transformer object associated to the XSL file
    Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile));

    // Holds a tree with the information of a document in the form of a DOM
    // tree
    DOMSource sourceId = new DOMSource(doc);

    // Makes the transformation from the source in XML to the output in
    // stream according the transformer in XSL
    ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
    transformer.transform(sourceId, new StreamResult(bAOS));

    // Returns the string value of the stream
    return bAOS.toString();
}

From source file:SimpleXMLTransform.java

public void transform(String inXML, String inXSL, String outTXT)
        throws TransformerConfigurationException, TransformerException {

    TransformerFactory factory = TransformerFactory.newInstance();

    StreamSource xslStream = new StreamSource(inXSL);
    Transformer transformer = factory.newTransformer(xslStream);
    transformer.setErrorListener(new MyErrorListener());

    StreamSource in = new StreamSource(inXML);
    StreamResult out = new StreamResult(outTXT);
    transformer.transform(in, out);//from   w w  w. j  a va2 s . co m
}

From source file:com.google.code.docbook4j.VfsURIResolver.java

public Source resolve(String href, String base) throws TransformerException {
    log.debug("Resolving href={} for base={}", href, base);

    try {/*from  ww  w  .j  a  v  a2s.  co m*/

        FileObject urlFileObject = FileObjectUtils.resolveFile(href, base);
        return new StreamSource(urlFileObject.getContent().getInputStream());

    } catch (FileSystemException e) {

        log.warn("Error resolving href=" + href + " for base=" + base, e);
        return null;
    }
}

From source file:Main.java

public static void printStream(InputStream is) {
    StreamSource source = new StreamSource(is);
    String msgString = null;// w  w  w  .  j  a  v  a 2  s .  c  o  m
    try {
        Transformer xFormer = TransformerFactory.newInstance().newTransformer();
        xFormer.setOutputProperty("omit-xml-declaration", "yes");
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        Result result = new StreamResult(outStream);
        xFormer.transform(source, result);
        outStream.writeTo(System.out);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * Transforms a memory document with XML format into an string according an
 * XSL/*from w w w. j  a  v  a2  s .c  o  m*/
 * 
 * @param doc
 *            The document to read
 * @param iS_xsl
 *            An InputStream with the XSL which allows transformate XML into
 *            String
 * 
 * @return An String with the DOM transformated
 * 
 * @throws FileNotFoundException
 *             java.io.FileNotFoundException
 * @throws TransformerException
 *             javax.xml.transform.TransformerException
 */
public static String write_DOM_into_an_String_With_An_XSL_InputStream(Document doc, InputStream iS_xsl)
        throws FileNotFoundException, TransformerException {
    // An instance of a object transformer factory
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Creates a transformer object associated to the XSL file
    Transformer transformer = tFactory.newTransformer(new StreamSource(iS_xsl));

    // Holds a tree with the information of a document in the form of a DOM
    // tree
    DOMSource sourceId = new DOMSource(doc);

    // Makes the transformation from the source in XML to the output in
    // stream according the transformer in XSL
    ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
    transformer.transform(sourceId, new StreamResult(bAOS));

    // Returns the string value of the stream
    return bAOS.toString();
}

From source file:Main.java

public static void transform(InputStream doc, URL xsltUrl, OutputStream out)
        throws URISyntaxException, TransformerException {
    StreamSource source = new StreamSource(doc);
    StreamSource xslt = new StreamSource(new File(xsltUrl.toURI()));
    TransformerFactory fac = TransformerFactory.newInstance();
    Transformer t = fac.newTransformer(xslt);
    Result result = new StreamResult(out);
    t.transform(source, result);/*from w ww. j ava 2  s .  c  om*/
}

From source file:Main.java

/**
 * Transforms a memory document with XML format into an string according an
 * XSL//from   w  w w  .ja v  a2 s  .c o m
 * 
 * @param doc
 *            The document to read
 * @param xsl
 *            An string with the XSL which allows transformate XML into
 *            String
 * 
 * @return An String with the DOM transformated
 * 
 * @throws FileNotFoundException
 *             java.io.FileNotFoundException
 * @throws TransformerException
 *             javax.xml.transform.TransformerException
 */
public static String write_DOM_into_an_String_With_An_XSL_String(Document doc, String xsl)
        throws FileNotFoundException, TransformerException {
    // An instance of a object transformer factory
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Creates a transformer object associated to the XSL file
    Transformer transformer = tFactory
            .newTransformer(new StreamSource(new ByteArrayInputStream(xsl.getBytes())));

    // Holds a tree with the information of a document in the form of a DOM
    // tree
    DOMSource sourceId = new DOMSource(doc);

    // Makes the transformation from the source in XML to the output in
    // stream according the transformer in XSL
    ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
    transformer.transform(sourceId, new StreamResult(bAOS));

    // Returns the string value of the stream
    return bAOS.toString();
}

From source file:com.cisco.cta.taxii.adapter.persistence.SimpleFileHandler.java

@Override
public TaxiiStatus load() {
    try {//from ww  w .  jav a 2  s  .c o  m
        Source source = new StreamSource(file);
        return (TaxiiStatus) marshaller.unmarshal(source);
    } catch (UnmarshallingFailureException e) {
        if (e.contains(FileNotFoundException.class)) {
            log.info("File {} doesn't exist yet", file);
            return new TaxiiStatus();
        } else {
            throw e;
        }
    }
}