List of usage examples for javax.xml.transform TransformerFactory getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = factory.newDocumentBuilder(); Document doc;//from w ww . j a v a2 s.co m doc = parser.parse(args[0]); TransformerFactory transFactory = TransformerFactory.newInstance(); System.out.println(transFactory.getClass().getName()); transFactory.setAttribute("indent-number", 2); Transformer idTransform = transFactory.newTransformer(); idTransform.setOutputProperty(OutputKeys.METHOD, "xml"); idTransform.setOutputProperty(OutputKeys.INDENT, "yes"); Source input = new DOMSource(doc); Result output = new StreamResult(System.out); idTransform.transform(input, output); }
From source file:Main.java
/** * Tests whether the given {@link TransformerFactory} is known to support XSLT 2.0. * <p>//www.ja v a 2 s . co m * (Currently, this involves checking for a suitable version of Saxon; this will * change once more processors become available.) */ public static boolean supportsXSLT20(final TransformerFactory tranformerFactory) { return tranformerFactory.getClass().getName().startsWith("net.sf.saxon."); }
From source file:Main.java
/** * Convenience method to get a new instance of a TransformerFactory. * If the {@link #TransformerFactory} is an instance of * net.sf.saxon.TransformerFactoryImpl, the attribute * {@link #FeatureKeys.VERSION_WARNING} will be set to false in order to * suppress the warning about using an XSLT1 stylesheet with an XSLT2 * processor./*from www . j a va 2 s. c om*/ * * @return a new instance of TransformerFactory */ public static TransformerFactory getTransformerFactory() { TransformerFactory factory = TransformerFactory.newInstance(); if (factory.getClass().getName().equals("net.sf.saxon.TransformerFactoryImpl")) { factory.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE); } return factory; }
From source file:com.microsoft.tfs.util.xml.JAXPUtils.java
/** * A convenience method to create a new {@link TransformerFactory}. If the * given {@link ClassLoader} is not <code>null</code>, it is temporarily set * as the calling thread's context classloader while calling into JAXP to * get a {@link TransformerFactory} instance. The factory is not configured * in any way by this method.//ww w . j a va2s . com * * @throws XMLException * if a new {@link TransformerFactory} can't be created * * @param contextClassloader * the context classloader or <code>null</code> if none * @return a new {@link TransformerFactory} (never <code>null</code>) */ public static TransformerFactory newTransformerFactory(final ClassLoader contextClassloader) { final boolean setTCL = contextClassloader != null; ClassLoader currentTCL = null; if (setTCL) { currentTCL = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(contextClassloader); } try { final TransformerFactory factory = TransformerFactory.newInstance(); if (log.isTraceEnabled()) { final String messageFormat = "Created a new TransformerFactory: {0} (loaded from: {1})"; //$NON-NLS-1$ final String message = MessageFormat.format(messageFormat, factory.getClass().getName(), factory.getClass().getClassLoader()); log.trace(message); } return factory; } catch (final TransformerFactoryConfigurationError e) { throw new XMLException(e); } finally { if (setTCL) { Thread.currentThread().setContextClassLoader(currentTCL); } } }
From source file:com.microsoft.tfs.util.xml.JAXPUtils.java
/** * A convenience method to create a new {@link Transformer} using the given * {@link TransformerFactory}. The new {@link Transformer} is not configured * in any way by this method.//from ww w . ja va 2 s . c o m * * @throws XMLException * if a new {@link Transformer} can't be created * * @param factory * the {@link TransformerFactory} to use or <code>null</code> to * create a new factory using the {@link #newTransformerFactory()} * method * @return a new {@link Transformer} created from the given * {@link TransformerFactory} (never <code>null</code>) */ public static Transformer newTransformer(TransformerFactory factory) { if (factory == null) { factory = newTransformerFactory(); } try { final Transformer transformer = factory.newTransformer(); if (log.isTraceEnabled()) { final String messageFormat = "Created a new Transformer: {0} (loaded from: {1}) (factory: {2})"; //$NON-NLS-1$ final String message = MessageFormat.format(messageFormat, transformer.getClass().getName(), transformer.getClass().getClassLoader(), factory.getClass().getName()); log.trace(message); } return transformer; } catch (final TransformerConfigurationException e) { throw new XMLException(e); } }
From source file:fedora.server.rest.BaseRestResource.java
protected void transform(String xml, String xslt, Writer out) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { File xslFile = new File(fedoraServer.getHomeDir(), xslt); TransformerFactory factory = TransformerFactory.newInstance(); if (factory.getClass().getName().equals("net.sf.saxon.TransformerFactoryImpl")) { factory.setAttribute(FeatureKeys.VERSION_WARNING, Boolean.FALSE); }/*from ww w . ja v a2s . c o m*/ Templates template = factory.newTemplates(new StreamSource(xslFile)); Transformer transformer = template.newTransformer(); String appContext = getContext().getEnvironmentValue(Constants.FEDORA_APP_CONTEXT_NAME); transformer.setParameter("fedora", appContext); transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(out)); }
From source file:jeeves.utils.Xml.java
/** * Clears the cache used in the stylesheet transformer factory. This will only work for the GeoNetwork Caching * stylesheet transformer factory. This is a no-op for other transformer factories. *//*from w ww . ja va 2 s . c o m*/ public static void clearTransformerFactoryStylesheetCache() { TransformerFactory transFact = TransformerFactory.newInstance(); try { Method cacheMethod = transFact.getClass().getDeclaredMethod("clearCache", null); cacheMethod.invoke(transFact, new Object[0]); } catch (Exception e) { Log.error(Log.ENGINE, "Failed to find/invoke clearCache method - continuing (" + e.getMessage() + ")"); } }
From source file:org.fcrepo.localservices.saxon.SaxonServlet.java
/** * Maintain prepared stylesheets in memory for reuse *//*from ww w. ja va 2 s . c o m*/ private Templates tryCache(String url) throws Exception { Templates x = (Templates) m_cache.get(url); if (x == null) { synchronized (m_cache) { if (!m_cache.containsKey(url)) { TransformerFactory factory = TransformerFactory.newInstance(); if (factory.getClass().getName().equals("net.sf.saxon.TransformerFactoryImpl")) { factory.setAttribute(FeatureKeys.VERSION_WARNING, Boolean.FALSE); } StreamSource ss = new StreamSource(getInputStream(url)); ss.setSystemId(url); x = factory.newTemplates(ss); m_cache.put(url, x); } } } return x; }
From source file:com.panet.imeta.job.entries.xslt.JobEntryXSLT.java
public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) { LogWriter log = LogWriter.getInstance(); Result result = previousResult; result.setResult(false);//from ww w .j ava2 s .c o m String realxmlfilename = getRealxmlfilename(); String realxslfilename = getRealxslfilename(); String realoutputfilename = getRealoutputfilename(); FileObject xmlfile = null; FileObject xslfile = null; FileObject outputfile = null; try { if (xmlfilename != null && xslfilename != null && outputfilename != null) { xmlfile = KettleVFS.getFileObject(realxmlfilename); xslfile = KettleVFS.getFileObject(realxslfilename); outputfile = KettleVFS.getFileObject(realoutputfilename); if (xmlfile.exists() && xslfile.exists()) { if (outputfile.exists() && iffileexists == 2) { //Output file exists // User want to fail log.logError(toString(), Messages.getString("JobEntryXSLT.OuputFileExists1.Label") + realoutputfilename + Messages.getString("JobEntryXSLT.OuputFileExists2.Label")); result.setResult(false); result.setNrErrors(1); } else if (outputfile.exists() && iffileexists == 1) { // Do nothing if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobEntryXSLT.OuputFileExists1.Label") + realoutputfilename + Messages.getString("JobEntryXSLT.OuputFileExists2.Label")); result.setResult(true); } 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 = realoutputfilename.substring(realoutputfilename.length() - 4, realoutputfilename.length()); if (wildcard.substring(0, 1).equals(".")) { // Find wildcard realoutputfilename = realoutputfilename.substring(0, realoutputfilename.length() - 4) + "_" + StringUtil.getFormattedDateTimeNow(true) + wildcard; } else { // did not find wildcard realoutputfilename = realoutputfilename + "_" + StringUtil.getFormattedDateTimeNow(true); } if (log.isDebug()) log.logDebug(toString(), Messages.getString("JobEntryXSLT.OuputFileExists1.Label") + realoutputfilename + Messages.getString("JobEntryXSLT.OuputFileExists2.Label")); log.logDebug(toString(), Messages.getString("JobEntryXSLT.OuputFileNameChange1.Label") + realoutputfilename + Messages.getString("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(Messages.getString("JobEntryXSL.Log.TransformerFactoryInfos"), Messages .getString("JobEntryXSL.Log.TransformerFactory", factory.getClass().getName())); // Use the factory to create a template containing the xsl file Templates template = factory .newTemplates(new StreamSource(KettleVFS.getInputStream(xslfile))); // Use the template to create a transformer Transformer xformer = template.newTransformer(); if (log.isDetailed()) log.logDetailed(Messages.getString("JobEntryXSL.Log.TransformerClassInfos"), Messages .getString("JobEntryXSL.Log.TransformerClass", xformer.getClass().getName())); // Prepare the input and output files Source source = new StreamSource(KettleVFS.getInputStream(xmlfile)); StreamResult resultat = new StreamResult(KettleVFS.getOutputStream(outputfile, false)); // 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(realoutputfilename), parentJob.getJobname(), toString()); result.getResultFiles().put(resultFile.getFile().toString(), resultFile); } // Everything is OK result.setResult(true); } } else { if (!xmlfile.exists()) { log.logError(toString(), Messages.getString("JobEntryXSLT.FileDoesNotExist1.Label") + realxmlfilename + Messages.getString("JobEntryXSLT.FileDoesNotExist2.Label")); } if (!xslfile.exists()) { log.logError(toString(), Messages.getString("JobEntryXSLT.FileDoesNotExist1.Label") + realxslfilename + Messages.getString("JobEntryXSLT.FileDoesNotExist2.Label")); } result.setResult(false); result.setNrErrors(1); } } else { log.logError(toString(), Messages.getString("JobEntryXSLT.AllFilesNotNull.Label")); result.setResult(false); result.setNrErrors(1); } } catch (Exception e) { log.logError(toString(), Messages.getString("JobEntryXSLT.ErrorXLST.Label") + Messages.getString("JobEntryXSLT.ErrorXLSTXML1.Label") + realxmlfilename + Messages.getString("JobEntryXSLT.ErrorXLSTXML2.Label") + Messages.getString("JobEntryXSLT.ErrorXLSTXSL1.Label") + realxslfilename + Messages.getString("JobEntryXSLT.ErrorXLSTXSL2.Label") + e.getMessage()); result.setResult(false); result.setNrErrors(1); } finally { try { if (xmlfile != null) xmlfile.close(); if (xslfile != null) xslfile.close(); if (outputfile != null) outputfile.close(); // file object is not properly garbaged collected and thus the file cannot // be deleted anymore. This is a known problem in the JVM. System.gc(); } catch (IOException e) { } } return result; }
From source file:nl.nn.adapterframework.util.XmlUtils.java
public static String getVersionInfo() { StringBuilder sb = new StringBuilder(); sb.append(AppConstants.getInstance().getProperty("application.name") + " " + AppConstants.getInstance().getProperty("application.version")).append(SystemUtils.LINE_SEPARATOR); sb.append("XML tool version info:").append(SystemUtils.LINE_SEPARATOR); SAXParserFactory spFactory = getSAXParserFactory(); sb.append("SAXParserFactory-class =").append(spFactory.getClass().getName()) .append(SystemUtils.LINE_SEPARATOR); DocumentBuilderFactory domFactory1 = getDocumentBuilderFactory(false); sb.append("DocumentBuilderFactory1-class =").append(domFactory1.getClass().getName()) .append(SystemUtils.LINE_SEPARATOR); DocumentBuilderFactory domFactory2 = getDocumentBuilderFactory(true); sb.append("DocumentBuilderFactory2-class =").append(domFactory2.getClass().getName()) .append(SystemUtils.LINE_SEPARATOR); TransformerFactory tFactory1 = getTransformerFactory(false); sb.append("TransformerFactory1-class =").append(tFactory1.getClass().getName()) .append(SystemUtils.LINE_SEPARATOR); TransformerFactory tFactory2 = getTransformerFactory(true); sb.append("TransformerFactory2-class =").append(tFactory2.getClass().getName()) .append(SystemUtils.LINE_SEPARATOR); sb.append("Apache-XML tool version info:").append(SystemUtils.LINE_SEPARATOR); try {//from w ww. ja v a 2 s.co m sb.append("Xerces-Version=").append(org.apache.xerces.impl.Version.getVersion()) .append(SystemUtils.LINE_SEPARATOR); } catch (Throwable t) { sb.append("Xerces-Version not found (").append(t.getClass().getName()).append(": ") .append(t.getMessage()).append(")").append(SystemUtils.LINE_SEPARATOR); } try { String xalanVersion; if (IbisContext.getApplicationServerType().startsWith("WAS")) { xalanVersion = nl.nn.org.apache.xalan.Version.getVersion(); } else { xalanVersion = org.apache.xalan.Version.getVersion(); } sb.append("Xalan-Version=" + xalanVersion + SystemUtils.LINE_SEPARATOR); } catch (Throwable t) { sb.append("Xalan-Version not found (").append(t.getClass().getName()).append(": ") .append(t.getMessage()).append(")").append(SystemUtils.LINE_SEPARATOR); } try { // sb.append("XmlCommons-Version="+org.apache.xmlcommons.Version.getVersion()+SystemUtils.LINE_SEPARATOR); } catch (Throwable t) { sb.append("XmlCommons-Version not found (").append(t.getClass().getName()).append(": ") .append(t.getMessage()).append(")").append(SystemUtils.LINE_SEPARATOR); } return sb.toString(); }