| 1 |
<!-- |
| 2 |
Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
contributor license agreements. See the NOTICE file distributed with |
| 4 |
this work for additional information regarding copyright ownership. |
| 5 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
(the "License"); you may not use this file except in compliance with |
| 7 |
the License. You may obtain a copy of the License at |
| 8 |
|
| 9 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
|
| 11 |
Unless required by applicable law or agreed to in writing, software |
| 12 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
See the License for the specific language governing permissions and |
| 15 |
limitations under the License. |
| 16 |
--> |
| 17 |
<!-- $Id$ --> |
| 18 |
<xsl:stylesheet version="1.0" |
| 19 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 20 |
<xsl:output method="text" /> |
| 21 |
|
| 22 |
<xsl:include href="propinc.xsl"/> |
| 23 |
|
| 24 |
<xsl:template match="root"> |
| 25 |
<xsl:text> |
| 26 |
package org.apache.fop.fo; |
| 27 |
|
| 28 |
import org.apache.fop.fo.Constants; |
| 29 |
import java.util.BitSet; |
| 30 |
import java.util.ArrayList; |
| 31 |
|
| 32 |
public class PropertySets { |
| 33 |
private static short[][] mapping = null; |
| 34 |
|
| 35 |
private Element[] elements = new Element[Constants.ELEMENT_COUNT+1]; |
| 36 |
private BitSet block_elems = new BitSet(); |
| 37 |
private BitSet inline_elems = new BitSet(); |
| 38 |
|
| 39 |
</xsl:text> |
| 40 |
<xsl:apply-templates select="common" mode="decl"/> |
| 41 |
<xsl:text> |
| 42 |
public void initializeElements() { |
| 43 |
block_elems.set(Constants.FO_BLOCK); |
| 44 |
block_elems.set(Constants.FO_BLOCK_CONTAINER); |
| 45 |
block_elems.set(Constants.FO_TABLE_AND_CAPTION); |
| 46 |
block_elems.set(Constants.FO_TABLE); |
| 47 |
block_elems.set(Constants.FO_LIST_BLOCK); |
| 48 |
|
| 49 |
inline_elems.set(Constants.FO_BIDI_OVERRIDE); |
| 50 |
inline_elems.set(Constants.FO_CHARACTER); |
| 51 |
inline_elems.set(Constants.FO_EXTERNAL_GRAPHIC); |
| 52 |
inline_elems.set(Constants.FO_INSTREAM_FOREIGN_OBJECT); |
| 53 |
inline_elems.set(Constants.FO_INLINE); |
| 54 |
inline_elems.set(Constants.FO_INLINE_CONTAINER); |
| 55 |
inline_elems.set(Constants.FO_LEADER); |
| 56 |
inline_elems.set(Constants.FO_PAGE_NUMBER); |
| 57 |
inline_elems.set(Constants.FO_PAGE_NUMBER_CITATION); |
| 58 |
inline_elems.set(Constants.FO_BASIC_LINK); |
| 59 |
inline_elems.set(Constants.FO_MULTI_TOGGLE); |
| 60 |
} |
| 61 |
|
| 62 |
public void initializeCommon() { |
| 63 |
</xsl:text> |
| 64 |
<xsl:apply-templates select="common"/> |
| 65 |
<xsl:text> |
| 66 |
} |
| 67 |
|
| 68 |
public void initialize() { |
| 69 |
// define the fo: elements |
| 70 |
for (int i = 1; i < elements.length; i++) { |
| 71 |
elements[i] = new Element(i); |
| 72 |
} |
| 73 |
|
| 74 |
// populate the elements with properties and content elements. |
| 75 |
Element elem; |
| 76 |
</xsl:text> |
| 77 |
|
| 78 |
<xsl:apply-templates select="//element"/> |
| 79 |
<xsl:text> |
| 80 |
|
| 81 |
// Merge the attributes from the children into the parent. |
| 82 |
for (boolean dirty = true; dirty; ) { |
| 83 |
dirty = false; |
| 84 |
for (int i = 1; i < elements.length; i++) { |
| 85 |
dirty = dirty || elements[i].merge(); |
| 86 |
} |
| 87 |
} |
| 88 |
// Calculate the sparse indices for each element. |
| 89 |
for (int i = 1; i < elements.length; i++) { |
| 90 |
mapping[i] = makeSparseIndices(elements[i].valid); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Turn a BitSet into an array of shorts with the first element |
| 96 |
* on the array the number of set bits in the BitSet. |
| 97 |
*/ |
| 98 |
private static short[] makeSparseIndices(BitSet set) { |
| 99 |
short[] indices = new short[Constants.PROPERTY_COUNT+1]; |
| 100 |
int j = 1; |
| 101 |
for (int i = 0; i < Constants.PROPERTY_COUNT+1; i++) { |
| 102 |
if (set.get(i)) { |
| 103 |
indices[i] = (short) j++; |
| 104 |
} |
| 105 |
} |
| 106 |
indices[0] = (short)j; |
| 107 |
return indices; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
public static short[] getPropertySet(int elementId) { |
| 112 |
if (mapping == null) { |
| 113 |
mapping = new short[Constants.ELEMENT_COUNT+1][]; |
| 114 |
PropertySets ps = new PropertySets(); |
| 115 |
ps.initializeElements(); |
| 116 |
ps.initializeCommon(); |
| 117 |
ps.initialize(); |
| 118 |
} |
| 119 |
return mapping[elementId]; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* An object that represent the properties and contents of a fo element |
| 124 |
*/ |
| 125 |
class Element { |
| 126 |
BitSet relevant = new BitSet(); |
| 127 |
BitSet valid = new BitSet(); |
| 128 |
int elementId; |
| 129 |
ArrayList children; |
| 130 |
|
| 131 |
Element(int elementId) { |
| 132 |
this.elementId = elementId; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Add a single property to the element. |
| 137 |
*/ |
| 138 |
public void addProperty(int propId) { |
| 139 |
relevant.set(propId); |
| 140 |
valid.set(propId); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Add a set of properties to the element. |
| 145 |
*/ |
| 146 |
public void addProperties(BitSet properties) { |
| 147 |
relevant.or(properties); |
| 148 |
valid.or(properties); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Add a single fo element as a content child. |
| 153 |
*/ |
| 154 |
public void addContent(int elementId) { |
| 155 |
if (children == null) { |
| 156 |
children = new ArrayList(); |
| 157 |
} |
| 158 |
children.add(elements[elementId]); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Add a set of fo elements as content children. |
| 163 |
*/ |
| 164 |
public void addContent(BitSet elements) { |
| 165 |
for (int i = 0; i < elements.size(); i++) { |
| 166 |
if (elements.get(i)) { |
| 167 |
addContent(i); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Merge the properties from the children into the set of valid |
| 174 |
* properties. Return true if at least one property could be added. |
| 175 |
*/ |
| 176 |
public boolean merge() { |
| 177 |
if (children == null) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
boolean dirty = false; |
| 181 |
for (int i = 0; i < children.size(); i++) { |
| 182 |
Element child = (Element) children.get(i); |
| 183 |
BitSet childValid = child.valid; |
| 184 |
int n = childValid.length(); |
| 185 |
for (int j = 0; j < n; j++) { |
| 186 |
if (childValid.get(j) && !valid.get(j)) { |
| 187 |
dirty = true; |
| 188 |
valid.set(j); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
return dirty; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
</xsl:text> |
| 197 |
</xsl:template> |
| 198 |
|
| 199 |
<xsl:template match="common" mode="decl"> |
| 200 |
<xsl:variable name="name" select="name"/> |
| 201 |
<xsl:text> BitSet </xsl:text><xsl:value-of select="$name"/> |
| 202 |
<xsl:text> = new BitSet(); |
| 203 |
</xsl:text> |
| 204 |
</xsl:template> |
| 205 |
|
| 206 |
<xsl:template match="common"> |
| 207 |
<xsl:variable name="name" select="name"/> |
| 208 |
<xsl:apply-templates select="property"> |
| 209 |
<xsl:with-param name="setname" select="$name"/> |
| 210 |
</xsl:apply-templates> |
| 211 |
<xsl:text> |
| 212 |
</xsl:text> |
| 213 |
</xsl:template> |
| 214 |
|
| 215 |
<xsl:template match="common/property"> |
| 216 |
<xsl:param name="setname"/> |
| 217 |
|
| 218 |
<xsl:text> </xsl:text> |
| 219 |
<xsl:value-of select="../name"/><xsl:text>.set(Constants.PR_</xsl:text> |
| 220 |
<xsl:call-template name="makeEnumConstant"> |
| 221 |
<xsl:with-param name="propstr" select="." /> |
| 222 |
</xsl:call-template>); |
| 223 |
</xsl:template> |
| 224 |
|
| 225 |
|
| 226 |
<xsl:template match="element"> |
| 227 |
<xsl:variable name="name"> |
| 228 |
<xsl:call-template name="makeEnumConstant"> |
| 229 |
<xsl:with-param name="propstr" select="name" /> |
| 230 |
</xsl:call-template> |
| 231 |
</xsl:variable> |
| 232 |
<xsl:text> elem = elements[Constants.FO_</xsl:text> |
| 233 |
<xsl:value-of select="$name"/> |
| 234 |
<xsl:text>]; |
| 235 |
</xsl:text> |
| 236 |
<xsl:apply-templates select="common-ref | property"/> |
| 237 |
<xsl:apply-templates select="content"/> |
| 238 |
<xsl:text> |
| 239 |
</xsl:text> |
| 240 |
</xsl:template> |
| 241 |
|
| 242 |
|
| 243 |
<xsl:template match="element/common-ref"> |
| 244 |
<xsl:param name="setname"/> |
| 245 |
|
| 246 |
<xsl:text> elem.addProperties(</xsl:text> |
| 247 |
<xsl:value-of select="."/>); |
| 248 |
</xsl:template> |
| 249 |
|
| 250 |
<xsl:template match="element/property"> |
| 251 |
<xsl:param name="setname"/> |
| 252 |
|
| 253 |
<xsl:text> elem.addProperty(Constants.PR_</xsl:text> |
| 254 |
<xsl:call-template name="makeEnumConstant"> |
| 255 |
<xsl:with-param name="propstr" select="." /> |
| 256 |
</xsl:call-template>); |
| 257 |
</xsl:template> |
| 258 |
|
| 259 |
<xsl:template match="element/content"> |
| 260 |
<xsl:variable name="name"> |
| 261 |
<xsl:text>Constants.FO_</xsl:text> |
| 262 |
<xsl:call-template name="makeEnumConstant"> |
| 263 |
<xsl:with-param name="propstr" select="." /> |
| 264 |
</xsl:call-template> |
| 265 |
</xsl:variable> |
| 266 |
|
| 267 |
<xsl:choose> |
| 268 |
<xsl:when test=". = '%block;'"> |
| 269 |
<xsl:text> elem.addContent(block_elems); |
| 270 |
</xsl:text> |
| 271 |
</xsl:when> |
| 272 |
<xsl:when test=". = '%inline;'"> |
| 273 |
<xsl:text> elem.addContent(inline_elems); |
| 274 |
</xsl:text> |
| 275 |
</xsl:when> |
| 276 |
<xsl:otherwise> |
| 277 |
<xsl:text> elem.addContent(</xsl:text> |
| 278 |
<xsl:value-of select="$name"/> |
| 279 |
<xsl:text>); |
| 280 |
</xsl:text> |
| 281 |
</xsl:otherwise> |
| 282 |
</xsl:choose> |
| 283 |
</xsl:template> |
| 284 |
|
| 285 |
|
| 286 |
<xsl:template match="text()"/> |
| 287 |
|
| 288 |
</xsl:stylesheet> |
| 289 |
|