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.java 16KB

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