You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

property-sets.xsl 8.5KB

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