|
|||||||||||||||||||
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 | 241 |
public XmlResourceProcessor(ClassResolver resolver, ErrorHandler errorHandler)
|
57 |
{ |
|
58 | 241 |
_resolver = resolver; |
59 | 241 |
_errorHandler = errorHandler; |
60 |
} |
|
61 |
|
|
62 | 255 |
public ModuleDescriptor processResource(Resource resource)
|
63 |
{ |
|
64 | 255 |
if (_contentHandler == null) |
65 | 241 |
_contentHandler = new DescriptorParser(_errorHandler);
|
66 |
|
|
67 | 255 |
_contentHandler.initialize(resource, _resolver); |
68 |
|
|
69 | 255 |
try
|
70 |
{ |
|
71 | 255 |
if (LOG.isDebugEnabled())
|
72 | 27 |
LOG.debug("Parsing " + resource);
|
73 |
|
|
74 | 255 |
ModuleDescriptor descriptor = parseResource(resource, getSAXParser(), _contentHandler); |
75 |
|
|
76 | 250 |
if (LOG.isDebugEnabled())
|
77 | 27 |
LOG.debug("Result: " + descriptor);
|
78 |
|
|
79 | 250 |
return descriptor;
|
80 |
} |
|
81 |
catch (ApplicationRuntimeException e)
|
|
82 |
{ |
|
83 | 5 |
throw e;
|
84 |
} |
|
85 |
catch (Exception e)
|
|
86 |
{ |
|
87 | 0 |
_saxParser = null;
|
88 |
|
|
89 | 0 |
throw new ApplicationRuntimeException( |
90 |
ParseMessages.errorReadingDescriptor(resource, e), resource, _contentHandler |
|
91 |
.getLocation(), e); |
|
92 |
} |
|
93 |
finally
|
|
94 |
{ |
|
95 | 255 |
_contentHandler.resetParser(); |
96 |
} |
|
97 |
} |
|
98 |
|
|
99 |
/**
|
|
100 |
* Returns the ModuleDescriptor obtained by parsing the specified Resource using the given
|
|
101 |
* SAXParser and DescriptorParser. Called by {@link #processResource(Resource)}after the
|
|
102 |
* DescriptorParser has been
|
|
103 |
* {@link DescriptorParser#initialize(Resource, ClassResolver) initialized}. Suitable for
|
|
104 |
* overriding by subclasses.
|
|
105 |
*/
|
|
106 | 255 |
protected ModuleDescriptor parseResource(Resource resource, SAXParser parser,
|
107 |
DescriptorParser contentHandler) throws SAXException, IOException
|
|
108 |
{ |
|
109 | 255 |
InputSource source = getInputSource(resource); |
110 |
|
|
111 | 253 |
parser.parse(source, contentHandler); |
112 |
|
|
113 | 250 |
return contentHandler.getModuleDescriptor();
|
114 |
} |
|
115 |
|
|
116 | 255 |
private InputSource getInputSource(Resource resource)
|
117 |
{ |
|
118 | 255 |
try
|
119 |
{ |
|
120 | 255 |
URL url = resource.getResourceURL(); |
121 |
|
|
122 | 255 |
return new InputSource(url.openStream()); |
123 |
} |
|
124 |
catch (Exception e)
|
|
125 |
{ |
|
126 | 2 |
throw new ApplicationRuntimeException(ParseMessages.missingResource(resource), |
127 |
resource, null, null); |
|
128 |
} |
|
129 |
} |
|
130 |
|
|
131 | 255 |
private SAXParser getSAXParser() throws ParserConfigurationException, SAXException, |
132 |
FactoryConfigurationError |
|
133 |
{ |
|
134 | 255 |
if (_saxParser == null) |
135 | 241 |
_saxParser = SAXParserFactory.newInstance().newSAXParser(); |
136 |
|
|
137 | 255 |
return _saxParser;
|
138 |
} |
|
139 |
|
|
140 |
} |
|