KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PropertyListBuilder.java,v 1.33.2.1 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
55 import org.apache.fop.apps.FOPException;
56
57 import org.xml.sax.Attributes JavaDoc;
58
59 import java.util.HashMap JavaDoc;
60
61 public class PropertyListBuilder {
62
63     /**
64      * Name of font-size property attribute to set first.
65      */

66     private static final String JavaDoc FONTSIZEATTR = "font-size";
67
68     private HashMap JavaDoc propertyListTable;
69     private HashMap JavaDoc elementTable;
70
71     public PropertyListBuilder() {
72         this.propertyListTable = new HashMap JavaDoc();
73         this.elementTable = new HashMap JavaDoc();
74     }
75
76     public void addList(HashMap JavaDoc list) {
77         propertyListTable.putAll(list);
78     }
79
80     public void addElementList(String JavaDoc element, HashMap JavaDoc list) {
81         elementTable.put(element, list);
82     }
83
84     public Property computeProperty(PropertyList propertyList, String JavaDoc space,
85                                     String JavaDoc element, String JavaDoc propertyName) {
86
87         Property p = null;
88         Property.Maker propertyMaker = findMaker(space, element,
89                                                  propertyName);
90         if (propertyMaker != null) {
91             try {
92                 p = propertyMaker.compute(propertyList);
93             } catch (FOPException e) {
94                 MessageHandler.errorln("exception occurred while computing"
95                                        + " value of property '"
96                                        + propertyName + "': "
97                                        + e.getMessage());
98             }
99         } else {
100             MessageHandler.errorln("property " + propertyName
101                                    + " ignored");
102         }
103         return p;
104     }
105
106     public boolean isInherited(String JavaDoc space, String JavaDoc element,
107                                String JavaDoc propertyName) {
108         boolean b;
109
110         Property.Maker propertyMaker = findMaker(space, element,
111                                                  propertyName);
112         if (propertyMaker != null) {
113             b = propertyMaker.isInherited();
114         } else {
115             // MessageHandler.errorln("Unknown property " + propertyName);
116
b = true;
117         }
118         return b;
119     }
120
121     public PropertyList makeList(String JavaDoc ns, String JavaDoc elementName, Attributes JavaDoc attributes,
122                                  PropertyList parentPropertyList,
123                                  FObj parentFO) throws FOPException {
124         String JavaDoc space = "http://www.w3.org/TR/1999/XSL/Format";
125         if (ns != null) {
126             space = ns;
127         }
128
129         PropertyList par = null;
130         if (parentPropertyList != null
131                 && space.equals(parentPropertyList.getNameSpace())) {
132             par = parentPropertyList;
133         }
134         PropertyList p = new PropertyList(par, space,
135                                           elementName);
136         p.setBuilder(this);
137         HashMap JavaDoc table;
138         table = (HashMap JavaDoc)elementTable.get(elementName);
139
140         /* Store names of properties already set. */
141         StringBuffer JavaDoc propsDone = new StringBuffer JavaDoc(256);
142         propsDone.append(' ');
143
144         /*
145          * If font-size is set on this FO, must set it first, since
146          * other attributes specified in terms of "ems" depend on it.
147          * When we do "shorthand" properties, must handle the "font"
148          * property as well to see if font-size is set.
149          */

150         String JavaDoc fontsizeval = attributes.getValue(FONTSIZEATTR);
151         if (fontsizeval != null) {
152             Property.Maker propertyMaker = findMaker(table, FONTSIZEATTR);
153             if (propertyMaker != null) {
154                 try {
155                     p.put(FONTSIZEATTR,
156                           propertyMaker.make(p, fontsizeval, parentFO));
157                 } catch (FOPException e) {}
158             }
159             // Put in the "done" list even if error or no Maker.
160
propsDone.append(FONTSIZEATTR + ' ');
161         }
162
163         for (int i = 0; i < attributes.getLength(); i++) {
164             String JavaDoc attributeName = attributes.getQName(i);
165             /* Handle "compound" properties, ex. space-before.minimum */
166             int sepchar = attributeName.indexOf('.');
167             String JavaDoc propName = attributeName;
168             String JavaDoc subpropName = null;
169             Property propVal = null;
170             if (sepchar > -1) {
171                 propName = attributeName.substring(0, sepchar);
172                 subpropName = attributeName.substring(sepchar + 1);
173             } else if (propsDone.toString().indexOf(' ' + propName + ' ')
174                        != -1) {
175                 // Already processed this property (base property
176
// for a property with sub-components or font-size)
177
continue;
178             }
179
180             Property.Maker propertyMaker = findMaker(table, propName);
181
182             if (propertyMaker != null) {
183                 try {
184                     if (subpropName != null) {
185                         Property baseProp = p.getExplicitBaseProp(propName);
186                         if (baseProp == null) {
187                             // See if it is specified later in this list
188
String JavaDoc baseValue = attributes.getValue(propName);
189                             if (baseValue != null) {
190                                 baseProp = propertyMaker.make(p, baseValue,
191                                                               parentFO);
192                                 propsDone.append(propName + ' ');
193                             }
194                             // else baseProp = propertyMaker.makeCompound(p, parentFO);
195
}
196                         propVal = propertyMaker.make(baseProp, subpropName,
197                                                      p,
198                                                      attributes.getValue(i),
199                                                      parentFO);
200                     } else {
201                         propVal = propertyMaker.make(p,
202                                                      attributes.getValue(i),
203                                                      parentFO);
204                     }
205                     if (propVal != null) {
206                         p.put(propName, propVal);
207                     }
208                 } catch (FOPException e) { /* Do other props. */
209                     MessageHandler.errorln(e.getMessage());
210                 }
211             } else {
212                 if (!attributeName.startsWith("xmlns"))
213                     MessageHandler.errorln("property '"
214                                            + attributeName + "' ignored");
215             }
216         }
217
218         return p;
219     }
220
221     public Property getSubpropValue(String JavaDoc space, String JavaDoc element,
222                                     String JavaDoc propertyName, Property p,
223                                     String JavaDoc subpropName) {
224         Property.Maker maker = findMaker(space, element, propertyName);
225         if (maker != null) {
226             return maker.getSubpropValue(p, subpropName);
227         } else
228             return null;
229     }
230
231
232     public boolean isCorrespondingForced(PropertyList propertyList,
233                                          String JavaDoc space, String JavaDoc element,
234                                          String JavaDoc propertyName) {
235         Property.Maker propertyMaker = findMaker(space, element,
236                                                  propertyName);
237         if (propertyMaker != null) {
238             return propertyMaker.isCorrespondingForced(propertyList);
239         } else {
240             MessageHandler.errorln("no Maker for " + propertyName);
241         }
242         return false;
243     }
244
245     public Property getShorthand(PropertyList propertyList, String JavaDoc space,
246                                  String JavaDoc element, String JavaDoc propertyName) {
247         Property.Maker propertyMaker = findMaker(space, element,
248                                                  propertyName);
249         if (propertyMaker != null) {
250             return propertyMaker.getShorthand(propertyList);
251         } else {
252             MessageHandler.errorln("no Maker for " + propertyName);
253             return null;
254         }
255     }
256
257
258     public Property makeProperty(PropertyList propertyList, String JavaDoc space,
259                                  String JavaDoc element,
260                                  String JavaDoc propertyName) throws FOPException {
261
262         Property p = null;
263
264         Property.Maker propertyMaker = findMaker(space, element,
265                                                  propertyName);
266         if (propertyMaker != null) {
267             p = propertyMaker.make(propertyList);
268         } else {
269             MessageHandler.errorln("property " + propertyName
270                                    + " ignored");
271         }
272         return p;
273     }
274
275     protected Property.Maker findMaker(String JavaDoc space, String JavaDoc elementName,
276                                        String JavaDoc propertyName) {
277         return findMaker((HashMap JavaDoc)elementTable.get(elementName),
278                          propertyName);
279     }
280
281     /**
282      * Convenience function to return the Maker for a given property
283      * given the HashMap containing properties specific to this element.
284      * If table is non-null and
285      * @param elemTable Element-specific properties or null if none.
286      * @param propertyName Name of property.
287      * @return A Maker for this property.
288      */

289     private Property.Maker findMaker(HashMap JavaDoc elemTable,
290                                      String JavaDoc propertyName) {
291         Property.Maker propertyMaker = null;
292         if (elemTable != null) {
293             propertyMaker = (Property.Maker)elemTable.get(propertyName);
294         }
295         if (propertyMaker == null) {
296             propertyMaker =
297                 (Property.Maker)propertyListTable.get(propertyName);
298         }
299         return propertyMaker;
300     }
301
302 }
303
Popular Tags