KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > configuration > ConfigurationParser


1 /*
2  * $Id: ConfigurationParser.java,v 1.9.2.3 2003/02/25 10:36:01 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.configuration;
52
53 // SAX
54
import org.xml.sax.helpers.DefaultHandler JavaDoc;
55 import org.xml.sax.Attributes JavaDoc;
56 import org.xml.sax.Locator JavaDoc;
57
58 // Java
59
import java.util.Map JavaDoc;
60 import java.util.List JavaDoc;
61
62 // FOP
63
import org.apache.fop.messaging.MessageHandler;
64
65
66 /**
67  * SAX2 Handler which retrieves the configuration information and stores them in Configuration.
68  * Normally this class doesn't need to be accessed directly.
69  */

70 public class ConfigurationParser extends DefaultHandler JavaDoc {
71     private static final int OUT = 0;
72     private static final int IN_ENTRY = 1;
73     private static final int IN_KEY = 2;
74     private static final int IN_VALUE = 4;
75     private static final int IN_LIST = 8;
76     private static final int IN_SUBENTRY = 16;
77     private static final int IN_SUBKEY = 32;
78     private static final int IN_FONTS = 64;
79     private static final int IN_FONT = 128;
80
81     private static final int STRING = 0;
82     private static final int LIST = 1;
83     private static final int MAP = 2;
84
85     // state of parser
86
private int status = OUT;
87     private int datatype = -1;
88
89     // store the result configuration
90
private static Map JavaDoc configuration;
91     private static Map JavaDoc activeConfiguration;
92
93     // stores key for new config entry
94
private String JavaDoc key = "";
95     private List JavaDoc keyStack = new java.util.ArrayList JavaDoc();
96
97     // stores string value
98
private String JavaDoc value = "";
99
100     // stores key for new config entry
101
private String JavaDoc subkey = "";
102
103     // stores list value
104
private List JavaDoc list = new java.util.ArrayList JavaDoc(15);
105
106     // stores Map value
107
private Map JavaDoc map = new java.util.HashMap JavaDoc(15);
108
109     /**
110      * locator for line number information
111      */

112     private Locator JavaDoc locator;
113
114     /**
115      * determines role / target of configuration information, default is standard
116      */

117     private String JavaDoc role = "standard";
118
119     // stores fonts
120
private List JavaDoc fontList = null;
121
122     // stores information on one font
123
private FontInfo fontInfo = null;
124
125     // stores information on a font triplet
126
private FontTriplet fontTriplet = null;
127
128     // information on a font
129
private String JavaDoc fontName, metricsFile, embedFile, kerningAsString;
130     private boolean kerning;
131     private List JavaDoc fontTriplets;
132
133     // information on a font triplet
134
private String JavaDoc fontTripletName, weight, style;
135
136     public void startDocument() {
137         configuration = Configuration.getConfiguration();
138     }
139
140     /**
141      * get locator for position information
142      */

143     public void setDocumentLocator(Locator JavaDoc locator) {
144         this.locator = locator;
145     }
146
147     /**
148      * extracts the element and attribute name and sets the fitting status and datatype values
149      */

150     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
151                              Attributes JavaDoc attributes) {
152         if (localName.equals("key")) {
153             status += IN_KEY;
154         } else if (localName.equals("value")) {
155             status += IN_VALUE;
156         } else if (localName.equals("list")) {
157             status += IN_LIST;
158         } else if (localName.equals("subentry")) {
159             status += IN_SUBENTRY;
160         } else if (localName.equals("entry")) {
161             // role=standard as default
162
if (attributes.getLength() == 0) {
163                 role = "standard";
164                 // retrieve attribute value for "role" which determines configuration target
165
} else {
166                 role = attributes.getValue("role");
167             }
168         } else if (localName.equals("configuration")) {}
169         else if (localName.equals("fonts")) { // list of fonts starts
170
fontList = new java.util.ArrayList JavaDoc(10);
171         } else if (localName.equals("font")) {
172             kerningAsString = attributes.getValue("kerning");
173             if (kerningAsString.equalsIgnoreCase("yes")) {
174                 kerning = true;
175             } else {
176                 kerning = false;
177             }
178             metricsFile = attributes.getValue("metrics-file");
179             embedFile = attributes.getValue("embed-file");
180             fontName = attributes.getValue("name");
181             fontTriplets = new java.util.ArrayList JavaDoc(5);
182         } else if (localName.equals("font-triplet")) {
183             fontTripletName = attributes.getValue("name");
184             weight = attributes.getValue("weight");
185             style = attributes.getValue("style");
186             fontTriplet = new FontTriplet(fontTripletName, weight, style);
187             fontTriplets.add(fontTriplet);
188         } else {
189             // to make sure that user knows about false tag
190
MessageHandler.errorln("Unknown tag in configuration file: "
191                                    + localName);
192         }
193     } // end startElement
194

195     /**
196      * stores subentries or entries into their hashes (map for subentries, configuration for entry)
197      */

198     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) {
199         if (localName.equals("entry")) {
200             switch (datatype) {
201             case STRING:
202                 this.store(role, key, value);
203                 break;
204             case LIST:
205                 this.store(role, key, list);
206                 break;
207             case MAP:
208                 this.store(role, key, map);
209             }
210             status = OUT;
211             role = "standard";
212             if (keyStack.size() > 0) {
213                 keyStack.remove(keyStack.size() - 1);
214             }
215             if (keyStack.size() > 0) {
216                 key = (String JavaDoc)keyStack.get(keyStack.size() - 1);
217             } else {
218                 key = "";
219             }
220             value = "";
221         } else if (localName.equals("subentry")) {
222             map.put(subkey, value);
223             status -= IN_SUBENTRY;
224             if (keyStack.size() > 0) {
225                 keyStack.remove(keyStack.size() - 1);
226             }
227             if (keyStack.size() > 0) {
228                 key = (String JavaDoc)keyStack.get(keyStack.size() - 1);
229             } else {
230                 key = "";
231             }
232             value = "";
233         } else if (localName.equals("key")) {
234             status -= IN_KEY;
235             keyStack.add(key);
236         } else if (localName.equals("list")) {
237             status -= IN_LIST;
238             value = "";
239         } else if (localName.equals("value")) {
240             status -= IN_VALUE;
241         } else if (localName.equals("fonts")) {
242             this.store("standard", "fonts", fontList);
243         } else if (localName.equals("font")) {
244             fontInfo = new FontInfo(fontName, metricsFile, kerning,
245                                     fontTriplets, embedFile);
246             fontList.add(fontInfo);
247             fontTriplets = null;
248             metricsFile = null;
249             embedFile = null;
250             fontName = null;
251             kerningAsString = "";
252         } else if (localName.equals("font-triplet")) {}
253     }
254
255     /**
256      * extracts characters from text nodes and puts them into their respective
257      * variables
258      */

259     public void characters(char[] ch, int start, int length) {
260         char characters[] = new char[length];
261         System.arraycopy(ch, start, characters, 0, length);
262         String JavaDoc text = new String JavaDoc(characters);
263         switch (status) {
264         case IN_KEY:
265             key = text;
266             break;
267         case IN_LIST + IN_SUBENTRY + IN_KEY:
268             subkey = text;
269             break;
270         case IN_VALUE:
271             value = text;
272             datatype = STRING;
273             break;
274         case IN_LIST + IN_VALUE:
275             list.add(text);
276             datatype = LIST;
277             break;
278         case IN_LIST + IN_SUBENTRY + IN_VALUE:
279             value = text;
280             datatype = MAP;
281             break;
282         }
283     } // end characters
284

285     /**
286      * stores configuration entry into configuration Map according to the role
287      *
288      * @param role a string containing the role / target for this configuration information
289      * @param key a string containing the key value for the configuration
290      * @param value a string containing the value for the configuration
291      */

292     private void store(String JavaDoc role, String JavaDoc key, Object JavaDoc value) {
293         activeConfiguration = (Map JavaDoc)configuration.get(role);
294         if (activeConfiguration != null) {
295             activeConfiguration.put(key, value);
296         } else {
297             MessageHandler.errorln("Unknown role >" + role
298                                    + "< for new configuration entry. \n"
299                                    + "Putting configuration with key:" + key
300                                    + " into standard configuration.");
301         }
302     }
303
304 }
305
Popular Tags