1
15
16 package gate.config;
17
18 import java.io.*;
19 import java.net.URL;
20
21 import javax.xml.parsers.*;
22
23 import org.xml.sax.SAXException;
24 import org.xml.sax.helpers.DefaultHandler;
25
26 import gate.util.*;
27
28
29
31 public class ConfigDataProcessor
32 {
33
34 protected static final boolean DEBUG = false;
35
36
37 protected SAXParser parser = null;
38
39
40 public ConfigDataProcessor() throws GateException {
41
42 try {
44 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
46
47 saxParserFactory.setValidating(false);
50 saxParserFactory.setNamespaceAware(true);
52
53 parser = saxParserFactory.newSAXParser();
55
56 } catch (SAXException e) {
57 if(DEBUG) Out.println(e);
58 throw(new GateException(e));
59 } catch (ParserConfigurationException e) {
60 if(DEBUG) Out.println(e);
61 throw(new GateException(e));
62 }
63
64 }
66
68 public void parseConfigFile(InputStream configStream, URL configUrl)
69 throws GateException
70 {
71 String nl = Strings.getNl();
72
73 String configString = null; try {
76 if(DEBUG) {
77 File configFile = new File(configUrl.getFile());
78 if(configFile.exists())
79 configString = Files.getString(configUrl.getFile());
80 else
81 configString = configUrl.toString();
82 }
83 DefaultHandler handler = new ConfigXmlHandler(configUrl);
84 parser.parse(configStream, handler);
85 if(DEBUG) {
86 Out.prln(
87 "done parsing " +
88 ((configUrl == null) ? "null" : configUrl.toString())
89 );
90 }
91 } catch (IOException e) {
92 Out.prln("conf file:"+nl+configString+nl);
93 throw(new GateException("Config data error 1 on "+configUrl+": "+nl+e));
94 } catch (SAXException e) {
95 Out.prln("conf file:"+nl+configString+nl);
96 throw(new GateException("Config data error 2 on "+configUrl+": "+nl+e));
97 }
98
99 }
101 }