123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- /*
- * Copyright 1999-2005 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- /* $Id$ */
-
- package org.apache.fop.apps;
-
- // Java
- import java.io.File;
- import java.io.IOException;
- import java.net.URL;
- import java.net.MalformedURLException;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import javax.xml.transform.Source;
- import javax.xml.transform.TransformerException;
- import javax.xml.transform.URIResolver;
-
- // avalon configuration
- import org.apache.avalon.framework.configuration.Configuration;
- import org.apache.avalon.framework.configuration.ConfigurationException;
-
- // commons logging
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
-
- // FOP
- import org.apache.fop.Version;
- import org.apache.fop.fo.ElementMapping;
- import org.apache.fop.fo.FOEventHandler;
- import org.apache.fop.layoutmgr.LayoutManagerMaker;
- import org.apache.fop.pdf.PDFEncryptionParams;
- import org.apache.fop.render.Renderer;
- import org.apache.fop.render.RendererFactory;
- import org.apache.fop.render.XMLHandlerRegistry;
-
- /**
- * The User Agent for fo.
- * This user agent is used by the processing to obtain user configurable
- * options.
- * <p>
- * Renderer specific extensions (that do not produce normal areas on
- * the output) will be done like so:
- * <br>
- * The extension will create an area, custom if necessary
- * <br>
- * this area will be added to the user agent with a key
- * <br>
- * the renderer will know keys for particular extensions
- * <br>
- * eg. bookmarks will be held in a special hierarchical area representing
- * the title and bookmark structure
- * <br>
- * These areas may contain resolvable areas that will be processed
- * with other resolvable areas
- */
- public class FOUserAgent {
-
- /** Defines the default resolution (72dpi) for FOP */
- public static final float DEFAULT_RESOLUTION = 72.0f; //dpi
- /** Defines the default resolution (72dpi) for FOP */
- public static final float DEFAULT_PX2MM = (25.4f / DEFAULT_RESOLUTION); //dpi (=25.4/dpi)
- /** Defines the default page-height */
- public static final String DEFAULT_PAGE_HEIGHT = "11in";
- /** Defines the default page-width */
- public static final String DEFAULT_PAGE_WIDTH = "8.26in";
-
- /** Factory for Renderers and FOEventHandlers */
- private RendererFactory rendererFactory = new RendererFactory();
-
- /** Registry for XML handlers */
- private XMLHandlerRegistry xmlHandlers = new XMLHandlerRegistry();
-
- private String baseURL;
-
- /** A user settable URI Resolver */
- private URIResolver uriResolver = null;
- /** Our default resolver if none is set */
- private URIResolver foURIResolver = new FOURIResolver();
-
- private PDFEncryptionParams pdfEncryptionParams;
- private float resolution = DEFAULT_RESOLUTION;
- private String pageHeight = DEFAULT_PAGE_HEIGHT;
- private String pageWidth = DEFAULT_PAGE_WIDTH;
- private Map rendererOptions = new java.util.HashMap();
- private File outputFile = null;
- private Renderer rendererOverride = null;
- private FOEventHandler foEventHandlerOverride = null;
- private LayoutManagerMaker lmMakerOverride = null;
- /* user configuration */
- private Configuration userConfig = null;
- private Log log = LogFactory.getLog("FOP");
-
- /* FOP has the ability, for some FO's, to continue processing even if the
- * input XSL violates that FO's content model. This is the default
- * behavior for FOP. However, this flag, if set, provides the user the
- * ability for FOP to halt on all content model violations if desired.
- */
- private boolean strictValidation = true;
-
- /* Additional fo.ElementMapping subclasses set by user */
- private List additionalElementMappings = null;
-
- /** Producer: Metadata element for the system/software that produces
- * the document. (Some renderers can store this in the document.)
- */
- protected String producer = "Apache FOP Version " + Version.getVersion();
-
- /** Creator: Metadata element for the user that created the
- * document. (Some renderers can store this in the document.)
- */
- protected String creator = null;
-
- /** Creation Date: Override of the date the document was created.
- * (Some renderers can store this in the document.)
- */
- protected Date creationDate = null;
-
- /** Author of the content of the document. */
- protected String author = null;
- /** Title of the document. */
- protected String title = null;
- /** Set of keywords applicable to this document. */
- protected String keywords = null;
-
- /**
- * Add the element mapping with the given class name.
- * @param elementMapping the class name representing the element mapping.
- */
- public void addElementMapping(ElementMapping elementMapping) {
- if (additionalElementMappings == null) {
- additionalElementMappings = new java.util.ArrayList();
- }
- additionalElementMappings.add(elementMapping);
- }
-
- /**
- * Returns the List of user-added ElementMapping class names
- * @return List of Strings holding ElementMapping names.
- */
- public List getAdditionalElementMappings() {
- return additionalElementMappings;
- }
-
- /**
- * Sets an explicit renderer to use which overrides the one defined by the
- * render type setting.
- * @param renderer the Renderer instance to use
- */
- public void setRendererOverride(Renderer renderer) {
- this.rendererOverride = renderer;
- }
-
- /**
- * Returns the overriding Renderer instance, if any.
- * @return the overriding Renderer or null
- */
- public Renderer getRendererOverride() {
- return rendererOverride;
- }
-
- /**
- * Sets an explicit FOEventHandler instance which overrides the one
- * defined by the render type setting.
- * @param handler the FOEventHandler instance
- */
- public void setFOEventHandlerOverride(FOEventHandler handler) {
- this.foEventHandlerOverride = handler;
- }
-
- /**
- * Returns the overriding FOEventHandler instance, if any.
- * @return the overriding FOEventHandler or null
- */
- public FOEventHandler getFOEventHandlerOverride() {
- return this.foEventHandlerOverride;
- }
-
- /**
- * Activates strict XSL content model validation for FOP
- * Default is false (FOP will continue processing where it can)
- * @param validateStrictly true to turn on strict validation
- */
- public void setStrictValidation(boolean validateStrictly) {
- this.strictValidation = validateStrictly;
- }
-
- /**
- * Returns whether FOP is strictly validating input XSL
- * @return true of strict validation turned on, false otherwise
- */
- public boolean validateStrictly() {
- return strictValidation;
- }
-
- /**
- * Sets an explicit LayoutManagerMaker instance which overrides the one
- * defined by the AreaTreeHandler.
- * @param lmMaker the LayoutManagerMaker instance
- */
- public void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker) {
- this.lmMakerOverride = lmMaker;
- }
-
- /**
- * Returns the overriding LayoutManagerMaker instance, if any.
- * @return the overriding LayoutManagerMaker or null
- */
- public LayoutManagerMaker getLayoutManagerMakerOverride() {
- return this.lmMakerOverride;
- }
-
- /**
- * Sets the producer of the document.
- * @param producer source of document
- */
- public void setProducer(String producer) {
- this.producer = producer;
- }
-
- /**
- * Returns the producer of the document
- * @return producer name
- */
- public String getProducer() {
- return producer;
- }
-
- /**
- * Sets the creator of the document.
- * @param creator of document
- */
- public void setCreator(String creator) {
- this.creator = creator;
- }
-
- /**
- * Returns the creator of the document
- * @return creator name
- */
- public String getCreator() {
- return creator;
- }
-
- /**
- * Sets the creation date of the document.
- * @param creationDate date of document
- */
- public void setCreationDate(Date creationDate) {
- this.creationDate = creationDate;
- }
-
- /**
- * Returns the creation date of the document
- * @return creation date of document
- */
- public Date getCreationDate() {
- return creationDate;
- }
-
- /**
- * Sets the author of the document.
- * @param author of document
- */
- public void setAuthor(String author) {
- this.author = author;
- }
-
- /**
- * Returns the author of the document
- * @return author name
- */
- public String getAuthor() {
- return author;
- }
-
- /**
- * Sets the title of the document. This will override any title coming from
- * an fo:title element.
- * @param title of document
- */
- public void setTitle(String title) {
- this.title = title;
- }
-
- /**
- * Returns the title of the document
- * @return title name
- */
- public String getTitle() {
- return title;
- }
-
- /**
- * Sets the keywords for the document.
- * @param keywords for the document
- */
- public void setKeywords(String keywords) {
- this.keywords = keywords;
- }
-
- /**
- * Returns the keywords for the document
- * @return the keywords
- */
- public String getKeywords() {
- return keywords;
- }
-
- /**
- * Returns the renderer options
- * @return renderer options
- */
- public Map getRendererOptions() {
- return rendererOptions;
- }
-
- /**
- * Set the user configuration.
- * @param userConfig configuration
- */
- public void setUserConfig(Configuration userConfig) {
- this.userConfig = userConfig;
- try {
- initUserConfig();
- } catch (ConfigurationException cfge) {
- log.error("Error initializing User Agent configuration: "
- + cfge.getMessage());
- }
- }
-
- /**
- * Get the user configuration.
- * @return the user configuration
- */
- public Configuration getUserConfig() {
- return userConfig;
- }
-
- /**
- * Initializes user agent settings from the user configuration
- * file, if present: baseURL, resolution, default page size,...
- *
- * @throws ConfigurationException when there is an entry that
- * misses the required attribute
- */
- public void initUserConfig() throws ConfigurationException {
- log.info("Initializing User Agent Configuration");
- if (userConfig.getChild("base", false) != null) {
- try {
- String cfgBaseDir = userConfig.getChild("base").getValue(null);
- if (cfgBaseDir != null) {
- File dir = new File(cfgBaseDir);
- if (dir.isDirectory()) {
- cfgBaseDir = "file://" + dir.getCanonicalPath()
- + System.getProperty("file.separator");
- cfgBaseDir = cfgBaseDir.replace(
- System.getProperty("file.separator").charAt(0), '/');
- } else {
- //The next statement is for validation only
- new URL(cfgBaseDir);
- }
- }
- setBaseURL(cfgBaseDir);
- } catch (MalformedURLException mue) {
- log.error("Base URL in user config is malformed!");
- } catch (IOException ioe) {
- log.error("Error converting relative base directory to absolute URL.");
- }
- log.info("Base URL set to: " + baseURL);
- }
- if (userConfig.getChild("pixelToMillimeter", false) != null) {
- this.resolution = 25.4f / userConfig.getChild("pixelToMillimeter")
- .getAttributeAsFloat("value", DEFAULT_PX2MM);
- log.info("resolution set to: " + resolution
- + "dpi (px2mm=" + getPixelUnitToMillimeter() + ")");
- } else if (userConfig.getChild("resolution", false) != null) {
- this.resolution
- = 25.4f / userConfig.getChild("resolution").getValueAsFloat(DEFAULT_RESOLUTION);
- log.info("resolution set to: " + resolution
- + "dpi (px2mm=" + getPixelUnitToMillimeter() + ")");
- }
- Configuration pageConfig = userConfig.getChild("default-page-settings");
- if (pageConfig.getAttribute("height", null) != null) {
- setPageHeight(pageConfig.getAttribute("height"));
- log.info("Default page-height set to: " + pageHeight);
- }
- if (pageConfig.getAttribute("width", null) != null) {
- setPageWidth(pageConfig.getAttribute("width"));
- log.info("Default page-width set to: " + pageWidth);
- }
- }
-
- /**
- * Returns the configuration subtree for a specific renderer.
- * @param mimeType MIME type of the renderer
- * @return the requested configuration subtree, null if there's no configuration
- */
- public Configuration getUserRendererConfig (String mimeType) {
-
- if (userConfig == null || mimeType == null) {
- return null;
- }
-
- Configuration userRendererConfig = null;
-
- Configuration[] cfgs
- = userConfig.getChild("renderers").getChildren("renderer");
- for (int i = 0; i < cfgs.length; ++i) {
- Configuration cfg = cfgs[i];
- try {
- if (cfg.getAttribute("mime").equals(mimeType)) {
- userRendererConfig = cfg;
- break;
- }
- } catch (ConfigurationException e) {
- // silently pass over configurations without mime type
- }
- }
- log.debug((userRendererConfig == null ? "No u" : "U")
- + "ser configuration found for MIME type " + mimeType);
- return userRendererConfig;
- }
-
- /**
- * Sets the base URL.
- * @param baseURL base URL
- */
- public void setBaseURL(String baseURL) {
- this.baseURL = baseURL;
- }
-
- /**
- * Returns the base URL.
- * @return the base URL
- */
- public String getBaseURL() {
- return this.baseURL;
- }
-
- /**
- * Sets the URI Resolver.
- * @param uriResolver the new URI resolver
- */
- public void setURIResolver(URIResolver uriResolver) {
- this.uriResolver = uriResolver;
- }
-
- /**
- * Returns the URI Resolver.
- * @return the URI Resolver
- */
- public URIResolver getURIResolver() {
- return this.uriResolver;
- }
-
- /**
- * Returns the parameters for PDF encryption.
- * @return the PDF encryption parameters, null if not applicable
- */
- public PDFEncryptionParams getPDFEncryptionParams() {
- return pdfEncryptionParams;
- }
-
- /**
- * Sets the parameters for PDF encryption.
- * @param pdfEncryptionParams the PDF encryption parameters, null to
- * disable PDF encryption
- */
- public void setPDFEncryptionParams(PDFEncryptionParams pdfEncryptionParams) {
- this.pdfEncryptionParams = pdfEncryptionParams;
- }
-
-
- /**
- * Attempts to resolve the given URI.
- * Will use the configured resolver and if not successful fall back
- * to the default resolver.
- * @param uri URI to access
- * @return A {@link javax.xml.transform.Source} object, or null if the URI
- * cannot be resolved.
- * @see org.apache.fop.apps.FOURIResolver
- */
- public Source resolveURI(String uri) {
- Source source = null;
- if (uriResolver != null) {
- try {
- source = uriResolver.resolve(uri, getBaseURL());
- } catch (TransformerException te) {
- log.error("Attempt to resolve URI '" + uri + "' failed: ", te);
- }
- }
- if (source == null) {
- // URI Resolver not configured or returned null, use default resolver
- try {
- source = foURIResolver.resolve(uri, getBaseURL());
- } catch (TransformerException te) {
- log.error("Attempt to resolve URI '" + uri + "' failed: ", te);
- }
- }
- return source;
- }
-
- /**
- * Sets the output File.
- * @param f the output File
- */
- public void setOutputFile(File f) {
- this.outputFile = f;
- }
-
- /**
- * Gets the output File.
- * @return the output File
- */
- public File getOutputFile() {
- return outputFile;
- }
-
- /**
- * Returns the conversion factor from pixel units to millimeters. This
- * depends on the desired reolution.
- * @return float conversion factor
- */
- public float getPixelUnitToMillimeter() {
- return 25.4f / this.resolution;
- }
-
- /** @return the resolution for resolution-dependant output */
- public float getResolution() {
- return this.resolution;
- }
-
- /**
- * Sets the resolution in dpi.
- * @param dpi resolution in dpi
- */
- public void setResolution(int dpi) {
- this.resolution = dpi;
- }
-
- /**
- * Gets the default page-height to use as fallback,
- * in case page-height="auto"
- *
- * @return the page-height, as a String
- */
- public String getPageHeight() {
- return this.pageHeight;
- }
-
- /**
- * Sets the page-height to use as fallback, in case
- * page-height="auto"
- *
- * @param pageHeight page-height as a String
- */
- public void setPageHeight(String pageHeight) {
- this.pageHeight = pageHeight;
- }
-
- /**
- * Gets the default page-width to use as fallback,
- * in case page-width="auto"
- *
- * @return the page-width, as a String
- */
- public String getPageWidth() {
- return this.pageWidth;
- }
-
- /**
- * Sets the page-width to use as fallback, in case
- * page-width="auto"
- *
- * @param pageWidth page-width as a String
- */
- public void setPageWidth(String pageWidth) {
- this.pageWidth = pageWidth;
- }
-
- /**
- * If to create hot links to footnotes and before floats.
- * @return True if hot links should be created
- */
- /* TODO This method is never referenced!
- public boolean linkToFootnotes() {
- return true;
- }*/
-
- /**
- * @return the RendererFactory
- */
- public RendererFactory getRendererFactory() {
- return this.rendererFactory;
- }
-
- /**
- * @return the XML handler registry
- */
- public XMLHandlerRegistry getXMLHandlerRegistry() {
- return this.xmlHandlers;
- }
-
-
- }
|