diff options
author | Glen Mazza <gmazza@apache.org> | 2003-12-14 01:40:50 +0000 |
---|---|---|
committer | Glen Mazza <gmazza@apache.org> | 2003-12-14 01:40:50 +0000 |
commit | b62b1b075649a1008eba946291154d0a7d2f0417 (patch) | |
tree | 68f3c568cdebe8915fbd0454fcdc8acca9ac3180 | |
parent | 3ec90e7bbe5e3592b4ba51c73feeb95690ee46c5 (diff) | |
download | xmlgraphics-fop-b62b1b075649a1008eba946291154d0a7d2f0417.tar.gz xmlgraphics-fop-b62b1b075649a1008eba946291154d0a7d2f0417.zip |
Consolidated PropertyListBuilder and PropertyList.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@197036 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | src/java/org/apache/fop/fo/FObj.java | 38 | ||||
-rw-r--r-- | src/java/org/apache/fop/fo/PropertyList.java | 437 | ||||
-rw-r--r-- | src/java/org/apache/fop/fo/PropertyListBuilder.java | 431 |
3 files changed, 379 insertions, 527 deletions
diff --git a/src/java/org/apache/fop/fo/FObj.java b/src/java/org/apache/fop/fo/FObj.java index acacfbf46..0cb569789 100644 --- a/src/java/org/apache/fop/fo/FObj.java +++ b/src/java/org/apache/fop/fo/FObj.java @@ -59,7 +59,6 @@ import java.util.Set; import org.apache.fop.apps.FOPException; import org.apache.fop.fo.flow.Marker; -import org.apache.fop.fo.properties.FOPropertyMapping; import org.xml.sax.Attributes; /** @@ -69,19 +68,12 @@ public class FObj extends FONode { private static final String FO_URI = "http://www.w3.org/1999/XSL/Format"; /** - * Static property list builder that converts xml attributes - * into fo properties. This is static since the underlying - * property mappings for fo are also static. - */ - protected static PropertyListBuilder plb = null; - - /** * Formatting properties for this fo element. */ public PropertyList properties; /** - * Property manager for handler some common properties. + * Property manager for handling some common properties. */ protected PropertyManager propMgr; @@ -127,22 +119,6 @@ public class FObj extends FONode { name = "fo:" + str; } - protected PropertyListBuilder getListBuilder() { - if (plb == null) { - plb = new PropertyListBuilder(); - plb.addList(FOPropertyMapping.getGenericMappings()); - - for (Iterator iter = - FOPropertyMapping.getElementMappings().iterator(); - iter.hasNext();) { - String elem = (String) iter.next(); - plb.addElementList(elem, - FOPropertyMapping.getElementMapping(elem)); - } - } - return plb; - } - /** * Handle the attributes for this element. * The attributes must be used immediately as the sax attributes @@ -151,8 +127,14 @@ public class FObj extends FONode { * @throws FOPException for invalid FO data */ public void handleAttrs(Attributes attlist) throws FOPException { - properties = getListBuilder().makeList(FO_URI, name, attlist, this); - properties.setFObj(this); + FObj parentFO = findNearestAncestorFObj(); + PropertyList parentProperties = null; + if (parentFO != null) { + parentProperties = parentFO.getPropertiesForNamespace(FO_URI); + } + + properties = new PropertyList(this, parentProperties, FO_URI, name); + properties.addAttributesToList(attlist); this.propMgr = makePropertyManager(properties); setWritingMode(); } @@ -166,7 +148,7 @@ public class FObj extends FONode { while (par != null && !(par instanceof FObj)) { par = par.parent; } - return (FObj)par; + return (FObj) par; } /** diff --git a/src/java/org/apache/fop/fo/PropertyList.java b/src/java/org/apache/fop/fo/PropertyList.java index 1f9224f21..6bbb2225b 100644 --- a/src/java/org/apache/fop/fo/PropertyList.java +++ b/src/java/org/apache/fop/fo/PropertyList.java @@ -50,10 +50,20 @@ */ package org.apache.fop.fo; +// Java import java.util.HashMap; +import java.util.Iterator; +import org.xml.sax.Attributes; + +// FOP +import org.apache.fop.apps.FOPException; +import org.apache.fop.fo.Property.Maker; +import org.apache.fop.fo.properties.FOPropertyMapping; import org.apache.fop.fo.properties.WritingMode; import org.apache.fop.apps.FOPException; + + /** * Class containing the collection of properties for a given FObj. */ @@ -115,31 +125,42 @@ public class PropertyList extends HashMap { }); } - private PropertyListBuilder builder; private PropertyList parentPropertyList = null; private String namespace = ""; private String element = ""; private FObj fobj = null; + private static HashMap propertyListTable = null; + private static HashMap elementTable = null; + /** * Basic constructor. - * @param parentPropertyList the PropertyList belongin to the new objects + * @param parentPropertyList the PropertyList belonging to the new objects * parent * @param space name of namespace * @param el name of element */ - public PropertyList(PropertyList parentPropertyList, String space, - String el) { + public PropertyList(FObj fObjToAttach, PropertyList parentPropertyList, + String space, String elementName) { + this.fobj = fObjToAttach; this.parentPropertyList = parentPropertyList; this.namespace = space; - this.element = el; - } - - /** - * @param fobj the FObj object to which this propertyList should be attached - */ - public void setFObj(FObj fobj) { - this.fobj = fobj; + this.element = elementName; + + if (propertyListTable == null) { + propertyListTable = new HashMap(); + propertyListTable.putAll(FOPropertyMapping.getGenericMappings()); + } + + if (elementTable == null) { + elementTable = new HashMap(); + for (Iterator iter = + FOPropertyMapping.getElementMappings().iterator(); + iter.hasNext();) { + String elem = (String) iter.next(); + elementTable.put(elem, FOPropertyMapping.getElementMapping(elem)); + } + } } /** @@ -178,10 +199,10 @@ public class PropertyList extends HashMap { } Property p = getExplicitBaseProp(baseName); if (p == null) { - p = builder.getShorthand(this, namespace, element, baseName); + p = getShorthand(namespace, element, baseName); } if (p != null && sepchar > -1) { - return builder.getSubpropValue(namespace, element, baseName, p, + return getSubpropValue(namespace, element, baseName, p, propertyName.substring(sepchar + 1)); } @@ -201,10 +222,8 @@ public class PropertyList extends HashMap { String baseName = propertyName.substring(0, sepchar); Property p = getExplicitBaseProp(baseName); if (p != null) { - return this.builder.getSubpropValue(namespace, element, - baseName, p, - propertyName.substring(sepchar - + 1)); + return getSubpropValue(namespace, element, baseName, p, + propertyName.substring(sepchar + 1)); } else { return null; } @@ -229,23 +248,19 @@ public class PropertyList extends HashMap { * @return The inherited value, otherwise null. */ public Property getInherited(String propertyName) { - if (builder != null) { - if (parentPropertyList != null - && builder.isInherited(namespace, element, - propertyName)) { - return parentPropertyList.get(propertyName); - } else { - // return the "initial" value - try { - return builder.makeProperty(this, namespace, element, - propertyName); - } catch (org.apache.fop.apps.FOPException e) { - //log.error("Exception in getInherited(): property=" - // + propertyName + " : " + e); - } + if (parentPropertyList != null + && isInherited(namespace, element, propertyName)) { + return parentPropertyList.get(propertyName); + } else { + // return the "initial" value + try { + return makeProperty(namespace, element, propertyName); + } catch (org.apache.fop.apps.FOPException e) { + //log.error("Exception in getInherited(): property=" + // + propertyName + " : " + e); } } - return null; // No builder or exception in makeProperty! + return null; // Exception in makeProperty! } /* @@ -258,25 +273,20 @@ public class PropertyList extends HashMap { */ private Property findProperty(String propertyName, boolean bTryInherit) { Property p = null; - if (builder.isCorrespondingForced(this, namespace, element, - propertyName)) { - p = builder.computeProperty(this, namespace, element, - propertyName); + if (isCorrespondingForced(namespace, element, propertyName)) { + p = computeProperty(namespace, element, propertyName); } else { p = getExplicitBaseProp(propertyName); if (p == null) { - p = this.builder.computeProperty(this, namespace, element, - propertyName); + p = this.computeProperty(namespace, element, propertyName); } if (p == null) { // check for shorthand specification - p = builder.getShorthand(this, namespace, element, - propertyName); + p = getShorthand(namespace, element, propertyName); } - if (p == null - && bTryInherit) { // else inherit (if has parent and is inheritable) + if (p == null && bTryInherit) { + // else inherit (if has parent and is inheritable) if (this.parentPropertyList != null - && builder.isInherited(namespace, element, - propertyName)) { + && isInherited(namespace, element, propertyName)) { p = parentPropertyList.findProperty(propertyName, true); } } @@ -317,10 +327,7 @@ public class PropertyList extends HashMap { private Property get(String propertyName, boolean bTryInherit, boolean bTryDefault) { - if (builder == null) { - //log.error("OH OH, builder has not been set"); - } - /* Handle request for one part of a compound property */ + /* Handle request for one part of a compound property */ int sepchar = propertyName.indexOf('.'); String subpropName = null; if (sepchar > -1) { @@ -331,8 +338,7 @@ public class PropertyList extends HashMap { Property p = findProperty(propertyName, bTryInherit); if (p == null && bTryDefault) { // default value for this FO! try { - p = this.builder.makeProperty(this, namespace, element, - propertyName); + p = makeProperty(namespace, element, propertyName); } catch (FOPException e) { // don't know what to do here } @@ -347,22 +353,14 @@ public class PropertyList extends HashMap { } if (subpropName != null && p != null) { - return this.builder.getSubpropValue(namespace, element, - propertyName, p, subpropName); + return getSubpropValue(namespace, element, propertyName, p, + subpropName); } else { return p; } } /** - * - * @param builder the PropertyListBuilder to attache to this object - */ - public void setBuilder(PropertyListBuilder builder) { - this.builder = builder; - } - - /** * @return the namespace of this element */ public String getNameSpace() { @@ -392,8 +390,7 @@ public class PropertyList extends HashMap { if (p == null) { // If no explicit setting found, return initial (default) value. try { - p = this.builder.makeProperty(this, namespace, element, - propertyName); + p = makeProperty(namespace, element, propertyName); } catch (FOPException e) { //log.error("Exception in getNearestSpecified(): property=" // + propertyName + " : " + e); @@ -412,17 +409,15 @@ public class PropertyList extends HashMap { public Property getFromParent(String propertyName) { if (parentPropertyList != null) { return parentPropertyList.get(propertyName); - } else if (builder != null) { - // return the "initial" value + } else { try { - return builder.makeProperty(this, namespace, element, - propertyName); + return makeProperty(namespace, element, propertyName); } catch (org.apache.fop.apps.FOPException e) { //log.error("Exception in getFromParent(): property=" // + propertyName + " : " + e); } } - return null; // No builder or exception in makeProperty! + return null; // Exception in makeProperty! } /** @@ -463,5 +458,311 @@ public class PropertyList extends HashMap { this.wmtable = (byte[])WRITING_MODE_TABLES.get(new Integer(writingMode)); } + /** + * + * @param nameSpaceURI URI for the namespace of the element to which + * the attributes belong. + * @param elementName Local name for the element to which the attributes + * belong. + * @param attributes Collection of attributes passed to us from the parser. + * @param fo The FObj to which the attributes need to be attached as + * properties. + * @throws FOPException If an error occurs while building the PropertyList + */ + public void addAttributesToList(Attributes attributes) + throws FOPException { + /* + * If font-size is set on this FO, must set it first, since + * other attributes specified in terms of "ems" depend on it. + */ + /** @todo When we do "shorthand" properties, must handle the + * "font" property as well to see if font-size is set. + */ + String attributeName = "font-size"; + String attributeValue = attributes.getValue(attributeName); + convertAttributeToProperty(attributes, attributeName, + attributeValue); + + for (int i = 0; i < attributes.getLength(); i++) { + attributeName = attributes.getQName(i); + attributeValue = attributes.getValue(i); + convertAttributeToProperty(attributes, attributeName, + attributeValue); + } + } + + /** + * + * @param attributes Collection of attributes + * @param attributeName Attribute name to convert + * @param attributeValue Attribute value to assign to property + * @param validProperties Collection of valid properties + * @param parentFO Parent FO of the object for which this property is being + * built + */ + private void convertAttributeToProperty(Attributes attributes, + String attributeName, + String attributeValue) { + + FObj parentFO = fobj.findNearestAncestorFObj(); + + HashMap validProperties; + validProperties = (HashMap) elementTable.get(element); + + /* Handle "compound" properties, ex. space-before.minimum */ + String basePropertyName = findBasePropertyName(attributeName); + String subPropertyName = findSubPropertyName(attributeName); + + Property.Maker propertyMaker = findMaker(validProperties, + basePropertyName); + if (propertyMaker == null) { + handleInvalidProperty(attributeName); + return; + } + if (attributeValue == null) { + return; + } + try { + Property prop = null; + if (subPropertyName == null) { + prop = propertyMaker.make(this, attributeValue, parentFO); + } else { + Property baseProperty = findBaseProperty(attributes, + parentFO, basePropertyName, propertyMaker); + prop = propertyMaker.make(baseProperty, subPropertyName, + this, attributeValue, parentFO); + } + if (prop != null) { + put(basePropertyName, prop); + } + } catch (FOPException e) { + /**@todo log this exception */ + // log.error(e.getMessage()); + } + } + + private Property findBaseProperty(Attributes attributes, + FObj parentFO, + String basePropName, + Maker propertyMaker) + throws FOPException { + // If the baseProperty has already been created, return it + Property baseProperty = getExplicitBaseProp(basePropName); + if (baseProperty != null) { + return baseProperty; + } + // If it is specified later in this list of Attributes, create it + String basePropertyValue = attributes.getValue(basePropName); + if (basePropertyValue != null) { + baseProperty = propertyMaker.make(this, basePropertyValue, + parentFO); + return baseProperty; + } + // Otherwise it is a compound property ?? + // baseProperty = propertyMaker.makeCompound(propertyList, parentFO); + return baseProperty; + } + + private void handleInvalidProperty(String attributeName) { + if (!attributeName.startsWith("xmlns")) { + //log.error("property '" + // + attributeName + "' ignored"); + } + } + + /** + * Finds the first or base part (up to any period) of an attribute name. + * For example, if input is "space-before.minimum", should return + * "space-before". + * @param attributeName String to be atomized + * @return the base portion of the attribute + */ + public static String findBasePropertyName(String attributeName) { + int sepCharIndex = attributeName.indexOf('.'); + String basePropName = attributeName; + if (sepCharIndex > -1) { + basePropName = attributeName.substring(0, sepCharIndex); + } + return basePropName; + } + + /** + * Finds the second or sub part (portion past any period) of an attribute + * name. For example, if input is "space-before.minimum", should return + * "minimum". + * @param attributeName String to be atomized + * @return the sub portion of the attribute + */ + public static String findSubPropertyName(String attributeName) { + int sepCharIndex = attributeName.indexOf('.'); + String subPropName = null; + if (sepCharIndex > -1) { + subPropName = attributeName.substring(sepCharIndex + 1); + } + return subPropName; + } + + /** + * @param space namespace of element + * @param element name of element + * @param propertyName name of property + * @param p a Property object + * @param subpropName name of the sub-property to get + * @return the sub-property + */ + public Property getSubpropValue(String space, String element, + String propertyName, Property p, + String subpropName) { + Property.Maker maker = findMaker(space, element, propertyName); + if (maker != null) { + return maker.getSubpropValue(p, subpropName); + } else { + return null; + } + } + + /** + * @param space namespace of element + * @param element name of element + * @param propertyName name of property + * @return value from the appropriate Property.Maker + */ + public boolean isCorrespondingForced(String space, String element, + String propertyName) { + Property.Maker propertyMaker = findMaker(space, element, + propertyName); + if (propertyMaker != null) { + return propertyMaker.isCorrespondingForced(this); + } else { + //log.error("no Maker for " + propertyName); + } + return false; + } + + /** + * @param space namespace of element + * @param element name of element + * @param propertyName name of property + * @return new Property object + */ + public Property getShorthand(String space, String element, + String propertyName) { + Property.Maker propertyMaker = findMaker(space, element, + propertyName); + if (propertyMaker != null) { + return propertyMaker.getShorthand(this); + } else { + //log.error("no Maker for " + propertyName); + return null; + } + } + + /** + * @param space namespace of element + * @param element name of element + * @param propertyName name of property + * @return new Property object + * @throws FOPException for errors in the input + */ + public Property makeProperty(String space, String element, + String propertyName) throws FOPException { + + Property p = null; + + Property.Maker propertyMaker = findMaker(space, element, + propertyName); + if (propertyMaker != null) { + p = propertyMaker.make(this); + } else { + //log.error("property " + propertyName + // + " ignored"); + } + return p; + } + + /** + * @param space namespace of element + * @param elementName name of element + * @param propertyName name of property + * @return the Property.Maker for this property + */ + protected Property.Maker findMaker(String space, String elementName, + String propertyName) { + return findMaker((HashMap)elementTable.get(elementName), + propertyName); + } + + /** + * Convenience function to return the Maker for a given property + * given the HashMap containing properties specific to this element. + * If table is non-null and + * @param elemTable Element-specific properties or null if none. + * @param propertyName Name of property. + * @return A Maker for this property. + */ + private Property.Maker findMaker(HashMap elemTable, + String propertyName) { + Property.Maker propertyMaker = null; + if (elemTable != null) { + propertyMaker = (Property.Maker)elemTable.get(propertyName); + } + if (propertyMaker == null) { + propertyMaker = + (Property.Maker)propertyListTable.get(propertyName); + } + return propertyMaker; + } + + /** + * + * @param propertyList collection of properties + * @param space namespace of element + * @param element name of element + * @param propertyName name of property + * @return the requested Property object + */ + public Property computeProperty(String space, String element, + String propertyName) { + + Property p = null; + Property.Maker propertyMaker = findMaker(space, element, + propertyName); + if (propertyMaker != null) { + try { + p = propertyMaker.compute(this); + } catch (FOPException e) { + //log.error("exception occurred while computing" + // + " value of property '" + // + propertyName + "': " + // + e.getMessage()); + } + } else { + //log.error("property " + propertyName + // + " ignored"); + } + return p; + } + + /** + * + * @param space namespace of element + * @param element name of element + * @param propertyName name of property + * @return isInherited value from the requested Property.Maker + */ + public boolean isInherited(String space, String element, + String propertyName) { + boolean b; + + Property.Maker propertyMaker = findMaker(space, element, + propertyName); + if (propertyMaker != null) { + b = propertyMaker.isInherited(); + } else { + // log.error("Unknown property " + propertyName); + b = true; + } + return b; + } } diff --git a/src/java/org/apache/fop/fo/PropertyListBuilder.java b/src/java/org/apache/fop/fo/PropertyListBuilder.java deleted file mode 100644 index bb370c859..000000000 --- a/src/java/org/apache/fop/fo/PropertyListBuilder.java +++ /dev/null @@ -1,431 +0,0 @@ -/* - * $Id: PropertyListBuilder.java,v 1.35 2003/03/05 21:48:02 jeremias Exp $ - * ============================================================================ - * The Apache Software License, Version 1.1 - * ============================================================================ - * - * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modifica- - * tion, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. The end-user documentation included with the redistribution, if any, must - * include the following acknowledgment: "This product includes software - * developed by the Apache Software Foundation (http://www.apache.org/)." - * Alternately, this acknowledgment may appear in the software itself, if - * and wherever such third-party acknowledgments normally appear. - * - * 4. The names "FOP" and "Apache Software Foundation" must not be used to - * endorse or promote products derived from this software without prior - * written permission. For written permission, please contact - * apache@apache.org. - * - * 5. Products derived from this software may not be called "Apache", nor may - * "Apache" appear in their name, without prior written permission of the - * Apache Software Foundation. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- - * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * ============================================================================ - * - * This software consists of voluntary contributions made by many individuals - * on behalf of the Apache Software Foundation and was originally created by - * James Tauber <jtauber@jtauber.com>. For more information on the Apache - * Software Foundation, please see <http://www.apache.org/>. - */ -package org.apache.fop.fo; - -// Java -import java.util.HashMap; -import org.xml.sax.Attributes; - -// FOP -import org.apache.fop.apps.FOPException; -import org.apache.fop.fo.Property.Maker; - -/** - * Class for managing the PropertyList objects, including adding items to them - * and retrieving items from them. - */ -public class PropertyListBuilder { - - /** - * Name of font-size property attribute to set first. - */ - private static final String FONTSIZEATTR = "font-size"; - - private HashMap propertyListTable; - private HashMap elementTable; - - /** - * Basic Constructor, initialized the tables. - */ - public PropertyListBuilder() { - this.propertyListTable = new HashMap(); - this.elementTable = new HashMap(); - } - - /** - * Add the entire propertyListTable to the list - * @param list list to which the elements of propertyListTable should be - * added - */ - public void addList(HashMap list) { - propertyListTable.putAll(list); - } - - /** - * Adds a single element to the list - * @param element name of element to add - * @param list list to which the element should be added - */ - public void addElementList(String element, HashMap list) { - elementTable.put(element, list); - } - - /** - * - * @param propertyList collection of properties - * @param space namespace of element - * @param element name of element - * @param propertyName name of property - * @return the requested Property object - */ - public Property computeProperty(PropertyList propertyList, String space, - String element, String propertyName) { - - Property p = null; - Property.Maker propertyMaker = findMaker(space, element, - propertyName); - if (propertyMaker != null) { - try { - p = propertyMaker.compute(propertyList); - } catch (FOPException e) { - //log.error("exception occurred while computing" - // + " value of property '" - // + propertyName + "': " - // + e.getMessage()); - } - } else { - //log.error("property " + propertyName - // + " ignored"); - } - return p; - } - - /** - * - * @param space namespace of element - * @param element name of element - * @param propertyName name of property - * @return isInherited value from the requested Property.Maker - */ - public boolean isInherited(String space, String element, - String propertyName) { - boolean b; - - Property.Maker propertyMaker = findMaker(space, element, - propertyName); - if (propertyMaker != null) { - b = propertyMaker.isInherited(); - } else { - // log.error("Unknown property " + propertyName); - b = true; - } - return b; - } - - /** - * - * @param nameSpaceURI URI for the namespace of the element to which - * the attributes belong. - * @param elementName Local name for the element to which the attributes - * belong. - * @param attributes Collection of attributes passed to us from the parser. - * @param fo The FObj to which the attributes need to be attached as - * properties. - * @return PropertyList object containing collection of Properties objects - * appropriate for the FObj - * @throws FOPException If an error occurs while building the PropertyList - */ - public PropertyList makeList(String nameSpaceURI, String elementName, - Attributes attributes, - FObj fo) throws FOPException { - String nameSpaceURIToUse = "http://www.w3.org/TR/1999/XSL/Format"; - if (nameSpaceURI != null) { - nameSpaceURIToUse = nameSpaceURI; - } - FObj parentFO = fo.findNearestAncestorFObj(); - PropertyList parentProperties = null; - if (parentFO != null) { - parentProperties = parentFO.getPropertiesForNamespace(nameSpaceURIToUse); - } - - PropertyList p = new PropertyList(parentProperties, nameSpaceURIToUse, - elementName); - p.setBuilder(this); - HashMap validProperties; - validProperties = (HashMap)elementTable.get(elementName); - - /* - * If font-size is set on this FO, must set it first, since - * other attributes specified in terms of "ems" depend on it. - */ - /** @todo When we do "shorthand" properties, must handle the "font" - * property as well to see if font-size is set. - */ - String attributeName = FONTSIZEATTR; - String attributeValue = attributes.getValue(attributeName); - convertAttributeToProperty(attributes, attributeName, attributeValue, - validProperties, p, parentFO); - - for (int i = 0; i < attributes.getLength(); i++) { - attributeName = attributes.getQName(i); - attributeValue = attributes.getValue(i); - convertAttributeToProperty(attributes, attributeName, attributeValue, - validProperties, p, parentFO); - } - return p; - } - - /** - * - * @param attributes Collection of attributes - * @param attributeName Attribute name to convert - * @param attributeValue Attribute value to assign to property - * @param validProperties Collection of valid properties - * @param propList PropertyList in which to add the newly created Property - * @param parentFO Parent FO of the object for which this property is being - * built - */ - private void convertAttributeToProperty(Attributes attributes, - String attributeName, - String attributeValue, - HashMap validProperties, - PropertyList propList, - FObj parentFO) { - /* Handle "compound" properties, ex. space-before.minimum */ - String basePropertyName = findBasePropertyName(attributeName); - String subPropertyName = findSubPropertyName(attributeName); - - Property.Maker propertyMaker = findMaker(validProperties, basePropertyName); - if (propertyMaker == null) { - handleInvalidProperty(attributeName); - return; - } - if (attributeValue == null) { - return; - } - try { - Property prop = null; - if (subPropertyName == null) { - prop = propertyMaker.make(propList, attributeValue, parentFO); - } else { - Property baseProperty = findBaseProperty(attributes, propList, - parentFO, basePropertyName, propertyMaker); - prop = propertyMaker.make(baseProperty, subPropertyName, - propList, attributeValue, parentFO); - } - if (prop != null) { - propList.put(basePropertyName, prop); - } - } catch (FOPException e) { - /**@todo log this exception */ - // log.error(e.getMessage()); - } - } - - private Property findBaseProperty(Attributes attributes, - PropertyList propertyList, - FObj parentFO, - String basePropName, - Maker propertyMaker) - throws FOPException { - // If the baseProperty has already been created, return it - Property baseProperty = propertyList.getExplicitBaseProp(basePropName); - if (baseProperty != null) { - return baseProperty; - } - // If it is specified later in this list of Attributes, create it - String basePropertyValue = attributes.getValue(basePropName); - if (basePropertyValue != null) { - baseProperty = propertyMaker.make(propertyList, basePropertyValue, - parentFO); - return baseProperty; - } - // Otherwise it is a compound property ?? - // baseProperty = propertyMaker.makeCompound(propertyList, parentFO); - return baseProperty; - } - - private void handleInvalidProperty(String attributeName) { - if (!attributeName.startsWith("xmlns")) { - //log.error("property '" - // + attributeName + "' ignored"); - } - } - - /** - * Finds the first or base part (up to any period) of an attribute name. - * For example, if input is "space-before.minimum", should return - * "space-before". - * @param attributeName String to be atomized - * @return the base portion of the attribute - */ - public static String findBasePropertyName(String attributeName) { - int sepCharIndex = attributeName.indexOf('.'); - String basePropName = attributeName; - if (sepCharIndex > -1) { - basePropName = attributeName.substring(0, sepCharIndex); - } - return basePropName; - } - - /** - * Finds the second or sub part (portion past any period) of an attribute - * name. For example, if input is "space-before.minimum", should return - * "minimum". - * @param attributeName String to be atomized - * @return the sub portion of the attribute - */ - public static String findSubPropertyName(String attributeName) { - int sepCharIndex = attributeName.indexOf('.'); - String subPropName = null; - if (sepCharIndex > -1) { - subPropName = attributeName.substring(sepCharIndex + 1); - } - return subPropName; - } - - /** - * @param space namespace of element - * @param element name of element - * @param propertyName name of property - * @param p a Property object - * @param subpropName name of the sub-property to get - * @return the sub-property - */ - public Property getSubpropValue(String space, String element, - String propertyName, Property p, - String subpropName) { - Property.Maker maker = findMaker(space, element, propertyName); - if (maker != null) { - return maker.getSubpropValue(p, subpropName); - } else { - return null; - } - } - - /** - * @param propertyList collection of properties - * @param space namespace of element - * @param element name of element - * @param propertyName name of property - * @return value from the appropriate Property.Maker - */ - public boolean isCorrespondingForced(PropertyList propertyList, - String space, String element, - String propertyName) { - Property.Maker propertyMaker = findMaker(space, element, - propertyName); - if (propertyMaker != null) { - return propertyMaker.isCorrespondingForced(propertyList); - } else { - //log.error("no Maker for " + propertyName); - } - return false; - } - - /** - * @param propertyList collection of properties - * @param space namespace of element - * @param element name of element - * @param propertyName name of property - * @return new Property object - */ - public Property getShorthand(PropertyList propertyList, String space, - String element, String propertyName) { - Property.Maker propertyMaker = findMaker(space, element, - propertyName); - if (propertyMaker != null) { - return propertyMaker.getShorthand(propertyList); - } else { - //log.error("no Maker for " + propertyName); - return null; - } - } - - /** - * @param propertyList collection of properties - * @param space namespace of element - * @param element name of element - * @param propertyName name of property - * @return new Property object - * @throws FOPException for errors in the input - */ - public Property makeProperty(PropertyList propertyList, String space, - String element, - String propertyName) throws FOPException { - - Property p = null; - - Property.Maker propertyMaker = findMaker(space, element, - propertyName); - if (propertyMaker != null) { - p = propertyMaker.make(propertyList); - } else { - //log.error("property " + propertyName - // + " ignored"); - } - return p; - } - - /** - * @param space namespace of element - * @param elementName name of element - * @param propertyName name of property - * @return the Property.Maker for this property - */ - protected Property.Maker findMaker(String space, String elementName, - String propertyName) { - return findMaker((HashMap)elementTable.get(elementName), - propertyName); - } - - /** - * Convenience function to return the Maker for a given property - * given the HashMap containing properties specific to this element. - * If table is non-null and - * @param elemTable Element-specific properties or null if none. - * @param propertyName Name of property. - * @return A Maker for this property. - */ - private Property.Maker findMaker(HashMap elemTable, - String propertyName) { - Property.Maker propertyMaker = null; - if (elemTable != null) { - propertyMaker = (Property.Maker)elemTable.get(propertyName); - } - if (propertyMaker == null) { - propertyMaker = - (Property.Maker)propertyListTable.get(propertyName); - } - return propertyMaker; - } - -} |