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./*from   www . j  av  a 2s . co  m*/
 *
 * @param xmlFile
 *            the xml file
 * @param xsltFile
 *            the xslt file
 * @param outputStream
 *            the output stream
 * @throws TransformerFactoryConfigurationError
 *             the transformer factory configuration error
 * @throws TransformerConfigurationException
 *             the transformer configuration exception
 * @throws TransformerException
 *             the transformer exception
 */
public static void transform(File xmlFile, File xsltFile, OutputStream outputStream)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    Source xmlSource = new StreamSource(xmlFile);
    Source xsltSource = new StreamSource(xsltFile);
    transform(xmlSource, xsltSource, outputStream);
}

From source file:nz.co.senanque.madura.spring.XSLSpringFactoryBean.java

/**
 * Load the XSLT document. The documentName can be a local resource file name
 * or a url pointing to a resource./*from   www.jav  a 2 s  . c  o  m*/
 * @param documentName
 * @return a Templates object containing the document
 * @throws DocumentException
 */
private Templates loadXSLT(InputStream in) {
    try {
        Source xsltSource = new StreamSource(in);
        TransformerFactory transFact = TransformerFactory.newInstance();
        Templates templates = transFact.newTemplates(xsltSource);
        return templates;
    } catch (Exception e) {
        throw new RuntimeException("failed to parse: " + getResource().getDescription(), e);
    }
}

From source file:com.betel.flowers.pdf.util.XMLtoHtml.java

public Boolean createHTML(String direccionDestino, String direccionDestinoHtml, String direccionXML,
        String barcode, int Type)
        throws IOException, FileNotFoundException, TransformerFactoryConfigurationError, TransformerException {
    Boolean exito = false;/* ww w  . jav  a 2s  .  c  om*/
    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();

        Source xmlDoc = new StreamSource(direccionXML);
        Source xslDoc = null;
        switch (Type) {
        case 0:
            BarcodeGenerator.createBarCode(direccionXML, direccionDestino);
            xslDoc = new StreamSource(
                    new File("/var/www/html/pdf/resources/tag/re/tagRegistroExportacion.xsl"));
            break;
        case 1:
            xslDoc = new StreamSource(new File("/var/www/html/mail/resources/sv/mailStockVentas.xsl"));
            break;
        case 2:
            break;
        default:
            LOG.log(Level.SEVERE, "ninguna opcion XMLtoHTML");
        }

        if (xslDoc != null) {
            StringWriter writer = new StringWriter();
            Transformer transformer = tFactory.newTransformer(xslDoc);
            transformer.setParameter("barcode", barcode);
            transformer.transform(xmlDoc, new javax.xml.transform.stream.StreamResult(writer));
            String result = writer.toString();
            exito = writeHTML(checkHTML(result), direccionDestinoHtml);
        }
    } catch (TransformerFactoryConfigurationError | FileNotFoundException | TransformerException ex) {
        LOG.log(Level.SEVERE, ex.getMessage());
        throw ex;
    } catch (IOException ex) {
        LOG.log(Level.SEVERE, ex.getMessage());
        throw ex;
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, ex.getMessage());
    } finally {
        try {

            exito = true;
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, ex.getMessage());
        }
    }

    return exito;
}

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

/**
 * Execute the transformation specified on the incoming XML file source specified by the XSL style sheet file source.
 * @param XMLFileSource file path to XML document
 * @param XSLFileSource file path to XSL style sheet
 *//*from   w  ww. j a  va  2 s .  co  m*/
public static void executeXslTransformUtility(String XMLFileSource, String XSLFileSource) {

    if (logger.isInfoEnabled()) {
        logger.info("ScriptUtil.XslTransformUtility:: XMLFileSource=" + XMLFileSource);
        logger.info("ScriptUtil.XslTransformUtility:: XSLFileSource=" + XSLFileSource);
        //System.out.println("ScriptUtil.XslTransformUtility:: XMLFileSource="+XMLFileSource);
        //System.out.println("ScriptUtil.XslTransformUtility:: XSLFileSource="+XSLFileSource);
    }

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder;
    Document doc;
    try {
        // Get the XML Document
        FileInputStream in = new FileInputStream(XMLFileSource);
        dBuilder = dbFactory.newDocumentBuilder();
        doc = dBuilder.parse(in);

        // Get the XSL stylesheet
        StreamSource ss = new StreamSource(new File(XSLFileSource));

        // Transform the document
        Document newdoc = XmlUtils.xslTransform(doc, ss);

        String output = XmlUtils.nodeToString(newdoc);

        if (logger.isInfoEnabled()) {
            logger.info("ScriptUtil.XslTransformUtility:: output:\n"
                    + XMLUtils.getPrettyXml(XMLUtils.getDocumentFromString(output)));
            //System.out.println("ScriptUtil.XslTransformUtility:: output:\n"+output);
        }
    } catch (Exception e) {
        logger.error(e.getMessage());
        //System.err.println("" + e.getMessage());
    }
}

From source file:Transform.java

/**
 * Asks the TransformerFactory to try to load a precompiled version of
 * the translet from the class path to construct a Transformer object.
 * The translet performs the transformation on behalf of the
 * Transformer.transform() method.//from   w w w .j  a v  a  2  s .  c  o  m
 */
public void run(String[] args) {
    String xml = args[0];
    String transletURI = args[1];

    try {
        // Set XSLTC's TransformerFactory implementation as the default
        System.setProperty("javax.xml.transform.TransformerFactory",
                "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");

        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("use-classpath", Boolean.TRUE);
        Transformer transformer = tf.newTransformer(new StreamSource(transletURI));

        StreamSource document = new StreamSource(xml);
        StreamResult result = new StreamResult(new OutputStreamWriter(System.out));
        transformer.transform(document, result);
    } catch (Exception e) {
        System.err.println("Exception: " + e);
        e.printStackTrace();
    }
    System.exit(0);
}

From source file:org.energyos.espi.common.domain.ServiceCategoryUnmarshallerTests.java

private ServiceCategory loadServiceCategory() throws DatatypeConfigurationException, IOException {
    String xml = FixtureFactory.loadFixture("/fixtures/ServiceCategory.xml");
    return (ServiceCategory) atomMarshaller.unmarshal(new StreamSource(new StringReader(xml)));
}

From source file:net.javacrumbs.springws.test.common.MessageGenerator.java

public WebServiceMessage generateMessage(WebServiceMessageFactory messageFactory, Resource resource)
        throws IOException {
    if (shouldCreateSoapEnvelope(resource)) {
        WebServiceMessage message = messageFactory.createWebServiceMessage();
        getXmlUtil().transform(new StreamSource(resource.getInputStream()), message.getPayloadResult());
        logMessage(message);//from   w  ww  .  j  a v  a2s. c om
        return message;
    } else {
        WebServiceMessage message = messageFactory.createWebServiceMessage(createInputStream(resource));
        logMessage(message);
        return message;
    }
}

From source file:Main.java

private static void render(Templates tpl, byte[] xmldata, Writer out) throws TransformerException {
    tpl.newTransformer().transform(new StreamSource(new ByteArrayInputStream(xmldata)), new StreamResult(out));
}

From source file:com.stratumsoft.xmlgen.SchemaUtilTest.java

@Test
public void testGetNames() throws Exception {
    final InputStream is = getClass().getResourceAsStream(personalXsd);
    assertNotNull(is);//from  www.j  a v  a  2s .  c  o  m

    final StreamSource source = new StreamSource(is);
    final XmlSchema schema = new XmlSchemaCollection().read(source);
    assertNotNull(schema);

    final Collection<QName> elements = SchemaUtil.getElements(schema);
    assertNotNull(elements);
    assertFalse(elements.isEmpty());

    System.out.println("Got the following elements from schema:");
    CollectionUtils.forAllDo(elements, new Closure() {
        @Override
        public void execute(Object input) {
            System.out.println(input);
        }
    });

}

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

/**
 * Filter the xpathExpression to the specified string.
 * //w  ww  .j a  v a 2s  . c o  m
 * @param content
 *            String to which the xpathExpression will be applied
 * @param xpathExpression
 *            XPath expression to apply to the content
 * @return TRUE if the xpathExpression evaluates to true, FALSE otherwise
 */
public static boolean filterString(String content, String xpathExpression) {

    try {

        return filter(new StreamSource(IOUtils.toInputStream(content, CharEncoding.UTF_8)), xpathExpression);

    } catch (IOException e) {

        log.error("Problems processing the content.  " + e.getMessage(), e);
        return false;

    }

}