From 9e3c365a2ccea075659a0c6ec18f3186b379afc1 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Thu, 19 Jun 2008 09:46:12 +0000 Subject: [PATCH] Separation of concerns: Split FOEventHandler into FOEventHandler and FOTreeBuilderContext. The latter contains stuff only used at tree-building stage. FOEventHandler is oriented towards output. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@669436 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/fop/fo/FOEventHandler.java | 80 ------------- src/java/org/apache/fop/fo/FONode.java | 11 +- src/java/org/apache/fop/fo/FOText.java | 2 +- src/java/org/apache/fop/fo/FOTreeBuilder.java | 17 ++- .../apache/fop/fo/FOTreeBuilderContext.java | 107 ++++++++++++++++++ src/java/org/apache/fop/fo/FObj.java | 4 +- src/java/org/apache/fop/fo/FObjMixed.java | 6 +- .../fop/fo/flow/AbstractRetrieveMarker.java | 14 ++- src/java/org/apache/fop/fo/flow/Marker.java | 10 +- .../org/apache/fop/fo/pagination/Root.java | 25 +++- 10 files changed, 168 insertions(+), 108 deletions(-) create mode 100644 src/java/org/apache/fop/fo/FOTreeBuilderContext.java diff --git a/src/java/org/apache/fop/fo/FOEventHandler.java b/src/java/org/apache/fop/fo/FOEventHandler.java index 7aa6e8eeb..880dd7868 100644 --- a/src/java/org/apache/fop/fo/FOEventHandler.java +++ b/src/java/org/apache/fop/fo/FOEventHandler.java @@ -19,9 +19,6 @@ package org.apache.fop.fo; -import java.util.HashSet; -import java.util.Set; - import org.xml.sax.SAXException; import org.apache.fop.apps.FOUserAgent; @@ -74,27 +71,6 @@ public abstract class FOEventHandler { */ protected FontInfo fontInfo; - /** - * The current set of id's in the FO tree. - * This is used so we know if the FO tree contains duplicates. - */ - private Set idReferences = new HashSet(); - - /** - * The property list maker. - */ - protected PropertyListMaker propertyListMaker; - - /** - * The XMLWhitespaceHandler for this tree - */ - protected XMLWhiteSpaceHandler whiteSpaceHandler = new XMLWhiteSpaceHandler(); - - /** - * Indicates whether processing descendants of a marker - */ - private boolean inMarker = false; - /** * Main constructor * @param foUserAgent the apps.FOUserAgent instance for this process @@ -105,14 +81,6 @@ public abstract class FOEventHandler { this.fontInfo.setEventListener(new FontEventAdapter(foUserAgent.getEventBroadcaster())); } - /** - * Retuns the set of ID references. - * @return the ID references - */ - public Set getIDReferences() { - return idReferences; - } - /** * Returns the User Agent object associated with this FOEventHandler. * @return the User Agent object @@ -129,54 +97,6 @@ public abstract class FOEventHandler { return this.fontInfo; } - /** - * Return the propertyListMaker. - * - * @return the currently active {@link PropertyListMaker} - */ - public PropertyListMaker getPropertyListMaker() { - return propertyListMaker; - } - - /** - * Set a new propertyListMaker. - * - * @param propertyListMaker the new {@link PropertyListMaker} to use - */ - public void setPropertyListMaker(PropertyListMaker propertyListMaker) { - this.propertyListMaker = propertyListMaker; - } - - /** - * Return the XMLWhiteSpaceHandler - * @return the whiteSpaceHandler - */ - public XMLWhiteSpaceHandler getXMLWhiteSpaceHandler() { - return whiteSpaceHandler; - } - - /** - * Switch to or from marker context - * (used by FOTreeBuilder when processing - * a marker) - * - * @param inMarker true if a marker is being processed; - * false otherwise - * - */ - protected void switchMarkerContext(boolean inMarker) { - this.inMarker = inMarker; - } - - /** - * Check whether in marker context - * - * @return true if a marker is being processed - */ - protected boolean inMarker() { - return this.inMarker; - } - /** * This method is called to indicate the start of a new document run. * @throws SAXException In case of a problem diff --git a/src/java/org/apache/fop/fo/FONode.java b/src/java/org/apache/fop/fo/FONode.java index 951e4c430..8ce570056 100644 --- a/src/java/org/apache/fop/fo/FONode.java +++ b/src/java/org/apache/fop/fo/FONode.java @@ -22,7 +22,6 @@ package org.apache.fop.fo; // Java import java.util.ListIterator; import java.util.Map; -import java.util.NoSuchElementException; import org.xml.sax.Attributes; import org.xml.sax.Locator; @@ -157,12 +156,20 @@ public abstract class FONode implements Cloneable { return parent.getFOEventHandler(); } + /** + * Returns the context class providing information used during FO tree building. + * @return the builder context + */ + public FOTreeBuilderContext getBuilderContext() { + return parent.getBuilderContext(); + } + /** * Indicates whether this node is a child of an fo:marker. * @return true if this node is a child of an fo:marker */ protected boolean inMarker() { - return getFOEventHandler().inMarker(); + return getBuilderContext().inMarker(); } /** diff --git a/src/java/org/apache/fop/fo/FOText.java b/src/java/org/apache/fop/fo/FOText.java index f21386075..c53937bb1 100644 --- a/src/java/org/apache/fop/fo/FOText.java +++ b/src/java/org/apache/fop/fo/FOText.java @@ -245,7 +245,7 @@ public class FOText extends FONode { * text-transform property. */ private void textTransform() { - if (getFOEventHandler().inMarker() + if (getBuilderContext().inMarker() || textTransform == Constants.EN_NONE) { return; } diff --git a/src/java/org/apache/fop/fo/FOTreeBuilder.java b/src/java/org/apache/fop/fo/FOTreeBuilder.java index b1a0467b1..264ef986d 100644 --- a/src/java/org/apache/fop/fo/FOTreeBuilder.java +++ b/src/java/org/apache/fop/fo/FOTreeBuilder.java @@ -66,6 +66,9 @@ public class FOTreeBuilder extends DefaultHandler { /** Current delegate ContentHandler to receive the SAX events */ protected ContentHandler delegate; + + /** Provides information used during tree building stage. */ + private FOTreeBuilderContext builderContext; /** The object that handles formatting and rendering to a stream */ private FOEventHandler foEventHandler; @@ -101,7 +104,8 @@ public class FOTreeBuilder extends DefaultHandler { //one of the RTF-, MIF- etc. Handlers. foEventHandler = foUserAgent.getRendererFactory().createFOEventHandler( foUserAgent, outputFormat, stream); - foEventHandler.setPropertyListMaker(new PropertyListMaker() { + builderContext = new FOTreeBuilderContext(); + builderContext.setPropertyListMaker(new PropertyListMaker() { public PropertyList make(FObj fobj, PropertyList parentPropertyList) { return new StaticPropertyList(fobj, parentPropertyList); } @@ -270,6 +274,7 @@ public class FOTreeBuilder extends DefaultHandler { foNode = fobjMaker.make(currentFObj); if (rootFObj == null) { rootFObj = (Root) foNode; + rootFObj.setBuilderContext(builderContext); rootFObj.setFOEventHandler(foEventHandler); } propertyList = foNode.createPropertyList( @@ -277,10 +282,10 @@ public class FOTreeBuilder extends DefaultHandler { foNode.processNode(localName, getEffectiveLocator(), attlist, propertyList); if (foNode.getNameId() == Constants.FO_MARKER) { - if (foEventHandler.inMarker()) { + if (builderContext.inMarker()) { nestedMarkerDepth++; } else { - foEventHandler.switchMarkerContext(true); + builderContext.switchMarkerContext(true); } } foNode.startOfNode(); @@ -309,7 +314,7 @@ public class FOTreeBuilder extends DefaultHandler { } currentFObj = foNode; - if (propertyList != null && !foEventHandler.inMarker()) { + if (propertyList != null && !builderContext.inMarker()) { currentPropertyList = propertyList; } } @@ -332,13 +337,13 @@ public class FOTreeBuilder extends DefaultHandler { if (currentPropertyList != null && currentPropertyList.getFObj() == currentFObj - && !foEventHandler.inMarker()) { + && !builderContext.inMarker()) { currentPropertyList = currentPropertyList.getParentPropertyList(); } if (currentFObj.getNameId() == Constants.FO_MARKER) { if (nestedMarkerDepth == 0) { - foEventHandler.switchMarkerContext(false); + builderContext.switchMarkerContext(false); } else { nestedMarkerDepth--; } diff --git a/src/java/org/apache/fop/fo/FOTreeBuilderContext.java b/src/java/org/apache/fop/fo/FOTreeBuilderContext.java new file mode 100644 index 000000000..0cbdd7797 --- /dev/null +++ b/src/java/org/apache/fop/fo/FOTreeBuilderContext.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.fo; + +import java.util.HashSet; +import java.util.Set; + +/** + * Context class providing information needed while building the FO tree. + */ +public class FOTreeBuilderContext { + + /** + * The current set of id's in the FO tree. + * This is used so we know if the FO tree contains duplicates. + */ + private Set idReferences = new HashSet(); + + /** + * The property list maker. + */ + protected PropertyListMaker propertyListMaker; + + /** + * The XMLWhitespaceHandler for this tree + */ + protected XMLWhiteSpaceHandler whiteSpaceHandler = new XMLWhiteSpaceHandler(); + + /** + * Indicates whether processing descendants of a marker + */ + private boolean inMarker = false; + + /** + * Returns the set of ID references. + * @return the ID references + */ + public Set getIDReferences() { + return idReferences; + } + + /** + * Return the propertyListMaker. + * + * @return the currently active {@link PropertyListMaker} + */ + public PropertyListMaker getPropertyListMaker() { + return propertyListMaker; + } + + /** + * Set a new propertyListMaker. + * + * @param propertyListMaker the new {@link PropertyListMaker} to use + */ + public void setPropertyListMaker(PropertyListMaker propertyListMaker) { + this.propertyListMaker = propertyListMaker; + } + + /** + * Return the XMLWhiteSpaceHandler + * @return the whiteSpaceHandler + */ + public XMLWhiteSpaceHandler getXMLWhiteSpaceHandler() { + return whiteSpaceHandler; + } + + /** + * Switch to or from marker context + * (used by FOTreeBuilder when processing + * a marker) + * + * @param inMarker true if a marker is being processed; + * false otherwise + * + */ + protected void switchMarkerContext(boolean inMarker) { + this.inMarker = inMarker; + } + + /** + * Check whether in marker context + * + * @return true if a marker is being processed + */ + protected boolean inMarker() { + return this.inMarker; + } + +} diff --git a/src/java/org/apache/fop/fo/FObj.java b/src/java/org/apache/fop/fo/FObj.java index 0bbec4d47..45b89dc8a 100644 --- a/src/java/org/apache/fop/fo/FObj.java +++ b/src/java/org/apache/fop/fo/FObj.java @@ -130,7 +130,7 @@ public abstract class FObj extends FONode implements Constants { */ protected PropertyList createPropertyList(PropertyList parent, FOEventHandler foEventHandler) throws FOPException { - return foEventHandler.getPropertyListMaker().make(this, parent); + return getBuilderContext().getPropertyListMaker().make(this, parent); } /** @@ -165,7 +165,7 @@ public abstract class FObj extends FONode implements Constants { */ private void checkId(String id) throws ValidationException { if (!inMarker() && !id.equals("")) { - Set idrefs = getFOEventHandler().getIDReferences(); + Set idrefs = getBuilderContext().getIDReferences(); if (!idrefs.contains(id)) { idrefs.add(id); } else { diff --git a/src/java/org/apache/fop/fo/FObjMixed.java b/src/java/org/apache/fop/fo/FObjMixed.java index bf7383398..b0b9cd04b 100644 --- a/src/java/org/apache/fop/fo/FObjMixed.java +++ b/src/java/org/apache/fop/fo/FObjMixed.java @@ -68,7 +68,7 @@ public abstract class FObjMixed extends FObj { flushText(); if (!inMarker() || getNameId() == FO_MARKER) { - getFOEventHandler().whiteSpaceHandler + getBuilderContext().whiteSpaceHandler .handleWhiteSpace(this, currentTextNode); } super.endOfNode(); @@ -83,7 +83,7 @@ public abstract class FObjMixed extends FObj { * @param fobj the node for which to handle white-space */ protected static void handleWhiteSpaceFor(FObjMixed fobj) { - fobj.getFOEventHandler().getXMLWhiteSpaceHandler() + fobj.getBuilderContext().getXMLWhiteSpaceHandler() .handleWhiteSpace(fobj, fobj.currentTextNode); } @@ -159,7 +159,7 @@ public abstract class FObjMixed extends FObj { } } else { // handle white-space for all text up to here - getFOEventHandler().whiteSpaceHandler + getBuilderContext().whiteSpaceHandler .handleWhiteSpace(this, currentTextNode, child); currentTextNode = null; } diff --git a/src/java/org/apache/fop/fo/flow/AbstractRetrieveMarker.java b/src/java/org/apache/fop/fo/flow/AbstractRetrieveMarker.java index 83a0ddbdc..a276e4214 100644 --- a/src/java/org/apache/fop/fo/flow/AbstractRetrieveMarker.java +++ b/src/java/org/apache/fop/fo/flow/AbstractRetrieveMarker.java @@ -16,20 +16,22 @@ */ /* $Id$ */ + package org.apache.fop.fo.flow; +import java.util.Iterator; + +import org.xml.sax.Locator; + +import org.apache.fop.apps.FOPException; import org.apache.fop.fo.FONode; import org.apache.fop.fo.FOText; import org.apache.fop.fo.FObj; import org.apache.fop.fo.FObjMixed; import org.apache.fop.fo.PropertyList; import org.apache.fop.fo.ValidationException; -import org.apache.fop.fo.flow.table.TableFObj; import org.apache.fop.fo.flow.table.Table; -import org.apache.fop.apps.FOPException; -import org.xml.sax.Locator; - -import java.util.Iterator; +import org.apache.fop.fo.flow.table.TableFObj; /** * Abstract base class for the @@ -80,7 +82,7 @@ public abstract class AbstractRetrieveMarker extends FObjMixed { } private PropertyList createPropertyListFor(FObj fo, PropertyList parent) { - return getFOEventHandler().getPropertyListMaker().make(fo, parent); + return getBuilderContext().getPropertyListMaker().make(fo, parent); } private void cloneSingleNode(FONode child, FONode newParent, diff --git a/src/java/org/apache/fop/fo/flow/Marker.java b/src/java/org/apache/fop/fo/flow/Marker.java index 7eda2101e..ea6721686 100644 --- a/src/java/org/apache/fop/fo/flow/Marker.java +++ b/src/java/org/apache/fop/fo/flow/Marker.java @@ -26,8 +26,8 @@ import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.apache.fop.apps.FOPException; -import org.apache.fop.fo.FOEventHandler; import org.apache.fop.fo.FONode; +import org.apache.fop.fo.FOTreeBuilderContext; import org.apache.fop.fo.FObj; import org.apache.fop.fo.FObjMixed; import org.apache.fop.fo.PropertyList; @@ -84,10 +84,10 @@ public class Marker extends FObjMixed { /** {@inheritDoc} */ protected void startOfNode() { - FOEventHandler foEventHandler = getFOEventHandler(); + FOTreeBuilderContext builderContext = getBuilderContext(); // Push a new property list maker which will make MarkerPropertyLists. - savePropertyListMaker = foEventHandler.getPropertyListMaker(); - foEventHandler.setPropertyListMaker(new PropertyListMaker() { + savePropertyListMaker = builderContext.getPropertyListMaker(); + builderContext.setPropertyListMaker(new PropertyListMaker() { public PropertyList make(FObj fobj, PropertyList parentPropertyList) { PropertyList pList = new MarkerPropertyList(fobj, parentPropertyList); descendantPropertyLists.put(fobj, pList); @@ -100,7 +100,7 @@ public class Marker extends FObjMixed { protected void endOfNode() throws FOPException { super.endOfNode(); // Pop the MarkerPropertyList maker. - getFOEventHandler().setPropertyListMaker(savePropertyListMaker); + getBuilderContext().setPropertyListMaker(savePropertyListMaker); savePropertyListMaker = null; } diff --git a/src/java/org/apache/fop/fo/pagination/Root.java b/src/java/org/apache/fop/fo/pagination/Root.java index 04014f6fc..c6346e9fb 100644 --- a/src/java/org/apache/fop/fo/pagination/Root.java +++ b/src/java/org/apache/fop/fo/pagination/Root.java @@ -27,6 +27,7 @@ import org.xml.sax.Locator; import org.apache.fop.apps.FOPException; import org.apache.fop.fo.FOEventHandler; import org.apache.fop.fo.FONode; +import org.apache.fop.fo.FOTreeBuilderContext; import org.apache.fop.fo.FObj; import org.apache.fop.fo.PropertyList; import org.apache.fop.fo.ValidationException; @@ -58,6 +59,11 @@ public class Root extends FObj { private int endingPageNumberOfPreviousSequence = 0; private int totalPagesGenerated = 0; + /** + * Context class used while building the FO tree. + */ + private FOTreeBuilderContext builderContext; + /** * FOEventHandler object for this FO Tree */ @@ -164,10 +170,23 @@ public class Root extends FObj { return foEventHandler; } - /** - * Gets the last page number generated by the previous page-sequence - * @return the last page number, 0 if no page sequences yet generated + /** + * Sets the builder context for this FO tree. + * @param context the builder context to be used */ + public void setBuilderContext(FOTreeBuilderContext context) { + this.builderContext = context; + } + + /** {@inheritDoc} */ + public FOTreeBuilderContext getBuilderContext() { + return this.builderContext; + } + + /** + * Gets the last page number generated by the previous page-sequence + * @return the last page number, 0 if no page sequences yet generated + */ public int getEndingPageNumberOfPreviousSequence() { return endingPageNumberOfPreviousSequence; } -- 2.39.5