2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:33:43 PST
file stats: LOC: 148   Methods: 5
NCLOC: 84   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XmlResourceProcessor.java 100% 92.3% 100% 94.9%
coverage coverage
 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  271 public XmlResourceProcessor(ClassResolver resolver, ErrorHandler errorHandler)
 57    {
 58  271 _resolver = resolver;
 59  271 _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  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    * 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  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    }