Moved only "string" method, the version we use internally--probably sufficient for others' work as well. (Note: an additional unused FOUserAgent object will be created in driver during command-line use--cp. its no-param constructor vs. setUserAgent() call in apps.Fop; this will need to be ironed out at some time.) git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@197794 13f79535-47bb-0310-9956-ffa450edef68pull/30/head
// FOP | // FOP | ||||
import org.apache.fop.fo.Constants; | import org.apache.fop.fo.Constants; | ||||
import org.apache.fop.fo.ElementMapping; | |||||
import org.apache.fop.fo.FOTreeBuilder; | import org.apache.fop.fo.FOTreeBuilder; | ||||
import org.apache.fop.render.awt.AWTRenderer; | import org.apache.fop.render.awt.AWTRenderer; | ||||
* render. Methods within FOUserAgent can be called to set the | * render. Methods within FOUserAgent can be called to set the | ||||
* Renderer to use, the (possibly multiple) ElementMapping(s) to | * Renderer to use, the (possibly multiple) ElementMapping(s) to | ||||
* use and the OutputStream to use to output the results of the | * use and the OutputStream to use to output the results of the | ||||
* rendering (where applicable). In the case of | |||||
* ElementMapping(s), the Driver may be supplied either with the | |||||
* object itself, or the name of the class, in which case Driver will | |||||
* instantiate the class itself. The advantage of the latter is it | |||||
* enables runtime determination of ElementMapping(s). | |||||
* rendering (where applicable). | |||||
* <P> | * <P> | ||||
* Once the Driver is set up, the render method | * Once the Driver is set up, the render method | ||||
* is called. The invocation of the method is | * is called. The invocation of the method is | ||||
*/ | */ | ||||
public class Driver implements Constants { | public class Driver implements Constants { | ||||
/** | |||||
* the FO tree builder | |||||
*/ | |||||
private FOTreeBuilder treeBuilder; | |||||
/** | /** | ||||
* the render type code given by setRender | * the render type code given by setRender | ||||
*/ | */ | ||||
* Main constructor for the Driver class. | * Main constructor for the Driver class. | ||||
*/ | */ | ||||
public Driver() { | public Driver() { | ||||
foUserAgent = new FOUserAgent(); | |||||
stream = null; | stream = null; | ||||
} | } | ||||
this.stream = stream; | this.stream = stream; | ||||
} | } | ||||
private boolean isInitialized() { | |||||
return (treeBuilder != null); | |||||
} | |||||
/** | |||||
* Initializes the Driver object. | |||||
*/ | |||||
private void initialize() { | |||||
if (isInitialized()) { | |||||
throw new IllegalStateException("Driver already initialized"); | |||||
} | |||||
treeBuilder = new FOTreeBuilder(); | |||||
if (foUserAgent == null) { | |||||
foUserAgent = new FOUserAgent(); | |||||
} | |||||
} | |||||
/** | /** | ||||
* Resets the Driver so it can be reused. Property and element | * Resets the Driver so it can be reused. Property and element | ||||
* mappings are reset to defaults. | * mappings are reset to defaults. | ||||
*/ | */ | ||||
public synchronized void reset() { | public synchronized void reset() { | ||||
stream = null; | stream = null; | ||||
if (treeBuilder != null) { | |||||
treeBuilder.reset(); | |||||
} | |||||
} | } | ||||
/** | /** | ||||
* @param agent FOUserAgent to use | * @param agent FOUserAgent to use | ||||
*/ | */ | ||||
public void setUserAgent(FOUserAgent agent) throws FOPException { | public void setUserAgent(FOUserAgent agent) throws FOPException { | ||||
if (foUserAgent != null) { | |||||
throw new IllegalStateException("FOUserAgent " + | |||||
"instance already set."); | |||||
} | |||||
foUserAgent = agent; | foUserAgent = agent; | ||||
} | } | ||||
* @return the user agent | * @return the user agent | ||||
*/ | */ | ||||
public FOUserAgent getUserAgent() { | public FOUserAgent getUserAgent() { | ||||
if (foUserAgent == null) { | |||||
foUserAgent = new FOUserAgent(); | |||||
} | |||||
return foUserAgent; | return foUserAgent; | ||||
} | } | ||||
this.renderType = renderType; | this.renderType = renderType; | ||||
} | } | ||||
/** | |||||
* 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) { | |||||
treeBuilder.addElementMapping(mapping); | |||||
} | |||||
/** | |||||
* Add the element mapping with the given class name. | |||||
* @param mappingClassName the class name representing the element mapping. | |||||
*/ | |||||
public void addElementMapping(String mappingClassName) { | |||||
treeBuilder.addElementMapping(mappingClassName); | |||||
} | |||||
/** | /** | ||||
* Determines which SAX ContentHandler is appropriate for the renderType. | * Determines which SAX ContentHandler is appropriate for the renderType. | ||||
* Structure renderers (e.g. MIF & RTF) each have a specialized | * Structure renderers (e.g. MIF & RTF) each have a specialized | ||||
* @throws FOPException if setting up the ContentHandler fails | * @throws FOPException if setting up the ContentHandler fails | ||||
*/ | */ | ||||
public ContentHandler getContentHandler() throws FOPException { | public ContentHandler getContentHandler() throws FOPException { | ||||
if (!isInitialized()) { | |||||
initialize(); | |||||
} | |||||
if (renderType != RENDER_PRINT && renderType != RENDER_AWT) { | if (renderType != RENDER_PRINT && renderType != RENDER_AWT) { | ||||
validateOutputStream(); | validateOutputStream(); | ||||
} | } | ||||
treeBuilder.initialize(renderType, foUserAgent, stream); | |||||
return treeBuilder; | |||||
return new FOTreeBuilder(renderType, foUserAgent, stream); | |||||
} | } | ||||
/** | /** |
package org.apache.fop.apps; | package org.apache.fop.apps; | ||||
// Java | // Java | ||||
import java.util.ArrayList; | |||||
import java.util.Date; | import java.util.Date; | ||||
import java.util.HashMap; | import java.util.HashMap; | ||||
import java.util.Map; | import java.util.Map; | ||||
private Configuration userConfig = null; | private Configuration userConfig = null; | ||||
private Log log = LogFactory.getLog("FOP"); | private Log log = LogFactory.getLog("FOP"); | ||||
/* Additional fo.ElementMapping subclasses set by user */ | |||||
private ArrayList additionalElementMappings = null; | |||||
/** Producer: Metadata element for the system/software that produces | /** Producer: Metadata element for the system/software that produces | ||||
* the document. (Some renderers can store this in the document.) | * the document. (Some renderers can store this in the document.) | ||||
*/ | */ | ||||
return inputHandler; | return inputHandler; | ||||
} | } | ||||
/** | |||||
* Add the element mapping with the given class name. | |||||
* @param mappingClassName the class name representing the element mapping. | |||||
*/ | |||||
public void addElementMapping(String mappingClassName) { | |||||
if (additionalElementMappings == null) { | |||||
additionalElementMappings = new ArrayList(); | |||||
} | |||||
additionalElementMappings.add(mappingClassName); | |||||
} | |||||
/** | |||||
* Returns the ArrayList of user-added ElementMapping class names | |||||
* @return ArrayList of Strings holding ElementMapping names. | |||||
*/ | |||||
public ArrayList getAdditionalElementMappings() { | |||||
return additionalElementMappings; | |||||
} | |||||
/** | /** | ||||
* Sets the producer of the document. | * Sets the producer of the document. | ||||
* @param producer source of document | * @param producer source of document |
import java.io.InputStreamReader; | import java.io.InputStreamReader; | ||||
import java.io.OutputStream; | import java.io.OutputStream; | ||||
import java.io.Reader; | import java.io.Reader; | ||||
import java.util.ArrayList; | |||||
import java.util.Enumeration; | import java.util.Enumeration; | ||||
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.List; | import java.util.List; | ||||
private Locator locator; | private Locator locator; | ||||
/** | /** | ||||
* Default constructor | |||||
* FOTreeBuilder constructor | |||||
* @param render type as defined in Constants class | |||||
* @param foUserAgent in effect for this process | |||||
* @param stream OutputStream to direct results | |||||
*/ | */ | ||||
public FOTreeBuilder() { | |||||
setupDefaultMappings(); | |||||
} | |||||
/** | |||||
* Creates the FOInputHandler object based on passed-in render type | |||||
* @param render type | |||||
*/ | |||||
public void initialize(int renderType, FOUserAgent foUserAgent, | |||||
public FOTreeBuilder(int renderType, FOUserAgent foUserAgent, | |||||
OutputStream stream) throws FOPException { | OutputStream stream) throws FOPException { | ||||
if (renderType == Constants.RENDER_MIF) { | if (renderType == Constants.RENDER_MIF) { | ||||
foInputHandler = new MIFHandler(foUserAgent, stream); | foInputHandler = new MIFHandler(foUserAgent, stream); | ||||
foInputHandler = new AreaTreeHandler(foUserAgent, renderType, | foInputHandler = new AreaTreeHandler(foUserAgent, renderType, | ||||
stream); | stream); | ||||
} | } | ||||
// Add standard element mappings | |||||
setupDefaultMappings(); | |||||
// add additional ElementMappings defined within FOUserAgent | |||||
ArrayList addlEMs = foUserAgent.getAdditionalElementMappings(); | |||||
if (addlEMs != null) { | |||||
for (int i = 0; i < addlEMs.size(); i++) { | |||||
addElementMapping((String) addlEMs.get(i)); | |||||
} | |||||
} | |||||
} | } | ||||
/** | /** | ||||
} | } | ||||
} | } | ||||
/** | |||||
* 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) { | |||||
this.fobjTable.put(mapping.getNamespaceURI(), mapping.getTable()); | |||||
this.namespaces.add(mapping.getNamespaceURI().intern()); | |||||
} | |||||
/** | /** | ||||
* Add the element mapping with the given class name. | * Add the element mapping with the given class name. | ||||
* @param mappingClassName the class name representing the element mapping. | * @param mappingClassName the class name representing the element mapping. | ||||
*/ | */ | ||||
public void addElementMapping(String mappingClassName) | public void addElementMapping(String mappingClassName) | ||||
throws IllegalArgumentException { | throws IllegalArgumentException { | ||||
try { | try { | ||||
ElementMapping mapping = | ElementMapping mapping = | ||||
(ElementMapping)Class.forName(mappingClassName).newInstance(); | (ElementMapping)Class.forName(mappingClassName).newInstance(); | ||||
} | } | ||||
} | } | ||||
private void addElementMapping(ElementMapping mapping) { | |||||
this.fobjTable.put(mapping.getNamespaceURI(), mapping.getTable()); | |||||
this.namespaces.add(mapping.getNamespaceURI().intern()); | |||||
} | |||||
/** | /** | ||||
* SAX Handler for locator | * SAX Handler for locator | ||||
* @see org.xml.sax.ContentHandler#setDocumentLocator(Locator) | * @see org.xml.sax.ContentHandler#setDocumentLocator(Locator) |