Search in sources :

Example 6 with Templates

use of javax.xml.transform.Templates in project spring-framework by spring-projects.

the class XsltView method renderMergedOutputModel.

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Templates templates = this.cachedTemplates;
    if (templates == null) {
        templates = loadTemplates();
    }
    Transformer transformer = createTransformer(templates);
    configureTransformer(model, response, transformer);
    configureResponse(model, response, transformer);
    Source source = null;
    try {
        source = locateSource(model);
        if (source == null) {
            throw new IllegalArgumentException("Unable to locate Source object in model: " + model);
        }
        transformer.transform(source, createResult(response));
    } finally {
        closeSourceIfNecessary(source);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) Templates(javax.xml.transform.Templates) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 7 with Templates

use of javax.xml.transform.Templates in project camel by apache.

the class TemplatesFactoryTest method testInstantiateAnInstanceOfTemplates.

@Test
public void testInstantiateAnInstanceOfTemplates() throws Exception {
    TemplatesFactory fac = TemplatesFactory.newInstance();
    TransformerFactory factory = new TransformerFactoryImpl();
    factory.setURIResolver(new ClassPathURIResolver(Constants.SCHEMATRON_TEMPLATES_ROOT_DIR, null));
    Templates templates = fac.getTemplates(ClassLoader.getSystemResourceAsStream(rules), factory);
    Assert.assertNotNull(templates);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) TransformerFactoryImpl(net.sf.saxon.TransformerFactoryImpl) Templates(javax.xml.transform.Templates) Test(org.junit.Test)

Example 8 with Templates

use of javax.xml.transform.Templates in project camel by apache.

the class XsltBuilderTest method testXsltTemplates.

public void testXsltTemplates() throws Exception {
    File file = new File("src/test/resources/org/apache/camel/builder/xml/example.xsl");
    Source source = new SAXSource(new InputSource(new FileInputStream(file)));
    XmlConverter converter = new XmlConverter();
    Templates styleSheet = converter.getTransformerFactory().newTemplates(source);
    XsltBuilder builder = XsltBuilder.xslt(styleSheet);
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("<hello>world!</hello>");
    builder.process(exchange);
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>", exchange.getOut().getBody());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Templates(javax.xml.transform.Templates) File(java.io.File) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) FileInputStream(java.io.FileInputStream) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 9 with Templates

use of javax.xml.transform.Templates in project felix by apache.

the class XSLTProcessor method createTransformer.

protected Transformer createTransformer(String path) {
    Transformer transformer = null;
    try {
        if (useCache && templatesCache.containsKey(path)) {
            transformer = ((Templates) templatesCache.get(path)).newTransformer();
        } else {
            InputStream file = getInputStream(path);
            if (file != null) {
                XSLTProcessor.log(LogService.LOG_INFO, "Creating template for path " + path, null);
                Templates template = factory.newTemplates(new StreamSource(file));
                transformer = template.newTransformer();
                if (useCache) {
                    templatesCache.put(path, template);
                }
            } else {
                XSLTProcessor.log(LogService.LOG_WARNING, "template for path " + path + " not found", null);
            }
        }
    } catch (TransformerConfigurationException e) {
        XSLTProcessor.log(LogService.LOG_ERROR, "Exception during template construction", e);
    }
    return transformer;
}
Also used : Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates)

Example 10 with Templates

use of javax.xml.transform.Templates in project pentaho-kettle by pentaho.

the class JobEntryXSLT method processOneXMLFile.

private boolean processOneXMLFile(String xmlfilename, String xslfilename, String outputfilename, Result result, Job parentJob) {
    boolean retval = false;
    FileObject xmlfile = null;
    FileObject xslfile = null;
    FileObject outputfile = null;
    try {
        xmlfile = KettleVFS.getFileObject(xmlfilename, this);
        xslfile = KettleVFS.getFileObject(xslfilename, this);
        outputfile = KettleVFS.getFileObject(outputfilename, this);
        if (xmlfile.exists() && xslfile.exists()) {
            if (outputfile.exists() && iffileexists == 2) {
                // Output file exists
                // User want to fail
                logError(BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists1.Label") + outputfilename + BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists2.Label"));
                return retval;
            } else if (outputfile.exists() && iffileexists == 1) {
                // Do nothing
                if (log.isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists1.Label") + outputfilename + BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists2.Label"));
                }
                retval = true;
                return retval;
            } else {
                if (outputfile.exists() && iffileexists == 0) {
                    // the output file exists and user want to create new one with unique name
                    // Format Date
                    // Try to clean filename (without wildcard)
                    String wildcard = outputfilename.substring(outputfilename.length() - 4, outputfilename.length());
                    if (wildcard.substring(0, 1).equals(".")) {
                        // Find wildcard
                        outputfilename = outputfilename.substring(0, outputfilename.length() - 4) + "_" + StringUtil.getFormattedDateTimeNow(true) + wildcard;
                    } else {
                        // did not find wildcard
                        outputfilename = outputfilename + "_" + StringUtil.getFormattedDateTimeNow(true);
                    }
                    if (log.isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists1.Label") + outputfilename + BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists2.Label"));
                        logDebug(BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileNameChange1.Label") + outputfilename + BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileNameChange2.Label"));
                    }
                }
                // Create transformer factory
                TransformerFactory factory = TransformerFactory.newInstance();
                if (xsltfactory.equals(FACTORY_SAXON)) {
                    // Set the TransformerFactory to the SAXON implementation.
                    factory = new net.sf.saxon.TransformerFactoryImpl();
                }
                if (log.isDetailed()) {
                    log.logDetailed(BaseMessages.getString(PKG, "JobEntryXSL.Log.TransformerFactoryInfos"), BaseMessages.getString(PKG, "JobEntryXSL.Log.TransformerFactory", factory.getClass().getName()));
                }
                InputStream xslInputStream = KettleVFS.getInputStream(xslfile);
                InputStream xmlInputStream = KettleVFS.getInputStream(xmlfile);
                OutputStream os = null;
                try {
                    // Use the factory to create a template containing the xsl file
                    Templates template = factory.newTemplates(new StreamSource(xslInputStream));
                    // Use the template to create a transformer
                    Transformer xformer = template.newTransformer();
                    if (log.isDetailed()) {
                        log.logDetailed(BaseMessages.getString(PKG, "JobEntryXSL.Log.TransformerClassInfos"), BaseMessages.getString(PKG, "JobEntryXSL.Log.TransformerClass", xformer.getClass().getName()));
                    }
                    // Do we need to set output properties?
                    if (setOutputProperties) {
                        xformer.setOutputProperties(outputProperties);
                    }
                    // Do we need to pass parameters?
                    if (useParameters) {
                        for (int i = 0; i < nrParams; i++) {
                            xformer.setParameter(nameOfParams[i], valueOfParams[i]);
                        }
                    }
                    // Prepare the input and output files
                    Source source = new StreamSource(xmlInputStream);
                    os = KettleVFS.getOutputStream(outputfile, false);
                    StreamResult resultat = new StreamResult(os);
                    // Apply the xsl file to the source file and write the result to the output file
                    xformer.transform(source, resultat);
                    if (isAddFileToResult()) {
                        // Add output filename to output files
                        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(outputfilename, this), parentJob.getJobname(), toString());
                        result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                    }
                    // Everything is OK
                    retval = true;
                } finally {
                    try {
                        xslInputStream.close();
                    } catch (IOException ignored) {
                    // ignore IO Exception on close
                    }
                    try {
                        xmlInputStream.close();
                    } catch (IOException ignored) {
                    // ignore IO Exception on close
                    }
                    try {
                        if (os != null) {
                            os.close();
                        }
                    } catch (IOException ignored) {
                    // ignore IO Exception on close
                    }
                }
            }
        } else {
            if (!xmlfile.exists()) {
                logError(BaseMessages.getString(PKG, "JobEntryXSLT.FileDoesNotExist1.Label") + xmlfilename + BaseMessages.getString(PKG, "JobEntryXSLT.FileDoesNotExist2.Label"));
            }
            if (!xslfile.exists()) {
                logError(BaseMessages.getString(PKG, "JobEntryXSLT.FileDoesNotExist1.Label") + xmlfilename + BaseMessages.getString(PKG, "JobEntryXSLT.FileDoesNotExist2.Label"));
            }
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLST.Label") + BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLSTXML1.Label") + xmlfilename + BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLSTXML2.Label") + BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLSTXSL1.Label") + xslfilename + BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLSTXSL2.Label") + e.getMessage());
    } finally {
        try {
            if (xmlfile != null) {
                xmlfile.close();
            }
            if (xslfile != null) {
                xslfile.close();
            }
            if (outputfile != null) {
                outputfile.close();
            }
        } catch (IOException e) {
            logError("Unable to close file", e);
        }
    }
    return retval;
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) IOException(java.io.IOException) ResultFile(org.pentaho.di.core.ResultFile) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) IOException(java.io.IOException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

Templates (javax.xml.transform.Templates)60 TransformerFactory (javax.xml.transform.TransformerFactory)25 StreamSource (javax.xml.transform.stream.StreamSource)25 Transformer (javax.xml.transform.Transformer)20 Source (javax.xml.transform.Source)19 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)17 StreamResult (javax.xml.transform.stream.StreamResult)16 TransformerException (javax.xml.transform.TransformerException)10 InputStream (java.io.InputStream)8 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)7 DOMResult (javax.xml.transform.dom.DOMResult)7 DOMSource (javax.xml.transform.dom.DOMSource)7 File (java.io.File)5 Result (javax.xml.transform.Result)5 SAXSource (javax.xml.transform.sax.SAXSource)5 FileOutputStream (java.io.FileOutputStream)4 StringReader (java.io.StringReader)4 BufferedOutputStream (java.io.BufferedOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3