1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/*
* Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/
package org.apache.fop.configuration;
//sax
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
//java
import java.util.Hashtable;
import java.util.Vector;
//fop
import org.apache.fop.messaging.MessageHandler;
/**
* SAX2 Handler which retrieves the configuration information and stores them in Configuration.
* Normally this class doesn't need to be accessed directly.
*/
public class ConfigurationParser extends DefaultHandler {
private final int OUT = 0;
private final int IN_ENTRY = 1;
private final int IN_KEY = 2;
private final int IN_VALUE = 4;
private final int IN_LIST = 8;
private final int IN_SUBENTRY = 16;
private final int IN_SUBKEY = 32;
private final int IN_FONTS = 64;
private final int IN_FONT = 128;
private final int STRING = 0;
private final int LIST = 1;
private final int MAP = 2;
//state of parser
private int status = OUT;
private int datatype = -1;
//store the result configuration
private static Hashtable configuration;
private static Hashtable activeConfiguration;
//stores key for new config entry
private String key = "";
private Vector keyStack = new Vector();
//stores string value
private String value = "";
//stores key for new config entry
private String subkey = "";
//stores list value
private Vector list = new Vector (15);
//stores hashtable value
private Hashtable map = new Hashtable(15);
/** locator for line number information */
private Locator locator;
/** determines role / target of configuration information, default is standard */
private String role = "standard";
//stores fonts
private Vector fontList = null;
//stores information on one font
private FontInfo fontInfo = null;
//stores information on a font triplet
private FontTriplet fontTriplet = null;
//information on a font
private String fontName, metricsFile, embedFile, kerningAsString;
private boolean kerning;
private Vector fontTriplets;
//information on a font triplet
private String fontTripletName, weight, style;
public void startDocument() {
configuration = Configuration.getConfiguration();
}
/** get locator for position information */
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}
/**
* extracts the element and attribute name and sets the fitting status and datatype values
*/
public void startElement(String uri, String localName,
String qName, Attributes attributes) {
if (localName.equals("key")) {
status += IN_KEY;
} else if (localName.equals("value")) {
status += IN_VALUE;
} else if (localName.equals("list")) {
status += IN_LIST;
} else if (localName.equals("subentry")) {
status += IN_SUBENTRY;
} else if (localName.equals("entry")) {
//role=standard as default
if (attributes.getLength() == 0) {
role = "standard";
//retrieve attribute value for "role" which determines configuration target
} else {
role = attributes.getValue("role");
}
} else if (localName.equals("configuration")) {
} else if (localName.equals("fonts")) { //list of fonts starts
fontList = new Vector (10);
} else if (localName.equals("font")) {
kerningAsString = attributes.getValue("kerning");
if (kerningAsString.equalsIgnoreCase("yes")) {
kerning = true;
} else {
kerning = false;
}
metricsFile = attributes.getValue("metrics-file");
embedFile = attributes.getValue("embed-file");
fontName = attributes.getValue("name");
fontTriplets = new Vector(5);
} else if (localName.equals("font-triplet")) {
fontTripletName = attributes.getValue("name");
weight = attributes.getValue("weight");
style = attributes.getValue("style");
fontTriplet = new FontTriplet(fontTripletName, weight, style);
fontTriplets.addElement(fontTriplet);
} else {
//to make sure that user knows about false tag
MessageHandler.errorln(
"Unknown tag in configuration file: " + localName);
}
} //end startElement
/**
* stores subentries or entries into their hashes (map for subentries, configuration for entry)
*/
public void endElement(String uri, String localName, String qName) {
if (localName.equals("entry")) {
switch (datatype) {
case STRING:
this.store(role, key, value);
break;
case LIST:
this.store(role, key, list);
break;
case MAP:
this.store(role, key, map);
}
status = OUT;
role = "standard";
if (keyStack.size() > 0) {
keyStack.removeElementAt(keyStack.size() - 1);
}
if (keyStack.size() > 0) {
key = (String) keyStack.elementAt(keyStack.size() - 1);
} else {
key = "";
}
value = "";
} else if (localName.equals("subentry")) {
map.put(subkey, value);
status -= IN_SUBENTRY;
if (keyStack.size() > 0) {
keyStack.removeElementAt(keyStack.size() - 1);
}
if (keyStack.size() > 0) {
key = (String) keyStack.elementAt(keyStack.size() - 1);
} else {
key = "";
}
value = "";
} else if (localName.equals("key")) {
status -= IN_KEY;
keyStack.addElement(key);
} else if (localName.equals("list")) {
status -= IN_LIST;
value = "";
} else if (localName.equals("value")) {
status -= IN_VALUE;
} else if (localName.equals("fonts")) {
this.store("standard", "fonts", fontList);
} else if (localName.equals("font")) {
fontInfo = new FontInfo(fontName, metricsFile, kerning,
fontTriplets, embedFile);
fontList.addElement(fontInfo);
fontTriplets = null;
metricsFile = null;
embedFile = null;
fontName = null;
kerningAsString = "";
} else if (localName.equals("font-triplet")) {
}
}
/**
* extracts characters from text nodes and puts them into their respective
* variables
*/
public void characters(char[] ch, int start, int length) {
char characters [] = new char [length];
System.arraycopy(ch, start, characters, 0, length);
String text = new String(characters);
switch (status) {
case IN_KEY:
key = text;
break;
case IN_LIST + IN_SUBENTRY + IN_KEY:
subkey = text;
break;
case IN_VALUE:
value = text;
datatype = STRING;
break;
case IN_LIST + IN_VALUE:
list.addElement(text);
datatype = LIST;
break;
case IN_LIST + IN_SUBENTRY + IN_VALUE:
value = text;
datatype = MAP;
break;
}
} //end characters
/**
* stores configuration entry into configuration hashtable according to the role
*
* @param role a string containing the role / target for this configuration information
* @param key a string containing the key value for the configuration
* @param value a string containing the value for the configuration
*/
private void store (String role, String key, Object value) {
activeConfiguration = (Hashtable) configuration.get(role);
if (activeConfiguration != null) {
activeConfiguration.put(key, value);
} else {
MessageHandler.errorln("Unknown role >" + role +
"< for new configuration entry. \n" +
"Putting configuration with key:" + key + " into standard configuration.");
}
}
}
|