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 |
| package com.jeantessier.metrics; |
34 |
| |
35 |
| import java.io.*; |
36 |
| import javax.xml.parsers.*; |
37 |
| |
38 |
| import org.apache.log4j.*; |
39 |
| import org.xml.sax.*; |
40 |
| |
41 |
| public class MetricsConfigurationLoader { |
42 |
| private static final boolean DEFAULT_VALIDATE = false; |
43 |
| |
44 |
| private MetricsConfigurationHandler handler; |
45 |
| private boolean validate; |
46 |
| |
47 |
8
| public MetricsConfigurationLoader() {
|
48 |
8
| this(new MetricsConfiguration(), DEFAULT_VALIDATE);
|
49 |
| } |
50 |
| |
51 |
0
| public MetricsConfigurationLoader(MetricsConfiguration configuration) {
|
52 |
0
| this(configuration, DEFAULT_VALIDATE);
|
53 |
| } |
54 |
| |
55 |
50
| public MetricsConfigurationLoader(boolean validate) {
|
56 |
50
| this(new MetricsConfiguration(), validate);
|
57 |
| } |
58 |
| |
59 |
67
| public MetricsConfigurationLoader(MetricsConfiguration configuration, boolean validate) {
|
60 |
67
| this.handler = new MetricsConfigurationHandler(configuration);
|
61 |
67
| this.validate = validate;
|
62 |
| } |
63 |
| |
64 |
58
| public MetricsConfiguration load(String filename) throws IOException, SAXException, ParserConfigurationException {
|
65 |
58
| MetricsConfiguration result = null;
|
66 |
| |
67 |
58
| FileReader in = null;
|
68 |
| |
69 |
58
| try {
|
70 |
58
| in = new FileReader(filename);
|
71 |
58
| result = load(in);
|
72 |
| } finally { |
73 |
58
| if (in != null) {
|
74 |
58
| in.close();
|
75 |
| } |
76 |
| } |
77 |
| |
78 |
58
| return result;
|
79 |
| } |
80 |
| |
81 |
0
| public MetricsConfiguration load(InputStream in) throws IOException, SAXException, ParserConfigurationException {
|
82 |
0
| return load(new InputSource(in));
|
83 |
| } |
84 |
| |
85 |
67
| public MetricsConfiguration load(Reader in) throws IOException, SAXException, ParserConfigurationException {
|
86 |
67
| return load(new InputSource(in));
|
87 |
| } |
88 |
| |
89 |
67
| public MetricsConfiguration load(InputSource in) throws IOException, SAXException, ParserConfigurationException {
|
90 |
67
| XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
|
91 |
67
| reader.setDTDHandler(handler);
|
92 |
67
| reader.setContentHandler(handler);
|
93 |
67
| reader.setErrorHandler(handler);
|
94 |
| |
95 |
67
| try {
|
96 |
67
| if (validate) {
|
97 |
0
| Logger.getLogger(getClass()).warn("XML validation turned on");
|
98 |
0
| reader.setFeature("http://xml.org/sax/features/validation", true);
|
99 |
0
| reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true);
|
100 |
| } else { |
101 |
67
| Logger.getLogger(getClass()).info("XML validation turned off");
|
102 |
67
| reader.setFeature("http://xml.org/sax/features/validation", false);
|
103 |
67
| reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
104 |
| } |
105 |
| } catch (Exception ex) { |
106 |
0
| Logger.getLogger(getClass()).warn("Problem setting validation feature on XML reader",ex);
|
107 |
| } |
108 |
| |
109 |
67
| reader.parse(in);
|
110 |
| |
111 |
65
| return handler.getMetricsConfiguration();
|
112 |
| } |
113 |
| } |