1
15
16 package gate.config;
17
18 import java.net.MalformedURLException;
19 import java.net.URL;
20 import java.util.List;
21 import java.util.Stack;
22
23 import org.xml.sax.*;
24 import org.xml.sax.helpers.DefaultHandler;
25
26 import gate.*;
27 import gate.creole.ResourceInstantiationException;
28 import gate.util.*;
29 import gate.xml.SimpleErrorHandler;
30
31
32
34 public class ConfigXmlHandler extends DefaultHandler {
35
36
41 private Stack contentStack = new Stack();
42
43
44 private SystemData systemData;
45
46
47 private Attributes currentAttributes;
48
49
50 private FeatureMap currentAttributeMap;
51
52
53 private static final boolean DEBUG = false;
54
55
56 private URL sourceUrl;
57
58
59 private SimpleErrorHandler _seh = new SimpleErrorHandler();
60
61
62
63 private StringBuffer contentBuffer = new StringBuffer("");
64
65
66 private boolean readCharacterStatus = false;
67
68
69
70 public ConfigXmlHandler(URL configUrl) {
71 this.register = Gate.getCreoleRegister();
72 this.sourceUrl = configUrl;
73 }
75
77 private CreoleRegister register;
78
79
80 public void startDocument() throws GateSaxException {
81 if(DEBUG) Out.prln("start document");
82 }
84
85 public void endDocument() throws GateSaxException {
86 if(DEBUG) Out.prln("end document");
87 if(! contentStack.isEmpty()) {
88 StringBuffer errorMessage =
89 new StringBuffer("document ended but element stack not empty:");
90 while(! contentStack.isEmpty())
91 errorMessage.append(Strings.getNl()+" "+(String) contentStack.pop());
92 throw new GateSaxException(errorMessage.toString());
93 }
94 }
96
97 private String attributes2String(Attributes atts){
98 StringBuffer strBuf = new StringBuffer("");
99 if (atts == null) return strBuf.toString();
100 for (int i = 0; i < atts.getLength(); i++) {
101 String attName = atts.getQName(i);
102 String attValue = atts.getValue(i);
103 strBuf.append(" ");
104 strBuf.append(attName);
105 strBuf.append("=");
106 strBuf.append(attValue);
107 } return strBuf.toString();
109 }
111
112 public void startElement (
113 String uri, String qName, String elementName, Attributes atts
114 ) throws SAXException {
115
116 if(readCharacterStatus) {
118 readCharacterStatus = false;
119 charactersAction(new String(contentBuffer).toCharArray(),0,contentBuffer.length());
120 }
121
122 if(DEBUG) {
123 Out.pr("startElement: ");
124 Out.println(
125 elementName + " " +
126 attributes2String(atts)
127 );
128 }
129
130 currentAttributes = atts;
132 currentAttributeMap = attributeListToParameterList();
133
134 if(elementName.toUpperCase().equals("SYSTEM")) {
136 systemData = new SystemData();
137 for(int i=0, len=currentAttributes.getLength(); i<len; i++) {
138 if(currentAttributes.getQName(i).toUpperCase().equals("NAME"))
139 systemData.systemName = currentAttributes.getValue(i);
140 }
141 } else if(elementName.toUpperCase().equals("DBCONFIG")) {
142 DataStoreRegister.addConfig(currentAttributeMap);
143 } else if(elementName.toUpperCase().equals(Gate.getUserConfigElement())) {
144 Gate.getUserConfig().putAll(currentAttributeMap);
145 }
146
147 }
149
150 private void checkStack(String methodName, String elementName)
151 throws GateSaxException {
152 if(contentStack.isEmpty())
153 throw new GateSaxException(
154 methodName + " called for element " + elementName + " with empty stack"
155 );
156 }
158
161 public void endElement (String uri, String qName, String elementName)
162 throws GateSaxException, SAXException {
163
164 if(readCharacterStatus) {
166 readCharacterStatus = false;
167 charactersAction(new String(contentBuffer).toCharArray(),0,contentBuffer.length());
168 }
169
170 if(DEBUG) Out.prln("endElement: " + elementName);
171
172 if(elementName.toUpperCase().equals("GATE")) {
174
175 } else if(elementName.toUpperCase().equals("CREOLE-DIRECTORY")) {
177 String dirUrlName = (String) contentStack.pop();
178 try {
179 register.addDirectory(new URL(dirUrlName));
180 } catch(MalformedURLException e) {
181 throw new GateSaxException("bad URL " + dirUrlName + e);
182 }
183
184 } else if(elementName.toUpperCase().equals("SYSTEM")) {
186 systemData.createSystem();
188
189 } else if(elementName.toUpperCase().equals("CONTROLLER")) {
191 systemData.controllerTypeName = (String) contentStack.pop();
192
193 } else if(elementName.toUpperCase().equals("LR")) {
195 createResource((String) contentStack.pop(), systemData.lrList);
197
198 } else if(elementName.toUpperCase().equals("PR")) {
200 createResource((String) contentStack.pop(), systemData.prList);
202
203 } else if(elementName.toUpperCase().equals("DBCONFIG")) {
205
207 }else if(elementName.toUpperCase().equals("GATECONFIG")) {
209
211 } else {
213 throw new GateSaxException(
214 "Unknown config data element: " + elementName +
215 "; encountered while parsing " + sourceUrl
216 );
217 }
218
220 }
222
223 public void characters(char [] text,int start,int length) throws SAXException {
224 if(!readCharacterStatus) {
225 contentBuffer = new StringBuffer(new String(text,start,length));
226 } else {
227 contentBuffer.append(new String(text,start,length));
228 }
229 readCharacterStatus = true;
230 }
231
232
235
236 public void charactersAction(char[] text, int start, int length)
237 throws SAXException {
238 String content = new String(text, start, length).trim();
240 if (content.length() == 0) return;
243 contentStack.push(content);
244 if(DEBUG) Out.println(content);
245 }
247
251 protected void createResource(String resourceTypeName, List resourceList)
252 throws GateSaxException
253 {
254 if(DEBUG) Out.prln(resourceTypeName + ": " + currentAttributeMap);
255 try {
256 resourceList.add(
257 Factory.createResource(
258 resourceTypeName, currentAttributeMap
259 )
260 );
261 } catch(ResourceInstantiationException e) {
262 throw new GateSaxException(
263 "Couldn't create resource for SYSTEM: " +
264 systemData.systemName + "; problem was: " + Strings.getNl() + e
265 );
266 }
267 }
269
272 protected FeatureMap attributeListToParameterList() {
273 FeatureMap params = Factory.newFeatureMap();
274
275 for(int i=0, len=currentAttributes.getLength(); i<len; i++) {
277 params.put(
278 currentAttributes.getQName(i), currentAttributes.getValue(i)
279 );
280 }
281
282 return params;
283 }
285
286 public void ignorableWhitespace(char ch[], int start, int length)
287 throws SAXException {
288 }
290
291 public void error(SAXParseException ex) throws SAXException {
292 _seh.error(ex);
293 }
295
296 public void fatalError(SAXParseException ex) throws SAXException {
297 _seh.fatalError(ex);
298 }
300
301 public void warning(SAXParseException ex) throws SAXException {
302 _seh.warning(ex);
303 }
305 }