1 23 24 28 package com.sun.enterprise.admin.util; 29 30 import org.w3c.dom.*; 31 import javax.xml.parsers.*; 32 import java.lang.reflect.*; 33 import java.io.*; 34 import javax.xml.transform.stream.*; 35 import javax.xml.transform.*; 36 import javax.xml.transform.dom.*; 37 38 39 import org.xml.sax.helpers.DefaultHandler ; 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.SAXException ; 42 43 74 75 class NOOPHandler extends DefaultHandler { 76 77 static String _dtdFileName; 78 79 NOOPHandler(String dtdFileName) { 80 super(); 81 _dtdFileName = dtdFileName; 82 } 83 84 public InputSource resolveEntity(String publicId, 85 String systemId) throws SAXException 86 { 87 InputSource is = null; 88 try { 89 is = new InputSource (new FileInputStream(_dtdFileName)); 90 } catch(Exception e) { 91 throw new SAXException ("cannot resolve dtd", e); 92 } 93 return is; 94 } 95 96 } 97 98 public class XMLAlterUtil { 99 100 106 public static void main(String [] args) { 107 try { 108 if (args.length != 4) { 110 System.out.println("Proper Usage: java XMLAlterUtil BASE_DOCUMENT_TO_CHANGE BASE_DOCUMENT_DTD CHANGES_DOCUMENT OUTPUT_DOCUMENT"); 111 System.exit(1); 112 } 113 114 String baseXML=args[0]; 115 String baseDTD=args[1]; 116 String changeXML=args[2]; 117 String outXML=args[3]; 118 119 if (System.getProperty("Debug") != null) { 120 bDebug=true; 121 } 122 123 XMLAlterUtil xau=new XMLAlterUtil(); 124 125 if (bDebug) System.out.println("parsing - " + baseXML); 127 Document baseDoc=null; 128 if(baseDTD.equals("null")) { 129 baseDoc=xau.readDOM(baseXML); 130 } else { 131 baseDoc=xau.readDOM(baseXML, baseDTD); 132 } 133 134 if (bDebug) System.out.println("parsing - " + changeXML); 135 Document changeDoc=xau.readDOM(changeXML); 136 137 xau.alterDOM(baseDoc, changeDoc); 139 140 DocumentType baseDocType=baseDoc.getDoctype(); 142 143 String doctype_system=null, doctype_public=null; 144 if (baseDocType != null) { 145 doctype_system=baseDocType.getSystemId(); 146 doctype_public=baseDocType.getPublicId(); 147 } 148 149 xau.writeDOM(baseDoc, outXML, doctype_system, doctype_public); 150 151 } catch (Exception e) { 152 e.printStackTrace(); 153 } 154 } 155 156 157 158 164 public void alterDOM(Document baseDoc, Document changeDoc) throws Exception { 165 166 Element baseRoot=baseDoc.getDocumentElement(); 168 Element changeRoot=changeDoc.getDocumentElement(); 169 170 NodeList nlx, bnl, bnlx; 171 Node bNode, bNodex; 172 Element cElement, bElement, bElementx; 173 String elementName, type, dnType, dnName, dnValue, dnModifier, bAttrValue; 174 175 NodeList nl=changeRoot.getElementsByTagName("alteration"); 178 boolean matched=false; 179 180 if (bDebug) System.out.println("\n** Base Root Node Name is '" + baseRoot.getNodeName() + "' **"); 181 182 for(int ii=0; ii < nl.getLength(); ii++) { 183 184 cElement=(Element)nl.item(ii); 186 187 matched=false; 189 elementName=cElement.getAttribute("elementName"); 190 type=cElement.getAttribute("type"); 191 dnType=cElement.getAttribute("dnType"); 192 dnName=cElement.getAttribute("dnName"); 193 dnValue=cElement.getAttribute("dnValue"); 194 dnModifier=cElement.getAttribute("dnModifier"); 195 196 if (bDebug) System.out.println("\n** Trying to match an element '" + elementName + "' with dnType='" 197 + dnType + "', dnName='" + dnName + "' and dnValue='" + dnValue + "'"); 198 199 if (elementName.equals(baseRoot.getNodeName())) { 202 bnl=baseDoc.getElementsByTagName(elementName); 204 } else { 205 bnl=baseRoot.getElementsByTagName(elementName); 206 } 207 208 if (bDebug) System.out.println("\n\tFound " + bnl.getLength() + " elements to match to."); 209 for (int jj=0; jj < bnl.getLength(); jj++) { 210 211 if (bnl.item(jj) instanceof Element) { 213 214 bElement=(Element)bnl.item(jj); 216 217 if (dnType.equals("attribute")) { 219 221 bAttrValue=bElement.getAttribute(dnName); 224 225 if ( matchValue(bAttrValue, dnValue, dnModifier) ) { 226 if (bDebug) System.out.println("\n\tFound match on Attibute! Executing alteration..."); 228 matched=true; 229 executeAlteration(bElement, cElement); 230 } 231 232 } else if(dnType.equals("text")) { 233 bnlx=bElement.getChildNodes(); 235 for (int mm=0; mm < bnlx.getLength(); mm++) { 236 if (bnlx.item(mm) instanceof Text) { 237 if (bDebug) System.out.println("Text - '" + bnlx.item(mm).getNodeValue() + "'"); 238 239 if ( matchValue(bnlx.item(mm).getNodeValue(), dnValue, dnModifier) ) { 240 if (bDebug) System.out.println("\n\tFound match on Text! Executing alteration..."); 242 matched=true; 243 executeAlteration(bElement, cElement); 244 } 245 } 246 } 247 248 } else if(dnType.equals("")) { 249 if (bDebug) System.out.println("\n\tMatch on All! Executing alteration..."); 251 matched=true; 252 executeAlteration(bElement, cElement); 253 } 254 } 255 256 if (type.equals("FIRST_OCCURRENCE") && matched) { 258 break; 260 } 261 } 262 } 263 } 264 265 266 276 public boolean matchValue(String targetString, String dnValue, String dnModifier) { 277 boolean bRet=false; 278 if (dnModifier.equals("startsWith")) { 279 bRet=targetString.startsWith(dnValue); 281 } else if (dnModifier.equals("endsWith")) { 282 bRet=targetString.endsWith(dnValue); 284 285 } else if (dnModifier.equals("contains")) { 286 bRet=targetString.indexOf(dnValue) >= 0 ? true : false; 288 289 } else { 290 bRet=targetString.equals(dnValue); 292 } 293 return bRet; 294 } 295 296 297 304 public void executeAlteration(Element bElement, Element cElement) throws Exception { 305 306 NodeList nlx=cElement.getChildNodes(); 309 NodeList nlChild; 310 Element cElementx; 311 312 for(int kk=0; kk < nlx.getLength(); kk++) { 314 if (nlx.item(kk) instanceof Element) { 316 cElementx=(Element)nlx.item(kk); 317 if (bDebug) System.out.println("\n\t* Performing Action - " + cElementx.getTagName()); 318 319 if (cElementx.getTagName().equals("addAttribute") || 321 cElementx.getTagName().equals("changeAttribute") ) { 322 323 bElement.setAttribute(cElementx.getAttribute("name"),cElementx.getAttribute("value")); 325 326 } else if (cElementx.getTagName().equals("prefixAttribute") || 327 cElementx.getTagName().equals("suffixAttribute")) { 328 String attName=cElementx.getAttribute("name"); 329 330 String newValue=cElementx.getAttribute("value"); 332 333 if(bElement.hasAttribute(attName)) { 335 if(cElementx.getTagName().equals("prefixAttrbute")) { 337 newValue=newValue + bElement.getAttribute(attName) ; 339 } else { 340 newValue=bElement.getAttribute(attName) + newValue; 342 } 343 } 344 if (bDebug) System.out.println("\n\t" + cElementx.getTagName() + " attribute:" + attName 345 + " final value is " + newValue); 346 bElement.setAttribute(attName, newValue); 348 349 } else if (cElementx.getTagName().equals("deleteAttrbute")) { 350 bElement.removeAttribute(cElementx.getAttribute("name")); 352 353 } else if (cElementx.getTagName().equals("removeElement")) { 354 bElement.getParentNode().removeChild(bElement); 356 break; 359 } else if (cElementx.getTagName().equals("addElement")) { 360 Node importedNode=null; 363 364 String docFragFile=cElementx.getAttribute("file"); 365 if (!docFragFile.equals("")) { 366 try { 368 Document docFrag=readDOM(docFragFile); 369 importedNode=bElement.getOwnerDocument().importNode(docFrag.getDocumentElement(),true); 370 } catch (Exception ee) { 372 if (bDebug) System.out.println("Exception encounted when reading/inserting fragment '" + docFragFile + "'***"); 373 throw ee; 374 } 375 376 } else { 377 NodeList nlxx=cElementx.getChildNodes(); 379 380 for(int ll=0; ll < nlxx.getLength(); ll++) { 382 if (nlxx.item(ll) instanceof Element) { 384 importedNode=bElement.getOwnerDocument().importNode(nlxx.item(ll),true); 385 } 387 } 388 } 389 390 String location=cElementx.getAttribute("location"); 393 String dnType=cElementx.getAttribute("dnType"); 394 String dnName=cElementx.getAttribute("dnName"); 395 String eName="null"; 396 Node lNode=null; 397 Element lElement=null, nElement=null; 398 399 if (bDebug) System.out.println("\n\t\tAddElement Match with location='" + location + "' with dnType='" + dnType + "' and dnName='" + dnName + "'"); 400 401 if (location.equals("first")) { 402 bElement.insertBefore(importedNode,bElement.getFirstChild()); 404 405 } else if (location.equals("before")) { 406 if (dnType.equals("element")) { 408 lNode=bElement.getFirstChild(); 409 while (lNode != null) { 410 if (lNode instanceof Element) { 411 lElement=(Element)lNode; 412 if (lElement.getNodeName().equals(dnName)) { 413 break; 415 } 416 } 417 418 lNode=lNode.getNextSibling(); 420 } 421 422 bElement.insertBefore(importedNode,lNode); 424 425 } else { 426 throw new UnsupportedOperationException ("Only addElement that has a location set to 'before' only supports a dnType of 'element'"); 427 } 428 429 } else if (location.equals("after")) { 430 if (dnType.equals("element")) { 432 lNode=bElement.getFirstChild(); 433 boolean loop=true; 434 435 while (lNode != null) { 437 if (lNode instanceof Element) { 439 lElement=(Element)lNode; 440 if (lElement.getNodeName().equals(dnName)) { 441 if (bDebug) System.out.println("\n\t\t\tMatch found on element for dnName='" + dnName + "', look for next element that is different"); 442 443 lNode=lNode.getNextSibling(); 446 while(lNode != null) { 447 if (lNode instanceof Element && !((Element)lNode).getNodeName().equals(dnName)) { 448 break; } 450 lNode=lNode.getNextSibling(); 451 } 452 453 if (lNode != null && lNode instanceof Element) eName=lNode.getNodeName(); 455 if (bDebug) System.out.println("\n\t\t\tFound last item of type '" + lElement.getNodeName() 456 + "' with nextSibling being '" + eName + "'"); 457 458 break; } 460 } 461 462 lNode=lNode.getNextSibling(); 464 } 465 466 bElement.insertBefore(importedNode,lNode); 468 469 } else { 470 throw new UnsupportedOperationException ("Only addElement that has a location set to 'after' only supports a dnType of 'element'"); 471 } 472 473 } else { 474 bElement.appendChild(importedNode); 476 } 477 } else if (cElementx.getTagName().equals("addTextToElement")) { 478 481 System.out.println("******** in addTextTOElement **********"); 482 483 String location=cElementx.getAttribute("location"); 485 String dataValue=cElementx.getAttribute("value"); 486 487 488 nlChild=bElement.getChildNodes(); 489 for (int mm=0; mm < nlChild.getLength(); mm++) { 490 if (nlChild.item(mm) instanceof Text) { 491 if (bDebug) System.out.println("addTextToElement - Text - " + dataValue + " - " + location + " - " + nlChild.item(mm).getNodeValue() + "'"); 493 if(location.equals("before")) { 495 nlChild.item(mm).setNodeValue(dataValue + nlChild.item(mm).getNodeValue()); 496 } else { 497 nlChild.item(mm).setNodeValue(nlChild.item(mm).getNodeValue() + dataValue); 498 } 499 break; 501 } 502 } 503 } 504 } 505 } 506 } 507 508 509 515 public void writeDOM(Document doc, String file, String doctype_system, String doctype_public) throws Exception { 516 DOMSource domSource=new DOMSource(doc); 517 StreamResult sr=new StreamResult(new File(file)); 518 TransformerFactory tf=TransformerFactory.newInstance(); 519 Transformer t=tf.newTransformer(); 520 t.setOutputProperty(OutputKeys.METHOD,"xml"); 521 t.setOutputProperty(OutputKeys.INDENT,"no"); 522 523 if (doctype_public !=null) t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype_public); 524 if (doctype_system !=null) t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype_system); 525 t.transform(domSource, sr); 526 } 527 528 529 536 public Document readDOM(String file) throws Exception { 537 DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); 538 DocumentBuilder db=dbf.newDocumentBuilder(); 539 return db.parse(file); 540 } 541 542 public Document readDOM(String file, String dtd) throws Exception { 543 DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); 544 DocumentBuilder db=dbf.newDocumentBuilder(); 545 db.setEntityResolver(new NOOPHandler(dtd)); 546 return db.parse(file); 547 } 548 549 550 private static boolean bDebug=false; 552 553 } 554 | Popular Tags |