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