2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

Clover coverage report - Cactus 1.8dev for J2EE API 1.3
Coverage timestamp: Sun Mar 26 2006 18:50:18 BRT
file stats: LOC: 273   Methods: 8
NCLOC: 153   Classes: 2
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
WebXmlIo.java 7.1% 14.9% 12.5% 13%
coverage coverage
 1   
 /* 
 2   
  * ========================================================================
 3   
  * 
 4   
  * Copyright 2003 The Apache Software Foundation.
 5   
  *
 6   
  * Licensed under the Apache License, Version 2.0 (the "License");
 7   
  * you may not use this file except in compliance with the License.
 8   
  * You may obtain a copy of the License at
 9   
  * 
 10   
  *   http://www.apache.org/licenses/LICENSE-2.0
 11   
  * 
 12   
  * Unless required by applicable law or agreed to in writing, software
 13   
  * distributed under the License is distributed on an "AS IS" BASIS,
 14   
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   
  * See the License for the specific language governing permissions and
 16   
  * limitations under the License.
 17   
  * 
 18   
  * ========================================================================
 19   
  */
 20   
 package org.apache.cactus.integration.ant.deployment.webapp;
 21   
 
 22   
 import java.io.File;
 23   
 import java.io.FileInputStream;
 24   
 import java.io.FileOutputStream;
 25   
 import java.io.IOException;
 26   
 import java.io.InputStream;
 27   
 import java.io.OutputStream;
 28   
 
 29   
 import javax.xml.parsers.DocumentBuilder;
 30   
 import javax.xml.parsers.DocumentBuilderFactory;
 31   
 import javax.xml.parsers.ParserConfigurationException;
 32   
 
 33   
 import org.apache.xml.serialize.OutputFormat;
 34   
 import org.apache.xml.serialize.XMLSerializer;
 35   
 import org.w3c.dom.Document;
 36   
 import org.w3c.dom.DocumentType;
 37   
 import org.xml.sax.EntityResolver;
 38   
 import org.xml.sax.InputSource;
 39   
 import org.xml.sax.SAXException;
 40   
 
 41   
 /**
 42   
  * Provides convenience methods for reading and writing web deployment
 43   
  * descriptors.
 44   
  *
 45   
  * @since Cactus 1.5
 46   
  * @version $Id: WebXmlIo.java 239003 2004-05-31 20:05:27Z vmassol $
 47   
  */
 48   
 public class WebXmlIo
 49   
 {
 50   
     
 51   
     // Inner Classes -----------------------------------------------------------
 52   
 
 53   
     /**
 54   
      * Implementation of the SAX EntityResolver interface that looks up the
 55   
      * web-app DTDs from the JAR.
 56   
      */
 57   
     private static class WebXmlEntityResolver implements EntityResolver
 58   
     {
 59   
 
 60   
         /**
 61   
          * @see org.xml.sax.EntityResolver#resolveEntity
 62   
          */
 63  0
         public InputSource resolveEntity(String thePublicId, String theSystemId)
 64   
             throws SAXException, IOException
 65   
         {
 66  0
             WebXmlVersion version = WebXmlVersion.valueOf(thePublicId);
 67  0
             if (version != null)
 68   
             {
 69  0
                 String fileName = version.getSystemId().substring(
 70   
                     version.getSystemId().lastIndexOf('/'));
 71  0
                 InputStream in = this.getClass().getResourceAsStream(
 72   
                     "/org/apache/cactus/integration/ant/deployment/resources"
 73   
                     + fileName);
 74  0
                 if (in != null)
 75   
                 {
 76  0
                     return new InputSource(in);
 77   
                 }
 78   
             }
 79  0
             return null;
 80   
         }
 81   
 
 82   
     }
 83   
 
 84   
     // Public Methods ----------------------------------------------------------
 85   
 
 86   
     /**
 87   
      * Creates a new empty deployment descriptor.
 88   
      * 
 89   
      * @param theVersion The version of the descriptor to create
 90   
      * @return The new descriptor
 91   
      * @throws ParserConfigurationException If the XML parser was not correctly
 92   
      *          configured
 93   
      */
 94  0
     public static WebXml newWebXml(WebXmlVersion theVersion)
 95   
         throws ParserConfigurationException
 96   
     {
 97  0
         DocumentBuilderFactory factory =
 98   
             DocumentBuilderFactory.newInstance();
 99  0
         factory.setValidating(false);
 100  0
         factory.setNamespaceAware(false);
 101  0
         DocumentBuilder builder = factory.newDocumentBuilder();
 102  0
         DocumentType docType = null;
 103  0
         if (theVersion != null)
 104   
         {
 105  0
             docType =
 106   
                 builder.getDOMImplementation().createDocumentType("web-app",
 107   
                     theVersion.getPublicId(), theVersion.getSystemId());
 108   
         }
 109  0
         Document doc = builder.getDOMImplementation().createDocument(
 110   
             "", "web-app", docType);
 111  0
         return new WebXml(doc);
 112   
     }
 113   
 
 114   
     /**
 115   
      * Parses a deployment descriptor stored in a regular file.
 116   
      * 
 117   
      * @param theFile The file to parse
 118   
      * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
 119   
      *        use the default
 120   
      * @return The parsed descriptor
 121   
      * @throws SAXException If the file could not be parsed
 122   
      * @throws ParserConfigurationException If the XML parser was not correctly
 123   
      *          configured
 124   
      * @throws IOException If an I/O error occurs
 125   
      */
 126  0
     public static WebXml parseWebXmlFromFile(File theFile,
 127   
         EntityResolver theEntityResolver)
 128   
         throws SAXException, ParserConfigurationException, IOException
 129   
     {
 130  0
         InputStream in = null;
 131  0
         try
 132   
         {
 133  0
             in = new FileInputStream(theFile);
 134  0
             return parseWebXml(in, theEntityResolver);
 135   
         }
 136   
         finally
 137   
         {
 138  0
             if (in != null)
 139   
             {
 140  0
                 try
 141   
                 {
 142  0
                     in.close();
 143   
                 }
 144   
                 catch (IOException ioe)
 145   
                 {
 146   
                     // we'll pass on the original IO error, so ignore this one
 147   
                 }
 148   
             }
 149   
         }
 150   
     }
 151   
     
 152   
     /**
 153   
      * Parses a deployment descriptor provided as input stream.
 154   
      * 
 155   
      * @param theInput The input stream
 156   
      * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
 157   
      *        use the default
 158   
      * @return The parsed descriptor
 159   
      * @throws SAXException If the input could not be parsed
 160   
      * @throws ParserConfigurationException If the XML parser was not correctly
 161   
      *          configured
 162   
      * @throws IOException If an I/O error occurs
 163   
      */
 164  7
     public static WebXml parseWebXml(InputStream theInput,
 165   
         EntityResolver theEntityResolver)
 166   
         throws SAXException, ParserConfigurationException, IOException
 167   
     {
 168  7
         DocumentBuilderFactory factory =
 169   
             DocumentBuilderFactory.newInstance();
 170  7
         factory.setValidating(false);
 171  7
         factory.setNamespaceAware(false);
 172  7
         DocumentBuilder builder = factory.newDocumentBuilder();
 173  7
         if (theEntityResolver != null)
 174   
         {
 175  0
             builder.setEntityResolver(theEntityResolver);
 176   
         }
 177   
         else
 178   
         {
 179  7
             builder.setEntityResolver(new WebXmlEntityResolver());
 180   
         }
 181  7
         return new WebXml(builder.parse(theInput));
 182   
     }
 183   
 
 184   
     /**
 185   
      * Writes the specified document to a file.
 186   
      * 
 187   
      * @param theWebXml The descriptor to serialize
 188   
      * @param theFile The file to write to
 189   
      * @throws IOException If an I/O error occurs
 190   
      */
 191  0
     public static void writeWebXml(WebXml theWebXml, File theFile)
 192   
         throws IOException
 193   
     {
 194  0
         writeWebXml(theWebXml, theFile, null, false);
 195   
     }
 196   
 
 197   
     /**
 198   
      * Writes the specified document to a file.
 199   
      * 
 200   
      * @param theWebXml The descriptor to serialize
 201   
      * @param theFile The file to write to
 202   
      * @param theEncoding The character encoding to use
 203   
      * @throws IOException If an I/O error occurs
 204   
      */
 205  0
     public static void writeWebXml(WebXml theWebXml, File theFile,
 206   
         String theEncoding)
 207   
         throws IOException
 208   
     {
 209  0
         writeWebXml(theWebXml, theFile, theEncoding, false);
 210   
     }
 211   
 
 212   
     /**
 213   
      * Writes the specified document to a file.
 214   
      * 
 215   
      * @param theWebXml The descriptor to serialize
 216   
      * @param theFile The file to write to
 217   
      * @param theEncoding The character encoding to use
 218   
      * @param isIndent Whether the written XML should be indented
 219   
      * @throws IOException If an I/O error occurs
 220   
      */
 221  0
     public static void writeWebXml(WebXml theWebXml, File theFile,
 222   
         String theEncoding, boolean isIndent)
 223   
         throws IOException
 224   
     {
 225  0
         OutputStream out = null;
 226  0
         try
 227   
         {
 228  0
             out = new FileOutputStream(theFile);
 229  0
             writeWebXml(theWebXml, out, theEncoding, isIndent);
 230   
         }
 231   
         finally
 232   
         {
 233  0
             if (out != null)
 234   
             {
 235  0
                 try
 236   
                 {
 237  0
                     out.close();
 238   
                 }
 239   
                 catch (IOException ioe)
 240   
                 {
 241   
                     // we'll pass on the original IO error, so ignore this one
 242   
                 }
 243   
             }
 244   
         }
 245   
     }
 246   
 
 247   
     /**
 248   
      * Writes the specified document to an output stream.
 249   
      * 
 250   
      * @param theWebXml The descriptor to serialize
 251   
      * @param theOutput The output stream to write to
 252   
      * @param theEncoding The character encoding to use
 253   
      * @param isIndent Whether the written XML should be indented
 254   
      * @throws IOException If an I/O error occurs
 255   
      */
 256  0
     public static void writeWebXml(WebXml theWebXml, OutputStream theOutput,
 257   
         String theEncoding, boolean isIndent)
 258   
         throws IOException
 259   
     {
 260  0
         OutputFormat outputFormat =
 261   
             new OutputFormat(theWebXml.getDocument());
 262  0
         if (theEncoding != null)
 263   
         {
 264  0
             outputFormat.setEncoding(theEncoding);
 265   
         }
 266  0
         outputFormat.setIndenting(isIndent);
 267  0
         outputFormat.setPreserveSpace(false);
 268  0
         XMLSerializer serializer = new XMLSerializer(theOutput, outputFormat);
 269  0
         serializer.serialize(theWebXml.getDocument());
 270   
     }
 271   
 
 272   
 }
 273