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 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * $Id: Property.java,v 1.22 2003/03/05 21:48:02 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.fo;
  52. import org.apache.fop.datatypes.ColorType;
  53. import org.apache.fop.datatypes.CondLength;
  54. import org.apache.fop.datatypes.Keep;
  55. import org.apache.fop.datatypes.Length;
  56. import org.apache.fop.datatypes.LengthPair;
  57. import org.apache.fop.datatypes.LengthRange;
  58. import org.apache.fop.datatypes.PercentBase;
  59. import org.apache.fop.datatypes.Space;
  60. import org.apache.fop.fo.expr.Numeric;
  61. import org.apache.fop.fo.expr.PropertyParser;
  62. import org.apache.fop.fo.expr.PropertyInfo;
  63. import org.apache.fop.apps.FOPException;
  64. import java.util.Vector;
  65. /**
  66. * Base class for all property objects
  67. * @author unascribed
  68. */
  69. public class Property {
  70. /**
  71. * Base class for all property makers
  72. * @author unascribed
  73. */
  74. public static class Maker {
  75. private static final String UNKNOWN = "UNKNOWN";
  76. private String propName;
  77. /**
  78. * @return the name of the property for this Maker
  79. */
  80. protected String getPropName() {
  81. return propName;
  82. }
  83. /**
  84. * Construct an instance of a Property.Maker for the given property.
  85. * @param propName The name of the property to be made.
  86. */
  87. protected Maker(String propName) {
  88. this.propName = propName;
  89. }
  90. /**
  91. * Construct an instance of a Property.Maker.
  92. * Note: the property name is set to "UNKNOWN".
  93. */
  94. protected Maker() {
  95. this.propName = UNKNOWN;
  96. }
  97. /**
  98. * Default implementation of isInherited.
  99. * @return A boolean indicating whether this property is inherited.
  100. */
  101. public boolean isInherited() {
  102. return false;
  103. }
  104. /**
  105. * Return a boolean indicating whether this property inherits the
  106. * "specified" value rather than the "computed" value. The default is
  107. * to inherit the "computed" value.
  108. * @return true, if the property inherits the value specified.
  109. */
  110. public boolean inheritsSpecified() {
  111. return false;
  112. }
  113. /**
  114. * This is used to handle properties specified as a percentage of
  115. * some "base length", such as the content width of their containing
  116. * box.
  117. * Overridden by subclasses which allow percent specifications. See
  118. * the documentation on properties.xsl for details.
  119. * @return an object implementing the PercentBase interface.
  120. */
  121. public PercentBase getPercentBase(FObj fo, PropertyList pl) {
  122. return null;
  123. }
  124. /**
  125. * Return a Maker object which is used to set the values on components
  126. * of compound property types, such as "space".
  127. * Overridden by property maker subclasses which handle
  128. * compound properties.
  129. * @param subprop The name of the component for which a Maker is to
  130. * returned, for example "optimum", if the FO attribute is
  131. * space.optimum='10pt'.
  132. */
  133. protected Maker getSubpropMaker(String subprop) {
  134. return null;
  135. }
  136. /**
  137. * Return a property value for the given component of a compound
  138. * property.
  139. * @param p A property value for a compound property type such as
  140. * SpaceProperty.
  141. * @param subprop The name of the component whose value is to be
  142. * returned.
  143. * NOTE: this is only to ease porting when calls are made to
  144. * PropertyList.get() using a component name of a compound property,
  145. * such as get("space.optimum"). The recommended technique is:
  146. * get("space").getOptimum().
  147. * Overridden by property maker subclasses which handle
  148. * compound properties.
  149. */
  150. public Property getSubpropValue(Property p, String subprop) {
  151. return null;
  152. }
  153. /**
  154. * Return a property value for a compound property. If the property
  155. * value is already partially initialized, this method will modify it.
  156. * @param baseProp The Property object representing the compound property,
  157. * such as SpaceProperty.
  158. * @param partName The name of the component whose value is specified.
  159. * @param propertyList The propertyList being built.
  160. * @param fo The FO whose properties are being set.
  161. * @return A compound property object.
  162. */
  163. public Property make(Property baseProp, String partName,
  164. PropertyList propertyList, String value,
  165. FObj fo) throws FOPException {
  166. if (baseProp == null) {
  167. baseProp = makeCompound(propertyList, fo);
  168. }
  169. Maker spMaker = getSubpropMaker(partName);
  170. if (spMaker != null) {
  171. Property p = spMaker.make(propertyList, value, fo);
  172. if (p != null) {
  173. return setSubprop(baseProp, partName, p);
  174. }
  175. } else {
  176. //getLogger().error("compound property component "
  177. // + partName + " unknown.");
  178. }
  179. return baseProp;
  180. }
  181. /**
  182. * Set a component in a compound property and return the modified
  183. * compound property object.
  184. * This default implementation returns the original base property
  185. * without modifying it.
  186. * It is overridden by property maker subclasses which handle
  187. * compound properties.
  188. * @param baseProp The Property object representing the compound property,
  189. * such as SpaceProperty.
  190. * @param partName The name of the component whose value is specified.
  191. * @param subProp A Property object holding the specified value of the
  192. * component to be set.
  193. * @return The modified compound property object.
  194. */
  195. protected Property setSubprop(Property baseProp, String partName,
  196. Property subProp) {
  197. return baseProp;
  198. }
  199. /**
  200. * Create a Property object from an attribute specification.
  201. * @param propertyList The PropertyList object being built for this FO.
  202. * @param value The attribute value.
  203. * @param fo The current FO whose properties are being set.
  204. * @return The initialized Property object.
  205. */
  206. public Property make(PropertyList propertyList, String value,
  207. FObj fo) throws FOPException {
  208. try {
  209. Property pret = null;
  210. String pvalue = value;
  211. pret = checkEnumValues(value);
  212. if (pret == null) {
  213. /* Check for keyword shorthand values to be substituted. */
  214. pvalue = checkValueKeywords(value);
  215. // Override parsePropertyValue in each subclass of Property.Maker
  216. Property p = PropertyParser.parse(pvalue,
  217. new PropertyInfo(this,
  218. propertyList, fo));
  219. pret = convertProperty(p, propertyList, fo);
  220. } else if (isCompoundMaker()) {
  221. pret = convertProperty(pret, propertyList, fo);
  222. }
  223. if (pret == null) {
  224. throw new org.apache.fop.fo.expr.PropertyException("No conversion defined");
  225. } else if (inheritsSpecified()) {
  226. pret.setSpecifiedValue(pvalue);
  227. }
  228. return pret;
  229. } catch (org.apache.fop.fo.expr.PropertyException propEx) {
  230. throw new FOPException("Error in " + propName
  231. + " property value '" + value + "': "
  232. + propEx);
  233. }
  234. }
  235. public Property convertShorthandProperty(PropertyList propertyList,
  236. Property prop, FObj fo) {
  237. Property pret = null;
  238. try {
  239. pret = convertProperty(prop, propertyList, fo);
  240. if (pret == null) {
  241. // If value is a name token, may be keyword or Enum
  242. String sval = prop.getNCname();
  243. if (sval != null) {
  244. // System.err.println("Convert shorthand ncname " + sval);
  245. pret = checkEnumValues(sval);
  246. if (pret == null) {
  247. /* Check for keyword shorthand values to be substituted. */
  248. String pvalue = checkValueKeywords(sval);
  249. if (!pvalue.equals(sval)) {
  250. // System.err.println("Convert shorthand keyword" + pvalue);
  251. // Substituted a value: must parse it
  252. Property p =
  253. PropertyParser.parse(pvalue,
  254. new PropertyInfo(this,
  255. propertyList,
  256. fo));
  257. pret = convertProperty(p, propertyList, fo);
  258. }
  259. }
  260. }
  261. }
  262. } catch (FOPException e) {
  263. //getLogger().error("convertShorthandProperty caught FOPException "
  264. // + e);
  265. } catch (org.apache.fop.fo.expr.PropertyException propEx) {
  266. //getLogger().error("convertShorthandProperty caught PropertyException "
  267. // + propEx);
  268. }
  269. if (pret != null) {
  270. /*
  271. * System.err.println("Return shorthand value " + pret.getString() +
  272. * " for " + getPropName());
  273. */
  274. }
  275. return pret;
  276. }
  277. protected boolean isCompoundMaker() {
  278. return false;
  279. }
  280. public Property checkEnumValues(String value) {
  281. return null;
  282. }
  283. /**
  284. * Return a String to be parsed if the passed value corresponds to
  285. * a keyword which can be parsed and used to initialize the property.
  286. * For example, the border-width family of properties can have the
  287. * initializers "thin", "medium", or "thick". The foproperties.xml
  288. * file specifies a length value equivalent for these keywords,
  289. * such as "0.5pt" for "thin". These values are considered parseable,
  290. * since the Length object is no longer responsible for parsing
  291. * unit expresssions.
  292. * @param value The string value of property attribute.
  293. * @return A String containging a parseable equivalent or null if
  294. * the passed value isn't a keyword initializer for this Property.
  295. */
  296. protected String checkValueKeywords(String value) {
  297. return value;
  298. }
  299. /**
  300. * Return a Property object based on the passed Property object.
  301. * This method is called if the Property object built by the parser
  302. * isn't the right type for this property.
  303. * It is overridden by subclasses when the property specification in
  304. * foproperties.xml specifies conversion rules.
  305. * @param p The Property object return by the expression parser
  306. * @param propertyList The PropertyList object being built for this FO.
  307. * @param fo The current FO whose properties are being set.
  308. * @return A Property of the correct type or null if the parsed value
  309. * can't be converted to the correct type.
  310. */
  311. public Property convertProperty(Property p,
  312. PropertyList propertyList,
  313. FObj fo) throws FOPException {
  314. return null;
  315. }
  316. protected Property convertPropertyDatatype(Property p,
  317. PropertyList propertyList,
  318. FObj fo) {
  319. return null;
  320. }
  321. /**
  322. * Return a Property object representing the initial value.
  323. * @param propertyList The PropertyList object being built for this FO.
  324. */
  325. public Property make(PropertyList propertyList) throws FOPException {
  326. return null;
  327. }
  328. /**
  329. * Return a Property object representing the initial value.
  330. * @param propertyList The PropertyList object being built for this FO.
  331. * @param parentFO The parent FO for the FO whose property is being made.
  332. * @return a Property subclass object holding a "compound" property object
  333. * initialized to the default values for each component.
  334. */
  335. protected Property makeCompound(PropertyList propertyList,
  336. FObj parentFO) throws FOPException {
  337. return null;
  338. }
  339. /**
  340. * Return a Property object representing the value of this property,
  341. * based on other property values for this FO.
  342. * A special case is properties which inherit the specified value,
  343. * rather than the computed value.
  344. * @param propertyList The PropertyList for the FO.
  345. * @return Property A computed Property value or null if no rules
  346. * are specified (in foproperties.xml) to compute the value.
  347. */
  348. public Property compute(PropertyList propertyList)
  349. throws FOPException {
  350. if (inheritsSpecified()) {
  351. // recalculate based on last specified value
  352. // Climb up propertylist and find last spec'd value
  353. // NEED PROPNAME!!! get from Maker
  354. Property specProp =
  355. propertyList.getNearestSpecified(propName);
  356. if (specProp != null) {
  357. // Only need to do this if the value is relative!!!
  358. String specVal = specProp.getSpecifiedValue();
  359. if (specVal != null) {
  360. try {
  361. return make(propertyList, specVal,
  362. propertyList.getParentFObj());
  363. } catch (FOPException e) {
  364. //getLogger()error("Error computing property value for "
  365. // + propName + " from "
  366. // + specVal);
  367. return null;
  368. }
  369. }
  370. }
  371. }
  372. return null; // standard
  373. }
  374. public boolean isCorrespondingForced(PropertyList propertyList) {
  375. return false;
  376. }
  377. public Property getShorthand(PropertyList propertyList) {
  378. return null;
  379. }
  380. } // end of nested Maker class
  381. /**
  382. * The original specified value for properties which inherit
  383. * specified values.
  384. */
  385. private String specVal;
  386. /**
  387. * Set the original value specified for the property attribute.
  388. * @param specVal The specified value.
  389. */
  390. public void setSpecifiedValue(String specVal) {
  391. this.specVal = specVal;
  392. }
  393. /**
  394. * Return the original value specified for the property attribute.
  395. * @return The specified value as a String.
  396. */
  397. public String getSpecifiedValue() {
  398. return specVal;
  399. }
  400. /**
  401. * Accessor functions for all possible Property datatypes
  402. */
  403. public Length getLength() {
  404. return null;
  405. }
  406. public ColorType getColorType() {
  407. return null;
  408. }
  409. public CondLength getCondLength() {
  410. return null;
  411. }
  412. public LengthRange getLengthRange() {
  413. return null;
  414. }
  415. public LengthPair getLengthPair() {
  416. return null;
  417. }
  418. public Space getSpace() {
  419. return null;
  420. }
  421. public Keep getKeep() {
  422. return null;
  423. }
  424. public int getEnum() {
  425. return 0;
  426. }
  427. public char getCharacter() {
  428. return 0;
  429. }
  430. public Vector getList() {
  431. return null;
  432. } // List of Property objects
  433. public Number getNumber() {
  434. return null;
  435. }
  436. // Classes used when evaluating property expressions
  437. public Numeric getNumeric() {
  438. return null;
  439. }
  440. public String getNCname() {
  441. return null;
  442. }
  443. public Object getObject() {
  444. return null;
  445. }
  446. public String getString() {
  447. Object o = getObject();
  448. return (o == null) ? null : o.toString();
  449. }
  450. }