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:no.digipost.signature.client.core.internal.xml.Marshalling.java

public static Object unmarshal(InputStream entityStream) {
    return Jaxb2MarshallerHolder.unmarshaller.unmarshal(new StreamSource(entityStream));
}

From source file:net.sf.mcf2pdf.mcfelements.util.PdfUtil.java

/**
 * Converts an FO file to a PDF file using Apache FOP.
 *
 * @param fo the FO file/*from   w ww . j ava 2  s.  co  m*/
 * @param pdf the target PDF file
 * @param dpi the DPI resolution to use for bitmaps in the PDF
 * @throws IOException In case of an I/O problem
 * @throws FOPException In case of a FOP problem
 * @throws TransformerException In case of XML transformer problem
 */
@SuppressWarnings("rawtypes")
public static void convertFO2PDF(InputStream fo, OutputStream pdf, int dpi)
        throws IOException, FOPException, TransformerException {

    FopFactory fopFactory = FopFactory.newInstance();

    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    // configure foUserAgent as desired
    foUserAgent.setTargetResolution(dpi);

    // Setup output stream. Note: Using BufferedOutputStream
    // for performance reasons (helpful with FileOutputStreams).
    OutputStream out = new BufferedOutputStream(pdf);

    // Construct fop with desired output format
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

    // Setup JAXP using identity transformer
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(); // identity
                                                        // transformer

    // Setup input stream
    Source src = new StreamSource(fo);

    // Resulting SAX events (the generated FO) must be piped through to FOP
    Result res = new SAXResult(fop.getDefaultHandler());

    // Start XSLT transformation and FOP processing
    transformer.transform(src, res);

    // Result processing
    FormattingResults foResults = fop.getResults();
    java.util.List pageSequences = foResults.getPageSequences();
    for (java.util.Iterator it = pageSequences.iterator(); it.hasNext();) {
        PageSequenceResults pageSequenceResults = (PageSequenceResults) it.next();
        log.debug("PageSequence "
                + (String.valueOf(pageSequenceResults.getID()).length() > 0 ? pageSequenceResults.getID()
                        : "<no id>")
                + " generated " + pageSequenceResults.getPageCount() + " pages.");
    }
    log.info("Generated " + foResults.getPageCount() + " PDF pages in total.");
    out.flush();
}

From source file:Main.java

private static Transformer getThreadedTransformer(final boolean omitXmlDeclaration, final boolean standalone,
        final Map threadMap, final String xslURL) throws TransformerConfigurationException {
    final Thread currentThread = Thread.currentThread();
    Transformer transformer = null;
    if (threadMap != null) {
        transformer = (Transformer) threadMap.get(currentThread);
    }//from  ww w .  java2s  . c om
    if (transformer == null) {
        if (xslURL == null) {
            transformer = tFactory.newTransformer(); // "never null"
        } else {
            transformer = tFactory.newTransformer(new StreamSource(xslURL));
        }
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        if (threadMap != null) {
            threadMap.put(currentThread, transformer);
        }
    }
    transformer.setOutputProperty(OutputKeys.STANDALONE, standalone ? "yes" : "no");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
    return transformer;
}

From source file:net.geoprism.gis.sld.SLDValidator.java

public void validate(String sld) {
    try {//from  w  w  w.  ja  va 2s.  com
        SchemaFactory f = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source xsdS = new StreamSource(xsd);
        Schema schema = f.newSchema(xsdS);
        Validator v = schema.newValidator();

        InputStream stream;
        stream = new ByteArrayInputStream(sld.getBytes("UTF-8"));
        Source s = new StreamSource(stream);
        v.validate(s);
    } catch (Throwable e) {
        log.error(sld, e);
        throw new ProgrammingErrorException("Invalid SLD", e);
    }
}

From source file:jlib.xml.XMLUtil.java

public static Document loadXML(ByteArrayInputStream byteArrayInputStream) {
    try {/*from  w  w  w . j  ava 2s  .  c o  m*/
        StreamSource streamSource = new StreamSource(byteArrayInputStream);
        DocumentBuilderFactory dbf = DocumentBuilderFactoryImpl.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();
        Result res = new DOMResult(doc);
        TransformerFactory tr = TransformerFactory.newInstance();
        Transformer xformer = tr.newTransformer();
        xformer.transform(streamSource, res);

        return doc;
    } catch (Exception e) {
        String csError = e.toString();
        Log.logImportant(csError);
        Log.logImportant(
                "ERROR while loading XML from byteArrayInputStream " + byteArrayInputStream.toString());
    }
    return null;
}

From source file:org.intalio.tempo.test.SpringTestSupport.java

protected Source getSourceFromClassPath(String fileOnClassPath) {
    InputStream stream = getClass().getResourceAsStream(fileOnClassPath);
    Assert.assertNotNull("Could not find file: " + fileOnClassPath + " on the classpath", stream);

    Source content = new StreamSource(stream);
    return content;
}

From source file:org.socialhistoryservices.pid.service.MappingsServiceImp.java

public MappingsServiceImp() {
    try {/*from   w  w w .  j  a  va 2s . com*/
        templates = TransformerFactory.newInstance()
                .newTemplates(new StreamSource(MappingsServiceImp.class.getResourceAsStream("/locations.xsl")));
    } catch (TransformerConfigurationException e) {
        log.fatal(e);
        System.exit(-1);
    }
}

From source file:com.elsevier.xml.XSLTProcessor.java

/**
 * Transform the content using the specified stylesheet.
 * /*from   www .j  a  va 2s .c  o m*/
 * @param stylesheetName name of the stylesheet (that was set in init)
 * @param content the xml to be transformed
 * @return transformed content
 */
public static String transform(String stylesheetName, String content) {

    try {

        // Get the stylesheet from the cache
        String stylesheet = stylesheetMap.get(stylesheetName);

        if (stylesheet == null) {
            log.error("Problems finding the stylesheet.  STYLESHEET_NAME: " + stylesheetName);
            return "</error>";
        }

        StreamSource stylesheetSource = new StreamSource(IOUtils.toInputStream(stylesheet, CharEncoding.UTF_8));

        // Create streamsource for the content
        StreamSource contentSource = new StreamSource(IOUtils.toInputStream(content, CharEncoding.UTF_8));

        // Apply transformation
        return transform(contentSource, stylesheetSource);

    } catch (IOException e) {

        log.error("Problems transforming the content.  STYLESHEET_NAME: " + stylesheetName + "  "
                + e.getMessage(), e);
        return "</error>";

    }

}

From source file:org.jasig.portlet.contacts.adapters.impl.xml.XMLContactAdapter.java

@Override
public Set<ContactSet> getContacts() {
    Set<ContactSet> contacts = new TreeSet<ContactSet>();
    InputStream is = null;//from www . jav a  2 s. c o  m

    for (String dataURI : dataURIs) {
        ContactSet contactSet;

        try {
            is = getClass().getClassLoader().getResourceAsStream(dataURI);
            contactSet = (ContactSet) unmarshaller.unmarshal(new StreamSource(is));
            contacts.add(contactSet);
        } catch (Exception ex) {
            Logger.getLogger(XMLContactAdapter.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ex) {
                    Logger.getLogger(XMLContactAdapter.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    return contacts;
}

From source file:gov.nih.nci.cabig.caaers.utils.XsltTransformer.java

/**
 * //from   w  w  w.ja  v a  2 s  . c  o m
 * @param inXml
 * @param xsltFile
 * @return
 * @throws Exception
 */
public String toText(String inXml, String xsltFile) throws Exception {
    Resource[] resources = getResources(xsltFile);
    if (resources != null && resources.length > 0) {
        InputStream stream = resources[0].getInputStream();

        Source xmlSource = new StreamSource(new ByteArrayInputStream(inXml.getBytes()));
        // File xslt = new File(xsltFile);

        Source xsltSource = new StreamSource(stream);

        StringWriter outStr = new StringWriter();
        StreamResult result = new StreamResult(outStr);

        // the factory pattern supports different XSLT processors
        TransformerFactory transFact = TransformerFactory.newInstance();
        Transformer trans = transFact.newTransformer(xsltSource);

        trans.transform(xmlSource, result);// transform(xmlSource, new StreamResult(System.out));

        return outStr.toString();
    }

    return "";

}