|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.jconfig.handler; |
|
8 |
| |
|
9 |
| import java.io.BufferedInputStream; |
|
10 |
| import java.io.File; |
|
11 |
| import java.io.FileInputStream; |
|
12 |
| import java.io.IOException; |
|
13 |
| import java.io.OutputStreamWriter; |
|
14 |
| import java.io.PrintWriter; |
|
15 |
| |
|
16 |
| import javax.xml.parsers.DocumentBuilder; |
|
17 |
| import javax.xml.parsers.DocumentBuilderFactory; |
|
18 |
| import javax.xml.parsers.ParserConfigurationException; |
|
19 |
| |
|
20 |
| import org.jconfig.Configuration; |
|
21 |
| import org.jconfig.ConfigurationManagerException; |
|
22 |
| import org.jconfig.error.ErrorReporter; |
|
23 |
| import org.jconfig.parser.ConfigurationParser; |
|
24 |
| import org.jconfig.parser.ConfigurationParserFactory; |
|
25 |
| import org.jconfig.utils.ConfigErrorHandler; |
|
26 |
| import org.w3c.dom.Document; |
|
27 |
| import org.xml.sax.InputSource; |
|
28 |
| import org.xml.sax.SAXException; |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| public class XMLFileHandler extends BaseXMLHandler implements ConfigurationHandler { |
|
37 |
| |
|
38 |
| private boolean validate = false; |
|
39 |
| private File file; |
|
40 |
| |
|
41 |
3
| public XMLFileHandler() {
|
|
42 |
| } |
|
43 |
| |
|
44 |
1
| public XMLFileHandler(String filename) {
|
|
45 |
1
| file = new File(filename);
|
|
46 |
1
| addFileListener(this);
|
|
47 |
| } |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
2
| public void setFile(File file) {
|
|
53 |
2
| this.file = file;
|
|
54 |
| } |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
0
| public void setValidation(boolean validate) {
|
|
62 |
0
| this.validate = validate;
|
|
63 |
| } |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
3
| public synchronized Configuration load(String configurationName)
|
|
68 |
| throws ConfigurationManagerException { |
|
69 |
3
| return load(file,configurationName);
|
|
70 |
| } |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
4
| public synchronized Configuration load(File file,String configName)
|
|
76 |
| throws ConfigurationManagerException { |
|
77 |
4
| if (file == null) {
|
|
78 |
0
| ErrorReporter.getErrorHandler().reportError("The file is NULL");
|
|
79 |
0
| throw new ConfigurationManagerException("The file is NULL");
|
|
80 |
| } |
|
81 |
4
| DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
|
82 |
| |
|
83 |
4
| dbf.setValidating(validate);
|
|
84 |
4
| DocumentBuilder db = null;
|
|
85 |
4
| try {
|
|
86 |
4
| db = dbf.newDocumentBuilder();
|
|
87 |
| } catch (ParserConfigurationException pce) { |
|
88 |
0
| ErrorReporter.getErrorHandler().reportError("The parser cannot create a new document builder",pce);
|
|
89 |
0
| throw new ConfigurationManagerException("The parser cannot create a new document builder: "
|
|
90 |
| + pce.getMessage()); |
|
91 |
| } |
|
92 |
| |
|
93 |
4
| if (validate) {
|
|
94 |
0
| try {
|
|
95 |
| |
|
96 |
0
| OutputStreamWriter errorWriter =
|
|
97 |
| new OutputStreamWriter(System.err, "UTF-8"); |
|
98 |
0
| db.setErrorHandler(
|
|
99 |
| new ConfigErrorHandler(new PrintWriter(errorWriter, true))); |
|
100 |
| } catch (Exception e) { |
|
101 |
0
| ErrorReporter.getErrorHandler().reportError("The parser cannot set up the error handler");
|
|
102 |
0
| throw new ConfigurationManagerException("The parser cannot set up the error handler");
|
|
103 |
| } |
|
104 |
| } |
|
105 |
4
| Document doc = null;
|
|
106 |
4
| try {
|
|
107 |
4
| FileInputStream fis = new FileInputStream(file);
|
|
108 |
| |
|
109 |
4
| InputSource ins = new InputSource(fis);
|
|
110 |
4
| BufferedInputStream buffy = new BufferedInputStream(fis);
|
|
111 |
4
| ins = new InputSource(buffy);
|
|
112 |
4
| doc = db.parse(file);
|
|
113 |
| } catch (SAXException se) { |
|
114 |
0
| ErrorReporter.getErrorHandler().reportError("The parser cannot parse the file",se);
|
|
115 |
0
| throw new ConfigurationManagerException(
|
|
116 |
| "The parser cannot parse the file: " + se.getMessage()); |
|
117 |
| } catch (IOException ioe) { |
|
118 |
0
| ErrorReporter.getErrorHandler().reportError("The parser cannot parse the file",ioe);
|
|
119 |
0
| throw new ConfigurationManagerException(
|
|
120 |
| "The parser cannot open the file: " + ioe.getMessage()); |
|
121 |
| } |
|
122 |
4
| ConfigurationParser parser = ConfigurationParserFactory.getParser(configName);
|
|
123 |
4
| Configuration config = parser.parse(doc, configName);
|
|
124 |
4
| try {
|
|
125 |
4
| java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader(file));
|
|
126 |
4
| String str;
|
|
127 |
?
| while ((str = in.readLine()) != null) {
|
|
128 |
0
| if(str.indexOf("encoding") > -1) config.setEncoding(getEncodingType(str));
|
|
129 |
| } |
|
130 |
4
| in.close();
|
|
131 |
| } |
|
132 |
| catch (Exception e) { |
|
133 |
| |
|
134 |
| } |
|
135 |
4
| config.resetCreated();
|
|
136 |
4
| return config;
|
|
137 |
| } |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
3
| public void store(Configuration configuration)
|
|
143 |
| throws ConfigurationManagerException { |
|
144 |
3
| store(file, configuration);
|
|
145 |
| } |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
1
| public File getFile() {
|
|
150 |
1
| return file;
|
|
151 |
| } |
|
152 |
| |
|
153 |
| } |