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.

PropertyConsts.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * Copyright 1999-2003 The Apache Software Foundation.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. *
  20. *
  21. * @author <a href="mailto:pbwest@powerup.com.au">Peter B. West</a>
  22. * @version $Revision$ $Name$
  23. */
  24. package org.apache.fop.fo;
  25. import java.util.StringTokenizer;
  26. import org.apache.fop.datastructs.ROStringArray;
  27. import org.apache.fop.datatypes.Numeric;
  28. import org.apache.fop.datatypes.PropertyValue;
  29. import org.apache.fop.fo.expr.PropertyException;
  30. import org.apache.fop.fo.properties.Property;
  31. /**
  32. * This class contains a number of arrays containing values indexed by the
  33. * property index value, determined from the PropNames class. These arrays
  34. * provide a means of accessing information about the nature of a property
  35. * through the property index value.
  36. * <p>Most of these arrays are initialised piecemeal as information is
  37. * required about a particular property.
  38. * There are also <tt>HashMap</tt>s which encode the various sets of
  39. * properties which are defined to apply to each of the Flow Objects,
  40. * and a <tt>BitSet</tt> of those properties which are
  41. * automatically inherited. The <tt>HashMap</tt>s provide a convenient
  42. * means of specifying the relationship between FOs and properties.
  43. */
  44. public class PropertyConsts {
  45. private static final String tag = "$Name$";
  46. private static final String revision = "$Revision$";
  47. private static final String packageName = "org.apache.fop.fo";
  48. public static final PropertyConsts pconsts;
  49. static {
  50. pconsts = new PropertyConsts();
  51. }
  52. public static final PropertyConsts getPropertyConsts() {
  53. return pconsts;
  54. }
  55. /**
  56. * A Property[] array containing Property objects corresponding to each
  57. * of the property indices in <tt>PropNames</tt>.
  58. * Initially empty, entries are filled on demand as calls for details
  59. * about individual properties are made.
  60. */
  61. private final Property[] properties
  62. = new Property[PropNames.LAST_PROPERTY_INDEX + 1];
  63. /**
  64. * Get the individual Property object denoted by the property index
  65. * @param propindex
  66. * @return
  67. * @throws PropertyException
  68. */
  69. public Property getProperty(int propindex) throws PropertyException {
  70. return setupProperty(propindex);
  71. }
  72. /**
  73. * get the initial value type for a property index.
  74. * @param propindex int index of the FO property
  75. * @return int enumerated initialValueType. These constants are defined
  76. * as static final ints in the Property class.
  77. * @throws PropertyException
  78. */
  79. public int getInitialValueType(int propindex)
  80. throws PropertyException
  81. {
  82. return setupProperty(propindex).getInitialValueType();
  83. }
  84. /**
  85. * A <tt>PropertyValue</tt> array containing the initial values of
  86. * each of the properties.
  87. */
  88. private final PropertyValue[] initialValues
  89. = new PropertyValue[PropNames.LAST_PROPERTY_INDEX + 1];
  90. /**
  91. * Get the initial value <tt>PropertyValue</tt> for a given property.
  92. * Note that this is a <b>raw</b> value; if it is
  93. * an unresolved percentage that value will be returned.
  94. * @param propindex - the property index.
  95. * @return a <tt>PropertyValue</tt> containing the initial property
  96. * value element for the indexed property.
  97. * @exception PropertyException
  98. */
  99. public PropertyValue getInitialValue(int propindex)
  100. throws PropertyException
  101. {
  102. if (initialValues[propindex] != null)
  103. return initialValues[propindex];
  104. return
  105. (initialValues[propindex] =
  106. setupProperty(propindex).getInitialValue(propindex));
  107. }
  108. /**
  109. * @param propindex <tt>int</tt> index of the property
  110. * @param foNode the node whose properties are being constructed.
  111. * @param value the <tt>PropertyValue</tt> being refined.
  112. * @return <tt>PropertyValue</tt> constructed by the property's
  113. * <i>refineParsing</i> method
  114. * @exception PropertyException
  115. */
  116. public PropertyValue refineParsing
  117. (int propindex, FONode foNode, PropertyValue value)
  118. throws PropertyException
  119. {
  120. Property property = setupProperty(propindex);
  121. return property.refineParsing(propindex, foNode, value);
  122. }
  123. /**
  124. * @param propindex <tt>int</tt> index of the property
  125. * @param foNode the node whose properties are being constructed.
  126. * @param value the <tt>PropertyValue</tt> being refined.
  127. * @param isNested indicates whether this method is
  128. * called normally (false), or as part of another <i>refineParsing</i>
  129. * method
  130. * @return <tt>PropertyValue</tt> constructed by the property's
  131. * <i>refineParsing</i> method
  132. * @exception PropertyException
  133. */
  134. public PropertyValue refineParsing
  135. (int propindex, FONode foNode, PropertyValue value, boolean isNested)
  136. throws PropertyException
  137. {
  138. Property property = setupProperty(propindex);
  139. return property.refineParsing(propindex, foNode, value, isNested);
  140. }
  141. /**
  142. * An <tt>int[]</tt> containing the <i>inherited</i> values from the
  143. * <tt>Property</tt> classes.
  144. */
  145. private final int[] inherited
  146. = new int[PropNames.LAST_PROPERTY_INDEX + 1];
  147. /**
  148. * @param property name of the FO property
  149. * @return int type of inheritance for this property
  150. * (See constants defined in Properties.)
  151. * @throws PropertyException
  152. */
  153. public int inheritance(String property) throws PropertyException {
  154. return inheritance(PropNames.getPropertyIndex(property));
  155. }
  156. /**
  157. * @param propindex int index of the FO property
  158. * @return int type of inheritance for this property
  159. * (See constants defined in Property.)
  160. * @throws PropertyException
  161. */
  162. public int inheritance(int propindex) throws PropertyException {
  163. setupProperty(propindex);
  164. return inherited[propindex];
  165. }
  166. /**
  167. * @param propindex int index of the FO property
  168. * @return <tt>boolean</tt> is property inherited?
  169. * @throws PropertyException
  170. */
  171. public boolean isInherited(int propindex) throws PropertyException {
  172. Property property = setupProperty(propindex);
  173. return inherited[propindex] != Property.NO;
  174. }
  175. /**
  176. * An int[] array of the values of the <i>dataTypes</i> field of each
  177. * property. The array is indexed by the index value constants that are
  178. * defined in the PropNames class in parallel to the
  179. * PropNames.propertyNames[] array.
  180. * The array elements are set from the values of the
  181. * <i>dataTypes</i> field in each property class.
  182. */
  183. private final int[] datatypes
  184. = new int[PropNames.LAST_PROPERTY_INDEX + 1];
  185. /**
  186. * Get the <tt>Numeric</tt> value corresponding to an enumerated value.
  187. * @param foNode the <tt>FONode</tt> being built
  188. * @param propindex int index of the FO property
  189. * @param enumval - the integer equivalent of the enumeration keyword.
  190. * @return the <tt>Numeric</tt> result.
  191. * @throws PropertyException
  192. */
  193. public Numeric getMappedNumeric(FONode foNode, int propindex, int enumval)
  194. throws PropertyException
  195. {
  196. Property property = setupProperty(propindex);
  197. if ((datatypes[propindex] & Property.MAPPED_LENGTH) != 0)
  198. return property.getMappedLength(foNode, enumval);
  199. else
  200. throw new PropertyException
  201. ("MAPPED_LENGTH not valid in "
  202. + PropNames.getPropertyName(propindex));
  203. }
  204. /**
  205. * @param propindex int index of the FO property
  206. * @return <tt>boolean</tt> is property a shorthand?
  207. * @throws PropertyException
  208. */
  209. public boolean isShorthand(int propindex) throws PropertyException {
  210. Property property = setupProperty(propindex);
  211. return (datatypes[propindex] & Property.SHORTHAND) != 0;
  212. }
  213. /**
  214. * @param propertyIndex int index of the FO property
  215. * @return <tt>boolean</tt> is property a compound?
  216. * @throws PropertyException
  217. */
  218. public boolean isCompound(int propertyIndex) throws PropertyException {
  219. Property property = setupProperty(propertyIndex);
  220. return (datatypes[propertyIndex] & Property.COMPOUND) != 0;
  221. }
  222. /**
  223. * @param propertyIndex int index of the FO property
  224. * @return <tt>int</tt> dataTypes value.
  225. * @throws PropertyException
  226. */
  227. public int getDataTypes(int propertyIndex) throws PropertyException {
  228. return setupProperty(propertyIndex).getDataTypes();
  229. }
  230. /**
  231. * Map the integer value of an enumval into its mapped value.
  232. * Only valid when the datatype of the property includes MAPPED_ENUM.
  233. * <p>Generally, the path will be enumText->enumIndex->mappedEnumText.
  234. * @param index <tt>int</tt> containing the enumeration index.
  235. * @param enumMap an <tt>ROStringArray</tt> of the <tt>String</tt>s
  236. * with the mapped enumeration values.
  237. * @return a <tt>String</tt> with the mapped enumeration text.
  238. */
  239. public String enumIndexToMapping(int index, ROStringArray enumMap)
  240. {
  241. return enumMap.get(index);
  242. }
  243. /**
  244. * @param propindex <tt>int</tt> property index.
  245. * @param enumval <tt>String</tt> containing the enumeration text.
  246. * @return <tt>int</tt> constant representing the enumeration value.
  247. * @exception PropertyException
  248. */
  249. public int getEnumIndex(int propindex, String enumval)
  250. throws PropertyException
  251. {
  252. Property property = setupProperty(propindex);
  253. return property.getEnumIndex(enumval);
  254. }
  255. /**
  256. * @param propindex <tt>int</tt> property index.
  257. * @param enumIndex <tt>int</tt> containing the enumeration index.
  258. * @return <tt>String</tt> containing the enumeration text.
  259. * @exception PropertyException
  260. */
  261. public String getEnumText(int propindex, int enumIndex)
  262. throws PropertyException
  263. {
  264. Property property = setupProperty(propindex);
  265. return property.getEnumText(enumIndex);
  266. }
  267. /** An array of boolean results of the <code>isCorrespondingAbsolute</code>
  268. * method */
  269. private final boolean[] correspondingAbs =
  270. new boolean[PropNames.LAST_PROPERTY_INDEX + 1];
  271. /**
  272. * Is the indicated property absolute corresponding?
  273. * @param propindex
  274. * @return
  275. * @throws PropertyException
  276. */
  277. public boolean isCorrespondingAbs(int propindex)
  278. throws PropertyException {
  279. Property property = setupProperty(propindex);
  280. return correspondingAbs[propindex];
  281. }
  282. /** An array of boolean results of the <code>isCorrespondingRelative</code>
  283. * method */
  284. private final boolean[] correspondingRel =
  285. new boolean[PropNames.LAST_PROPERTY_INDEX + 1];
  286. /**
  287. * Is the indicated property relative corresponding?
  288. * @param propindex
  289. * @return
  290. * @throws PropertyException
  291. */
  292. public boolean isCorrespondingRel(int propindex)
  293. throws PropertyException {
  294. Property property = setupProperty(propindex);
  295. return correspondingRel[propindex];
  296. }
  297. /**
  298. * Set up the details of a single property and return the
  299. * <tt>Property</tt> object. If the <tt>Property</tt> object
  300. * corresponding to the property index has not been resolved before,
  301. * derive the Class and Property objects, and extract certain field
  302. * values from the Property.
  303. * @param propindex - the <tt>int</tt> index.
  304. * @return - the <tt>Property</tt> corresponding to the index.
  305. * @throws PropertyException
  306. */
  307. public Property setupProperty(int propindex)
  308. throws PropertyException
  309. {
  310. Property property;
  311. if ((property = properties[propindex]) != null) return property;
  312. String cname = "";
  313. Class pclass;
  314. // Get the property class name
  315. StringTokenizer stoke;
  316. stoke = new StringTokenizer
  317. (PropNames.getPropertyName(propindex), "-.:");
  318. while (stoke.hasMoreTokens()) {
  319. String token = stoke.nextToken();
  320. String pname = new Character(
  321. Character.toUpperCase(token.charAt(0))
  322. ).toString() + token.substring(1);
  323. cname = cname + pname;
  324. }
  325. // Get the class for this property name
  326. String name = packageName + ".properties." + cname;
  327. try {
  328. pclass = Class.forName(name);
  329. // Instantiate the class
  330. property = (Property)(pclass.newInstance());
  331. properties[propindex] = property;
  332. inherited[propindex] = property.getInherited();
  333. correspondingAbs[propindex] = property.isCorrespondingAbsolute();
  334. correspondingRel[propindex] = property.isCorrespondingRelative();
  335. } catch (ClassNotFoundException e) {
  336. throw new PropertyException
  337. ("ClassNotFoundException" + e.getMessage());
  338. } catch (IllegalAccessException e) {
  339. throw new PropertyException
  340. ("IllegalAccessException" + e.getMessage());
  341. } catch (InstantiationException e) {
  342. throw new PropertyException
  343. ("InstantiationException" + e.getMessage());
  344. }
  345. return property;
  346. }
  347. private PropertyConsts () {}
  348. }