diff options
author | Simon Pepping <spepping@apache.org> | 2009-10-16 13:12:22 +0000 |
---|---|---|
committer | Simon Pepping <spepping@apache.org> | 2009-10-16 13:12:22 +0000 |
commit | 37b65497ae06b915d2fcea87d7bc070abfbdf2d5 (patch) | |
tree | dd64ad02082280111754f5677784b86ffaa63c35 /src | |
parent | abf9b193b96b66254a654506de2c0c3d2665408f (diff) | |
download | xmlgraphics-fop-37b65497ae06b915d2fcea87d7bc070abfbdf2d5.tar.gz xmlgraphics-fop-37b65497ae06b915d2fcea87d7bc070abfbdf2d5.zip |
Added a command-line option '-catalog' to use a catalog resolver for
the XML and XSLT files. Implemented it in InputHandler.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@825875 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r-- | src/java/org/apache/fop/cli/CommandLineOptions.java | 7 | ||||
-rw-r--r-- | src/java/org/apache/fop/cli/InputHandler.java | 99 |
2 files changed, 90 insertions, 16 deletions
diff --git a/src/java/org/apache/fop/cli/CommandLineOptions.java b/src/java/org/apache/fop/cli/CommandLineOptions.java index 3bc694a79..ad854891b 100644 --- a/src/java/org/apache/fop/cli/CommandLineOptions.java +++ b/src/java/org/apache/fop/cli/CommandLineOptions.java @@ -108,6 +108,8 @@ public class CommandLineOptions { private boolean useStdIn = false; /* true if System.out (stdout) should be used for the output file */ private boolean useStdOut = false; + /* true if a catalog resolver should be used for entity and uri resolution */ + private boolean useCatalogResolver = false; /* rendering options (for the user agent) */ private Map renderingOptions = new java.util.HashMap(); /* target resolution (for the user agent) */ @@ -351,6 +353,8 @@ public class CommandLineOptions { } else { throw new FOPException("invalid param usage: use -param <name> <value>"); } + } else if (args[i].equals("-catalog")) { + useCatalogResolver = true; } else if (args[i].equals("-o")) { i = i + parsePDFOwnerPassword(args, i); } else if (args[i].equals("-u")) { @@ -1021,7 +1025,7 @@ public class CommandLineOptions { case IF_INPUT: return new IFInputHandler(iffile); case XSLT_INPUT: - return new InputHandler(xmlfile, xsltfile, xsltParams); + return new InputHandler(xmlfile, xsltfile, xsltParams, useCatalogResolver); case IMAGE_INPUT: return new ImageInputHandler(imagefile, xsltfile, xsltParams); default: @@ -1162,6 +1166,7 @@ public class CommandLineOptions { + " -xsl stylesheet xslt stylesheet \n \n" + " -param name value <value> to use for parameter <name> in xslt stylesheet\n" + " (repeat '-param name value' for each parameter)\n \n" + + " -catalog use catalog resolver for input XML and XSLT files\n" + " [OUTPUT] \n" + " outfile input will be rendered as PDF into outfile\n" + " (use '-' for outfile to pipe output to stdout)\n" diff --git a/src/java/org/apache/fop/cli/InputHandler.java b/src/java/org/apache/fop/cli/InputHandler.java index 4f49ea269..14811d7fa 100644 --- a/src/java/org/apache/fop/cli/InputHandler.java +++ b/src/java/org/apache/fop/cli/InputHandler.java @@ -34,23 +34,20 @@ import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; +import javax.xml.transform.URIResolver; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.Fop; import org.apache.fop.apps.FopFactory; import org.apache.fop.render.awt.viewer.Renderable; +import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; @@ -66,12 +63,15 @@ public class InputHandler implements ErrorListener, Renderable { protected File sourcefile = null; private File stylesheet = null; // for XML/XSLT usage private Vector xsltParams = null; // for XML/XSLT usage + private EntityResolver entityResolver = null; + private URIResolver uriResolver = null; /** the logger */ protected Log log = LogFactory.getLog(InputHandler.class); /** * Constructor for XML->XSLT->FO input + * * @param xmlfile XML file * @param xsltfile XSLT file * @param params Vector of command-line parameters (name, value, @@ -84,6 +84,23 @@ public class InputHandler implements ErrorListener, Renderable { } /** + * Constructor for XML->XSLT->FO input + * + * @param xmlfile XML file + * @param xsltfile XSLT file + * @param params Vector of command-line parameters (name, value, + * name, value, ...) for XSL stylesheet, null if none + * @param useCatalogResolver if true, use a catalog resolver + * for XML parsing and XSLT URI resolution + */ + public InputHandler(File xmlfile, File xsltfile, Vector params, boolean useCatalogResolver) { + this(xmlfile, xsltfile, params); + if (useCatalogResolver) { + createCatalogResolver(); + } + } + + /** * Constructor for FO input * @param fofile the file to read the FO document. */ @@ -151,7 +168,7 @@ public class InputHandler implements ErrorListener, Renderable { * @return the Source for the main input file */ protected Source createMainSource() { - Source result; + Source source; InputStream in; String uri; if (this.sourcefile != null) { @@ -173,33 +190,82 @@ public class InputHandler implements ErrorListener, Renderable { spf.setFeature("http://xml.org/sax/features/namespaces", true); spf.setFeature("http://apache.org/xml/features/xinclude", true); XMLReader xr = spf.newSAXParser().getXMLReader(); - result = new SAXSource(xr, is); + if (entityResolver != null) { + xr.setEntityResolver(entityResolver); + } + source = new SAXSource(xr, is); } catch (SAXException e) { if (this.sourcefile != null) { - result = new StreamSource(this.sourcefile); + source = new StreamSource(this.sourcefile); } else { - result = new StreamSource(in, uri); + source = new StreamSource(in, uri); } } catch (ParserConfigurationException e) { if (this.sourcefile != null) { - result = new StreamSource(this.sourcefile); + source = new StreamSource(this.sourcefile); } else { - result = new StreamSource(in, uri); + source = new StreamSource(in, uri); } } - return result; + return source; + } + + /** + * Create a catalog resolver and use it for XML parsing and XSLT URI resolution + * Try the Apache Commons Resolver, and if unsuccessful, + * try the same built into Java 6 + */ + protected void createCatalogResolver() { + String[] classNames = + new String[] {"org.apache.xml.resolver.tools.CatalogResolver", + "com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver"}; + Class resolverClass = null; + for (int i = 0; i < classNames.length && resolverClass == null; ++i) { + try { + resolverClass = Class.forName(classNames[i]); + } catch (ClassNotFoundException e) { } + } + if (resolverClass == null) { + log.error("Could not find catalog resolver in class path"); + return; + } + try { + entityResolver = (EntityResolver) resolverClass.newInstance(); + uriResolver = (URIResolver) resolverClass.newInstance(); + } catch (InstantiationException e) { + log.error("Error creating the catalog resolver: " + e.getMessage()); + } catch (IllegalAccessException e) { + log.error("Error creating the catalog resolver: " + e.getMessage()); + } } /** * Creates a Source for the selected stylesheet. + * * @return the Source for the selected stylesheet or null if there's no stylesheet */ protected Source createXSLTSource() { + Source xslt = null; if (this.stylesheet != null) { - return new StreamSource(this.stylesheet); - } else { - return null; + if (entityResolver != null) { + try { + InputSource is = new InputSource(this.stylesheet.getPath()); + SAXParserFactory spf = SAXParserFactory.newInstance(); + spf.setFeature("http://xml.org/sax/features/namespaces", true); + spf.setFeature("http://apache.org/xml/features/xinclude", true); + XMLReader xr = spf.newSAXParser().getXMLReader(); + xr.setEntityResolver(entityResolver); + xslt = new SAXSource(xr, is); + } catch (SAXException e) { + // return StreamSource + } catch (ParserConfigurationException e) { + // return StreamSource + } + } + if (xslt == null) + xslt = new StreamSource(this.stylesheet); } + return xslt; } /** @@ -226,6 +292,9 @@ public class InputHandler implements ErrorListener, Renderable { (String) xsltParams.elementAt(i + 1)); } } + if (uriResolver != null) { + transformer.setURIResolver(uriResolver); + } } transformer.setErrorListener(this); |