diff options
author | Finn Bock <bckfnn@apache.org> | 2004-02-09 21:30:56 +0000 |
---|---|---|
committer | Finn Bock <bckfnn@apache.org> | 2004-02-09 21:30:56 +0000 |
commit | 0bc8d5b1f7d2f3bd7fa0d482d185075c7d202a94 (patch) | |
tree | 36ccf8f83a261bebb652b6999185466846e5dcb6 /src/codegen | |
parent | bbe1a6381296280398f897d8c422fb33552c9081 (diff) | |
download | xmlgraphics-fop-0bc8d5b1f7d2f3bd7fa0d482d185075c7d202a94.tar.gz xmlgraphics-fop-0bc8d5b1f7d2f3bd7fa0d482d185075c7d202a94.zip |
Simplefy the huge merge loop and allow compilation with jdk1.3.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@197342 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/codegen')
-rw-r--r-- | src/codegen/property-sets.xsl | 298 |
1 files changed, 192 insertions, 106 deletions
diff --git a/src/codegen/property-sets.xsl b/src/codegen/property-sets.xsl index 09848b7d7..ab9cf45ef 100644 --- a/src/codegen/property-sets.xsl +++ b/src/codegen/property-sets.xsl @@ -56,64 +56,192 @@ Software Foundation, please see <http://www.apache.org/>. <xsl:template match="root"> <xsl:text> package org.apache.fop.fo; + +import org.apache.fop.fo.Constants; import java.util.BitSet; +import java.util.ArrayList; public class PropertySets { - public static short[][] mapping = null; + private static short[][] mapping = null; + + private Element[] elements = new Element[Constants.ELEMENT_COUNT+1]; + private BitSet block_elems = new BitSet(); + private BitSet inline_elems = new BitSet(); - public static void initialize() { - mapping = new short[Constants.ELEMENT_COUNT][]; </xsl:text> - <xsl:apply-templates/> - <xsl:text> - boolean loop = true; - while (loop) { - loop = false; + <xsl:apply-templates select="common" mode="decl"/> +<xsl:text> + public void initializeElements() { + block_elems.set(Constants.FO_BLOCK); + block_elems.set(Constants.FO_BLOCK_CONTAINER); + block_elems.set(Constants.FO_TABLE_AND_CAPTION); + block_elems.set(Constants.FO_TABLE); + block_elems.set(Constants.FO_LIST_BLOCK); + + inline_elems.set(Constants.FO_BIDI_OVERRIDE); + inline_elems.set(Constants.FO_CHARACTER); + inline_elems.set(Constants.FO_EXTERNAL_GRAPHIC); + inline_elems.set(Constants.FO_INSTREAM_FOREIGN_OBJECT); + inline_elems.set(Constants.FO_INLINE); + inline_elems.set(Constants.FO_INLINE_CONTAINER); + inline_elems.set(Constants.FO_LEADER); + inline_elems.set(Constants.FO_PAGE_NUMBER); + inline_elems.set(Constants.FO_PAGE_NUMBER_CITATION); + inline_elems.set(Constants.FO_BASIC_LINK); + inline_elems.set(Constants.FO_MULTI_TOGGLE); + } + + public void initializeCommon() { </xsl:text> -<xsl:apply-templates mode="content"/> - } -<xsl:apply-templates mode="mapping"/> - <xsl:text> +<xsl:apply-templates select="common"/> +<xsl:text> } + public void initialize() { + // define the fo: elements + for (int i = 1; i < elements.length; i++) { + elements[i] = new Element(i); + } + + // populate the elements with properties and content elements. + Element elem; +</xsl:text> + +<xsl:apply-templates select="//element"/> +<xsl:text> + + // Merge the attributes from the children into the parent. + for (boolean dirty = true; dirty; ) { + dirty = false; + for (int i = 1; i < elements.length; i++) { + dirty = dirty || elements[i].merge(); + } + } + // Calculate the sparse indices for each element. + for (int i = 1; i < elements.length; i++) { + mapping[i] = makeSparseIndices(elements[i].valid); + } + } + + /** + * Turn a BitSet into an array of shorts with the first element + * on the array the number of set bits in the BitSet. + */ private static short[] makeSparseIndices(BitSet set) { - short[] indices = new short[Constants.PROPERTY_COUNT]; - indices[0] = (short) (set.cardinality() + 1); + short[] indices = new short[Constants.PROPERTY_COUNT+1]; int j = 1; - for (int i = set.nextSetBit(0); i >= 0; i = set.nextSetBit(i+1)) { - indices[i] = (short) j++; + for (int i = 0; i < Constants.PROPERTY_COUNT+1; i++) { + if (set.get(i)) { + indices[i] = (short) j++; + } } + indices[0] = (short)j; return indices; } - private static boolean mergeContent(BitSet node, BitSet content, - boolean loop) - { - int c = node.cardinality(); - node.or(content); - if (c == node.cardinality()) - return loop; - return true; - } public static short[] getPropertySet(int elementId) { - if (mapping == null) - initialize(); + if (mapping == null) { + mapping = new short[Constants.ELEMENT_COUNT+1][]; + PropertySets ps = new PropertySets(); + ps.initializeElements(); + ps.initializeCommon(); + ps.initialize(); + } return mapping[elementId]; } + + /** + * An object that represent the properties and contents of a fo element + */ + class Element { + BitSet relevant = new BitSet(); + BitSet valid = new BitSet(); + int elementId; + ArrayList children; + + Element(int elementId) { + this.elementId = elementId; + } + + /** + * Add a single property to the element. + */ + public void addProperty(int propId) { + relevant.set(propId); + valid.set(propId); + } + + /** + * Add a set of properties to the element. + */ + public void addProperties(BitSet properties) { + relevant.or(properties); + valid.or(properties); + } + + /** + * Add a single fo element as a content child. + */ + public void addContent(int elementId) { + if (children == null) { + children = new ArrayList(); + } + children.add(elements[elementId]); + } + + /** + * Add a set of fo elements as content children. + */ + public void addContent(BitSet elements) { + for (int i = 0; i < elements.size(); i++) { + if (elements.get(i)) { + addContent(i); + } + } + } + + /** + * Merge the properties from the children into the set of valid + * properties. Return true if at least one property could be added. + */ + public boolean merge() { + if (children == null) { + return false; + } + boolean dirty = false; + for (int i = 0; i < children.size(); i++) { + Element child = (Element) children.get(i); + BitSet childValid = child.valid; + int n = childValid.length(); + for (int j = 0; j < n; j++) { + if (childValid.get(j) && !valid.get(j)) { + dirty = true; + valid.set(j); + } + } + } + return dirty; + } + } } - </xsl:text> +</xsl:text> </xsl:template> -<xsl:template match="common"> +<xsl:template match="common" mode="decl"> <xsl:variable name="name" select="name"/> - <xsl:text> - BitSet </xsl:text><xsl:value-of select="$name"/> + <xsl:text> BitSet </xsl:text><xsl:value-of select="$name"/> <xsl:text> = new BitSet(); </xsl:text> +</xsl:template> + +<xsl:template match="common"> + <xsl:variable name="name" select="name"/> <xsl:apply-templates select="property"> <xsl:with-param name="setname" select="$name"/> </xsl:apply-templates> + <xsl:text> +</xsl:text> </xsl:template> <xsl:template match="common/property"> @@ -128,108 +256,66 @@ public class PropertySets { <xsl:template match="element"> -<!-- - <xsl:text> - public static final int FO_</xsl:text> - <xsl:call-template name="makeEnumName"> - <xsl:with-param name="propstr" select="name" /> - </xsl:call-template> - <xsl:text> = </xsl:text> - <xsl:value-of select="count(preceding-sibling::element)"/>; ---> - <xsl:variable name="name"> - <xsl:text>fo_</xsl:text> - <xsl:value-of select="translate(name, '-', '_')"/> + <xsl:call-template name="makeEnumConstant"> + <xsl:with-param name="propstr" select="name" /> + </xsl:call-template> </xsl:variable> + <xsl:text> elem = elements[Constants.FO_</xsl:text> + <xsl:value-of select="$name"/> + <xsl:text>]; +</xsl:text> + <xsl:apply-templates select="common-ref | property"/> + <xsl:apply-templates select="content"/> <xsl:text> - BitSet </xsl:text><xsl:value-of select="$name"/> - <xsl:text> = new BitSet(); </xsl:text> - <xsl:apply-templates select="common-ref | property"> - <xsl:with-param name="setname" select="$name"/> - </xsl:apply-templates> </xsl:template> -<xsl:template match="element" mode="content"> - <xsl:variable name="name"> - <xsl:text>fo_</xsl:text> - <xsl:value-of select="translate(name, '-', '_')"/> - </xsl:variable> +<xsl:template match="element/common-ref"> + <xsl:param name="setname"/> - <xsl:apply-templates select="content"> - <xsl:with-param name="setname" select="$name"/> - </xsl:apply-templates> + <xsl:text> elem.addProperties(</xsl:text> + <xsl:value-of select="."/>); </xsl:template> - -<xsl:template match="element/content"> +<xsl:template match="element/property"> <xsl:param name="setname"/> + <xsl:text> elem.addProperty(Constants.PR_</xsl:text> + <xsl:call-template name="makeEnumConstant"> + <xsl:with-param name="propstr" select="." /> + </xsl:call-template>); +</xsl:template> + +<xsl:template match="element/content"> <xsl:variable name="name"> - <xsl:text>fo_</xsl:text> - <xsl:value-of select="translate(., '-', '_')"/> + <xsl:text>Constants.FO_</xsl:text> + <xsl:call-template name="makeEnumConstant"> + <xsl:with-param name="propstr" select="." /> + </xsl:call-template> </xsl:variable> <xsl:choose> <xsl:when test=". = '%block;'"> - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_block, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_block_container, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_table_and_caption, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_table, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_list_block, loop); + <xsl:text> elem.addContent(block_elems); +</xsl:text> </xsl:when> <xsl:when test=". = '%inline;'"> - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_bidi_override, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_character, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_external_graphic, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_instream_foreign_object, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_inline, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_inline_container, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_leader, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_page_number, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_page_number_citation, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_basic_link, loop); - loop = mergeContent(<xsl:value-of select="$setname"/>, fo_multi_toggle, loop); + <xsl:text> elem.addContent(inline_elems); +</xsl:text> </xsl:when> <xsl:otherwise> - loop = mergeContent(<xsl:value-of select="$setname"/>, <xsl:value-of select="$name"/>, loop); + <xsl:text> elem.addContent(</xsl:text> + <xsl:value-of select="$name"/> + <xsl:text>); +</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:template> -<xsl:template match="element" mode="mapping"> - <xsl:variable name="name"> - <xsl:text>fo_</xsl:text> - <xsl:value-of select="translate(name, '-', '_')"/> - </xsl:variable> - <xsl:text> mapping[Constants.</xsl:text> - <xsl:call-template name="makeEnumConstant"> - <xsl:with-param name="propstr" select="$name" /> - </xsl:call-template>] = makeSparseIndices(<xsl:value-of select="$name"/>); -</xsl:template> - -<xsl:template match="element/common-ref"> - <xsl:param name="setname"/> - - <xsl:text> </xsl:text> - <xsl:value-of select="$setname"/>.or(<xsl:value-of select="."/>); -</xsl:template> - -<xsl:template match="element/property"> - <xsl:param name="setname"/> - - <xsl:text> </xsl:text> - <xsl:value-of select="$setname"/><xsl:text>.set(Constants.PR_</xsl:text> - <xsl:call-template name="makeEnumConstant"> - <xsl:with-param name="propstr" select="." /> - </xsl:call-template>); -</xsl:template> <xsl:template match="text()"/> -<xsl:template match="text()" mode="content"/> -<xsl:template match="text()" mode="mapping"/> </xsl:stylesheet> |