1 17 18 19 20 package org.apache.lenya.xml; 21 22 import java.io.StringReader ; 23 import java.util.Vector ; 24 25 import org.apache.log4j.Category; 26 import org.w3c.dom.Attr ; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 32 36 public class DOMUtil { 37 static Category log = Category.getInstance(DOMUtil.class); 38 39 DOMParserFactory dpf = null; 40 41 XPointerFactory xpf = null; 42 43 46 public DOMUtil() { 47 dpf = new DOMParserFactory(); 48 xpf = new XPointerFactory(); 49 } 50 51 55 public static void main(String [] args) { 56 try { 57 DOMUtil du = new DOMUtil(); 58 Document document = du 59 .create("<?xml version=\"1.0\"?><Artikel><Datum><Monat Name=\"Juli\"/><Tag>23</Tag></Datum><Content/></Artikel>"); 60 new DOMWriter(System.out).printWithoutFormatting(document); 61 du.setElementValue(document, "/Artikel/Datum/Tag", "25"); 62 du.setElementValue(document, "/Artikel/Datum/Monat", "7"); 63 du.setElementValue(document, "/Artikel/Datum/Monat", "9"); 64 du.setElementValue(document, "/Artikel/Datm/Mont", "13"); 65 du.setAttributeValue(document, "/Artikel/Datum/Monat/@Name", "Oktober"); 66 du.setAttributeValue(document, "/Artikel/Datu/Monat/@Nam", "August"); 67 du.setElementValue(document, "/Artikel/Datu/Monat", "8"); 68 du.addElement(document, "/Artikel/Datum/Tag", "26"); 69 du.setElementValue(document, "/Artikel/Datum/Tag", "24"); 70 71 new DOMWriter(System.out).printWithoutFormatting(document); 72 System.out.print("\n"); 73 System.out.print("\n"); 74 75 String [] elements = du.getAllElementValues(document, new XPath("/Artikel/Datum/Monat")); 76 77 for (int i = 0; i < elements.length; i++) { 78 System.out.println("Elements=" + elements[i]); 79 } 80 81 System.out.print("\n"); 82 System.out.println("Datum/Monat=" 83 + du.getElementValue(document.getDocumentElement(), new XPath("Datum/Monat"))); 84 System.out.println("Datm=" 85 + du.getElementValue(document.getDocumentElement(), new XPath("Datm"))); 86 87 System.out.println("Datum/Monat/@Name=" 88 + du.getAttributeValue(document.getDocumentElement(), new XPath( 89 "Datum/Monat/@Name"))); 90 } catch (Exception e) { 91 System.err.println(e); 92 } 93 } 94 95 104 public Document create(String xml) throws Exception { 105 return dpf.getDocument(new StringReader (xml)); 106 } 107 108 120 public Element [] select(Document document, String xpath) throws Exception { 121 log.debug(".select(): " + xpath); 122 123 Vector nodes = xpf.select(document, "xpointer(" + xpath + ")"); 124 Element [] elements = new Element [nodes.size()]; 125 126 for (int i = 0; i < nodes.size(); i++) { 127 elements[i] = (Element ) nodes.elementAt(i); 128 } 129 130 return elements; 131 } 132 133 143 public void replaceText(Element element, String text) { 144 NodeList nl = element.getChildNodes(); 145 146 for (int i = nl.getLength() - 1; i >= 0; i--) { 147 element.removeChild(nl.item(i)); 148 } 149 150 element.appendChild(dpf.newTextNode(element.getOwnerDocument(), text)); 151 } 152 153 164 public String getElementValue(Document document, XPath xpath) throws Exception { 165 return getElementValue(document.getDocumentElement(), xpath); 166 } 167 168 179 public String getElementValue(Element element, XPath xpath) throws Exception { 180 String value = ""; 181 NodeList nl = getElement(element, xpath).getChildNodes(); 182 183 for (int i = 0; i < nl.getLength(); i++) { 184 short nodeType = nl.item(i).getNodeType(); 185 186 if (nodeType == Node.TEXT_NODE) { 187 value = value + nl.item(i).getNodeValue(); 188 } else { 189 log.warn("XPath " + xpath + " contains node types other than just TEXT_NODE"); 190 } 191 } 192 193 return value; 194 } 195 196 211 public boolean elementExists(Element element, XPath xpath) throws Exception { 212 log.debug(xpath); 213 214 if (xpath.parts.length > 0) { 215 NodeList nl = element.getElementsByTagName(xpath.parts[0]); 216 217 if (nl.getLength() == 0) { 218 return false; 219 } else if (nl.getLength() == 1) { 220 return true; 221 } 222 } 223 return false; 224 } 225 226 233 public Element getElement(Element element, XPath xpath) throws Exception { 234 log.debug(xpath); 235 236 if (xpath.parts.length > 0) { 237 NodeList nl = element.getElementsByTagName(xpath.parts[0]); 238 239 if (nl.getLength() == 0) { 240 throw new Exception ("There are no elements with Name \"" + xpath.parts[0] + "\"."); 241 } else if (nl.getLength() == 1) { 242 log.debug("There is one element with Name \"" + xpath.parts[0] + "\" (" 243 + xpath.parts.length + ")."); 244 245 if (xpath.parts.length == 1) { 246 return (Element ) nl.item(0); 247 } else { 248 String newXPathString = xpath.parts[1]; 249 250 for (int i = 2; i < xpath.parts.length; i++) { 251 newXPathString = newXPathString + "/" + xpath.parts[i]; 252 } 253 254 return getElement((Element ) nl.item(0), new XPath(newXPathString)); 255 } 256 } else { 257 throw new Exception ("There are more elements than one with Name \"" 258 + xpath.parts[0] + "\"."); 259 } 260 } 261 262 return null; 263 } 264 265 279 public Element [] getAllElements(Document document, XPath xpath) throws Exception { 280 Vector nodes = xpf.select(document.getDocumentElement(), "xpointer(" + xpath.toString() 281 + ")"); 282 Element [] elements = new Element [nodes.size()]; 283 284 for (int i = 0; i < nodes.size(); i++) { 285 elements[i] = (Element ) nodes.elementAt(i); 286 } 287 288 return elements; 289 } 290 291 301 public String [] getAllElementValues(Document document, XPath xpath) throws Exception { 302 Vector nodes = xpf.select(document.getDocumentElement(), "xpointer(" + xpath.toString() 303 + ")"); 304 log.debug("n elements " + nodes.size()); 305 306 String [] values = new String [nodes.size()]; 307 308 for (int i = 0; i < nodes.size(); i++) { 309 values[i] = getElementValue((Element ) nodes.elementAt(i)); 310 } 311 312 return values; 313 } 314 315 331 public String getElementValue(Element element) throws Exception { 332 String value = ""; 333 NodeList nl = element.getChildNodes(); 334 335 for (int i = 0; i < nl.getLength(); i++) { 336 short nodeType = nl.item(i).getNodeType(); 337 338 if (nodeType == Node.TEXT_NODE) { 339 value = value + nl.item(i).getNodeValue(); 340 } else { 341 log.warn("Element " + element.getNodeName() 342 + " contains node types other than just TEXT_NODE"); 343 } 344 } 345 346 return value; 347 } 348 349 366 public String getAttributeValue(Element element, XPath xpath) throws Exception { 367 Element el = getElement(element, new XPath(xpath.getElementName())); 368 369 return el.getAttribute(xpath.getName()); 370 } 371 372 390 public void setElementValue(Document document, String xpath, String value) throws Exception { 391 Element [] elements = select(document, xpath); 392 393 if (elements.length >= 1) { 394 if (elements.length > 1) { 395 log.warn("There are more elements than one with XPath \"" + xpath 396 + "\". The value of the first element will be replaced"); 397 } 398 399 replaceText(elements[0], value); 400 } else { 401 XPath xp = new XPath(xpath); 402 log.warn("XPath does not exist, but will be created: " + xp); 403 404 Element element = (Element ) createNode(document, xp); 405 replaceText(element, value); 406 } 407 } 408 409 420 public void addElement(Document document, String xpath, String value) throws Exception { 421 XPath xp = new XPath(xpath); 422 Node parent = createNode(document, xp.getParent()); 423 Element element = dpf.newElementNode(document, xp.getName()); 424 parent.appendChild(element); 425 426 if (value != null) { 427 element.appendChild(dpf.newTextNode(element.getOwnerDocument(), value)); 428 } 429 } 430 431 441 public void setAttributeValue(Document document, String xpath, String value) throws Exception { 442 Vector nodes = xpf.select(document, "xpointer(" + xpath + ")"); 443 444 if (nodes.size() >= 1) { 445 Attr attribute = (Attr ) nodes.elementAt(0); 446 attribute.setValue(value); 447 } else { 448 XPath xp = new XPath(xpath); 449 log.debug("XPath does not exist, but will be created: " + xp); 450 451 Attr attribute = (Attr ) createNode(document, xp); 452 attribute.setValue(value); 453 } 454 } 455 456 469 public void setValue(Document document, XPath xpath, String value) throws Exception { 470 short type = xpath.getType(); 471 472 if (type == Node.ATTRIBUTE_NODE) { 473 setAttributeValue(document, xpath.toString(), value); 474 } else if (type == Node.ELEMENT_NODE) { 475 setElementValue(document, xpath.toString(), value); 476 } else { 477 log.error("No such type: " + type); 478 } 479 } 480 481 492 public Node createNode(Document document, XPath xpath) throws Exception { 493 log.debug(xpath); 494 495 Node node = null; 496 Vector nodes = xpf.select(document, "xpointer(" + xpath + ")"); 497 498 if (nodes.size() >= 1) { 499 node = (Node ) nodes.elementAt(0); 500 } else { 501 Node parentNode = createNode(document, xpath.getParent()); 502 503 if (xpath.getType() == Node.ATTRIBUTE_NODE) { 504 ((Element ) parentNode).setAttribute(xpath.getNameWithoutPredicates(), "null"); 505 node = ((Element ) parentNode).getAttributeNode(xpath.getNameWithoutPredicates()); 506 } else { 507 node = dpf.newElementNode(document, xpath.getNameWithoutPredicates()); 508 parentNode.appendChild(node); 509 } 510 } 511 512 return node; 513 } 514 } | Popular Tags |