1 23 24 30 package org.enhydra.dods.xml; 31 32 import java.io.File ; 33 import java.io.FileOutputStream ; 34 import java.io.OutputStream ; 35 import java.util.Properties ; 36 import javax.xml.parsers.DocumentBuilder ; 37 import javax.xml.parsers.DocumentBuilderFactory ; 38 import javax.xml.transform.Transformer ; 39 import javax.xml.transform.TransformerFactory ; 40 import javax.xml.transform.dom.DOMSource ; 41 import javax.xml.transform.stream.StreamResult ; 42 import org.apache.xalan.processor.TransformerFactoryImpl; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Element ; 45 import org.w3c.dom.NodeList ; 46 47 public class XmlUtil { 48 49 protected String xmlFile; 50 protected Document document; 51 public XmlUtil(String xmlFile) throws XmlUtilException { 52 this.xmlFile = xmlFile; 53 try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 55 DocumentBuilder db = dbf.newDocumentBuilder(); 56 57 document = db.parse(xmlFile); 58 } catch (Exception e) { 59 e.printStackTrace(); 60 } 61 } 62 63 66 public void store() throws XmlUtilException { 67 store(null); 68 } 69 70 75 public void store(Properties properties) throws XmlUtilException { 76 store(null, properties); 77 } 78 79 85 public void store(String fileName, Properties properties) throws XmlUtilException { 86 TransformerFactory tf = null; 87 DOMSource src = null; 88 89 if (fileName == null) { 90 fileName = xmlFile; 91 } 92 tf = TransformerFactoryImpl.newInstance(); 93 src = new DOMSource (document.getDocumentElement()); 94 if (tf == null) { 95 return; 96 } 97 if (src == null) { 98 return; 99 } 100 OutputStream outputs = null; 101 102 try { 103 File f = new File (fileName); 104 File pf = f.getParentFile(); 105 106 if (pf != null) { 107 pf.mkdirs(); 108 } 109 outputs = new FileOutputStream (f.getPath()); 110 Transformer serializer = tf.newTransformer(); 111 112 if (properties != null) { 113 serializer.setOutputProperties(properties); 114 } 115 serializer.transform(src, new StreamResult (outputs)); 116 } catch (java.io.FileNotFoundException e) { 117 throw new XmlUtilException(e); 118 } catch (javax.xml.transform.TransformerConfigurationException e) { 119 throw new XmlUtilException(e); 120 } catch (javax.xml.transform.TransformerException e) { 121 throw new XmlUtilException(e); 122 } 123 } 124 125 133 public String getSingleTextNode(String key) throws XmlUtilException { 134 return getSingleTextNode(key, null); 135 } 136 137 148 public String getSingleTextNode(String key, int levelNodes[]) throws XmlUtilException { 149 String nodeValue = ""; 150 NodeList tagElems; 151 Element tagElem; 152 String tag; 153 String subKey; 154 int cuurentLevel = 0; 155 int pos = key.indexOf("/"); 156 157 if (pos < 0) { 158 return null; 159 } 160 if (pos == 0) { subKey = key.substring(pos + 1); 162 pos = subKey.indexOf("/"); 163 } else { 164 subKey = key; 165 } 166 try { 167 if (pos != -1) { 168 tag = subKey.substring(0, pos); 169 subKey = subKey.substring(pos + 1); 170 tagElems = document.getElementsByTagName(tag); 171 tagElem = (Element ) tagElems.item(0); 172 cuurentLevel++; 173 pos = subKey.indexOf("/"); 174 if (pos != -1) { 175 while (pos != -1) { 176 tag = subKey.substring(0, pos); 177 subKey = subKey.substring(pos + 1); 178 tagElems = tagElem.getElementsByTagName(tag); 179 if (levelNodes != null 180 && cuurentLevel < levelNodes.length) { 181 tagElem = (Element ) tagElems.item(levelNodes[cuurentLevel]); 182 } else { 183 tagElem = (Element ) tagElems.item(0); 184 } 185 if (tagElem == null) { 186 return null; 187 } 188 cuurentLevel++; 189 pos = subKey.indexOf("/"); 190 } 191 } 192 tagElems = tagElem.getElementsByTagName(subKey); 193 if (levelNodes != null && cuurentLevel < levelNodes.length) { 194 tagElem = (Element ) tagElems.item(levelNodes[cuurentLevel]); 195 } else { 196 tagElem = (Element ) tagElems.item(0); 197 } 198 tagElems = tagElem.getChildNodes(); 199 String text = ""; 200 201 for (int i = 0; i < tagElems.getLength(); i++) { 202 if (tagElems.item(i).getNodeType() == 3) { text += tagElems.item(i).getNodeValue(); 204 } 205 } 206 return text; 207 } else { 208 tagElems = document.getElementsByTagName(subKey); 209 if (levelNodes != null && cuurentLevel < levelNodes.length) { 210 tagElem = (Element ) tagElems.item(levelNodes[cuurentLevel]); 211 } else { 212 tagElem = (Element ) tagElems.item(0); 213 } 214 tagElems = tagElem.getChildNodes(); 215 String text = ""; 216 217 for (int i = 0; i < tagElems.getLength(); i++) { 218 if (tagElems.item(i).getNodeType() == 3) { text += tagElems.item(i).getNodeValue(); 220 } 221 } 222 return text; 223 } 224 } catch (NullPointerException ex) { 225 throw new XmlUtilException(ex); 226 } 227 } 228 229 238 public String getSingleAttribute(String key) throws XmlUtilException { 239 return getSingleAttribute(key, null); 240 } 241 242 254 public String getSingleAttribute(String key, int levelNodes[]) throws XmlUtilException { 255 String nodeValue = ""; 256 NodeList tagElems = null; 257 Element tagElem = null; 258 String tag; 259 String subKey; 260 String attrValue = null; 261 int cuurentLevel = 0; 262 263 int pos = key.indexOf("/"); 264 265 if (pos < 0) { 266 return null; 267 } 268 try { 269 if (pos == 0) { subKey = key.substring(pos + 1); 271 pos = subKey.indexOf("/"); 272 } else { 273 subKey = key; 274 } 275 if (pos != -1) { 276 tag = subKey.substring(0, pos); 277 subKey = subKey.substring(pos + 1); 278 tagElems = document.getElementsByTagName(tag); 279 tagElem = (Element ) tagElems.item(0); 280 cuurentLevel++; 281 pos = subKey.indexOf("/"); 282 if (pos != -1) { 283 while (pos != -1) { 284 tag = subKey.substring(0, pos); 285 subKey = subKey.substring(pos + 1); 286 tagElems = tagElem.getElementsByTagName(tag); 287 if (levelNodes != null 288 && cuurentLevel < levelNodes.length) { 289 tagElem = (Element ) tagElems.item(levelNodes[cuurentLevel]); 290 } else { 291 tagElem = (Element ) tagElems.item(0); 292 } 293 if (tagElem == null) { 294 return null; 295 } 296 cuurentLevel++; 297 pos = subKey.indexOf("/"); 298 } 299 } 300 pos = subKey.indexOf("@"); 301 tag = subKey.substring(0, pos); 302 if (pos != -1) { 303 tagElems = tagElem.getElementsByTagName(tag); 304 if (levelNodes != null && cuurentLevel < levelNodes.length) { 305 tagElem = (Element ) tagElems.item(levelNodes[cuurentLevel]); 306 } else { 307 tagElem = (Element ) tagElems.item(0); 308 } 309 String attrName = subKey.substring(pos + 1); 310 311 attrValue = tagElem.getAttribute(attrName); 312 } 313 } else { 314 pos = subKey.indexOf("@"); 315 tag = subKey.substring(0, pos); 316 if (pos != -1) { 317 tagElems = document.getElementsByTagName(tag); 318 if (levelNodes != null && cuurentLevel < levelNodes.length) { 319 tagElem = (Element ) tagElems.item(levelNodes[cuurentLevel]); 320 } else { 321 tagElem = (Element ) tagElems.item(0); 322 } 323 String attrName = subKey.substring(pos + 1); 324 325 attrValue = tagElem.getAttribute(attrName); 326 } 327 } 328 } catch (NullPointerException ex) { 329 throw new XmlUtilException(ex); 330 } 331 if (attrValue.equals("")) { 332 attrValue = null; 333 } 334 return attrValue; 335 } 336 337 349 public void setSingleAttribute(String key, int levelNodes[], String value) throws XmlUtilException { 350 String nodeValue = ""; 351 NodeList tagElems = null; 352 Element tagElem = null; 353 String tag; 354 String subKey; 355 int cuurentLevel = 0; 356 357 int pos = key.indexOf("/"); 358 359 if (pos < 0) { 360 return; 361 } 362 try { 363 if (pos == 0) { subKey = key.substring(pos + 1); 365 pos = subKey.indexOf("/"); 366 } else { 367 subKey = key; 368 } 369 if (pos != -1) { 370 tag = subKey.substring(0, pos); 371 subKey = subKey.substring(pos + 1); 372 tagElems = document.getElementsByTagName(tag); 373 tagElem = (Element ) tagElems.item(0); 374 cuurentLevel++; 375 pos = subKey.indexOf("/"); 376 if (pos != -1) { 377 while (pos != -1) { 378 tag = subKey.substring(0, pos); 379 subKey = subKey.substring(pos + 1); 380 tagElems = tagElem.getElementsByTagName(tag); 381 if (levelNodes != null 382 && cuurentLevel < levelNodes.length) { 383 tagElem = (Element ) tagElems.item(levelNodes[cuurentLevel]); 384 } else { 385 tagElem = (Element ) tagElems.item(0); 386 } 387 if (tagElem == null) { 388 return; 389 } 390 cuurentLevel++; 391 pos = subKey.indexOf("/"); 392 } 393 } 394 pos = subKey.indexOf("@"); 395 tag = subKey.substring(0, pos); 396 if (pos != -1) { 397 tagElems = tagElem.getElementsByTagName(tag); 398 if (levelNodes != null && cuurentLevel < levelNodes.length) { 399 tagElem = (Element ) tagElems.item(levelNodes[cuurentLevel]); 400 } else { 401 tagElem = (Element ) tagElems.item(0); 402 } 403 String attrName = subKey.substring(pos + 1); 404 405 tagElem.setAttribute(attrName, value); 406 } 407 } else { 408 pos = subKey.indexOf("@"); 409 tag = subKey.substring(0, pos); 410 if (pos != -1) { 411 tagElems = document.getElementsByTagName(tag); 412 if (levelNodes != null && cuurentLevel < levelNodes.length) { 413 tagElem = (Element ) tagElems.item(levelNodes[cuurentLevel]); 414 } else { 415 tagElem = (Element ) tagElems.item(0); 416 } 417 String attrName = subKey.substring(pos + 1); 418 419 tagElem.setAttribute(attrName, value); 420 } 421 } 422 } catch (NullPointerException ex) { 423 throw new XmlUtilException(ex); 424 } 425 } 426 427 436 public int getNumberOfNodes(String key) throws XmlUtilException { 437 return getNumberOfNodes(key, null); 438 } 439 440 452 public int getNumberOfNodes(String key, int levelNodes[]) throws XmlUtilException { 453 String nodeValue = ""; 454 NodeList tagElems; 455 Element tagElem; 456 String tag; 457 String subKey; 458 int cuurentLevel = 0; 459 int pos = key.indexOf("/"); 460 461 if (pos < 0) { 462 return 0; 463 } 464 if (pos == 0) { subKey = key.substring(pos + 1); 466 pos = subKey.indexOf("/"); 467 } else { 468 subKey = key; 469 } 470 try { 471 if (pos != -1) { 472 tag = subKey.substring(0, pos); 473 subKey = subKey.substring(pos + 1); 474 tagElems = document.getElementsByTagName(tag); 475 tagElem = (Element ) tagElems.item(0); 476 cuurentLevel++; 477 pos = subKey.indexOf("/"); 478 if (pos != -1) { 479 while (pos != -1) { 480 tag = subKey.substring(0, pos); 481 subKey = subKey.substring(pos + 1); 482 tagElems = tagElem.getElementsByTagName(tag); 483 if (levelNodes != null 484 && cuurentLevel < levelNodes.length) { 485 tagElem = (Element ) tagElems.item(levelNodes[cuurentLevel]); 486 } else { 487 tagElem = (Element ) tagElems.item(0); 488 } 489 if (tagElem == null) { 490 return 0; 491 } 492 cuurentLevel++; 493 pos = subKey.indexOf("/"); 494 } 495 } 496 tagElems = tagElem.getElementsByTagName(subKey); 497 return tagElems.getLength(); 498 } else { 499 tagElems = document.getElementsByTagName(subKey); 500 return tagElems.getLength(); 501 } 502 } catch (NullPointerException ex) { 503 throw new XmlUtilException(ex); 504 } 505 } 506 507 549 static public String getSingleTextNode(String xmlfile, String key, int levelNodes[]) throws XmlUtilException { 550 XmlUtil xml = new XmlUtil(xmlfile); 551 552 return xml.getSingleTextNode(key, levelNodes); 553 } 554 555 595 static public String getSingleAttribute(String xmlfile, String key, int levelNodes[]) throws XmlUtilException { 596 XmlUtil xml = new XmlUtil(xmlfile); 597 598 return xml.getSingleAttribute(key, levelNodes); 599 } 600 601 614 static public void setSingleAttribute(String xmlfile, String key, int levelNodes[], String value, Properties prop) 615 throws XmlUtilException { 616 XmlUtil xml = new XmlUtil(xmlfile); 617 618 xml.setSingleAttribute(key, levelNodes, value); 619 xml.store(prop); 620 } 621 622 663 static public int getNumberOfNodes(String xmlfile, String key, int levelNodes[]) 664 throws XmlUtilException { 665 XmlUtil xml = new XmlUtil(xmlfile); 666 667 return xml.getNumberOfNodes(key, levelNodes); 668 } 669 670 673 public static void main(String [] args) { 674 int levelNodes[] = {0, 0, 1}; 675 String key = "/databaseManager/database/objectId@name"; 676 String value = "webdocwf"; 677 678 try { 679 XmlUtil xml = new XmlUtil("discRackDods.xml"); 680 681 xml.setSingleAttribute(key, levelNodes, value); 682 System.out.println(key + " = " 683 + xml.getSingleAttribute(key, levelNodes)); 684 xml.store(); 685 } catch (XmlUtilException e) { 686 e.printStackTrace(); 687 } 688 } 689 } 690 | Popular Tags |