TransformerFactoryImpl transformerFactory = new TransformerFactoryImpl(); Configuration config = transformerFactory.getConfiguration(); config.registerExtensionFunction(new DateExtensionFunction());
/** Output error message when incorrect command line options/arguments are used * * @param message Error message to be displayed */ protected void badUsage(String message) { System.err.println(message); System.err.println(factory.getConfiguration().getProductTitle()); System.err.println("Usage: java net.sf.saxon.Compile [options] stylesheet-file output-file"); System.err.println("Options: "); System.err.println(" -r classname Use specified URIResolver class"); System.err.println(" -t Display version and timing information"); System.err.println(" -u Names are URLs not filenames"); System.err.println(" -y classname Use specified SAX parser for stylesheet"); System.err.println(" -debug Produce trace output to diagnose failures"); System.err.println(" -1.1 Allow XML 1.1 documents"); System.err.println(" -? Display this message "); System.exit(2); }
public static ByteArrayOutputStream transformXML( ByteArrayInputStream inputStream,String xslt) throws ParserConfigurationException, IOException, SAXException, TransformerException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Source xslSource = new StreamSource(inputStream); TransformerFactory transFact = TransformerFactory.newInstance(); ((TransformerFactoryImpl) transFact).getConfiguration().setMessageEmitterClass(Log4jEmitter.class.getName()); // Templates templates = transFact.newTemplates(xslSource); Transformer trans = StylesheetCache.newTransformer(xslt); Source xmlSource = new StreamSource(inputStream); trans.transform(xmlSource, new StreamResult(outputStream)); return outputStream; }
/** Output error message when incorrect command line options/arguments are used * * @param message Error message to be displayed */ protected void badUsage(String message) { System.err.println(message); System.err.println(factory.getConfiguration().getProductTitle()); System.err.println("Usage: java net.sf.saxon.Compile [options] stylesheet-file output-file"); System.err.println("Options: "); System.err.println(" -r classname Use specified URIResolver class"); System.err.println(" -t Display version and timing information"); System.err.println(" -u Names are URLs not filenames"); System.err.println(" -y classname Use specified SAX parser for stylesheet"); System.err.println(" -debug Produce trace output to diagnose failures"); System.err.println(" -1.1 Allow XML 1.1 documents"); System.err.println(" -? Display this message "); System.exit(2); }
/** * Registers Saxon full integrated function definitions. * The intgrated function should be an instance of net.sf.saxon.lib.ExtensionFunctionDefinition abstract class. * @see <a href="https://www.saxonica.com/html/documentation/extensibility/integratedfunctions/ext-full-J.html">Saxon * Java extension functions: full interface</a> */ private void configureSaxonExtensions(net.sf.saxon.TransformerFactoryImpl tfi) { final net.sf.saxon.Configuration conf = tfi.getConfiguration(); for (ExtensionFunctionDefinition def : ServiceLoader.load(ExtensionFunctionDefinition.class)) { try { conf.registerExtensionFunction(def.getClass().newInstance()); } catch (InstantiationException e) { throw new RuntimeException("Failed to register " + def.getFunctionQName().getDisplayName() + ". Cannot create instance of " + def.getClass().getName() + ": " + e.getMessage(), e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } }
/** * Obtain a new Transformer instance for the * specified XSLT file name. * A new entry will be added to the * cache if this is the first request * for the specified file name. * * @param xsltFile the file name * of an XSLT stylesheet. * @return a transformation context * for the given stylesheet. */ public static synchronized Transformer newTransformer(String xsltFile) throws TransformerConfigurationException { MapEntry entry = (MapEntry) cache.get(xsltFile); InputStream inputStream = StylesheetCache.class.getClassLoader().getResourceAsStream(xsltFile); // create a new entry in the cache if necessary if (entry == null) { Source xslSource = new StreamSource(inputStream); TransformerFactory transFact = TransformerFactory.newInstance(); ((TransformerFactoryImpl) transFact).getConfiguration().setMessageEmitterClass(Log4jEmitter.class.getName()); Templates templates = transFact.newTemplates(xslSource); entry = new MapEntry(xsltFile, templates); cache.put(xsltFile, entry); } return entry.templates.newTransformer(); }
((TransformerFactoryImpl) transFact).getConfiguration().setMessageEmitterClass(Log4jEmitter.class.getName()); Templates templates = transFact.newTemplates(xslSource);
try { TransformerFactoryImpl factory = new TransformerFactoryImpl(); factory.getConfiguration().registerExtensionFunction(new DateExtensionFunction()); String xsl = "<e xsl:version='3.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' " + "result='{Q{http://date.com}getFormattedNow()}'/>";
System.err.println(factory.getConfiguration().getProductTitle()); if (args.length < i+2) badUsage("No URIResolver class"); String r = args[i++]; factory.setURIResolver(factory.getConfiguration().makeURIResolver(r)); styleSource = factory.getURIResolver().resolve(styleFileName, null); if (styleSource == null) { styleSource = factory.getConfiguration().getSystemURIResolver().resolve(styleFileName, null); styleSource = new SAXSource(factory.getConfiguration().getStyleParser(), eis);
System.err.println(factory.getConfiguration().getProductTitle()); if (args.length < i+2) badUsage("No URIResolver class"); String r = args[i++]; factory.setURIResolver(factory.getConfiguration().makeURIResolver(r)); styleSource = factory.getURIResolver().resolve(styleFileName, null); if (styleSource == null) { styleSource = factory.getConfiguration().getSystemURIResolver().resolve(styleFileName, null); styleSource = new SAXSource(factory.getConfiguration().getStyleParser(), eis);
quit(err.getMessage(), 2); config = factory.getConfiguration(); config.setVersionWarning(true); // unless suppressed by command line options schemaAware = config.isSchemaAware(Configuration.XSLT);
quit(err.getMessage(), 2); config = factory.getConfiguration(); schemaAware = config.isSchemaAware(Configuration.XSLT);