Clover coverage report - Code Coverage for hivemind release 1.0-beta-2
Coverage timestamp: Sun Aug 1 2004 14:03:45 EDT
file stats: LOC: 111   Methods: 2
NCLOC: 57   Classes: 1
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
SDLResourceParser.java 100% 94.4% 100% 95.5%
coverage coverage
 1   
 //  Copyright 2004 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.sdl;
 16   
 
 17   
 import java.io.BufferedInputStream;
 18   
 import java.io.IOException;
 19   
 import java.io.InputStream;
 20   
 
 21   
 import org.apache.commons.logging.Log;
 22   
 import org.apache.commons.logging.LogFactory;
 23   
 import org.apache.hivemind.Resource;
 24   
 import org.apache.hivemind.sdl.parser.SimpleDataLanguageParser;
 25   
 import org.apache.hivemind.sdl.parser.TokenMgrError;
 26   
 import org.xml.sax.ContentHandler;
 27   
 import org.xml.sax.Locator;
 28   
 import org.xml.sax.SAXParseException;
 29   
 
 30   
 /**
 31   
  * Wrapper around {@link org.apache.hivemind.sdl.parser.SimpleDataLanguageParser}
 32   
  * (which is generated code). Parses resources that contained Simple Data Language - a language
 33   
  * inspired by XML, but rationalized for storing hiearachical data (a frequent application of XML,
 34   
  * even though XML is better suited for rich document markup than storing of data).  SDL
 35   
  * is isomorphic to a subset of XML: It has equivalents for XML elements, XML attributes,
 36   
  * and parsable character data (text within an element), but does not support processing
 37   
  * instructions, namespaces, internal or external entities, doctype, etc.
 38   
  *
 39   
  * @author Howard Lewis Ship
 40   
  */
 41   
 public class SDLResourceParser
 42   
 {
 43   
     private static final Log LOG = LogFactory.getLog(SDLResourceParser.class);
 44   
 
 45   
     /**
 46   
      * Parses the resource, passing SAX parse events to the handler. SDL does
 47   
      * not (at this time) support namespaces or other constructs of XML, so only
 48   
      * the following methods of ContentHandler will be invoked:
 49   
      * <ul>
 50   
      * <li>{@link ContentHandler#setDocumentLocator(org.xml.sax.Locator)} - before any other calls
 51   
      * <ii>{@link ContentHandler#startDocument()}
 52   
      * <li>{@link ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
 53   
      *         - passing only the local name (namespace URL and qname will be null)
 54   
      * <li>{@link ContentHandler#characters(char[], int, int) for literal (and extended literal) strings
 55   
      * <li>{@link ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String) - again, with the namespace
 56   
      * parameters passed as null
 57   
      * <li>{@link ContentHandler#endDocument() - last
 58   
      * </ul>
 59   
      * 
 60   
      * <p>
 61   
      * At some future point, SDL may support more XML-like things, including names spaces.
 62   
      */
 63  121
     public void parseResource(Resource resource, ContentHandler handler)
 64   
         throws SAXParseException, IOException
 65   
     {
 66  121
         BufferedInputStream bis = null;
 67  121
         SimpleDataLanguageParser parser = null;
 68  121
         Locator locator = null;
 69   
 
 70  121
         try
 71   
         {
 72  121
             InputStream rawStream = resource.getResourceURL().openStream();
 73   
 
 74  121
             bis = new BufferedInputStream(rawStream);
 75   
 
 76  121
             parser = new SimpleDataLanguageParser(rawStream);
 77  121
             locator = parser.getLocator();
 78   
 
 79  121
             parser.parse(handler);
 80   
 
 81  119
             bis.close();
 82  119
             bis = null;
 83   
         }
 84   
         catch (TokenMgrError ex)
 85   
         {
 86  1
             throw new SAXParseException(SDLMessages.parseError(resource, ex), locator);
 87   
         }
 88   
         catch (Exception ex)
 89   
         {
 90  1
             throw new SAXParseException(SDLMessages.parseError(resource, ex), locator, ex);
 91   
         }
 92   
         finally
 93   
         {
 94  121
             close(resource, bis);
 95   
         }
 96   
     }
 97   
 
 98  121
     private void close(Resource resource, InputStream stream)
 99   
     {
 100  121
         try
 101   
         {
 102  121
             if (stream != null)
 103  2
                 stream.close();
 104   
         }
 105   
         catch (IOException ex)
 106   
         {
 107  0
             LOG.error(SDLMessages.unableToCloseStream(resource, ex), ex);
 108   
         }
 109   
     }
 110   
 }
 111