Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 245   Methods: 9
NCLOC: 82   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JAXPUtils.java 66.7% 57.1% 77.8% 62.8%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.util;
 55   
 
 56   
 import java.io.File;
 57   
 import javax.xml.parsers.FactoryConfigurationError;
 58   
 import javax.xml.parsers.ParserConfigurationException;
 59   
 import javax.xml.parsers.SAXParser;
 60   
 import javax.xml.parsers.SAXParserFactory;
 61   
 import org.apache.tools.ant.BuildException;
 62   
 import org.xml.sax.Parser;
 63   
 import org.xml.sax.SAXException;
 64   
 import org.xml.sax.XMLReader;
 65   
 
 66   
 /**
 67   
  * Collection of helper methods that retrieve a ParserFactory or
 68   
  * Parsers and Readers.
 69   
  *
 70   
  * <p>This class will create only a single factory instance.</p>
 71   
  *
 72   
  * @author Stefan Bodewig
 73   
  *
 74   
  * @since Ant 1.5
 75   
  */
 76   
 public class JAXPUtils {
 77   
 
 78   
     /**
 79   
      * Helper for systemId.
 80   
      *
 81   
      * @since Ant 1.6
 82   
      */
 83   
     private static final FileUtils fu = FileUtils.newFileUtils();
 84   
 
 85   
     /**
 86   
      * Parser factory to use to create parsers.
 87   
      * @see #getParserFactory
 88   
      *
 89   
      * @since Ant 1.5
 90   
      */
 91   
     private static SAXParserFactory parserFactory = null;
 92   
 
 93   
     /**
 94   
      * Parser Factory to create Namespace aware parsers.
 95   
      *
 96   
      * @since Ant 1.6
 97   
      */
 98   
     private static SAXParserFactory nsParserFactory = null;
 99   
 
 100   
 
 101   
     /**
 102   
      * Returns the parser factory to use. Only one parser factory is
 103   
      * ever created by this method and is then cached for future use.
 104   
      *
 105   
      * @return a SAXParserFactory to use
 106   
      *
 107   
      * @since Ant 1.5
 108   
      */
 109  12
     public synchronized static SAXParserFactory getParserFactory()
 110   
         throws BuildException {
 111   
 
 112  12
         if (parserFactory == null) {
 113  1
             parserFactory = newParserFactory();
 114   
         }
 115  12
         return parserFactory;
 116   
     }
 117   
 
 118   
     /**
 119   
      * Returns the parser factory to use to create namespace aware parsers.
 120   
      *
 121   
      * @return a SAXParserFactory to use which supports manufacture of
 122   
      * namespace aware parsers
 123   
      *
 124   
      * @since Ant 1.6
 125   
      */
 126  566
     public synchronized static SAXParserFactory getNSParserFactory()
 127   
         throws BuildException {
 128   
 
 129  566
         if (nsParserFactory == null) {
 130  1
             nsParserFactory = newParserFactory();
 131  1
             nsParserFactory.setNamespaceAware(true);
 132   
         }
 133  566
         return nsParserFactory;
 134   
     }
 135   
 
 136   
     /**
 137   
      * Returns a new  parser factory instance.
 138   
      *
 139   
      * @since Ant 1.5
 140   
      */
 141  2
     public static SAXParserFactory newParserFactory() throws BuildException {
 142   
 
 143  2
         try {
 144  2
             return SAXParserFactory.newInstance();
 145   
         } catch (FactoryConfigurationError e) {
 146  0
             throw new BuildException("XML parser factory has not been "
 147   
                                      + "configured correctly: "
 148   
                                      + e.getMessage(), e);
 149   
         }
 150   
     }
 151   
 
 152   
     /**
 153   
      * Returns a newly created SAX 1 Parser, using the default parser
 154   
      * factory.
 155   
      *
 156   
      * @return a SAX 1 Parser.
 157   
      * @see #getParserFactory
 158   
      * @since Ant 1.5
 159   
      */
 160  0
     public static Parser getParser() throws BuildException {
 161  0
         try {
 162  0
             return newSAXParser(getParserFactory()).getParser();
 163   
         } catch (SAXException e) {
 164  0
             throw convertToBuildException(e);
 165   
         }
 166   
     }
 167   
 
 168   
     /**
 169   
      * Returns a newly created SAX 2 XMLReader, using the default parser
 170   
      * factory.
 171   
      *
 172   
      * @return a SAX 2 XMLReader.
 173   
      * @see #getParserFactory
 174   
      * @since Ant 1.5
 175   
      */
 176  12
     public static XMLReader getXMLReader() throws BuildException {
 177  12
         try {
 178  12
             return newSAXParser(getParserFactory()).getXMLReader();
 179   
         } catch (SAXException e) {
 180  0
             throw convertToBuildException(e);
 181   
         }
 182   
     }
 183   
 
 184   
     /**
 185   
      * Returns a newly created SAX 2 XMLReader, which is namespace aware
 186   
      *
 187   
      * @return a SAX 2 XMLReader.
 188   
      * @see #getParserFactory
 189   
      * @since Ant 1.6
 190   
      */
 191  566
     public static XMLReader getNamespaceXMLReader() throws BuildException {
 192  566
         try {
 193  566
             return newSAXParser(getNSParserFactory()).getXMLReader();
 194   
         } catch (SAXException e) {
 195  0
             throw convertToBuildException(e);
 196   
         }
 197   
     }
 198   
 
 199   
     /**
 200   
      * This is a best attempt to provide a URL.toExternalForm() from
 201   
      * a file URL. Some parsers like Crimson choke on uri that are made of
 202   
      * backslashed paths (ie windows) as it is does not conform
 203   
      * URI specifications.
 204   
      * @param file the file to create the system id from.
 205   
      * @return the systemid corresponding to the given file.
 206   
      * @since Ant 1.5.2
 207   
      */
 208  48
     public static String getSystemId(File file){
 209  48
         return fu.toURI(file.getAbsolutePath());
 210   
     }
 211   
 
 212   
     /**
 213   
      * @return a new SAXParser instance as helper for getParser and
 214   
      * getXMLReader.
 215   
      *
 216   
      * @since Ant 1.5
 217   
      */
 218  578
     private static SAXParser newSAXParser(SAXParserFactory factory)
 219   
          throws BuildException {
 220  578
         try {
 221  578
             return factory.newSAXParser();
 222   
         } catch (ParserConfigurationException e) {
 223  0
             throw new BuildException("Cannot create parser for the given "
 224   
                                      + "configuration: " + e.getMessage(), e);
 225   
         } catch (SAXException e) {
 226  0
             throw convertToBuildException(e);
 227   
         }
 228   
     }
 229   
 
 230   
     /**
 231   
      * Translate a SAXException into a BuildException
 232   
      *
 233   
      * @since Ant 1.5
 234   
      */
 235  0
     private static BuildException convertToBuildException(SAXException e) {
 236  0
         Exception nested = e.getException();
 237  0
         if (nested != null) {
 238  0
             return new BuildException(nested);
 239   
         } else {
 240  0
             return new BuildException(e);
 241   
         }
 242   
     }
 243   
 
 244   
 }
 245