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:edu.wisc.hrs.dao.bnsumm.SoapBenefitSummaryDaoTest.java

@Test
public void testDataMapping() throws Exception {
    final InputStream xmlStream = this.getClass().getResourceAsStream("/hrs/bnsumm.xml");
    assertNotNull(xmlStream);/*from w w  w. j  a v  a  2s .c  o  m*/

    final GetCompIntfcUWPORTAL1BNSUMMResponse response = (GetCompIntfcUWPORTAL1BNSUMMResponse) this.unmarshaller
            .unmarshal(new StreamSource(xmlStream));

    final BenefitSummary benefitSummary = client.convertBenefitSummary(response);
    verifyMappedData(benefitSummary);
}

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

public LocAttType getLocations(Handle handle) {

    final StreamSource xmlSource = new StreamSource(new ByteArrayInputStream(handle.getData()));
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    final Result result = new StreamResult(os);

    try {/*from   www  .  j ava 2  s. co m*/
        templates.newTransformer().transform(xmlSource, result);
    } catch (TransformerException e) {
        log.error("Handle: " + handle.getHandle());
        log.error(e);
        return null;
    }

    final StreamSource source = new StreamSource(new ByteArrayInputStream(os.toByteArray()));
    final JAXBElement element = (JAXBElement) marshaller.unmarshal(source);
    return (LocAttType) element.getValue();
}

From source file:edu.unc.lib.dl.schematron.SchematronValidator.java

/**
 * Check whether a given document conforms to a known schema.
 *
 * @param resource//  w w  w .ja v a 2 s  . c  o m
 *                a Spring resource that retrieves an XML stream
 * @param schema
 *                name of schema to use
 * @return true if document conforms to schema
 */
public boolean isValid(Resource resource, String schema) throws IOException {
    Source source = new StreamSource(resource.getInputStream());
    return this.isValid(source, schema);
}

From source file:com.twinsoft.convertigo.engine.requesters.PdfServletRequester.java

protected Object performXSLT(Document document) throws Exception {
    String t1 = context.statistics.start(EngineStatistics.XSLT);

    try {//from   ww w  . j av a 2s.  co  m
        Engine.logEngine.debug("XSL/FO process is beginning");

        Object result = null;

        Engine.logEngine.debug("Sheet absolute URL: " + context.absoluteSheetUrl);
        if (context.absoluteSheetUrl == null)
            throw new EngineException(
                    "You have required an XSL/FO process, but Convertigo has been unable to find a stylesheet for your request. Verify your project's settings.");

        // XSL/FO engine
        StreamSource streamSource = null;
        try {
            // Configure foUserAgent as desired
            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            foUserAgent.setBaseURL(
                    new File(Engine.PROJECTS_PATH + "/" + context.projectName).toURI().toASCIIString());
            foUserAgent.setAuthor("Convertigo EMS");

            // Setup output
            org.apache.commons.io.output.ByteArrayOutputStream out = new org.apache.commons.io.output.ByteArrayOutputStream();

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

            // Setup XSLT
            streamSource = new StreamSource(new File(context.absoluteSheetUrl).toURI().toASCIIString());
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(streamSource);

            // Setup input for XSLT transformation
            Element element = document.getDocumentElement();
            DOMSource src = new DOMSource(element);

            // 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 = out.toByteArray();
        } finally {
            if (streamSource != null) {
                InputStream inputStream = streamSource.getInputStream();
                if (inputStream != null)
                    inputStream.close();
            }
        }

        Engine.logEngine.trace("XSLT result:\n" + result);

        return result;
    } finally {
        Engine.logEngine.debug("XSLT process has finished");
        context.statistics.stop(t1);
    }
}

From source file:com.cisco.cta.taxii.adapter.AdapterConfiguration.java

@Bean
public Templates templates() throws Exception {
    return transformerFactory().newTemplates(new StreamSource(transformSettings.getStylesheet()));
}

From source file:edu.wisc.hrs.dao.msstime.SoapManagerTimeDaoTest.java

@Test
public void testDataMapping() throws Exception {
    final InputStream xmlStream = this.getClass().getResourceAsStream("/hrs/msstime.xml");
    assertNotNull(xmlStream);//from w w w . j  ava 2  s  .  c  om

    final GetCompIntfcUWPORTAL1MSSTIMEResponse response = (GetCompIntfcUWPORTAL1MSSTIMEResponse) this.unmarshaller
            .unmarshal(new StreamSource(xmlStream));

    final List<ManagedTime> managedTimes = client.convertManagedTimes(response);
    verifyMappedData(managedTimes);
}

From source file:edu.wisc.hrs.dao.mssabs.SoapManagerAbsenceDaoTest.java

@Test
public void testDataMapping() throws Exception {
    final InputStream xmlStream = this.getClass().getResourceAsStream("/hrs/mssabs.xml");
    assertNotNull(xmlStream);/*from  www .j  a v  a2s . co  m*/

    final GetCompIntfcUWPORTAL1MSSABSResponse response = (GetCompIntfcUWPORTAL1MSSABSResponse) this.unmarshaller
            .unmarshal(new StreamSource(xmlStream));

    final List<ManagedAbsence> managedAbsences = client.convertManagedAbsences(response);
    verifyMappedData(managedAbsences);
}

From source file:net.sf.ginp.util.GinpUtil.java

/**
 *
 * @param stylePath the classpath resource name of the transforming xsl doc
 * @param visitDoc the doc to transform// w  ww  .  ja  v  a  2  s  .  c  o m
 * @return the transformed document
 * @throws TransformerException
 */
public static Document transform(final String stylePath, final Document visitDoc) throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory
            .newTransformer(new StreamSource(GinpUtil.class.getResourceAsStream(stylePath)));
    DocumentResult result = new DocumentResult();
    transformer.transform(new DocumentSource(visitDoc), result);

    return result.getDocument();
}

From source file:com.twinsoft.convertigo.engine.requesters.JavelinServletRequester.java

protected Object performXSLT(Document document) throws Exception {
    String t1 = context.statistics.start(EngineStatistics.XSLT);

    try {/*from www.j  a v a 2  s  .  c o m*/
        Engine.logEngine.debug("XSL/FO process is beginning");

        Object result = null;

        Engine.logEngine.debug("Sheet absolute URL: " + context.absoluteSheetUrl);
        if (context.absoluteSheetUrl == null)
            throw new EngineException(
                    "You have required an XSL/FO process, but Convertigo has been unable to find a stylesheet for your request. Verify your project's settings.");

        // XSL/FO engine
        StreamSource streamSource = null;
        try {
            // Construct/Configure a FopFactory
            FopFactory fopFactory = FopFactory.newInstance();
            fopFactory.setBaseURL(Engine.TEMPLATES_PATH);

            // Configure foUserAgent as desired
            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            foUserAgent.setBaseURL(Engine.PROJECTS_PATH + "/" + context.projectName);
            foUserAgent.setAuthor("Convertigo EMS");

            // Setup output
            org.apache.commons.io.output.ByteArrayOutputStream out = new org.apache.commons.io.output.ByteArrayOutputStream();

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

            // Setup XSLT
            streamSource = new StreamSource(new File(context.absoluteSheetUrl).toURI().toASCIIString());
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(streamSource);

            // Setup input for XSLT transformation
            Element element = document.getDocumentElement();
            DOMSource src = new DOMSource(element);

            // 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 = out.toByteArray();
        } finally {
            if (streamSource != null) {
                InputStream inputStream = streamSource.getInputStream();
                if (inputStream != null)
                    inputStream.close();
            }
        }

        Engine.logEngine.trace("XSLT result:\n" + result);

        return result;
    } finally {
        Engine.logEngine.debug("XSLT process has finished");
        context.statistics.stop(t1);
    }
}

From source file:com.cisco.dvbu.ps.common.adapters.util.XmlUtils.java

public static Document xslTransform(Node xmlData, String xsl) throws Exception {
    if (xsl.trim().length() == 0) {
        //System.out.println("No XSL provided.");
        return (Document) xmlData;
    } else {//from  w w w .  ja  va 2s  .  c o m
        return xslTransform(xmlData, new StreamSource(new StringReader(xsl)));
    }
}