KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > deployment > application > DefaultApplicationXml


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.application;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.DocumentType JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
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,v 1.1 2004/05/31 20:05:24 vmassol Exp $
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 JavaDoc document;
48     
49     /**
50      * The root element of the descriptor.
51      */

52     private final Element JavaDoc rootElement;
53     
54     // Constructors ------------------------------------------------------------
55

56     /**
57      * Constructor.
58      *
59      * @param theDocument The DOM document representing the parsed deployment
60      * descriptor
61      */

62     public DefaultApplicationXml(Document JavaDoc theDocument)
63     {
64         this.document = theDocument;
65         this.rootElement = theDocument.getDocumentElement();
66     }
67     
68     // Public Methods ----------------------------------------------------------
69

70     /**
71      * @see ApplicationXml#getDocument()
72      */

73     public final Document JavaDoc getDocument()
74     {
75         return this.document;
76     }
77
78     /**
79      * @see ApplicationXml#getVersion()
80      */

81     public final ApplicationXmlVersion getVersion()
82     {
83         DocumentType JavaDoc docType = this.document.getDoctype();
84         if (docType != null)
85         {
86             return ApplicationXmlVersion.valueOf(docType);
87         }
88         return null;
89     }
90
91     /**
92      * @see ApplicationXml#getWebModule(String)
93      */

94     public final Element JavaDoc getWebModule(String JavaDoc theWebUri)
95     {
96         if (theWebUri == null)
97         {
98             throw new NullPointerException JavaDoc();
99         }
100         Iterator JavaDoc moduleElements = getElements(ApplicationXmlTag.MODULE);
101         while (moduleElements.hasNext())
102         {
103             Element JavaDoc moduleElement = (Element JavaDoc) moduleElements.next();
104             Iterator JavaDoc webElements =
105                 getNestedElements(moduleElement, ApplicationXmlTag.WEB);
106             if (webElements.hasNext())
107             {
108                 Element JavaDoc webElement = (Element JavaDoc) webElements.next();
109                 if (theWebUri.equals(getNestedText(
110                     webElement, ApplicationXmlTag.WEB_URI)))
111                 {
112                     return webElement;
113                 }
114             }
115         }
116         return null;
117     }
118     
119     /**
120      * @see ApplicationXml#getWebModuleContextRoot(String)
121      */

122     public final String JavaDoc getWebModuleContextRoot(String JavaDoc theWebUri)
123     {
124         Element JavaDoc webModuleElement = getWebModule(theWebUri);
125         if (webModuleElement == null)
126         {
127             throw new IllegalArgumentException JavaDoc("Web module [" + theWebUri
128                 + "] is not defined");
129         }
130         return getNestedText(webModuleElement, ApplicationXmlTag.CONTEXT_ROOT);
131     }
132
133     /**
134      * @see ApplicationXml#getWebModuleUris()
135      */

136     public final Iterator JavaDoc getWebModuleUris()
137     {
138         List JavaDoc webUris = new ArrayList JavaDoc();
139         Iterator JavaDoc moduleElements = getElements(ApplicationXmlTag.MODULE);
140         while (moduleElements.hasNext())
141         {
142             Element JavaDoc moduleElement = (Element JavaDoc) moduleElements.next();
143             Iterator JavaDoc webElements =
144                 getNestedElements(moduleElement, ApplicationXmlTag.WEB);
145             if (webElements.hasNext())
146             {
147                 Element JavaDoc webElement = (Element JavaDoc) webElements.next();
148                 String JavaDoc webUri =
149                     getNestedText(webElement, ApplicationXmlTag.WEB_URI);
150                 if (webUri != null)
151                 {
152                     webUris.add(webUri);
153                 }
154             }
155         }
156         return webUris.iterator();
157     }
158
159     /**
160      * @see ApplicationXml#getElements(ApplicationXmlTag)
161      */

162     public final Iterator JavaDoc getElements(ApplicationXmlTag theTag)
163     {
164         List JavaDoc elements = new ArrayList JavaDoc();
165         NodeList JavaDoc nodeList =
166             this.rootElement.getElementsByTagName(theTag.getTagName());
167         for (int i = 0; i < nodeList.getLength(); i++)
168         {
169             elements.add(nodeList.item(i));
170         }
171         return elements.iterator();
172     }
173     
174     // Private Methods ---------------------------------------------------------
175

176     /**
177      * Returns an iterator over the child elements of the specified element that
178      * match the specified tag.
179      *
180      * @param theParent The element of which the nested elements should be
181      * retrieved
182      * @param theTag The descriptor tag of which the elements should be
183      * returned
184      * @return An iterator over the elements matching the tag, in the order
185      * they occur in the descriptor
186      */

187     private Iterator JavaDoc getNestedElements(Element JavaDoc theParent,
188         ApplicationXmlTag theTag)
189     {
190         List JavaDoc elements = new ArrayList JavaDoc();
191         NodeList JavaDoc nodeList = theParent.getElementsByTagName(theTag.getTagName());
192         for (int i = 0; i < nodeList.getLength(); i++)
193         {
194             elements.add(nodeList.item(i));
195         }
196         return elements.iterator();
197     }
198
199     /**
200      * Returns the text nested inside a child element of the specified element.
201      *
202      * @param theElement The element of which the nested text should be
203      * returned
204      * @param theTag The descriptor tag in which the text is nested
205      * @return The text nested in the element
206      */

207     private String JavaDoc getNestedText(Element JavaDoc theElement,
208         ApplicationXmlTag theTag)
209     {
210         NodeList JavaDoc nestedElements =
211             theElement.getElementsByTagName(theTag.getTagName());
212         if (nestedElements.getLength() > 0)
213         {
214             Node JavaDoc nestedText = nestedElements.item(0).getFirstChild();
215             if (nestedText != null)
216             {
217                 return nestedText.getNodeValue();
218             }
219         }
220         return null;
221     }
222     
223 }
224
Popular Tags