1 45 46 package org.openejb.alt.assembler.classic.xml; 47 48 import java.util.Properties ; 49 import java.util.Vector ; 50 51 import org.openejb.OpenEJBException; 52 import org.openejb.util.SafeToolkit; 53 import org.w3c.dom.DOMException ; 54 import org.w3c.dom.Element ; 55 import org.w3c.dom.NamedNodeMap ; 56 import org.w3c.dom.Node ; 57 import org.w3c.dom.NodeList ; 58 import org.w3c.dom.Text ; 59 60 65 public class DomTools{ 66 67 public static final SafeToolkit toolkit = SafeToolkit.getToolkit("XML configuration loader"); 68 69 72 public static final String PROPERTIES = "properties"; 73 74 77 public static final String PROPERTY = "property"; 78 79 82 public static final String PROPERTY_NAME = "property-name"; 83 84 87 public static final String PROPERTY_VALUE = "property-value"; 88 89 90 91 public static Properties readProperties(Node node) { 92 Node propertiesElement = getChildElement(node, PROPERTIES); 93 94 if(propertiesElement == null) return new Properties (); 95 96 Node [] property = getChildElements(propertiesElement,PROPERTY); 97 Properties properties = new Properties (); 98 String name = null, value = null; 99 100 for (int i=0; i< property.length; i++){ 101 name = getChildElementPCData(property[i], PROPERTY_NAME); 102 value = getChildElementPCData(property[i], PROPERTY_VALUE); 103 if (name == null || value == null) continue; 104 properties.setProperty( name, value ); 105 } 106 return properties; 107 } 108 109 113 public static final boolean debug = false; 114 115 public static int debugRecursionDepth = 0; 116 117 118 130 protected static DomObject[] collectChildElementsByType(Node node, Class classType, String elementType) throws OpenEJBException{ 131 132 if (debug){ 133 debugRecursionDepth++; 134 for(int i=0;i<debugRecursionDepth;i++)System.out.print("\t"); 135 System.out.println(node.getNodeName()+"."+ elementType); 136 } 137 138 if (node == null) return null; 139 140 NodeList list = node.getChildNodes(); 141 Vector tmp = new Vector (); 142 Node child = null; 143 144 for (int i=0; i< list.getLength(); i++){ 145 child = list.item(i); 146 if (child.getNodeType() == Node.ELEMENT_NODE){ 147 Element element = (Element )child; 148 if (element.getTagName().equals(elementType)){ 149 DomObject info = (DomObject)toolkit.newInstance(classType); 150 tmp.addElement(info); 151 info.initializeFromDOM(element); 152 } 153 } 154 } 155 156 if (debug){ 157 debugRecursionDepth--; 158 } 159 160 DomObject[] domObjects = new DomObject[tmp.size()]; 161 tmp.copyInto(domObjects); 162 return domObjects; 163 } 164 165 177 protected static DomObject collectChildElementByType(Node node, Class classType, String elementType) throws OpenEJBException{ 178 try{ 179 180 181 if (debug){ 182 debugRecursionDepth++; 183 for(int i=0;i<debugRecursionDepth;i++)System.out.print("\t"); 184 System.out.println(node.getNodeName()+"."+ elementType); 185 } 186 187 NodeList list = node.getChildNodes(); 188 Node child = null; 189 DomObject domObject = null; 190 for (int i=0; i< list.getLength(); i++){ 191 child = list.item(i); 192 if (child.getNodeType() == Node.ELEMENT_NODE){ 193 Element element = (Element )child; 194 if (element.getTagName().equals(elementType)){ 195 domObject = (DomObject)toolkit.newInstance(classType); 196 domObject.initializeFromDOM(element); 197 break; 198 } 199 } 200 } 201 202 if (debug){ 203 debugRecursionDepth--; 204 } 205 206 return domObject; 207 } catch(Exception e){ 208 e.printStackTrace(); 209 throw new OpenEJBException(e); 210 } 211 212 } 213 214 222 protected static String [] getChildElementsPCData(Node node, String elementType) { 223 224 if (debug){ 225 debugRecursionDepth++; 226 for(int i=0;i<debugRecursionDepth;i++)System.out.print("\t"); 227 System.out.print(node.getNodeName()+"."+ elementType); 228 } 229 230 NodeList list = node.getChildNodes(); 231 232 Node child = null; 233 Vector tmp = new Vector (); 234 235 for (int i=0; i< list.getLength(); i++){ 236 child = list.item(i); 237 if (child.getNodeType() == Node.ELEMENT_NODE){ 238 Element element = (Element )child; 239 if (element.getTagName().equals(elementType)){ 240 tmp.addElement(getElementPCData(element)); 241 } 242 } 243 } 244 String [] pcdata = new String [tmp.size()]; 245 tmp.copyInto(pcdata); 246 247 if (debug){ 248 249 String tabs = ""; 250 for(int i=0;i<debugRecursionDepth;i++){tabs+="\t";} 251 System.out.println(".length = "+pcdata.length); 252 for(int i=0;i<pcdata.length;i++) System.out.println(tabs + node.getNodeName()+"."+ elementType + "["+ i +"] = " +pcdata[i]); 253 debugRecursionDepth--; 254 } 255 256 return pcdata; 257 } 258 259 267 protected static String getChildElementPCData(Node node, String elementType) { 268 269 if (debug){ 270 debugRecursionDepth++; 271 for(int i=0;i<debugRecursionDepth;i++)System.out.print("\t"); 272 System.out.print(node.getNodeName()+"."+ elementType); 273 } 274 275 NodeList list = node.getChildNodes(); 276 Node child = null; 277 String pcdata = null; 278 for (int i=0; i< list.getLength(); i++){ 279 child = list.item(i); 280 if (child.getNodeType() == Node.ELEMENT_NODE){ 281 Element element = (Element )child; 282 if (element.getTagName().equals(elementType)){ 283 pcdata = getElementPCData(element); 284 break; 285 } 286 } 287 } 288 289 if (debug){ 290 System.out.println(" = "+pcdata); 291 debugRecursionDepth--; 292 } 293 294 return pcdata; 295 } 296 297 303 protected static String getElementPCData(Node node) { 304 Node child = node.getFirstChild(); 305 if (child == null || child.getNodeType() != Node.TEXT_NODE) return null; 306 307 try{ 308 Text text = (Text )child; 309 String pcdata = text.getData(); 310 return (pcdata!=null)?pcdata.trim():null; 311 } catch (DOMException e) { 312 throw e; 313 } 314 } 315 316 322 protected static Properties getElementAttributes(Node node){ 323 NamedNodeMap nodeMap = node.getAttributes(); 324 int size = nodeMap.getLength(); 325 Properties attributes = new Properties (); 326 for(int i = 0; i < size; i++){ 327 node = nodeMap.item(i); 328 attributes.setProperty(node.getNodeName(), node.getNodeValue()); 329 } 330 return attributes; 331 } 332 333 340 protected static Node getChildElement(Node node, String childName) { 341 342 NodeList list = node.getChildNodes(); 343 Node child = null; 344 345 for (int i=0; i< list.getLength(); i++){ 346 child = list.item(i); 347 if (child.getNodeType() == Node.ELEMENT_NODE){ 348 Element element = (Element )child; 349 if (element.getTagName().equals(childName)) 350 return child; 351 } 352 } 353 return null; 354 } 355 356 363 protected static Node [] getChildElements(Node node, String childName) { 364 365 NodeList list = node.getChildNodes(); 366 Node child = null; 367 Vector tmp = new Vector (); 368 369 for (int i=0; i< list.getLength(); i++){ 370 child = list.item(i); 371 if (child.getNodeType() == Node.ELEMENT_NODE){ 372 Element element = (Element )child; 373 if (element.getTagName().equals(childName)){ 374 tmp.addElement(element); 375 } 376 } 377 } 378 379 Node [] children = new Node [tmp.size()]; 380 tmp.copyInto(children); 381 return children; 382 } 383 384 } | Popular Tags |