|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
XmlResourceProcessor.java | 100% | 92.3% | 100% | 94.9% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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 |
* The XmlResourceProcessor processes XML {@link Resource resources} using the
|
|
36 |
* {@link DescriptorParser} which is used as a SAX ContentHandler. The result of
|
|
37 |
* {@link #processResource(Resource) processing a resource} is a {@link ModuleDescriptor}.
|
|
38 |
*
|
|
39 |
* @see org.apache.hivemind.parse.DescriptorParser
|
|
40 |
* @see org.apache.hivemind.ModuleDescriptorProvider
|
|
41 |
* @since 1.1
|
|
42 |
* @author Knut Wannheden
|
|
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 | 256 |
public XmlResourceProcessor(ClassResolver resolver, ErrorHandler errorHandler)
|
57 |
{ |
|
58 | 256 |
_resolver = resolver; |
59 | 256 |
_errorHandler = errorHandler; |
60 |
} |
|
61 |
|
|
62 |
/**
|
|
63 |
* Initializes the {@link DescriptorParser parser},
|
|
64 |
* {@link #processResource(Resource) processes} the Resource, resets the parser, and finally
|
|
65 |
* returns the parsed {@link ModuleDescriptor}.
|
|
66 |
*
|
|
67 |
* @throws ApplicationRuntimeException
|
|
68 |
* Thrown if errors are encountered while parsing the resource.
|
|
69 |
*/
|
|
70 | 270 |
public ModuleDescriptor processResource(Resource resource)
|
71 |
{ |
|
72 | 270 |
if (_contentHandler == null) |
73 | 256 |
_contentHandler = new DescriptorParser(_errorHandler);
|
74 |
|
|
75 | 270 |
_contentHandler.initialize(resource, _resolver); |
76 |
|
|
77 | 270 |
try
|
78 |
{ |
|
79 | 270 |
if (LOG.isDebugEnabled())
|
80 | 27 |
LOG.debug("Parsing " + resource);
|
81 |
|
|
82 | 270 |
ModuleDescriptor descriptor = parseResource(resource, getSAXParser(), _contentHandler); |
83 |
|
|
84 | 265 |
if (LOG.isDebugEnabled())
|
85 | 27 |
LOG.debug("Result: " + descriptor);
|
86 |
|
|
87 | 265 |
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 | 270 |
_contentHandler.resetParser(); |
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
/**
|
|
108 |
* Returns the ModuleDescriptor obtained by parsing the specified Resource using the given
|
|
109 |
* {@link SAXParser} and {@link DescriptorParser}. Called by {@link #processResource(Resource)}
|
|
110 |
* after the DescriptorParser has been
|
|
111 |
* {@link DescriptorParser#initialize(Resource, ClassResolver) initialized}. Suitable for
|
|
112 |
* overriding by subclasses.
|
|
113 |
*/
|
|
114 | 270 |
protected ModuleDescriptor parseResource(Resource resource, SAXParser parser,
|
115 |
DescriptorParser contentHandler) throws SAXException, IOException
|
|
116 |
{ |
|
117 | 270 |
InputSource source = getInputSource(resource); |
118 |
|
|
119 | 268 |
parser.parse(source, contentHandler); |
120 |
|
|
121 | 265 |
return contentHandler.getModuleDescriptor();
|
122 |
} |
|
123 |
|
|
124 | 270 |
private InputSource getInputSource(Resource resource)
|
125 |
{ |
|
126 | 270 |
try
|
127 |
{ |
|
128 | 270 |
URL url = resource.getResourceURL(); |
129 |
|
|
130 | 270 |
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 | 270 |
private SAXParser getSAXParser() throws ParserConfigurationException, SAXException, |
140 |
FactoryConfigurationError |
|
141 |
{ |
|
142 | 270 |
if (_saxParser == null) |
143 | 256 |
_saxParser = SAXParserFactory.newInstance().newSAXParser(); |
144 |
|
|
145 | 270 |
return _saxParser;
|
146 |
} |
|
147 |
|
|
148 |
} |
|