gmazza 2003/07/04 11:45:48 Modified: src/java/org/apache/fop/apps Driver.java src/java/org/apache/fop/fo FOTreeBuilder.java Log: FOTreeBuilder's ElementMapping initialization moved from Driver to FOTreeBuilder class. For embedded coding, AddElementMapping() functions retained in Driver class but reimplemented to just wrap the FOTreeBuilder versions. Revision Changes Path 1.8 +3 -136 xml-fop/src/java/org/apache/fop/apps/Driver.java Index: Driver.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/apps/Driver.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- Driver.java 29 Jun 2003 19:34:33 -0000 1.7 +++ Driver.java 4 Jul 2003 18:45:48 -0000 1.8 @@ -81,13 +81,7 @@ // Java import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; -import java.io.Reader; -import java.util.Enumeration; -import java.util.Iterator; -import java.util.List; import java.util.Map; /** @@ -277,7 +271,6 @@ } treeBuilder = new FOTreeBuilder(); treeBuilder.setUserAgent(getUserAgent()); - setupDefaultMappings(); } /** @@ -392,31 +385,6 @@ } /** - * Sets all the element and property list mappings to their default values. - * - */ - public void setupDefaultMappings() { - addElementMapping("org.apache.fop.fo.FOElementMapping"); - addElementMapping("org.apache.fop.svg.SVGElementMapping"); - addElementMapping("org.apache.fop.extensions.ExtensionElementMapping"); - - // add mappings from available services - Iterator providers = - Service.providers(org.apache.fop.fo.ElementMapping.class); - if (providers != null) { - while (providers.hasNext()) { - String str = (String)providers.next(); - try { - addElementMapping(str); - } catch (IllegalArgumentException e) { - getLogger().warn("Error while adding element mapping", e); - } - - } - } - } - - /** * Shortcut to set the rendering type to use. Must be one of * <ul> * <li>RENDER_PDF</li> @@ -539,7 +507,7 @@ * @param mapping the element mappingto add */ public void addElementMapping(ElementMapping mapping) { - mapping.addToBuilder(treeBuilder); + treeBuilder.addElementMapping(mapping); } /** @@ -547,25 +515,8 @@ * @param mappingClassName the class name representing the element mapping. * @throws IllegalArgumentException if there was not such element mapping. */ - public void addElementMapping(String mappingClassName) - throws IllegalArgumentException { - try { - ElementMapping mapping = - (ElementMapping)Class.forName(mappingClassName).newInstance(); - addElementMapping(mapping); - } catch (ClassNotFoundException e) { - throw new IllegalArgumentException("Could not find " - + mappingClassName); - } catch (InstantiationException e) { - throw new IllegalArgumentException("Could not instantiate " - + mappingClassName); - } catch (IllegalAccessException e) { - throw new IllegalArgumentException("Could not access " - + mappingClassName); - } catch (ClassCastException e) { - throw new IllegalArgumentException(mappingClassName - + " is not an ElementMapping"); - } + public void addElementMapping(String mappingClassName) { + treeBuilder.addElementMapping(mappingClassName); } /** @@ -698,90 +649,6 @@ } else { render(reader, source); } - } - -} - -// code stolen from org.apache.batik.util and modified slightly -// does what sun.misc.Service probably does, but it cannot be relied on. -// hopefully will be part of standard jdk sometime. - -/** - * This class loads services present in the class path. - */ -class Service { - - private static Map providerMap = new java.util.Hashtable(); - - public static synchronized Iterator providers(Class cls) { - ClassLoader cl = cls.getClassLoader(); - // null if loaded by bootstrap class loader - if (cl == null) { - cl = ClassLoader.getSystemClassLoader(); - } - String serviceFile = "META-INF/services/" + cls.getName(); - - // getLogger().debug("File: " + serviceFile); - - List lst = (List)providerMap.get(serviceFile); - if (lst != null) { - return lst.iterator(); - } - - lst = new java.util.Vector(); - providerMap.put(serviceFile, lst); - - Enumeration e; - try { - e = cl.getResources(serviceFile); - } catch (IOException ioe) { - return lst.iterator(); - } - - while (e.hasMoreElements()) { - try { - java.net.URL u = (java.net.URL)e.nextElement(); - //getLogger().debug("URL: " + u); - - InputStream is = u.openStream(); - Reader r = new InputStreamReader(is, "UTF-8"); - BufferedReader br = new BufferedReader(r); - - String line = br.readLine(); - while (line != null) { - try { - // First strip any comment... - int idx = line.indexOf('#'); - if (idx != -1) { - line = line.substring(0, idx); - } - - // Trim whitespace. - line = line.trim(); - - // If nothing left then loop around... - if (line.length() == 0) { - line = br.readLine(); - continue; - } - // getLogger().debug("Line: " + line); - - // Try and load the class - // Object obj = cl.loadClass(line).newInstance(); - // stick it into our vector... - lst.add(line); - } catch (Exception ex) { - // Just try the next line - } - - line = br.readLine(); - } - } catch (Exception ex) { - // Just try the next file... - } - - } - return lst.iterator(); } } 1.5 +159 -0 xml-fop/src/java/org/apache/fop/fo/FOTreeBuilder.java Index: FOTreeBuilder.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/fo/FOTreeBuilder.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- FOTreeBuilder.java 30 Jun 2003 21:04:06 -0000 1.4 +++ FOTreeBuilder.java 4 Jul 2003 18:45:48 -0000 1.5 @@ -55,6 +55,7 @@ import java.util.Map; import java.util.Set; +// SAX import org.apache.avalon.framework.logger.Logger; import org.apache.fop.apps.FOPException; import org.apache.fop.fo.ElementMapping.Maker; @@ -62,6 +63,16 @@ import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; +// Java +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.Reader; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.List; + /** * SAX Handler that builds the formatting object tree. * @@ -108,6 +119,7 @@ * Default constructor */ public FOTreeBuilder() { + setupDefaultMappings(); } private Logger getLogger() { @@ -135,6 +147,31 @@ } /** + * Sets all the element and property list mappings to their default values. + * + */ + private void setupDefaultMappings() { + addElementMapping("org.apache.fop.fo.FOElementMapping"); + addElementMapping("org.apache.fop.svg.SVGElementMapping"); + addElementMapping("org.apache.fop.extensions.ExtensionElementMapping"); + + // add mappings from available services + Iterator providers = + Service.providers(ElementMapping.class); + if (providers != null) { + while (providers.hasNext()) { + String str = (String)providers.next(); + try { + addElementMapping(str); + } catch (IllegalArgumentException e) { + getLogger().warn("Error while adding element mapping", e); + } + + } + } + } + + /** * Adds a mapping from a namespace to a table of makers. * * @param namespaceURI namespace URI of formatting object elements @@ -146,6 +183,42 @@ } /** + * Add the given element mapping. + * An element mapping maps element names to Java classes. + * + * @param mapping the element mappingto add + */ + public void addElementMapping(ElementMapping mapping) { + mapping.addToBuilder(this); + } + + /** + * Add the element mapping with the given class name. + * @param mappingClassName the class name representing the element mapping. + * @throws IllegalArgumentException if there was not such element mapping. + */ + public void addElementMapping(String mappingClassName) + throws IllegalArgumentException { + try { + ElementMapping mapping = + (ElementMapping)Class.forName(mappingClassName).newInstance(); + addElementMapping(mapping); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException("Could not find " + + mappingClassName); + } catch (InstantiationException e) { + throw new IllegalArgumentException("Could not instantiate " + + mappingClassName); + } catch (IllegalAccessException e) { + throw new IllegalArgumentException("Could not access " + + mappingClassName); + } catch (ClassCastException e) { + throw new IllegalArgumentException(mappingClassName + + " is not an ElementMapping"); + } + } + + /** * SAX Handler for characters * @see org.xml.sax.ContentHandler#characters(char[], int, int) */ @@ -277,4 +350,90 @@ public boolean hasData() { return (rootFObj != null); } + +} + +// code stolen from org.apache.batik.util and modified slightly +// does what sun.misc.Service probably does, but it cannot be relied on. +// hopefully will be part of standard jdk sometime. + +/** + * This class loads services present in the class path. + */ +class Service { + + private static Map providerMap = new java.util.Hashtable(); + + public static synchronized Iterator providers(Class cls) { + ClassLoader cl = cls.getClassLoader(); + // null if loaded by bootstrap class loader + if (cl == null) { + cl = ClassLoader.getSystemClassLoader(); + } + String serviceFile = "META-INF/services/" + cls.getName(); + + // getLogger().debug("File: " + serviceFile); + + List lst = (List)providerMap.get(serviceFile); + if (lst != null) { + return lst.iterator(); + } + + lst = new java.util.Vector(); + providerMap.put(serviceFile, lst); + + Enumeration e; + try { + e = cl.getResources(serviceFile); + } catch (IOException ioe) { + return lst.iterator(); + } + + while (e.hasMoreElements()) { + try { + java.net.URL u = (java.net.URL)e.nextElement(); + //getLogger().debug("URL: " + u); + + InputStream is = u.openStream(); + Reader r = new InputStreamReader(is, "UTF-8"); + BufferedReader br = new BufferedReader(r); + + String line = br.readLine(); + while (line != null) { + try { + // First strip any comment... + int idx = line.indexOf('#'); + if (idx != -1) { + line = line.substring(0, idx); + } + + // Trim whitespace. + line = line.trim(); + + // If nothing left then loop around... + if (line.length() == 0) { + line = br.readLine(); + continue; + } + // getLogger().debug("Line: " + line); + + // Try and load the class + // Object obj = cl.loadClass(line).newInstance(); + // stick it into our vector... + lst.add(line); + } catch (Exception ex) { + // Just try the next line + } + + line = br.readLine(); + } + } catch (Exception ex) { + // Just try the next file... + } + + } + return lst.iterator(); + } + } +
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]