You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by sp...@apache.org on 2009/10/16 15:12:23 UTC

svn commit: r825875 - in /xmlgraphics/fop/trunk/src/java/org/apache/fop/cli: CommandLineOptions.java InputHandler.java

Author: spepping
Date: Fri Oct 16 13:12:22 2009
New Revision: 825875

URL: http://svn.apache.org/viewvc?rev=825875&view=rev
Log:
Added a command-line option '-catalog' to use a catalog resolver for
the XML and XSLT files. Implemented it in InputHandler.

Modified:
    xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/InputHandler.java

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java?rev=825875&r1=825874&r2=825875&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java Fri Oct 16 13:12:22 2009
@@ -108,6 +108,8 @@
     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 @@
                   } 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 @@
             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 @@
             + "  -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"

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/InputHandler.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/InputHandler.java?rev=825875&r1=825874&r2=825875&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/InputHandler.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/InputHandler.java Fri Oct 16 13:12:22 2009
@@ -34,23 +34,20 @@
 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 @@
     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 @@
     }
 
     /**
+     * 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 @@
      * @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 @@
             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 @@
                             (String) xsltParams.elementAt(i + 1));
                     }
                 }
+                if (uriResolver != null) {
+                    transformer.setURIResolver(uriResolver);
+                }
             }
             transformer.setErrorListener(this);
 



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org


Re: Initial Values of Variables [was: Re: svn commit: r825875 - in /xmlgraphics/fop/trunk/src/java/org/apache/fop/cli: CommandLineOptions.java InputHandler.java]

Posted by "Peter B. West" <li...@pbw.id.au>.
Yep, they sure are. Does it hurt to spell it out? Like redundant  
parentheses, it does no harm, and makes the code just that little bit  
more explicit, for the dodos like me.

Peter

On 20/10/2009, at 8:13 PM, Vincent Hennebert wrote:

> Hi,
>
> Just a nit:
>
>> +    private boolean useCatalogResolver = false;
>> +    private EntityResolver entityResolver = null;
>> +    private URIResolver uriResolver = null;
>
> Those fields are being initialized to their default values. The Java
> Language Specification states [1] that every field must be initialized
> with a default value, basically 0 for numbers, false for booleans, and
> null for objects. So explicitly initializing them with their default
> values is just noise.
> I’d like to suggest everyone to remove those unnecessary  
> initializations
> in the future.
>
> [1] http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.5
>
> Thanks,
> Vincent


Initial Values of Variables [was: Re: svn commit: r825875 - in /xmlgraphics/fop/trunk/src/java/org/apache/fop/cli: CommandLineOptions.java InputHandler.java]

Posted by Vincent Hennebert <vh...@gmail.com>.
Hi,

Just a nit:

> +    private boolean useCatalogResolver = false;
> +    private EntityResolver entityResolver = null;
> +    private URIResolver uriResolver = null;

Those fields are being initialized to their default values. The Java
Language Specification states [1] that every field must be initialized
with a default value, basically 0 for numbers, false for booleans, and
null for objects. So explicitly initializing them with their default
values is just noise.
I’d like to suggest everyone to remove those unnecessary initializations
in the future.

[1] http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.5

Thanks,
Vincent