@Override protected void doStart() throws Exception { ObjectHelper.notNull(camelContext, "camelContext", this); // create a transformer to see if its saxon, as we then need to do some initial preparation Transformer t = getTemplate().newTransformer(); if (isSaxonTransformer(t)) { // pre-load saxon classes as we need to call the setMessageEmitter on the transformer to hook // saxon to use the JAXP // error listener, so we can capture errors and xsl:message outputs which end users may define // in the xslt files try { saxonReceiverClass = getCamelContext() .getClassResolver() .resolveMandatoryClass("net.sf.saxon.event.Receiver"); saxonWarnerClass = getCamelContext() .getClassResolver() .resolveMandatoryClass("net.sf.saxon.serialize.MessageWarner"); setMessageEmitterMethod = t.getClass().getMethod("setMessageEmitter", saxonReceiverClass); } catch (Exception e) { throw new IllegalStateException( "Error pre-loading Saxon classes. Make sure you have saxon on the classpath," + " and the classloader can load the following two classes: net.sf.saxon.event.Receiver, net.sf.saxon.serialize.MessageWarner.", e); } } }
public void configure(CompassSettings settings) throws CompassException { try { this.documentBuilder = doCreateDocumentBuilder(settings); } catch (ParserConfigurationException e) { throw new ConfigurationException("Failed to create document builder", e); } if (log.isDebugEnabled()) { log.debug("Using document builder [" + documentBuilder.getClass().getName() + "]"); } try { this.transformer = doCreateTransformer(settings); } catch (TransformerConfigurationException e) { throw new ConfigurationException("Failed to create transformer", e); } if (log.isDebugEnabled()) { log.debug("Using transformer [" + transformer.getClass().getName() + "]"); } }
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; }
boolean isSaxonTransformer(Transformer transformer) { return transformer.getClass().getName().startsWith("net.sf.saxon"); }
boolean isXalanTransformer(Transformer transformer) { return transformer.getClass().getName().startsWith("org.apache.xalan.transformer"); }