1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.parse; |
16 |
| |
17 |
| import java.io.IOException; |
18 |
| import java.net.URL; |
19 |
| |
20 |
| import javax.xml.parsers.FactoryConfigurationError; |
21 |
| import javax.xml.parsers.ParserConfigurationException; |
22 |
| import javax.xml.parsers.SAXParser; |
23 |
| import javax.xml.parsers.SAXParserFactory; |
24 |
| |
25 |
| import org.apache.commons.logging.Log; |
26 |
| import org.apache.commons.logging.LogFactory; |
27 |
| import org.apache.hivemind.ApplicationRuntimeException; |
28 |
| import org.apache.hivemind.ClassResolver; |
29 |
| import org.apache.hivemind.ErrorHandler; |
30 |
| import org.apache.hivemind.Resource; |
31 |
| import org.xml.sax.InputSource; |
32 |
| import org.xml.sax.SAXException; |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| public class XmlResourceProcessor |
45 |
| { |
46 |
| private static final Log LOG = LogFactory.getLog(XmlResourceProcessor.class); |
47 |
| |
48 |
| protected ClassResolver _resolver; |
49 |
| |
50 |
| protected ErrorHandler _errorHandler; |
51 |
| |
52 |
| private DescriptorParser _contentHandler; |
53 |
| |
54 |
| private SAXParser _saxParser; |
55 |
| |
56 |
271
| public XmlResourceProcessor(ClassResolver resolver, ErrorHandler errorHandler)
|
57 |
| { |
58 |
271
| _resolver = resolver;
|
59 |
271
| _errorHandler = errorHandler;
|
60 |
| } |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
285
| public ModuleDescriptor processResource(Resource resource)
|
71 |
| { |
72 |
285
| if (_contentHandler == null)
|
73 |
271
| _contentHandler = new DescriptorParser(_errorHandler);
|
74 |
| |
75 |
285
| _contentHandler.initialize(resource, _resolver);
|
76 |
| |
77 |
285
| try
|
78 |
| { |
79 |
285
| if (LOG.isDebugEnabled())
|
80 |
27
| LOG.debug("Parsing " + resource);
|
81 |
| |
82 |
285
| ModuleDescriptor descriptor = parseResource(resource, getSAXParser(), _contentHandler);
|
83 |
| |
84 |
280
| if (LOG.isDebugEnabled())
|
85 |
27
| LOG.debug("Result: " + descriptor);
|
86 |
| |
87 |
280
| return descriptor;
|
88 |
| } |
89 |
| catch (ApplicationRuntimeException e) |
90 |
| { |
91 |
5
| throw e;
|
92 |
| } |
93 |
| catch (Exception e) |
94 |
| { |
95 |
0
| _saxParser = null;
|
96 |
| |
97 |
0
| throw new ApplicationRuntimeException(
|
98 |
| ParseMessages.errorReadingDescriptor(resource, e), resource, _contentHandler |
99 |
| .getLocation(), e); |
100 |
| } |
101 |
| finally |
102 |
| { |
103 |
285
| _contentHandler.resetParser();
|
104 |
| } |
105 |
| } |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
285
| protected ModuleDescriptor parseResource(Resource resource, SAXParser parser,
|
115 |
| DescriptorParser contentHandler) throws SAXException, IOException |
116 |
| { |
117 |
285
| InputSource source = getInputSource(resource);
|
118 |
| |
119 |
283
| parser.parse(source, contentHandler);
|
120 |
| |
121 |
280
| return contentHandler.getModuleDescriptor();
|
122 |
| } |
123 |
| |
124 |
285
| private InputSource getInputSource(Resource resource)
|
125 |
| { |
126 |
285
| try
|
127 |
| { |
128 |
285
| URL url = resource.getResourceURL();
|
129 |
| |
130 |
285
| return new InputSource(url.openStream());
|
131 |
| } |
132 |
| catch (Exception e) |
133 |
| { |
134 |
2
| throw new ApplicationRuntimeException(ParseMessages.missingResource(resource),
|
135 |
| resource, null, e); |
136 |
| } |
137 |
| } |
138 |
| |
139 |
285
| private SAXParser getSAXParser() throws ParserConfigurationException, SAXException,
|
140 |
| FactoryConfigurationError |
141 |
| { |
142 |
285
| if (_saxParser == null)
|
143 |
271
| _saxParser = SAXParserFactory.newInstance().newSAXParser();
|
144 |
| |
145 |
285
| return _saxParser;
|
146 |
| } |
147 |
| |
148 |
| } |