Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Property.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.fo;
  8. import org.apache.fop.datatypes.*;
  9. import org.apache.fop.fo.expr.Numeric;
  10. import org.apache.fop.fo.expr.PropertyParser;
  11. import org.apache.fop.fo.expr.PropertyInfo;
  12. import org.apache.fop.fo.expr.PropertyException;
  13. import org.apache.fop.apps.FOPException;
  14. import java.util.Vector;
  15. public class Property {
  16. public static class Maker {
  17. private static final String UNKNOWN = "UNKNOWN";
  18. private String propName;
  19. /**
  20. * Return the name of the property whose value is being set.
  21. */
  22. protected String getPropName() {
  23. return propName;
  24. }
  25. /**
  26. * Construct an instance of a Property.Maker for the given property.
  27. * @param propName The name of the property to be made.
  28. */
  29. protected Maker(String propName) {
  30. this.propName = propName;
  31. }
  32. /**
  33. * Construct an instance of a Property.Maker.
  34. * Note: the property name is set to "UNKNOWN".
  35. */
  36. protected Maker() {
  37. this.propName = UNKNOWN;
  38. }
  39. /**
  40. * Default implementation of isInherited.
  41. * @return A boolean indicating whether this property is inherited.
  42. */
  43. public boolean isInherited() {
  44. return false;
  45. }
  46. /**
  47. * Return a boolean indicating whether this property inherits the
  48. * "specified" value rather than the "computed" value. The default is
  49. * to inherit the "computed" value.
  50. * @return If true, property inherits the value specified.
  51. */
  52. public boolean inheritsSpecified() {
  53. return false;
  54. }
  55. /**
  56. * Return an object implementing the PercentBase interface.
  57. * This is used to handle properties specified as a percentage of
  58. * some "base length", such as the content width of their containing
  59. * box.
  60. * Overridden by subclasses which allow percent specifications. See
  61. * the documentation on properties.xsl for details.
  62. */
  63. public PercentBase getPercentBase(FObj fo, PropertyList pl) {
  64. return null;
  65. }
  66. /**
  67. * Return a Maker object which is used to set the values on components
  68. * of compound property types, such as "space".
  69. * Overridden by property maker subclasses which handle
  70. * compound properties.
  71. * @param subprop The name of the component for which a Maker is to
  72. * returned, for example "optimum", if the FO attribute is
  73. * space.optimum='10pt'.
  74. */
  75. protected Maker getSubpropMaker(String subprop) {
  76. return null;
  77. }
  78. /**
  79. * Return a property value for the given component of a compound
  80. * property.
  81. * @param p A property value for a compound property type such as
  82. * SpaceProperty.
  83. * @param subprop The name of the component whose value is to be
  84. * returned.
  85. * NOTE: this is only to ease porting when calls are made to
  86. * PropertyList.get() using a component name of a compound property,
  87. * such as get("space.optimum"). The recommended technique is:
  88. * get("space").getOptimum().
  89. * Overridden by property maker subclasses which handle
  90. * compound properties.
  91. */
  92. public Property getSubpropValue(Property p, String subprop) {
  93. return null;
  94. }
  95. /**
  96. * Return a property value for a compound property. If the property
  97. * value is already partially initialized, this method will modify it.
  98. * @param baseProp The Property object representing the compound property,
  99. * such as SpaceProperty.
  100. * @param partName The name of the component whose value is specified.
  101. * @param propertyList The propertyList being built.
  102. * @param fo The FO whose properties are being set.
  103. * @return A compound property object.
  104. */
  105. public Property make(Property baseProp, String partName,
  106. PropertyList propertyList, String value,
  107. FObj fo) throws FOPException {
  108. if (baseProp == null) {
  109. baseProp = makeCompound(propertyList, fo);
  110. }
  111. Maker spMaker = getSubpropMaker(partName);
  112. if (spMaker != null) {
  113. Property p = spMaker.make(propertyList, value, fo);
  114. if (p != null) {
  115. return setSubprop(baseProp, partName, p);
  116. }
  117. } else {
  118. //getLogger().error("compound property component "
  119. // + partName + " unknown.");
  120. }
  121. return baseProp;
  122. }
  123. /**
  124. * Set a component in a compound property and return the modified
  125. * compound property object.
  126. * This default implementation returns the original base property
  127. * without modifying it.
  128. * It is overridden by property maker subclasses which handle
  129. * compound properties.
  130. * @param baseProp The Property object representing the compound property,
  131. * such as SpaceProperty.
  132. * @param partName The name of the component whose value is specified.
  133. * @param subProp A Property object holding the specified value of the
  134. * component to be set.
  135. * @return The modified compound property object.
  136. */
  137. protected Property setSubprop(Property baseProp, String partName,
  138. Property subProp) {
  139. return baseProp;
  140. }
  141. /**
  142. * Create a Property object from an attribute specification.
  143. * @param propertyList The PropertyList object being built for this FO.
  144. * @param value The attribute value.
  145. * @param fo The current FO whose properties are being set.
  146. * @return The initialized Property object.
  147. */
  148. public Property make(PropertyList propertyList, String value,
  149. FObj fo) throws FOPException {
  150. try {
  151. Property pret = null;
  152. String pvalue = value;
  153. pret = checkEnumValues(value);
  154. if (pret == null) {
  155. /* Check for keyword shorthand values to be substituted. */
  156. pvalue = checkValueKeywords(value);
  157. // Override parsePropertyValue in each subclass of Property.Maker
  158. Property p = PropertyParser.parse(pvalue,
  159. new PropertyInfo(this,
  160. propertyList, fo));
  161. pret = convertProperty(p, propertyList, fo);
  162. } else if (isCompoundMaker()) {
  163. pret = convertProperty(pret, propertyList, fo);
  164. }
  165. if (pret == null) {
  166. throw new org.apache.fop.fo.expr.PropertyException("No conversion defined");
  167. } else if (inheritsSpecified()) {
  168. pret.setSpecifiedValue(pvalue);
  169. }
  170. return pret;
  171. } catch (org.apache.fop.fo.expr.PropertyException propEx) {
  172. throw new FOPException("Error in " + propName +
  173. " property value '" + value + "': " +
  174. propEx);
  175. }
  176. }
  177. public Property convertShorthandProperty(PropertyList propertyList,
  178. Property prop, FObj fo) {
  179. Property pret = null;
  180. try {
  181. pret = convertProperty(prop, propertyList, fo);
  182. if (pret == null) {
  183. // If value is a name token, may be keyword or Enum
  184. String sval = prop.getNCname();
  185. if (sval != null) {
  186. // System.err.println("Convert shorthand ncname " + sval);
  187. pret = checkEnumValues(sval);
  188. if (pret == null) {
  189. /* Check for keyword shorthand values to be substituted. */
  190. String pvalue = checkValueKeywords(sval);
  191. if (!pvalue.equals(sval)) {
  192. // System.err.println("Convert shorthand keyword" + pvalue);
  193. // Substituted a value: must parse it
  194. Property p =
  195. PropertyParser.parse(pvalue,
  196. new PropertyInfo(this,
  197. propertyList,
  198. fo));
  199. pret = convertProperty(p, propertyList, fo);
  200. }
  201. }
  202. }
  203. }
  204. } catch (FOPException e) {
  205. //getLogger().error("convertShorthandProperty caught FOPException "
  206. // + e);
  207. } catch (org.apache.fop.fo.expr.PropertyException propEx) {
  208. //getLogger().error("convertShorthandProperty caught PropertyException "
  209. // + propEx);
  210. }
  211. if (pret != null) {
  212. /*
  213. * System.err.println("Return shorthand value " + pret.getString() +
  214. * " for " + getPropName());
  215. */
  216. }
  217. return pret;
  218. }
  219. protected boolean isCompoundMaker() {
  220. return false;
  221. }
  222. public Property checkEnumValues(String value) {
  223. return null;
  224. }
  225. /**
  226. * Return a String to be parsed if the passed value corresponds to
  227. * a keyword which can be parsed and used to initialize the property.
  228. * For example, the border-width family of properties can have the
  229. * initializers "thin", "medium", or "thick". The foproperties.xml
  230. * file specifies a length value equivalent for these keywords,
  231. * such as "0.5pt" for "thin". These values are considered parseable,
  232. * since the Length object is no longer responsible for parsing
  233. * unit expresssions.
  234. * @param value The string value of property attribute.
  235. * @return A String containging a parseable equivalent or null if
  236. * the passed value isn't a keyword initializer for this Property.
  237. */
  238. protected String checkValueKeywords(String value) {
  239. return value;
  240. }
  241. /**
  242. * Return a Property object based on the passed Property object.
  243. * This method is called if the Property object built by the parser
  244. * isn't the right type for this property.
  245. * It is overridden by subclasses when the property specification in
  246. * foproperties.xml specifies conversion rules.
  247. * @param p The Property object return by the expression parser
  248. * @param propertyList The PropertyList object being built for this FO.
  249. * @param fo The current FO whose properties are being set.
  250. * @return A Property of the correct type or null if the parsed value
  251. * can't be converted to the correct type.
  252. */
  253. public Property convertProperty(Property p,
  254. PropertyList propertyList,
  255. FObj fo) throws FOPException {
  256. return null;
  257. }
  258. protected Property convertPropertyDatatype(Property p,
  259. PropertyList propertyList,
  260. FObj fo) {
  261. return null;
  262. }
  263. /**
  264. * Return a Property object representing the initial value.
  265. * @param propertyList The PropertyList object being built for this FO.
  266. */
  267. public Property make(PropertyList propertyList) throws FOPException {
  268. return null;
  269. }
  270. /**
  271. * Return a Property object representing the initial value.
  272. * @param propertyList The PropertyList object being built for this FO.
  273. * @param parentFO The parent FO for the FO whose property is being made.
  274. * @return a Property subclass object holding a "compound" property object
  275. * initialized to the default values for each component.
  276. */
  277. protected Property makeCompound(PropertyList propertyList,
  278. FObj parentFO) throws FOPException {
  279. return null;
  280. }
  281. /**
  282. * Return a Property object representing the value of this property,
  283. * based on other property values for this FO.
  284. * A special case is properties which inherit the specified value,
  285. * rather than the computed value.
  286. * @param propertyList The PropertyList for the FO.
  287. * @return Property A computed Property value or null if no rules
  288. * are specified (in foproperties.xml) to compute the value.
  289. */
  290. public Property compute(PropertyList propertyList)
  291. throws FOPException {
  292. if (inheritsSpecified()) {
  293. // recalculate based on last specified value
  294. // Climb up propertylist and find last spec'd value
  295. // NEED PROPNAME!!! get from Maker
  296. Property specProp =
  297. propertyList.getNearestSpecified(propName);
  298. if (specProp != null) {
  299. // Only need to do this if the value is relative!!!
  300. String specVal = specProp.getSpecifiedValue();
  301. if (specVal != null) {
  302. try {
  303. return make(propertyList, specVal,
  304. propertyList.getParentFObj());
  305. } catch (FOPException e) {
  306. //getLogger()error("Error computing property value for "
  307. // + propName + " from "
  308. // + specVal);
  309. return null;
  310. }
  311. }
  312. }
  313. }
  314. return null; // standard
  315. }
  316. public boolean isCorrespondingForced(PropertyList propertyList) {
  317. return false;
  318. }
  319. public Property getShorthand(PropertyList propertyList) {
  320. return null;
  321. }
  322. } // end of nested Maker class
  323. /**
  324. * The original specified value for properties which inherit
  325. * specified values.
  326. */
  327. private String specVal;
  328. /**
  329. * Set the original value specified for the property attribute.
  330. * @param specVal The specified value.
  331. */
  332. public void setSpecifiedValue(String specVal) {
  333. this.specVal = specVal;
  334. }
  335. /**
  336. * Return the original value specified for the property attribute.
  337. * @return The specified value as a String.
  338. */
  339. public String getSpecifiedValue() {
  340. return specVal;
  341. }
  342. /**
  343. * Accessor functions for all possible Property datatypes
  344. */
  345. public Length getLength() {
  346. return null;
  347. }
  348. public ColorType getColorType() {
  349. return null;
  350. }
  351. public CondLength getCondLength() {
  352. return null;
  353. }
  354. public LengthRange getLengthRange() {
  355. return null;
  356. }
  357. public LengthPair getLengthPair() {
  358. return null;
  359. }
  360. public Space getSpace() {
  361. return null;
  362. }
  363. public Keep getKeep() {
  364. return null;
  365. }
  366. public int getEnum() {
  367. return 0;
  368. }
  369. public char getCharacter() {
  370. return 0;
  371. }
  372. public Vector getList() {
  373. return null;
  374. } // List of Property objects
  375. public Number getNumber() {
  376. return null;
  377. }
  378. // Classes used when evaluating property expressions
  379. public Numeric getNumeric() {
  380. return null;
  381. }
  382. public String getNCname() {
  383. return null;
  384. }
  385. public Object getObject() {
  386. return null;
  387. }
  388. public String getString() {
  389. Object o = getObject();
  390. return (o == null) ? null : o.toString();
  391. }
  392. }