1 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 40 public class DefaultApplicationXml implements ApplicationXml 41 { 42 44 47 private final Document document; 48 49 52 private final Element rootElement; 53 54 56 62 public DefaultApplicationXml(Document theDocument) 63 { 64 this.document = theDocument; 65 this.rootElement = theDocument.getDocumentElement(); 66 } 67 68 70 73 public final Document getDocument() 74 { 75 return this.document; 76 } 77 78 81 public final ApplicationXmlVersion getVersion() 82 { 83 DocumentType docType = this.document.getDoctype(); 84 if (docType != null) 85 { 86 return ApplicationXmlVersion.valueOf(docType); 87 } 88 return null; 89 } 90 91 94 public final Element getWebModule(String theWebUri) 95 { 96 if (theWebUri == null) 97 { 98 throw new NullPointerException (); 99 } 100 Iterator moduleElements = getElements(ApplicationXmlTag.MODULE); 101 while (moduleElements.hasNext()) 102 { 103 Element moduleElement = (Element ) moduleElements.next(); 104 Iterator webElements = 105 getNestedElements(moduleElement, ApplicationXmlTag.WEB); 106 if (webElements.hasNext()) 107 { 108 Element webElement = (Element ) 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 122 public final String getWebModuleContextRoot(String theWebUri) 123 { 124 Element webModuleElement = getWebModule(theWebUri); 125 if (webModuleElement == null) 126 { 127 throw new IllegalArgumentException ("Web module [" + theWebUri 128 + "] is not defined"); 129 } 130 return getNestedText(webModuleElement, ApplicationXmlTag.CONTEXT_ROOT); 131 } 132 133 136 public final Iterator getWebModuleUris() 137 { 138 List webUris = new ArrayList (); 139 Iterator moduleElements = getElements(ApplicationXmlTag.MODULE); 140 while (moduleElements.hasNext()) 141 { 142 Element moduleElement = (Element ) moduleElements.next(); 143 Iterator webElements = 144 getNestedElements(moduleElement, ApplicationXmlTag.WEB); 145 if (webElements.hasNext()) 146 { 147 Element webElement = (Element ) webElements.next(); 148 String 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 162 public final Iterator getElements(ApplicationXmlTag theTag) 163 { 164 List elements = new ArrayList (); 165 NodeList 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 176 187 private Iterator getNestedElements(Element theParent, 188 ApplicationXmlTag theTag) 189 { 190 List elements = new ArrayList (); 191 NodeList 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 207 private String getNestedText(Element theElement, 208 ApplicationXmlTag theTag) 209 { 210 NodeList nestedElements = 211 theElement.getElementsByTagName(theTag.getTagName()); 212 if (nestedElements.getLength() > 0) 213 { 214 Node 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 |