You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by cm...@apache.org on 2003/05/14 12:43:53 UTC

cvs commit: jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment ApplicationXml.java ApplicationXmlIo.java ApplicationXmlTag.java

cmlenz      2003/05/14 03:43:53

  Added:       integration/ant/src/java/org/apache/cactus/integration/ant/deployment
                        ApplicationXml.java ApplicationXmlIo.java
                        ApplicationXmlTag.java
  Log:
  Multipart commit: Move classes from applicationxml into deployment package
  
  Revision  Changes    Path
  1.1                  jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/ApplicationXml.java
  
  Index: ApplicationXml.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
   *    Foundation" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.cactus.integration.ant.deployment;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  /**
   * Encapsulates the DOM representation of a enterprise application descriptor 
   * (<code>application.xml</code>) to provide convenience methods for easy access
   * and manipulation.
   *
   * @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
   *
   * @since Cactus 1.5
   * @version $Id: ApplicationXml.java,v 1.1 2003/05/14 10:43:52 cmlenz Exp $
   */
  public class ApplicationXml
  {
      
      // Instance Variables ------------------------------------------------------
      
      /**
       * The DOM representation of the deployment descriptor.
       */
      private final Document document;
      
      /**
       * The root element of the descriptor.
       */
      private final Element rootElement;
      
      // Constructors ------------------------------------------------------------
      
      /**
       * Constructor.
       * 
       * @param theDocument The DOM document representing the parsed deployment
       *         descriptor
       */
      public ApplicationXml(Document theDocument)
      {
          this.document = theDocument;
          this.rootElement = theDocument.getDocumentElement();
      }
      
      // Public Methods ----------------------------------------------------------
      
      /**
       * Returns the DOM document representing the deployment descriptor. The 
       * document will contain any modifications made through this instance.
       * 
       * @return The document representing the deploy descriptor
       */
      public final Document getDocument()
      {
          return this.document;
      }
  
      /**
       * Returns the element that contains the definition of a specific web
       * module, or <code>null</code> if a web module with the specified web-uri
       * is not defined.
       * 
       * @param theWebUri The uri of the web module
       * @return The DOM element representing the filter definition
       */
      public final Element getWebModule(String theWebUri)
      {
          if (theWebUri == null)
          {
              throw new NullPointerException();
          }
          Iterator moduleElements = getElements(ApplicationXmlTag.MODULE);
          while (moduleElements.hasNext())
          {
              Element moduleElement = (Element) moduleElements.next();
              Iterator webElements =
                  getNestedElements(moduleElement, ApplicationXmlTag.WEB);
              if (webElements.hasNext())
              {
                  Element webElement = (Element) webElements.next(); 
                  if (theWebUri.equals(getNestedText(
                      webElement, ApplicationXmlTag.WEB_URI)))
                  {
                      return webElement;
                  }
              }
          }
          return null;
      }
      
      /**
       * Returns the context root of the the specified web module.
       * 
       * @param theWebUri The uri of the web module
       * @return The context root of the web module
       */
      public final String getWebModuleContextRoot(String theWebUri)
      {
          Element webModuleElement = getWebModule(theWebUri);
          if (webModuleElement == null)
          {
              throw new IllegalArgumentException("Web Module '" + theWebUri
                  + "' not defined");
          }
          return getNestedText(webModuleElement, ApplicationXmlTag.CONTEXT_ROOT);
      }
  
      /**
       * Returns an iterator over the URIs of the web modules defined in the 
       * descriptor.
       * 
       * @return An iterator over the URIs of the web modules
       */
      public final Iterator getWebModuleUris()
      {
          List webUris = new ArrayList();
          Iterator moduleElements = getElements(ApplicationXmlTag.MODULE);
          while (moduleElements.hasNext())
          {
              Element moduleElement = (Element) moduleElements.next();
              Iterator webElements =
                  getNestedElements(moduleElement, ApplicationXmlTag.WEB);
              if (webElements.hasNext())
              {
                  Element webElement = (Element) webElements.next(); 
                  String webUri =
                      getNestedText(webElement, ApplicationXmlTag.WEB_URI);
                  if (webUri != null)
                  {
                      webUris.add(webUri);
                  }
              }
          }
          return webUris.iterator();
      }
  
      /**
       * Returns an iterator over the elements that match the specified tag.
       * 
       * @param theTag The descriptor tag of which the elements should be
       *        returned
       * @return An iterator over the elements matching the tag, in the order 
       *         they occur in the descriptor
       */
      public final Iterator getElements(ApplicationXmlTag theTag)
      {
          List elements = new ArrayList();
          NodeList nodeList =
              this.rootElement.getElementsByTagName(theTag.getTagName()); 
          for (int i = 0; i < nodeList.getLength(); i++)
          {
              elements.add(nodeList.item(i));
          }
          return elements.iterator();
      }
      
      // Private Methods ---------------------------------------------------------
  
      /**
       * Returns an iterator over the child elements of the specified element that
       * match the specified tag.
       *  
       * @param theParent The element of which the nested elements should be
       *        retrieved
       * @param theTag The descriptor tag of which the elements should be
       *        returned
       * @return An iterator over the elements matching the tag, in the order 
       *         they occur in the descriptor
       */
      private Iterator getNestedElements(Element theParent,
          ApplicationXmlTag theTag)
      {
          List elements = new ArrayList();
          NodeList nodeList = theParent.getElementsByTagName(theTag.getTagName());
          for (int i = 0; i < nodeList.getLength(); i++)
          {
              elements.add(nodeList.item(i));
          }
          return elements.iterator();
      }
  
      /**
       * Returns the text nested inside a child element of the specified element.
       * 
       * @param theElement The element of which the nested text should be
       *         returned
       * @param theTag The descriptor tag in which the text is nested
       * @return The text nested in the element
       */
      private String getNestedText(Element theElement,
          ApplicationXmlTag theTag)
      {
          NodeList nestedElements =
              theElement.getElementsByTagName(theTag.getTagName());
          if (nestedElements.getLength() > 0)
          {
              Node nestedText = nestedElements.item(0).getFirstChild();
              if (nestedText != null)
              {
                  return nestedText.getNodeValue();
              }
          }
          return null;
      }
      
  }
  
  
  
  1.1                  jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/ApplicationXmlIo.java
  
  Index: ApplicationXmlIo.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
   *    Foundation" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.cactus.integration.ant.deployment;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.jar.JarInputStream;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  
  import org.apache.cactus.integration.ant.util.ResourceUtils;
  import org.xml.sax.EntityResolver;
  import org.xml.sax.SAXException;
  
  /**
   * Provides convenience methods for reading and writing enterprise application
   * deployment descriptors (application.xml).
   *
   * @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
   *
   * @since Cactus 1.5
   * @version $Id: ApplicationXmlIo.java,v 1.1 2003/05/14 10:43:52 cmlenz Exp $
   */
  public class ApplicationXmlIo
  {
      
      // Public Methods ----------------------------------------------------------
  
      /**
       * Parses the deployment descriptor of a enterprise application archive
       * (EAR).
       * 
       * @param theEar The enterprise application archive
       * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
       *        use the default
       * @return The parsed descriptor, or <code>null</code> if no descriptor was
       *         found in the WAR
       * @throws SAXException If the descriptor could not be parsed
       * @throws ParserConfigurationException If the XML parser was not correctly
       *          configured
       * @throws IOException If an I/O error occurs
       */
      public static ApplicationXml parseApplicationXmlFromEar(
          JarInputStream theEar, EntityResolver theEntityResolver)
          throws SAXException, ParserConfigurationException, IOException
      {
          InputStream in = null;
          try
          {
              in = ResourceUtils.getResource(theEar, "META-INF/application.xml");
              return parseApplicationXml(in, theEntityResolver);
          }
          finally
          {
              if (in != null)
              {
                  try
                  {
                      in.close();
                  }
                  catch (IOException ioe)
                  {
                      // we'll pass on the original IO error, so ignore this one
                  }
              }
          }
      }
      
      /**
       * Parses a deployment descriptor stored in a regular file.
       * 
       * @param theFile The file to parse
       * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
       *        use the default
       * @return The parsed descriptor
       * @throws SAXException If the file could not be parsed
       * @throws ParserConfigurationException If the XML parser was not correctly
       *          configured
       * @throws IOException If an I/O error occurs
       */
      public static ApplicationXml parseApplicationXmlFromFile(File theFile,
          EntityResolver theEntityResolver)
          throws SAXException, ParserConfigurationException, IOException
      {
          InputStream in = null;
          try
          {
              in = new FileInputStream(theFile);
              return parseApplicationXml(in, theEntityResolver);
          }
          finally
          {
              if (in != null)
              {
                  try
                  {
                      in.close();
                  }
                  catch (IOException ioe)
                  {
                      // we'll pass on the original IO error, so ignore this one
                  }
              }
          }
      }
      
      /**
       * Parses a deployment descriptor provided as input stream.
       * 
       * @param theInput The input stream
       * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
       *        use the default
       * @return The parsed descriptor
       * @throws SAXException If the input could not be parsed
       * @throws ParserConfigurationException If the XML parser was not correctly
       *          configured
       * @throws IOException If an I/O error occurs
       */
      public static ApplicationXml parseApplicationXml(InputStream theInput,
          EntityResolver theEntityResolver)
          throws SAXException, ParserConfigurationException, IOException
      {
          DocumentBuilderFactory factory =
              DocumentBuilderFactory.newInstance();
          factory.setValidating(false);
          factory.setNamespaceAware(false);
          DocumentBuilder builder = factory.newDocumentBuilder();
          if (theEntityResolver != null)
          {
              builder.setEntityResolver(theEntityResolver);
          }
          return new ApplicationXml(builder.parse(theInput));
      }
  
  }
  
  
  
  1.1                  jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/ApplicationXmlTag.java
  
  Index: ApplicationXmlTag.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
   *    Foundation" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.cactus.integration.ant.deployment;
  
  /**
   * Represents the various top-level tags in a enterprise application deployment
   * descriptor as a typesafe enumeration.
   * 
   * @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
   *
   * @since Cactus 1.5
   * @version $Id: ApplicationXmlTag.java,v 1.1 2003/05/14 10:43:52 cmlenz Exp $
   */
  public class ApplicationXmlTag
  {
      
      // Public Constants --------------------------------------------------------
      
      /**
       * Element name 'icon'.
       */
      public static final ApplicationXmlTag ICON =
          new ApplicationXmlTag("icon");
      
      /**
       * Element name 'display-name'.
       */
      public static final ApplicationXmlTag DISPLAY_NAME =
          new ApplicationXmlTag("display-name");
      
      /**
       * Element name 'description'.
       */
      public static final ApplicationXmlTag DESCRIPTION =
          new ApplicationXmlTag("description");
      
      /**
       * Element name 'module'.
       */
      public static final ApplicationXmlTag MODULE =
          new ApplicationXmlTag("module");
      
      /**
       * Element name 'web',
       */
      public static final ApplicationXmlTag WEB =
          new ApplicationXmlTag("web");
      
      /**
       * Element name 'web-uri',
       */
      public static final ApplicationXmlTag WEB_URI =
          new ApplicationXmlTag("web-uri");
      
      /**
       * Element name 'context-root',
       */
      public static final ApplicationXmlTag CONTEXT_ROOT =
          new ApplicationXmlTag("context-root");
      
      // Instance Variables ------------------------------------------------------
      
      /**
       * The tag name,
       */
      private String tagName;
      
      // Constructors ------------------------------------------------------------
      
      /**
       * Constructor.
       * 
       * @param theTagName The tag name of the element
       */
      private ApplicationXmlTag(String theTagName)
      {
          this.tagName = theTagName;
      }
  
      // Public Methods ----------------------------------------------------------
      
      /**
       * @see java.lang.Object#toString
       */
      public final boolean equals(Object theOther)
      {
          return super.equals(theOther);
      }
      
      /**
       * @see java.lang.Object#hashCode
       */
      public final int hashCode()
      {
          return super.hashCode();
      }
      
      /**
       * Returns the tag name.
       * 
       * @return The tag name
       */
      public final String getTagName()
      {
          return this.tagName;
      }
      
      /**
       * @see java.lang.Object#toString
       */
      public final String toString()
      {
          return getTagName();
      }
      
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cactus-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: cactus-dev-help@jakarta.apache.org