Clover coverage report - Cactus 1.5 for J2EE API 1.3
Coverage timestamp: Wed Feb 18 2004 09:09:13 EST
file stats: LOC: 264   Methods: 9
NCLOC: 125   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultApplicationXml.java 76.9% 85.7% 77.8% 82.1%
coverage coverage
 1   
 /*
 2   
  * ====================================================================
 3   
  *
 4   
  * The Apache Software License, Version 1.1
 5   
  *
 6   
  * Copyright (c) 2003 The Apache Software Foundation.  All rights
 7   
  * reserved.
 8   
  *
 9   
  * Redistribution and use in source and binary forms, with or without
 10   
  * modification, are permitted provided that the following conditions
 11   
  * are met:
 12   
  *
 13   
  * 1. Redistributions of source code must retain the above copyright
 14   
  *    notice, this list of conditions and the following disclaimer.
 15   
  *
 16   
  * 2. Redistributions in binary form must reproduce the above copyright
 17   
  *    notice, this list of conditions and the following disclaimer in
 18   
  *    the documentation and/or other materials provided with the
 19   
  *    distribution.
 20   
  *
 21   
  * 3. The end-user documentation included with the redistribution, if
 22   
  *    any, must include the following acknowlegement:
 23   
  *       "This product includes software developed by the
 24   
  *        Apache Software Foundation (http://www.apache.org/)."
 25   
  *    Alternately, this acknowlegement may appear in the software itself,
 26   
  *    if and wherever such third-party acknowlegements normally appear.
 27   
  *
 28   
  * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
 29   
  *    Foundation" must not be used to endorse or promote products
 30   
  *    derived from this software without prior written permission. For
 31   
  *    written permission, please contact apache@apache.org.
 32   
  *
 33   
  * 5. Products derived from this software may not be called "Apache"
 34   
  *    nor may "Apache" appear in their names without prior written
 35   
  *    permission of the Apache Group.
 36   
  *
 37   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 38   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 39   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 40   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 41   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 42   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 43   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 44   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 45   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 46   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 47   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 48   
  * SUCH DAMAGE.
 49   
  * ====================================================================
 50   
  *
 51   
  * This software consists of voluntary contributions made by many
 52   
  * individuals on behalf of the Apache Software Foundation.  For more
 53   
  * information on the Apache Software Foundation, please see
 54   
  * <http://www.apache.org/>.
 55   
  *
 56   
  */
 57   
 package org.apache.cactus.integration.ant.deployment;
 58   
 
 59   
 import java.util.ArrayList;
 60   
 import java.util.Iterator;
 61   
 import java.util.List;
 62   
 
 63   
 import org.w3c.dom.Document;
 64   
 import org.w3c.dom.DocumentType;
 65   
 import org.w3c.dom.Element;
 66   
 import org.w3c.dom.Node;
 67   
 import org.w3c.dom.NodeList;
 68   
 
 69   
 /**
 70   
  * Encapsulates the DOM representation of an EAR descriptor 
 71   
  * (<code>application.xml</code>) to provide convenience methods for easy 
 72   
  * access and manipulation.
 73   
  *
 74   
  * @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
 75   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
 76   
  *
 77   
  * @since Cactus 1.5
 78   
  * @version $Id: DefaultApplicationXml.java,v 1.1.2.1 2003/10/25 17:22:05 vmassol Exp $
 79   
  */
 80   
 public class DefaultApplicationXml implements ApplicationXml
 81   
 {
 82   
     // Instance Variables ------------------------------------------------------
 83   
     
 84   
     /**
 85   
      * The DOM representation of the deployment descriptor.
 86   
      */
 87   
     private final Document document;
 88   
     
 89   
     /**
 90   
      * The root element of the descriptor.
 91   
      */
 92   
     private final Element rootElement;
 93   
     
 94   
     // Constructors ------------------------------------------------------------
 95   
     
 96   
     /**
 97   
      * Constructor.
 98   
      * 
 99   
      * @param theDocument The DOM document representing the parsed deployment
 100   
      *         descriptor
 101   
      */
 102  11
     public DefaultApplicationXml(Document theDocument)
 103   
     {
 104  11
         this.document = theDocument;
 105  11
         this.rootElement = theDocument.getDocumentElement();
 106   
     }
 107   
     
 108   
     // Public Methods ----------------------------------------------------------
 109   
     
 110   
     /**
 111   
      * @see ApplicationXml#getDocument()
 112   
      */
 113  0
     public final Document getDocument()
 114   
     {
 115  0
         return this.document;
 116   
     }
 117   
 
 118   
     /**
 119   
      * @see ApplicationXml#getVersion()
 120   
      */
 121  0
     public final ApplicationXmlVersion getVersion()
 122   
     {
 123  0
         DocumentType docType = this.document.getDoctype();
 124  0
         if (docType != null)
 125   
         {
 126  0
             return ApplicationXmlVersion.valueOf(docType);
 127   
         }
 128  0
         return null;
 129   
     }
 130   
 
 131   
     /**
 132   
      * @see ApplicationXml#getWebModule(String)
 133   
      */
 134  7
     public final Element getWebModule(String theWebUri)
 135   
     {
 136  7
         if (theWebUri == null)
 137   
         {
 138  0
             throw new NullPointerException();
 139   
         }
 140  7
         Iterator moduleElements = getElements(ApplicationXmlTag.MODULE);
 141  7
         while (moduleElements.hasNext())
 142   
         {
 143  10
             Element moduleElement = (Element) moduleElements.next();
 144  10
             Iterator webElements =
 145   
                 getNestedElements(moduleElement, ApplicationXmlTag.WEB);
 146  10
             if (webElements.hasNext())
 147   
             {
 148  9
                 Element webElement = (Element) webElements.next(); 
 149  9
                 if (theWebUri.equals(getNestedText(
 150   
                     webElement, ApplicationXmlTag.WEB_URI)))
 151   
                 {
 152  6
                     return webElement;
 153   
                 }
 154   
             }
 155   
         }
 156  1
         return null;
 157   
     }
 158   
     
 159   
     /**
 160   
      * @see ApplicationXml#getWebModuleContextRoot(String)
 161   
      */
 162  7
     public final String getWebModuleContextRoot(String theWebUri)
 163   
     {
 164  7
         Element webModuleElement = getWebModule(theWebUri);
 165  7
         if (webModuleElement == null)
 166   
         {
 167  1
             throw new IllegalArgumentException("Web module [" + theWebUri
 168   
                 + "] is not defined");
 169   
         }
 170  6
         return getNestedText(webModuleElement, ApplicationXmlTag.CONTEXT_ROOT);
 171   
     }
 172   
 
 173   
     /**
 174   
      * @see ApplicationXml#getWebModuleUris()
 175   
      */
 176  7
     public final Iterator getWebModuleUris()
 177   
     {
 178  7
         List webUris = new ArrayList();
 179  7
         Iterator moduleElements = getElements(ApplicationXmlTag.MODULE);
 180  7
         while (moduleElements.hasNext())
 181   
         {
 182  8
             Element moduleElement = (Element) moduleElements.next();
 183  8
             Iterator webElements =
 184   
                 getNestedElements(moduleElement, ApplicationXmlTag.WEB);
 185  8
             if (webElements.hasNext())
 186   
             {
 187  7
                 Element webElement = (Element) webElements.next(); 
 188  7
                 String webUri =
 189   
                     getNestedText(webElement, ApplicationXmlTag.WEB_URI);
 190  7
                 if (webUri != null)
 191   
                 {
 192  7
                     webUris.add(webUri);
 193   
                 }
 194   
             }
 195   
         }
 196  7
         return webUris.iterator();
 197   
     }
 198   
 
 199   
     /**
 200   
      * @see ApplicationXml#getElements(ApplicationXmlTag)
 201   
      */
 202  14
     public final Iterator getElements(ApplicationXmlTag theTag)
 203   
     {
 204  14
         List elements = new ArrayList();
 205  14
         NodeList nodeList =
 206   
             this.rootElement.getElementsByTagName(theTag.getTagName()); 
 207  14
         for (int i = 0; i < nodeList.getLength(); i++)
 208   
         {
 209  21
             elements.add(nodeList.item(i));
 210   
         }
 211  14
         return elements.iterator();
 212   
     }
 213   
     
 214   
     // Private Methods ---------------------------------------------------------
 215   
 
 216   
     /**
 217   
      * Returns an iterator over the child elements of the specified element that
 218   
      * match the specified tag.
 219   
      *  
 220   
      * @param theParent The element of which the nested elements should be
 221   
      *        retrieved
 222   
      * @param theTag The descriptor tag of which the elements should be
 223   
      *        returned
 224   
      * @return An iterator over the elements matching the tag, in the order 
 225   
      *         they occur in the descriptor
 226   
      */
 227  18
     private Iterator getNestedElements(Element theParent,
 228   
         ApplicationXmlTag theTag)
 229   
     {
 230  18
         List elements = new ArrayList();
 231  18
         NodeList nodeList = theParent.getElementsByTagName(theTag.getTagName());
 232  18
         for (int i = 0; i < nodeList.getLength(); i++)
 233   
         {
 234  16
             elements.add(nodeList.item(i));
 235   
         }
 236  18
         return elements.iterator();
 237   
     }
 238   
 
 239   
     /**
 240   
      * Returns the text nested inside a child element of the specified element.
 241   
      * 
 242   
      * @param theElement The element of which the nested text should be
 243   
      *         returned
 244   
      * @param theTag The descriptor tag in which the text is nested
 245   
      * @return The text nested in the element
 246   
      */
 247  22
     private String getNestedText(Element theElement,
 248   
         ApplicationXmlTag theTag)
 249   
     {
 250  22
         NodeList nestedElements =
 251   
             theElement.getElementsByTagName(theTag.getTagName());
 252  22
         if (nestedElements.getLength() > 0)
 253   
         {
 254  22
             Node nestedText = nestedElements.item(0).getFirstChild();
 255  22
             if (nestedText != null)
 256   
             {
 257  22
                 return nestedText.getNodeValue();
 258   
             }
 259   
         }
 260  0
         return null;
 261   
     }
 262   
     
 263   
 }
 264