KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > parse > XmlResourceProcessor


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 JavaDoc;
18 import java.net.URL JavaDoc;
19
20 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
21 import javax.xml.parsers.ParserConfigurationException JavaDoc;
22 import javax.xml.parsers.SAXParser JavaDoc;
23 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
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  * @since 1.1
41  * @author Knut Wannheden
42  */

43 public class XmlResourceProcessor
44 {
45     private static final Log LOG = LogFactory.getLog(XmlResourceProcessor.class);
46
47     protected ClassResolver _resolver;
48
49     protected ErrorHandler _errorHandler;
50
51     private DescriptorParser _contentHandler;
52
53     private SAXParser JavaDoc _saxParser;
54
55     public XmlResourceProcessor(ClassResolver resolver, ErrorHandler errorHandler)
56     {
57         _resolver = resolver;
58         _errorHandler = errorHandler;
59     }
60
61     /**
62      * Initializes the {@link DescriptorParser parser},
63      * {@link #processResource(Resource) processes} the Resource, resets the parser, and finally
64      * returns the parsed {@link ModuleDescriptor}.
65      *
66      * @throws ApplicationRuntimeException
67      * Thrown if errors are encountered while parsing the resource.
68      */

69     public ModuleDescriptor processResource(Resource resource)
70     {
71         if (_contentHandler == null)
72             _contentHandler = new DescriptorParser(_errorHandler);
73
74         _contentHandler.initialize(resource, _resolver);
75
76         try
77         {
78             if (LOG.isDebugEnabled())
79                 LOG.debug("Parsing " + resource);
80
81             ModuleDescriptor descriptor = parseResource(resource, getSAXParser(), _contentHandler);
82
83             if (LOG.isDebugEnabled())
84                 LOG.debug("Result: " + descriptor);
85
86             return descriptor;
87         }
88         catch (ApplicationRuntimeException e)
89         {
90             throw e;
91         }
92         catch (Exception JavaDoc e)
93         {
94             _saxParser = null;
95
96             throw new ApplicationRuntimeException(
97                     ParseMessages.errorReadingDescriptor(resource, e), resource, _contentHandler
98                             .getLocation(), e);
99         }
100         finally
101         {
102             _contentHandler.resetParser();
103         }
104     }
105
106     /**
107      * Returns the ModuleDescriptor obtained by parsing the specified Resource using the given
108      * {@link SAXParser} and {@link DescriptorParser}. Called by {@link #processResource(Resource)}
109      * after the DescriptorParser has been
110      * {@link DescriptorParser#initialize(Resource, ClassResolver) initialized}. Suitable for
111      * overriding by subclasses.
112      */

113     protected ModuleDescriptor parseResource(Resource resource, SAXParser JavaDoc parser,
114             DescriptorParser contentHandler) throws SAXException JavaDoc, IOException JavaDoc
115     {
116         InputSource JavaDoc source = getInputSource(resource);
117
118         parser.parse(source, contentHandler);
119
120         return contentHandler.getModuleDescriptor();
121     }
122
123     private InputSource JavaDoc getInputSource(Resource resource)
124     {
125         try
126         {
127             URL JavaDoc url = resource.getResourceURL();
128
129             return new InputSource JavaDoc(url.openStream());
130         }
131         catch (Exception JavaDoc e)
132         {
133             throw new ApplicationRuntimeException(ParseMessages.missingResource(resource),
134                     resource, null, e);
135         }
136     }
137
138     private SAXParser JavaDoc getSAXParser() throws ParserConfigurationException JavaDoc, SAXException JavaDoc,
139             FactoryConfigurationError JavaDoc
140     {
141         if (_saxParser == null)
142             _saxParser = SAXParserFactory.newInstance().newSAXParser();
143
144         return _saxParser;
145     }
146
147 }
Popular Tags