Save This Page
Home » cactus-1.8.0-src » org.apache.cactus.integration.ant.deployment.webapp » [javadoc | source]
    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,v 1.1 2004/05/31 20:05:23 vmassol Exp $
   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           public InputSource resolveEntity(String thePublicId, String theSystemId)
   64               throws SAXException, IOException
   65           {
   66               WebXmlVersion version = WebXmlVersion.valueOf(thePublicId);
   67               if (version != null)
   68               {
   69                   String fileName = version.getSystemId().substring(
   70                       version.getSystemId().lastIndexOf('/'));
   71                   InputStream in = this.getClass().getResourceAsStream(
   72                       "/org/apache/cactus/integration/ant/deployment/resources"
   73                       + fileName);
   74                   if (in != null)
   75                   {
   76                       return new InputSource(in);
   77                   }
   78               }
   79               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       public static WebXml newWebXml(WebXmlVersion theVersion)
   95           throws ParserConfigurationException
   96       {
   97           DocumentBuilderFactory factory =
   98               DocumentBuilderFactory.newInstance();
   99           factory.setValidating(false);
  100           factory.setNamespaceAware(false);
  101           DocumentBuilder builder = factory.newDocumentBuilder();
  102           DocumentType docType = null;
  103           if (theVersion != null)
  104           {
  105               docType =
  106                   builder.getDOMImplementation().createDocumentType("web-app",
  107                       theVersion.getPublicId(), theVersion.getSystemId());
  108           }
  109           Document doc = builder.getDOMImplementation().createDocument(
  110               "", "web-app", docType);
  111           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       public static WebXml parseWebXmlFromFile(File theFile,
  127           EntityResolver theEntityResolver)
  128           throws SAXException, ParserConfigurationException, IOException
  129       {
  130           InputStream in = null;
  131           try
  132           {
  133               in = new FileInputStream(theFile);
  134               return parseWebXml(in, theEntityResolver);
  135           }
  136           finally
  137           {
  138               if (in != null)
  139               {
  140                   try
  141                   {
  142                       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       public static WebXml parseWebXml(InputStream theInput,
  165           EntityResolver theEntityResolver)
  166           throws SAXException, ParserConfigurationException, IOException
  167       {
  168           DocumentBuilderFactory factory =
  169               DocumentBuilderFactory.newInstance();
  170           factory.setValidating(false);
  171           factory.setNamespaceAware(false);
  172           DocumentBuilder builder = factory.newDocumentBuilder();
  173           if (theEntityResolver != null)
  174           {
  175               builder.setEntityResolver(theEntityResolver);
  176           }
  177           else
  178           {
  179               builder.setEntityResolver(new WebXmlEntityResolver());
  180           }
  181           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       public static void writeWebXml(WebXml theWebXml, File theFile)
  192           throws IOException
  193       {
  194           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       public static void writeWebXml(WebXml theWebXml, File theFile,
  206           String theEncoding)
  207           throws IOException
  208       {
  209           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       public static void writeWebXml(WebXml theWebXml, File theFile,
  222           String theEncoding, boolean isIndent)
  223           throws IOException
  224       {
  225           OutputStream out = null;
  226           try
  227           {
  228               out = new FileOutputStream(theFile);
  229               writeWebXml(theWebXml, out, theEncoding, isIndent);
  230           }
  231           finally
  232           {
  233               if (out != null)
  234               {
  235                   try
  236                   {
  237                       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       public static void writeWebXml(WebXml theWebXml, OutputStream theOutput,
  257           String theEncoding, boolean isIndent)
  258           throws IOException
  259       {
  260           OutputFormat outputFormat =
  261               new OutputFormat(theWebXml.getDocument());
  262           if (theEncoding != null)
  263           {
  264               outputFormat.setEncoding(theEncoding);
  265           }
  266           outputFormat.setIndenting(isIndent);
  267           outputFormat.setPreserveSpace(false);
  268           XMLSerializer serializer = new XMLSerializer(theOutput, outputFormat);
  269           serializer.serialize(theWebXml.getDocument());
  270       }
  271   
  272   }

Save This Page
Home » cactus-1.8.0-src » org.apache.cactus.integration.ant.deployment.webapp » [javadoc | source]