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: 318   Methods: 13
NCLOC: 187   Classes: 1
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
DefaultApplicationXml.java 55.6% 56.8% 53.8% 56.1%
coverage coverage
 1   
 /* 
 2   
  * ========================================================================
 3   
  * 
 4   
  * Copyright 2003-2005 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.application;
 21   
 
 22   
 import java.util.ArrayList;
 23   
 import java.util.Iterator;
 24   
 import java.util.List;
 25   
 
 26   
 import org.w3c.dom.Document;
 27   
 import org.w3c.dom.DocumentType;
 28   
 import org.w3c.dom.Element;
 29   
 import org.w3c.dom.Node;
 30   
 import org.w3c.dom.NodeList;
 31   
 
 32   
 /**
 33   
  * Encapsulates the DOM representation of an EAR descriptor 
 34   
  * (<code>application.xml</code>) to provide convenience methods for easy 
 35   
  * access and manipulation.
 36   
  *
 37   
  * @since Cactus 1.5
 38   
  * @version $Id: DefaultApplicationXml.java 239141 2005-02-15 10:31:44Z vmassol $
 39   
  */
 40   
 public class DefaultApplicationXml implements ApplicationXml
 41   
 {
 42   
     // Instance Variables ------------------------------------------------------
 43   
     
 44   
     /**
 45   
      * The DOM representation of the deployment descriptor.
 46   
      */
 47   
     private final Document document;
 48   
     
 49   
     /**
 50   
      * The root element of the descriptor.
 51   
      */
 52   
     private final Element rootElement;
 53   
     
 54   
     /**
 55   
      * Specifies the order in which the top-level elements must appear in the
 56   
      * descriptor, according to the DTD.
 57   
      */
 58   
     private static final ApplicationXmlTag[] ELEMENT_ORDER = {
 59   
         ApplicationXmlTag.ICON,
 60   
         ApplicationXmlTag.DISPLAY_NAME,
 61   
         ApplicationXmlTag.DESCRIPTION,
 62   
         ApplicationXmlTag.MODULE,
 63   
         ApplicationXmlTag.SECURITY_ROLE
 64   
     };
 65   
     
 66   
     // Constructors ------------------------------------------------------------
 67   
     
 68   
     /**
 69   
      * Constructor.
 70   
      * 
 71   
      * @param theDocument The DOM document representing the parsed deployment
 72   
      *         descriptor
 73   
      */
 74  11
     public DefaultApplicationXml(Document theDocument)
 75   
     {
 76  11
         this.document = theDocument;
 77  11
         this.rootElement = theDocument.getDocumentElement();
 78   
     }
 79   
     
 80   
     // Public Methods ----------------------------------------------------------
 81   
     
 82   
     /**
 83   
      * @see ApplicationXml#getDocument()
 84   
      */
 85  0
     public final Document getDocument()
 86   
     {
 87  0
         return this.document;
 88   
     }
 89   
 
 90   
     /**
 91   
      * @see ApplicationXml#getVersion()
 92   
      */
 93  0
     public final ApplicationXmlVersion getVersion()
 94   
     {
 95  0
         DocumentType docType = this.document.getDoctype();
 96  0
         if (docType != null)
 97   
         {
 98  0
             return ApplicationXmlVersion.valueOf(docType);
 99   
         }
 100  0
         return null;
 101   
     }
 102   
 
 103   
     /**
 104   
      * @see ApplicationXml#getWebModule(String)
 105   
      */
 106  7
     public final Element getWebModule(String theWebUri)
 107   
     {
 108  7
         if (theWebUri == null)
 109   
         {
 110  0
             throw new NullPointerException();
 111   
         }
 112  7
         Iterator moduleElements = getElements(ApplicationXmlTag.MODULE);
 113  7
         while (moduleElements.hasNext())
 114   
         {
 115  10
             Element moduleElement = (Element) moduleElements.next();
 116  10
             Iterator webElements =
 117   
                 getNestedElements(moduleElement, ApplicationXmlTag.WEB);
 118  10
             if (webElements.hasNext())
 119   
             {
 120  9
                 Element webElement = (Element) webElements.next(); 
 121  9
                 if (theWebUri.equals(getNestedText(
 122   
                     webElement, ApplicationXmlTag.WEB_URI)))
 123   
                 {
 124  6
                     return webElement;
 125   
                 }
 126   
             }
 127   
         }
 128  1
         return null;
 129   
     }
 130   
     
 131   
     /**
 132   
      * @see ApplicationXml#getWebModuleContextRoot(String)
 133   
      */
 134  7
     public final String getWebModuleContextRoot(String theWebUri)
 135   
     {
 136  7
         Element webModuleElement = getWebModule(theWebUri);
 137  7
         if (webModuleElement == null)
 138   
         {
 139  1
             throw new IllegalArgumentException("Web module [" + theWebUri
 140   
                 + "] is not defined");
 141   
         }
 142  6
         return getNestedText(webModuleElement, ApplicationXmlTag.CONTEXT_ROOT);
 143   
     }
 144   
 
 145   
     /**
 146   
      * @see ApplicationXml#getWebModuleUris()
 147   
      */
 148  7
     public final Iterator getWebModuleUris()
 149   
     {
 150  7
         List webUris = new ArrayList();
 151  7
         Iterator moduleElements = getElements(ApplicationXmlTag.MODULE);
 152  7
         while (moduleElements.hasNext())
 153   
         {
 154  8
             Element moduleElement = (Element) moduleElements.next();
 155  8
             Iterator webElements =
 156   
                 getNestedElements(moduleElement, ApplicationXmlTag.WEB);
 157  8
             if (webElements.hasNext())
 158   
             {
 159  7
                 Element webElement = (Element) webElements.next(); 
 160  7
                 String webUri =
 161   
                     getNestedText(webElement, ApplicationXmlTag.WEB_URI);
 162  7
                 if (webUri != null)
 163   
                 {
 164  7
                     webUris.add(webUri);
 165   
                 }
 166   
             }
 167   
         }
 168  7
         return webUris.iterator();
 169   
     }
 170   
 
 171   
     /**
 172   
      * @see ApplicationXml#getElements(ApplicationXmlTag)
 173   
      */
 174  14
     public final Iterator getElements(ApplicationXmlTag theTag)
 175   
     {
 176  14
         List elements = new ArrayList();
 177  14
         NodeList nodeList =
 178   
             this.rootElement.getElementsByTagName(theTag.getTagName()); 
 179  14
         for (int i = 0; i < nodeList.getLength(); i++)
 180   
         {
 181  21
             elements.add(nodeList.item(i));
 182   
         }
 183  14
         return elements.iterator();
 184   
     }
 185   
     
 186   
     /**
 187   
      * @see ApplicationXml#addWebModule(String, String)
 188   
      */
 189  0
     public void addWebModule(String theUri, String theContext)
 190   
     {
 191  0
         Element moduleElement =
 192   
             this.document.createElement(ApplicationXmlTag.MODULE.getTagName());
 193  0
         Element webElement = 
 194   
             this.document.createElement(ApplicationXmlTag.WEB.getTagName());
 195  0
         webElement.appendChild(
 196   
             createNestedText(ApplicationXmlTag.WEB_URI, theUri));
 197  0
         webElement.appendChild(
 198   
             createNestedText(ApplicationXmlTag.CONTEXT_ROOT, theContext));
 199  0
         moduleElement.appendChild(webElement);
 200  0
         addElement(ApplicationXmlTag.MODULE, moduleElement);
 201   
     }
 202   
     
 203   
     // Private Methods ---------------------------------------------------------
 204   
 
 205   
     /**
 206   
      * Returns an iterator over the child elements of the specified element that
 207   
      * match the specified tag.
 208   
      *  
 209   
      * @param theParent The element of which the nested elements should be
 210   
      *        retrieved
 211   
      * @param theTag The descriptor tag of which the elements should be
 212   
      *        returned
 213   
      * @return An iterator over the elements matching the tag, in the order 
 214   
      *         they occur in the descriptor
 215   
      */
 216  18
     private Iterator getNestedElements(Element theParent,
 217   
         ApplicationXmlTag theTag)
 218   
     {
 219  18
         List elements = new ArrayList();
 220  18
         NodeList nodeList = theParent.getElementsByTagName(theTag.getTagName());
 221  18
         for (int i = 0; i < nodeList.getLength(); i++)
 222   
         {
 223  16
             elements.add(nodeList.item(i));
 224   
         }
 225  18
         return elements.iterator();
 226   
     }
 227   
 
 228   
     /**
 229   
      * Returns the text nested inside a child element of the specified element.
 230   
      * 
 231   
      * @param theElement The element of which the nested text should be
 232   
      *         returned
 233   
      * @param theTag The descriptor tag in which the text is nested
 234   
      * @return The text nested in the element
 235   
      */
 236  22
     private String getNestedText(Element theElement,
 237   
         ApplicationXmlTag theTag)
 238   
     {
 239  22
         NodeList nestedElements =
 240   
             theElement.getElementsByTagName(theTag.getTagName());
 241  22
         if (nestedElements.getLength() > 0)
 242   
         {
 243  22
             Node nestedText = nestedElements.item(0).getFirstChild();
 244  22
             if (nestedText != null)
 245   
             {
 246  22
                 return nestedText.getNodeValue();
 247   
             }
 248   
         }
 249  0
         return null;
 250   
     }
 251   
     
 252   
     /**
 253   
      * Creates an element that contains nested text.
 254   
      * 
 255   
      * @param theTag The tag to create an instance of
 256   
      * @param theText The text that should be nested in the element
 257   
      * @return The created DOM element
 258   
      */
 259  0
     private Element createNestedText(ApplicationXmlTag theTag, String theText)
 260   
     {
 261  0
         Element element = this.document.createElement(theTag.getTagName());
 262  0
         element.appendChild(this.document.createTextNode(theText));
 263  0
         return element;
 264   
     }
 265   
     
 266   
     /**
 267   
      * Adds an element of the specified tag to the descriptor.
 268   
      * 
 269   
      * @param theTag The descriptor tag
 270   
      * @param theElement The element to add
 271   
      */
 272  0
     public final void addElement(ApplicationXmlTag theTag, Element theElement)
 273   
     {
 274  0
         Node importedNode = this.document.importNode(theElement, true);
 275  0
         Node refNode = getInsertionPointFor(theTag);
 276  0
         this.rootElement.insertBefore(importedNode, refNode);
 277   
     }
 278   
 
 279   
     /**
 280   
      * Returns the node before which the specified tag should be inserted, or
 281   
      * <code>null</code> if the node should be inserted at the end of the 
 282   
      * descriptor.
 283   
      * 
 284   
      * @param theTag The tag that should be inserted
 285   
      * @return The node before which the tag can be inserted
 286   
      */
 287  0
     private Node getInsertionPointFor(ApplicationXmlTag theTag)
 288   
     {
 289  0
         for (int i = 0; i < ELEMENT_ORDER.length; i++)
 290   
         {
 291  0
             if (ELEMENT_ORDER[i] == theTag)
 292   
             {
 293  0
                 for (int j = i + 1; j < ELEMENT_ORDER.length; j++)
 294   
                 {
 295  0
                     NodeList elements =
 296   
                         this.rootElement.getElementsByTagName(
 297   
                             ELEMENT_ORDER[j].getTagName());
 298  0
                     if (elements.getLength() > 0)
 299   
                     {
 300  0
                         Node result = elements.item(0);
 301  0
                         Node previous = result.getPreviousSibling();
 302  0
                         while ((previous != null)
 303   
                             && ((previous.getNodeType() == Node.COMMENT_NODE)
 304   
                              || (previous.getNodeType() == Node.TEXT_NODE)))
 305   
                         {
 306  0
                             result = previous;
 307  0
                             previous = result.getPreviousSibling();
 308   
                         }
 309  0
                         return result;
 310   
                     }
 311   
                 }
 312  0
                 break;
 313   
             }
 314   
         }
 315  0
         return null;
 316   
     }
 317   
 }
 318