KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PropertyList.java,v 1.14.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 import org.apache.fop.messaging.MessageHandler;
54 import org.apache.fop.fo.properties.WritingMode;
55 import org.apache.fop.apps.FOPException;
56
57 import java.util.HashMap JavaDoc;
58
59
60 public class PropertyList extends HashMap JavaDoc {
61
62     private byte[] wmtable = null; // writing-mode values
63
public static final int LEFT = 0;
64     public static final int RIGHT = 1;
65     public static final int TOP = 2;
66     public static final int BOTTOM = 3;
67     public static final int HEIGHT = 4;
68     public static final int WIDTH = 5;
69
70     public static final int START = 0;
71     public static final int END = 1;
72     public static final int BEFORE = 2;
73     public static final int AFTER = 3;
74     public static final int BLOCKPROGDIM = 4;
75     public static final int INLINEPROGDIM = 5;
76
77     private static final String JavaDoc[] sAbsNames = new String JavaDoc[] {
78         "left", "right", "top", "bottom", "height", "width"
79     };
80
81     private static final String JavaDoc[] sRelNames = new String JavaDoc[] {
82         "start", "end", "before", "after", "block-progression-dimension",
83         "inline-progression-dimension"
84     };
85
86     private static final byte[][] wmtables;
87     static{
88         int i = Math.max( Math.max( WritingMode.LR_TB, WritingMode.RL_TB), WritingMode.TB_RL)+1;
89         wmtables = new byte[i][];
90         wmtables[ WritingMode.LR_TB] = /* lr-tb */
91             new byte[] {
92                 START, END, BEFORE, AFTER, BLOCKPROGDIM, INLINEPROGDIM
93             };
94         wmtables[ WritingMode.RL_TB] = /* rl-tb */
95             new byte[] {
96                 END, START, BEFORE, AFTER, BLOCKPROGDIM, INLINEPROGDIM
97             };
98         wmtables[ WritingMode.TB_RL] = /* tb-rl */
99             new byte[] {
100                 AFTER, BEFORE, START, END, INLINEPROGDIM, BLOCKPROGDIM
101             };
102     }
103
104     private PropertyListBuilder builder;
105     private PropertyList parentPropertyList = null;
106     String JavaDoc namespace = "";
107     String JavaDoc element = "";
108     FObj fobj = null;
109
110     public PropertyList(PropertyList parentPropertyList, String JavaDoc space,
111                         String JavaDoc el) {
112         this.parentPropertyList = parentPropertyList;
113         this.namespace = space;
114         this.element = el;
115     }
116
117     public void setFObj(FObj fobj) {
118         this.fobj = fobj;
119     }
120
121     public FObj getFObj() {
122         return this.fobj;
123     }
124
125     public FObj getParentFObj() {
126         if (parentPropertyList != null) {
127             return parentPropertyList.getFObj();
128         } else
129             return null;
130     }
131
132     /**
133      * Return the value explicitly specified on this FO.
134      * @param propertyName The name of the property whose value is desired.
135      * It may be a compound name, such as space-before.optimum.
136      * @return The value if the property is explicitly set or set by
137      * a shorthand property, otherwise null.
138      */

139     public Property getExplicitOrShorthand(String JavaDoc propertyName) {
140         /* Handle request for one part of a compound property */
141         int sepchar = propertyName.indexOf('.');
142         String JavaDoc baseName;
143         if (sepchar > -1) {
144             baseName = propertyName.substring(0, sepchar);
145         } else
146             baseName = propertyName;
147         Property p = getExplicitBaseProp(baseName);
148         if (p == null) {
149             p = builder.getShorthand(this, namespace, element, baseName);
150         }
151         if (p != null && sepchar > -1) {
152             return builder.getSubpropValue(namespace, element, baseName, p,
153                                            propertyName.substring(sepchar
154                                            + 1));
155         }
156         return p;
157     }
158
159     /**
160      * Return the value explicitly specified on this FO.
161      * @param propertyName The name of the property whose value is desired.
162      * It may be a compound name, such as space-before.optimum.
163      * @return The value if the property is explicitly set, otherwise null.
164      */

165     public Property getExplicit(String JavaDoc propertyName) {
166         /* Handle request for one part of a compound property */
167         int sepchar = propertyName.indexOf('.');
168         if (sepchar > -1) {
169             String JavaDoc baseName = propertyName.substring(0, sepchar);
170             Property p = getExplicitBaseProp(baseName);
171             if (p != null) {
172                 return this.builder.getSubpropValue(namespace, element,
173                                                     baseName, p,
174                                                     propertyName.substring(sepchar
175                                                     + 1));
176             } else
177                 return null;
178         }
179         return (Property)super.get(propertyName);
180     }
181
182     /**
183      * Return the value explicitly specified on this FO.
184      * @param propertyName The name of the base property whose value is desired.
185      * @return The value if the property is explicitly set, otherwise null.
186      */

187     public Property getExplicitBaseProp(String JavaDoc propertyName) {
188         return (Property)super.get(propertyName);
189     }
190
191     /**
192      * Return the value of this property inherited by this FO.
193      * Implements the inherited-property-value function.
194      * The property must be inheritable!
195      * @param propertyName The name of the property whose value is desired.
196      * @return The inherited value, otherwise null.
197      */

198     public Property getInherited(String JavaDoc propertyName) {
199         if (builder != null) {
200             if (parentPropertyList != null
201                     && builder.isInherited(namespace, element,
202                                            propertyName)) {
203                 return parentPropertyList.get(propertyName);
204             } else {
205                 // return the "initial" value
206
try {
207                     return builder.makeProperty(this, namespace, element,
208                                                 propertyName);
209                 } catch (org.apache.fop.apps.FOPException e) {
210                     MessageHandler.errorln("Exception in getInherited(): property="
211                                            + propertyName + " : " + e);
212                 }
213             }
214         }
215         return null; // No builder or exception in makeProperty!
216
}
217
218     /*
219      * If the property is a relative property with a corresponding absolute
220      * value specified, the absolute value is used. This is also true of
221      * the inheritance priority (I think...)
222      * If the property is an "absolute" property and it isn't specified, then
223      * we try to compute it from the corresponding relative property: this
224      * happends in computeProperty.
225      */

226     private Property findProperty(String JavaDoc propertyName, boolean bTryInherit) {
227         Property p = null;
228         if (builder.isCorrespondingForced(this, namespace, element,
229                                           propertyName)) {
230             p = builder.computeProperty(this, namespace, element,
231                                         propertyName);
232         } else {
233             p = getExplicitBaseProp(propertyName);
234             if (p == null) {
235                 p = this.builder.computeProperty(this, namespace, element,
236                                                  propertyName);
237             }
238             if (p == null) { // check for shorthand specification
239
p = builder.getShorthand(this, namespace, element,
240                                          propertyName);
241             }
242             if (p == null
243                     && bTryInherit) { // else inherit (if has parent and is inheritable)
244
if (this.parentPropertyList != null
245                         && builder.isInherited(namespace, element,
246                                                propertyName)) {
247                     p = parentPropertyList.findProperty(propertyName, true);
248                 }
249             }
250         }
251         return p;
252     }
253
254
255     /**
256      * Return the property on the current FlowObject if it is specified, or if a
257      * corresponding property is specified. If neither is specified, it returns null.
258      */

259     public Property getSpecified(String JavaDoc propertyName) {
260         return get(propertyName, false, false);
261     }
262
263
264     /**
265      * Return the property on the current FlowObject. If it isn't set explicitly,
266      * this will try to compute it based on other properties, or if it is
267      * inheritable, to return the inherited value. If all else fails, it returns
268      * the default value.
269      */

270     public Property get(String JavaDoc propertyName) {
271         return get(propertyName, true, true);
272     }
273
274     /**
275      * Return the property on the current FlowObject. Depending on the passed flags,
276      * this will try to compute it based on other properties, or if it is
277      * inheritable, to return the inherited value. If all else fails, it returns
278      * the default value.
279      */

280     private Property get(String JavaDoc propertyName, boolean bTryInherit,
281                          boolean bTryDefault) {
282
283         if (builder == null)
284             MessageHandler.errorln("OH OH, builder has not been set");
285
286             /* Handle request for one part of a compound property */
287         int sepchar = propertyName.indexOf('.');
288         String JavaDoc subpropName = null;
289         if (sepchar > -1) {
290             subpropName = propertyName.substring(sepchar + 1);
291             propertyName = propertyName.substring(0, sepchar);
292         }
293
294         Property p = findProperty(propertyName, bTryInherit);
295         if (p == null && bTryDefault) { // default value for this FO!
296
try {
297                 p = this.builder.makeProperty(this, namespace, element,
298                                               propertyName);
299             } catch (FOPException e) {
300                 // don't know what to do here
301
}
302         }
303 /*
304         // if value is inherit then get computed value from
305         // parent
306         // namespaces??
307         if(p != null && "inherit".equals(p.getString())) {
308             if (this.parentPropertyList != null) {
309                 p = parentPropertyList.get(propertyName, true, false);
310             }
311         }
312 */

313         if (subpropName != null && p != null) {
314             return this.builder.getSubpropValue(namespace, element,
315                                                 propertyName, p, subpropName);
316         } else
317             return p;
318     }
319
320     public void setBuilder(PropertyListBuilder builder) {
321         this.builder = builder;
322     }
323
324     public String JavaDoc getNameSpace() {
325         return namespace;
326     }
327
328     public String JavaDoc getElement() {
329         return element;
330     }
331
332     /**
333      * Return the "nearest" specified value for the given property.
334      * Implements the from-nearest-specified-value function.
335      * @param propertyName The name of the property whose value is desired.
336      * @return The computed value if the property is explicitly set on some
337      * ancestor of the current FO, else the initial value.
338      */

339     public Property getNearestSpecified(String JavaDoc propertyName) {
340         Property p = null;
341         for (PropertyList plist = this; p == null && plist != null;
342                 plist = plist.parentPropertyList) {
343             p = plist.getExplicit(propertyName);
344         }
345         if (p == null) {
346             // If no explicit setting found, return initial (default) value.
347
try {
348                 p = this.builder.makeProperty(this, namespace, element,
349                                               propertyName);
350             } catch (FOPException e) {
351                 MessageHandler.errorln("Exception in getNearestSpecified(): property="
352                                        + propertyName + " : " + e);
353             }
354         }
355         return p;
356     }
357
358     /**
359      * Return the value of this property on the parent of this FO.
360      * Implements the from-parent function.
361      * @param propertyName The name of the property whose value is desired.
362      * @return The computed value on the parent or the initial value if this
363      * FO is the root or is in a different namespace from its parent.
364      */

365     public Property getFromParent(String JavaDoc propertyName) {
366         if (parentPropertyList != null) {
367             return parentPropertyList.get(propertyName);
368         } else if (builder != null) {
369             // return the "initial" value
370
try {
371                 return builder.makeProperty(this, namespace, element,
372                                             propertyName);
373             } catch (org.apache.fop.apps.FOPException e) {
374                 MessageHandler.errorln("Exception in getFromParent(): property="
375                                        + propertyName + " : " + e);
376             }
377         }
378         return null; // No builder or exception in makeProperty!
379
}
380
381     /**
382      * Given an absolute direction (top, bottom, left, right),
383      * return the corresponding writing model relative direction name
384      * for the flow object. Uses the stored writingMode.
385      */

386     public String JavaDoc wmAbsToRel(int absdir) {
387         if (wmtable != null) {
388             return sRelNames[wmtable[absdir]];
389         } else
390             return "";
391     }
392
393     /**
394      * Given a writing mode relative direction (start, end, before, after)
395      * return the corresponding absolute direction name
396      * for the flow object. Uses the stored writingMode.
397      */

398     public String JavaDoc wmRelToAbs(int reldir) {
399         if (wmtable != null) {
400             for (int i = 0; i < wmtable.length; i++) {
401                 if (wmtable[i] == reldir)
402                     return sAbsNames[i];
403             }
404         }
405         return "";
406     }
407
408     /**
409      * Set the writing mode traits for the FO with this property list.
410      */

411     public void setWritingMode(int writingMode) {
412         this.wmtable = wmtables[writingMode];
413     }
414
415 }
416
417
Popular Tags