KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > Property


1 /*
2  * $Id: Property.java,v 1.18.2.4 2003/02/25 12:56:54 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
53 // FOP
54
import org.apache.fop.datatypes.*;
55 import org.apache.fop.fo.expr.Numeric;
56 import org.apache.fop.fo.expr.PropertyParser;
57 import org.apache.fop.fo.expr.PropertyInfo;
58 import org.apache.fop.apps.FOPException;
59
60 // Avalon
61
import org.apache.avalon.framework.logger.Logger;
62
63 // Java
64
import java.util.ArrayList JavaDoc;
65
66 public class Property {
67
68     public static class Maker {
69         private static final String JavaDoc UNKNOWN = "UNKNOWN";
70         private String JavaDoc propName;
71
72         /**
73          * Return the name of the property whose value is being set.
74          */

75         protected String JavaDoc getPropName() {
76             return propName;
77         }
78
79         /**
80          * Construct an instance of a Property.Maker for the given property.
81          * @param propName The name of the property to be made.
82          */

83         protected Maker(String JavaDoc propName) {
84             this.propName = propName;
85         }
86
87         /**
88          * Construct an instance of a Property.Maker.
89          * Note: the property name is set to "UNKNOWN".
90          */

91         protected Maker() {
92             this.propName = UNKNOWN;
93         }
94
95
96         /**
97          * Default implementation of isInherited.
98          * @return A boolean indicating whether this property is inherited.
99          */

100         public boolean isInherited() {
101             return false;
102         }
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 If true, property inherits the value specified.
109          */

110         public boolean inheritsSpecified() {
111             return false;
112         }
113
114
115         /**
116          * Return an object implementing the PercentBase interface.
117          * This is used to handle properties specified as a percentage of
118          * some "base length", such as the content width of their containing
119          * box.
120          * Overridden by subclasses which allow percent specifications. See
121          * the documentation on properties.xsl for details.
122          */

123         public PercentBase getPercentBase(FObj fo, PropertyList pl) {
124             return null;
125         }
126
127         /**
128          * Return a Maker object which is used to set the values on components
129          * of compound property types, such as "space".
130          * Overridden by property maker subclasses which handle
131          * compound properties.
132          * @param subprop The name of the component for which a Maker is to
133          * returned, for example "optimum", if the FO attribute is
134          * space.optimum='10pt'.
135          */

136         protected Maker getSubpropMaker(String JavaDoc subprop) {
137             return null;
138         }
139
140         /**
141          * Return a property value for the given component of a compound
142          * property.
143          * @param p A property value for a compound property type such as
144          * SpaceProperty.
145          * @param subprop The name of the component whose value is to be
146          * returned.
147          * NOTE: this is only to ease porting when calls are made to
148          * PropertyList.get() using a component name of a compound property,
149          * such as get("space.optimum"). The recommended technique is:
150          * get("space").getOptimum().
151          * Overridden by property maker subclasses which handle
152          * compound properties.
153          */

154         public Property getSubpropValue(Property p, String JavaDoc subprop) {
155             return null;
156         }
157
158         /**
159          * Return a property value for a compound property. If the property
160          * value is already partially initialized, this method will modify it.
161          * @param baseProp The Property object representing the compound property,
162          * such as SpaceProperty.
163          * @param partName The name of the component whose value is specified.
164          * @param propertyList The propertyList being built.
165          * @param fo The FO whose properties are being set.
166          * @return A compound property object.
167          */

168         public Property make(Property baseProp, String JavaDoc partName,
169                              PropertyList propertyList, String JavaDoc value,
170                              FObj fo) throws FOPException {
171             if (baseProp == null) {
172                 baseProp = makeCompound(propertyList, fo);
173             }
174             Maker spMaker = getSubpropMaker(partName);
175             if (spMaker != null) {
176                 Property p = spMaker.make(propertyList, value, fo);
177                 if (p != null) {
178                     return setSubprop(baseProp, partName, p);
179                 }
180             } else {
181                 //MessageHandler.errorln("compound property component "
182
// + partName + " unknown.");
183
}
184             return baseProp;
185         }
186
187         /**
188          * Set a component in a compound property and return the modified
189          * compound property object.
190          * This default implementation returns the original base property
191          * without modifying it.
192          * It is overridden by property maker subclasses which handle
193          * compound properties.
194          * @param baseProp The Property object representing the compound property,
195          * such as SpaceProperty.
196          * @param partName The name of the component whose value is specified.
197          * @param subProp A Property object holding the specified value of the
198          * component to be set.
199          * @return The modified compound property object.
200          */

201         protected Property setSubprop(Property baseProp, String JavaDoc partName,
202                                       Property subProp) {
203             return baseProp;
204         }
205
206         /**
207          * Create a Property object from an attribute specification.
208          * @param propertyList The PropertyList object being built for this FO.
209          * @param value The attribute value.
210          * @param fo The current FO whose properties are being set.
211          * @return The initialized Property object.
212          */

213         public Property make(PropertyList propertyList, String JavaDoc value,
214                              FObj fo) throws FOPException {
215             try {
216                 Property pret = null;
217                 String JavaDoc pvalue = value;
218                 pret = checkEnumValues(value);
219                 if (pret == null) {
220                     /* Check for keyword shorthand values to be substituted. */
221                     pvalue = checkValueKeywords(value);
222                     // Override parsePropertyValue in each subclass of Property.Maker
223
Property p = PropertyParser.parse(pvalue,
224                                                       new PropertyInfo(this,
225                                                       propertyList, fo));
226                     pret = convertProperty(p, propertyList, fo);
227                 } else if (isCompoundMaker()) {
228                     pret = convertProperty(pret, propertyList, fo);
229                 }
230                 if (pret == null) {
231                     throw new org.apache.fop.fo.expr.PropertyException("No conversion defined");
232                 } else if (inheritsSpecified()) {
233                     pret.setSpecifiedValue(pvalue);
234                 }
235                 return pret;
236             } catch (org.apache.fop.fo.expr.PropertyException propEx) {
237                 throw new FOPException("Error in " + propName +
238                                        " property value '" + value + "': " +
239                                        propEx);
240             }
241         }
242
243         public Property convertShorthandProperty(PropertyList propertyList,
244                                                  Property prop, FObj fo) {
245             Property pret = null;
246             try {
247                 pret = convertProperty(prop, propertyList, fo);
248                 if (pret == null) {
249                     // If value is a name token, may be keyword or Enum
250
String JavaDoc sval = prop.getNCname();
251                     if (sval != null) {
252                         // System.err.println("Convert shorthand ncname " + sval);
253
pret = checkEnumValues(sval);
254                         if (pret == null) {
255                             /* Check for keyword shorthand values to be substituted. */
256                             String JavaDoc pvalue = checkValueKeywords(sval);
257                             if (!pvalue.equals(sval)) {
258                                 // System.err.println("Convert shorthand keyword" + pvalue);
259
// Substituted a value: must parse it
260
Property p =
261                                     PropertyParser.parse(pvalue,
262                                                          new PropertyInfo(this,
263                                                                           propertyList,
264                                                                           fo));
265                                 pret = convertProperty(p, propertyList, fo);
266                             }
267                         }
268                     }
269                 }
270             } catch (FOPException e) {
271
272                 //MessageHandler.errorln("convertShorthandProperty caught FOPException "
273
// + e);
274
} catch (org.apache.fop.fo.expr.PropertyException propEx) {
275                 //MessageHandler.errorln("convertShorthandProperty caught PropertyException "
276
// + propEx);
277
}
278             if (pret != null) {
279                 /*
280                  * System.err.println("Return shorthand value " + pret.getString() +
281                  * " for " + getPropName());
282                  */

283             }
284             return pret;
285         }
286
287         protected boolean isCompoundMaker() {
288             return false;
289         }
290
291         public Property checkEnumValues(String JavaDoc value) {
292             return null;
293         }
294
295         /**
296          * Return a String to be parsed if the passed value corresponds to
297          * a keyword which can be parsed and used to initialize the property.
298          * For example, the border-width family of properties can have the
299          * initializers "thin", "medium", or "thick". The foproperties.xml
300          * file specifies a length value equivalent for these keywords,
301          * such as "0.5pt" for "thin". These values are considered parseable,
302          * since the Length object is no longer responsible for parsing
303          * unit expresssions.
304          * @param value The string value of property attribute.
305          * @return A String containging a parseable equivalent or null if
306          * the passed value isn't a keyword initializer for this Property.
307          */

308         protected String JavaDoc checkValueKeywords(String JavaDoc value) {
309             return value;
310         }
311
312         /**
313          * Return a Property object based on the passed Property object.
314          * This method is called if the Property object built by the parser
315          * isn't the right type for this property.
316          * It is overridden by subclasses when the property specification in
317          * foproperties.xml specifies conversion rules.
318          * @param p The Property object return by the expression parser
319          * @param propertyList The PropertyList object being built for this FO.
320          * @param fo The current FO whose properties are being set.
321          * @return A Property of the correct type or null if the parsed value
322          * can't be converted to the correct type.
323          */

324         public Property convertProperty(Property p,
325                                         PropertyList propertyList,
326                                         FObj fo) throws FOPException {
327             return null;
328         }
329
330         protected Property convertPropertyDatatype(Property p,
331                                                    PropertyList propertyList,
332                                                    FObj fo) {
333             return null;
334         }
335
336         /**
337          * Return a Property object representing the initial value.
338          * @param propertyList The PropertyList object being built for this FO.
339          */

340         public Property make(PropertyList propertyList) throws FOPException {
341             return null;
342         }
343
344         /**
345          * Return a Property object representing the initial value.
346          * @param propertyList The PropertyList object being built for this FO.
347          * @param parentFO The parent FO for the FO whose property is being made.
348          * @return a Property subclass object holding a "compound" property object
349          * initialized to the default values for each component.
350          */

351         protected Property makeCompound(PropertyList propertyList,
352                                         FObj parentFO) throws FOPException {
353             return null;
354         }
355
356         /**
357          * Return a Property object representing the value of this property,
358          * based on other property values for this FO.
359          * A special case is properties which inherit the specified value,
360          * rather than the computed value.
361          * @param propertyList The PropertyList for the FO.
362          * @return Property A computed Property value or null if no rules
363          * are specified (in foproperties.xml) to compute the value.
364          */

365         public Property compute(PropertyList propertyList)
366                 throws FOPException {
367             if (inheritsSpecified()) {
368                 // recalculate based on last specified value
369
// Climb up propertylist and find last spec'd value
370
// NEED PROPNAME!!! get from Maker
371
Property specProp =
372                     propertyList.getNearestSpecified(propName);
373                 if (specProp != null) {
374                     // Only need to do this if the value is relative!!!
375
String JavaDoc specVal = specProp.getSpecifiedValue();
376                     if (specVal != null) {
377                         try {
378                             return make(propertyList, specVal,
379                                         propertyList.getParentFObj());
380                         } catch (FOPException e) {
381                             //MessageHandler.errorln("Error computing property value for "
382
// + propName + " from "
383
// + specVal);
384
return null;
385                         }
386                     }
387                 }
388             }
389             return null; // standard
390
}
391
392         public boolean isCorrespondingForced(PropertyList propertyList) {
393             return false;
394         }
395
396         public Property getShorthand(PropertyList propertyList) {
397             return null;
398         }
399
400     } // end of nested Maker class
401

402     /**
403      * The original specified value for properties which inherit
404      * specified values.
405      */

406     private String JavaDoc specVal;
407
408     protected Logger log;
409
410     public void setLogger(Logger logger) {
411         log = logger;
412     }
413
414     /**
415      * Set the original value specified for the property attribute.
416      * @param specVal The specified value.
417      */

418     public void setSpecifiedValue(String JavaDoc specVal) {
419         this.specVal = specVal;
420     }
421
422     /**
423      * Return the original value specified for the property attribute.
424      * @return The specified value as a String.
425      */

426     public String JavaDoc getSpecifiedValue() {
427         return specVal;
428     }
429
430     /**
431      * Accessor functions for all possible Property datatypes
432      */

433     public Length getLength() {
434         return null;
435     }
436
437     public ColorType getColorType() {
438         return null;
439     }
440
441     public CondLength getCondLength() {
442         return null;
443     }
444
445     public LengthRange getLengthRange() {
446         return null;
447     }
448
449     public LengthPair getLengthPair() {
450         return null;
451     }
452
453     public Space getSpace() {
454         return null;
455     }
456
457     public Keep getKeep() {
458         return null;
459     }
460
461     public int getEnum() {
462         return 0;
463     }
464
465     public char getCharacter() {
466         return 0;
467     }
468
469     public ArrayList JavaDoc getList() {
470         return null;
471     } // List of Property objects
472

473     public Number JavaDoc getNumber() {
474         return null;
475     }
476
477     // Classes used when evaluating property expressions
478
public Numeric getNumeric() {
479         return null;
480     }
481
482     public String JavaDoc getNCname() {
483         return null;
484     }
485
486     public Object JavaDoc getObject() {
487         return null;
488     }
489
490     public String JavaDoc getString() {
491         Object JavaDoc o = getObject();
492         return (o == null) ? null : o.toString();
493     }
494
495 }
496
Popular Tags