1 19 20 package org.netbeans.modules.j2ee.ddloaders.common.xmlutils; 21 22 import org.w3c.dom.Element ; 23 import org.w3c.dom.NodeList ; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.CharacterData ; 26 import java.util.*; 27 28 import org.openide.NotifyDescriptor; 29 import org.openide.util.NbBundle; 30 import org.openide.DialogDisplayer; 31 32 36 37 public class XMLJ2eeUtils { 38 39 44 public static void updateDocument(javax.swing.text.Document doc, String newDoc, String prefixMark) throws javax.swing.text.BadLocationException { 45 int origLen = doc.getLength(); 46 String origDoc = doc.getText(0, origLen);; 47 int prefixInd=0; 48 if (prefixMark!=null) { 49 prefixInd = origDoc.indexOf(prefixMark); 50 if (prefixInd>0) { 51 origLen-=prefixInd; 52 origDoc=doc.getText(prefixInd,origLen); 53 } 54 else { 55 prefixInd=0; 56 } 57 int prefixIndNewDoc=newDoc.indexOf(prefixMark); 58 if (prefixIndNewDoc>0) 59 newDoc=newDoc.substring(prefixIndNewDoc); 60 } 61 int newLen = newDoc.length(); 63 64 if (origDoc.equals(newDoc)) { 65 return; 67 } 68 69 final int granularity = 20; 70 71 int prefix = -1; 72 int postfix = -1; 73 String toInsert = newDoc; 74 75 if ((origLen > granularity) && (newLen > granularity)) { 76 int pos1 = 0; 77 int len = Math.min(origLen, newLen); 78 for (;;) { 80 if (origDoc.regionMatches(pos1, newDoc, pos1, granularity)) { 81 pos1 += granularity; 82 if (pos1 + granularity >= len) { 83 break; 84 } 85 } 86 else { 87 break; 88 } 89 } 90 if (pos1 > 0) 91 prefix = pos1; 92 93 pos1 = origLen - granularity; 94 int pos2 = newLen - granularity; 95 for (;;) { 96 if (origDoc.regionMatches(pos1, newDoc, pos2, granularity)) { 97 pos1 -= granularity; 98 pos2 -= granularity; 99 if (pos1 < 0) { 100 pos1 += granularity; 101 break; 102 } 103 if (pos2 < 0) { 104 pos2 += granularity; 105 break; 106 } 107 } 108 else { 109 pos1 += granularity; 110 pos2 += granularity; 111 break; 112 } 113 } 114 if (pos1 < origLen - granularity) { 115 postfix = pos1; 116 } 117 } 118 119 if ((prefix != -1) && (postfix != -1)) { 120 if (postfix < prefix) { 121 postfix = prefix; 122 } 123 124 int delta = (prefix + (origLen - postfix) - newLen); 125 if (delta > 0) { 126 postfix += delta; 127 } 128 } 129 130 int removeBeginIndex = (prefix == -1) ? 0 : prefix; 131 int removeEndIndex = (postfix == -1) ? origLen - 1 : postfix; 132 133 doc.remove(prefixInd+removeBeginIndex, removeEndIndex - removeBeginIndex); 134 135 if (toInsert.length() > 0) { 136 int p1 = (prefix == -1) ? 0 : prefix; 137 int p2 = toInsert.length(); 138 if (postfix != -1) 139 p2 = p2 - (origLen - postfix); 140 141 if (p2 > p1) { 142 toInsert = toInsert.substring(p1, p2); 143 doc.insertString(prefixInd+removeBeginIndex, toInsert, null); 144 } 145 } 146 } 147 159 public static void replaceDocument(javax.swing.text.Document doc, String newDoc, String prefixMark) throws javax.swing.text.BadLocationException { 160 int origLen = doc.getLength(); 161 String origDoc = doc.getText(0, origLen); 162 int prefixInd=0; 163 if (prefixMark!=null) { 164 prefixInd = origDoc.indexOf(prefixMark); 165 if (prefixInd>0) { 166 origLen-=prefixInd; 167 origDoc=doc.getText(prefixInd,origLen); 168 } 169 else { 170 prefixInd=0; 171 } 172 int prefixIndNewDoc=newDoc.indexOf(prefixMark); 173 if (prefixIndNewDoc>0) 174 newDoc=newDoc.substring(prefixIndNewDoc); 175 } 176 newDoc=filterEndLines(newDoc); 177 int newLen = newDoc.length(); 178 179 if (origDoc.equals(newDoc)) { 180 return; 182 } 183 184 final int granularity = 20; 185 186 int prefix = -1; 187 int postfix = -1; 188 String toInsert = newDoc; 189 190 if ((origLen > granularity) && (newLen > granularity)) { 191 int pos1 = 0; 192 int len = Math.min(origLen, newLen); 193 for (;;) { 195 if (origDoc.regionMatches(pos1, newDoc, pos1, granularity)) { 196 pos1 += granularity; 197 if (pos1 + granularity >= len) { 198 break; 199 } 200 } 201 else { 202 break; 203 } 204 } 205 if (pos1 > 0) 206 prefix = pos1; 207 208 pos1 = origLen - granularity; 209 int pos2 = newLen - granularity; 210 for (;;) { 211 if (origDoc.regionMatches(pos1, newDoc, pos2, granularity)) { 212 pos1 -= granularity; 213 pos2 -= granularity; 214 if (pos1 < 0) { 215 pos1 += granularity; 216 break; 217 } 218 if (pos2 < 0) { 219 pos2 += granularity; 220 break; 221 } 222 } 223 else { 224 pos1 += granularity; 225 pos2 += granularity; 226 break; 227 } 228 } 229 if (pos1 < origLen - granularity) { 230 postfix = pos1; 231 } 232 } 233 234 if ((prefix != -1) && (postfix != -1)) { 235 if (postfix < prefix) { 236 postfix = prefix; 237 } 238 239 int delta = (prefix + (origLen - postfix) - newLen); 240 if (delta > 0) { 241 postfix += delta; 242 } 243 } 244 245 int removeBeginIndex = (prefix == -1) ? 0 : prefix; 246 int removeEndIndex; 247 if (postfix == -1){ 248 if(doc.getText(0, doc.getLength()).charAt(doc.getLength()-1) == '>'){ 249 removeEndIndex = origLen; 250 } 251 else 252 removeEndIndex = origLen-1; 253 } 254 else 255 removeEndIndex = postfix; 256 257 doc.remove(prefixInd+removeBeginIndex, removeEndIndex - removeBeginIndex); 258 259 if (toInsert.length() > 0) { 260 int p1 = (prefix == -1) ? 0 : prefix; 261 int p2 = toInsert.length(); 262 if (postfix != -1) 263 p2 = p2 - (origLen - postfix); 264 265 if (p2 > p1) { 266 toInsert = toInsert.substring(p1, p2); 267 doc.insertString(prefixInd+removeBeginIndex, toInsert, null); 268 } 269 } 270 } 271 272 public static void replaceDocument(javax.swing.text.Document doc, String newDoc) throws javax.swing.text.BadLocationException { 273 replaceDocument(doc,newDoc,null); 274 } 275 279 public static String filterEndLines(String str) { 280 char[] text = str.toCharArray(); 281 int pos = 0; 282 for (int i = 0; i < text.length; i++) { 283 char c = text[i]; 284 if (c != 13) { 285 if (pos != i) 286 text[pos] = c; 287 pos++; 288 } 289 } 290 return new String (text, 0, pos - 1); 291 } 292 299 public static boolean changeAttribute (Element root, List elementPath, String attrName, String attrValue) 300 throws org.w3c.dom.DOMException { 301 302 if (elementPath==null) return false; 304 Iterator it = elementPath.iterator(); 305 Element element = root; 306 String keyAttributeName=null; 307 NodeList lastNodeList=null; 308 int elementIndex=-1; 309 while (it.hasNext()){ 310 elementIndex=-1; 311 ElementAttrInfo info = (ElementAttrInfo)it.next(); 312 String attributeName = info.getAttributeName(); 313 String attributeValue = info.getAttributeValue(); 314 NodeList nodeList = element.getElementsByTagName(info.getElementName()); 315 if (nodeList.getLength()==0) return false; 316 lastNodeList = nodeList; 317 Element newElement=null; 318 if (attributeName==null) { newElement = (Element )nodeList.item(0); 320 } else { for (int i=0;i<nodeList.getLength();i++){ 322 Element el = (Element )nodeList.item(i); 323 String value = el.getAttribute(attributeName); 324 if (value!=null && value.equals(attributeValue)){ 325 newElement = el; 326 keyAttributeName=attributeName; 327 elementIndex=i; 328 break; 329 } 330 } 331 } 332 if (newElement==null) return false; 333 else element=newElement; 334 } 335 if (attrValue==null) 336 element.removeAttribute(attrName); 337 else { 338 if (attrName!=null && elementIndex>=0 && attrName.equals(keyAttributeName)) { 340 for (int i=0;i<lastNodeList.getLength();i++) { 341 if (elementIndex!=i) { 342 Element el = (Element )lastNodeList.item(i); 343 if (el.getAttribute(attrName).equals(attrValue)){ 344 showDialog(element.getNodeName(),attrName,attrValue); 345 return false; 346 } 347 } 348 } 349 } 350 element.setAttribute(attrName,attrValue); 351 } 352 return true; 353 } 354 public static boolean changeAttribute (Element root, List elementPath, String elementName, int index, String attrName, String attrValue) 355 throws org.w3c.dom.DOMException { 356 if (elementPath.size()==0) return false; 357 Iterator it = elementPath.iterator(); 358 Element element = root; 359 NodeList lastNodeList=null; 361 int elementIndex=-1; 362 while (it.hasNext()){ 363 elementIndex=-1; 364 ElementAttrInfo info = (ElementAttrInfo)it.next(); 365 String attributeName = info.getAttributeName(); 366 String attributeValue = info.getAttributeValue(); 367 NodeList nodeList = element.getElementsByTagName(info.getElementName()); 368 if (nodeList.getLength()==0) return false; 369 lastNodeList = nodeList; 370 Element newElement=null; 371 if (attributeName==null) { newElement = (Element )nodeList.item(0); 373 } else { for (int i=0;i<nodeList.getLength();i++){ 375 Element el = (Element )nodeList.item(i); 376 String value = el.getAttribute(attributeName); 377 if (value!=null && value.equals(attributeValue)){ 378 newElement = el; 379 elementIndex=i; 381 break; 382 } 383 } 384 } 385 if (newElement==null) return false; 386 else element=newElement; 387 } 388 NodeList nodeList = element.getElementsByTagName(elementName); 389 element = (Element )nodeList.item(index); 390 if (attrValue==null) 391 element.removeAttribute(attrName); 392 else { 393 element.setAttribute(attrName,attrValue); 394 } 395 return true; 396 } 397 402 public static boolean deleteElement (Element root, List elementPath) 403 throws org.w3c.dom.DOMException { 404 if (elementPath.size()==0) return false; 405 Iterator it = elementPath.iterator(); 406 Element parent = null; 407 Element element = root; 408 while (it.hasNext()){ 409 ElementAttrInfo info = (ElementAttrInfo)it.next(); 410 String attributeName = info.getAttributeName(); 411 String attributeValue = info.getAttributeValue(); 412 NodeList nodeList = element.getElementsByTagName(info.getElementName()); 413 if (nodeList.getLength()==0) return false; 414 Element newElement=null; 415 if (attributeName==null) { newElement = (Element )nodeList.item(0); 417 } else { for (int i=0;i<nodeList.getLength();i++){ 419 Element el = (Element )nodeList.item(i); 420 String name = el.getAttribute(attributeName); 421 if (name!=null && name.equals(attributeValue)){ 422 newElement = el; 423 break; 424 } 425 } 426 } 427 if (newElement==null) return false; 428 else { 429 parent=element; 430 element=newElement; 431 } 432 } 433 Node previous = element.getPreviousSibling(); 435 boolean firstElement=false; 436 if (previous==null) firstElement=true; 437 else if (previous instanceof CharacterData ) { 438 if (previous.getPreviousSibling()==null) firstElement=true; 439 parent.removeChild(previous); 440 } 441 if (firstElement) { 443 Node next = element.getNextSibling(); 444 if (next!=null && next instanceof CharacterData && next.getNextSibling()==null) 445 parent.removeChild(next); 446 } 447 448 parent.removeChild(element); 449 450 return true; 451 } 452 462 public static boolean addElement (Element root, List elementPath, String elementName, String keyAttribute, String [] attrNames, String [] attrValues){ 463 Iterator it = elementPath.iterator(); 464 Element element = root; 465 while (it.hasNext()){ 466 ElementAttrInfo info = (ElementAttrInfo)it.next(); 467 String attributeName = info.getAttributeName(); 468 String attributeValue = info.getAttributeValue(); 469 NodeList nodeList = element.getElementsByTagName(info.getElementName()); 470 if (nodeList.getLength()==0) return false; 471 Element newElement=null; 472 if (attributeName==null) { newElement = (Element )nodeList.item(0); 474 } else { for (int i=0;i<nodeList.getLength();i++){ 476 Element el = (Element )nodeList.item(i); 477 String name = el.getAttribute(attributeName); 478 if (name!=null && name.equals(attributeValue)){ 479 newElement = el; 480 break; 481 } 482 } 483 } 484 if (newElement==null) return false; 485 else element=newElement; 486 } 487 488 org.w3c.dom.Document doc = root.getOwnerDocument(); 490 Element newElement = doc.createElement(elementName); 491 for (int i=0;i<attrNames.length;i++) 493 newElement.setAttribute(attrNames[i],attrValues[i]); 494 495 if (keyAttribute!=null) { 497 String newKeyValue = newElement.getAttribute(keyAttribute); 498 if (newKeyValue!=null) { 499 NodeList nodeList = element.getElementsByTagName(elementName); 500 if (nodeList!=null) { 501 for (int i=0;i<nodeList.getLength();i++){ 502 if (newKeyValue.equals(((Element )nodeList.item(i)).getAttribute(keyAttribute))){ 503 showDialog(elementName,keyAttribute,newKeyValue); 504 return false; 505 } 506 } 507 } 508 } 509 } 510 511 513 if (!element.hasChildNodes()) { element.appendChild(doc.createTextNode(getIndentBefore(elementPath.size()))); 515 element.appendChild(newElement); 516 element.appendChild(doc.createTextNode(getIndentAfter(elementPath.size()))); 517 } else { 518 NodeList list = element.getElementsByTagName(elementName); 519 if (list.getLength()==0) { Node lastChild = element.getLastChild(); 521 if (lastChild instanceof CharacterData ) { 522 element.appendChild(doc.createTextNode(" ")); element.appendChild(newElement); 524 element.appendChild(lastChild.cloneNode(false)); 526 } else { 527 element.appendChild(newElement); 528 } 529 } else { Node lastInList = list.item(list.getLength()-1); 531 Node previous = lastInList.getPreviousSibling(); 532 Node next = lastInList.getNextSibling(); 533 if (next != null) { 534 if (previous!=null && previous instanceof CharacterData ) 535 element.insertBefore(previous.cloneNode(false),next); 536 element.insertBefore(newElement,next); 537 } else { 538 if (previous!=null && previous instanceof CharacterData ) 539 element.appendChild(previous.cloneNode(false)); 540 element.appendChild(newElement); 541 } 542 } 543 } 544 return true; 545 } 546 547 555 public static boolean addStringElement (Element root, List elementPath, String elementName, String elementValue){ 556 Iterator it = elementPath.iterator(); 557 Element element = root; 558 559 while (it.hasNext()){ 560 ElementAttrInfo info = (ElementAttrInfo)it.next(); 561 String attributeName = info.getAttributeName(); 562 String attributeValue = info.getAttributeValue(); 563 NodeList nodeList = element.getElementsByTagName(info.getElementName()); 564 if (nodeList.getLength()==0) return false; 565 Element newElement=null; 566 if (attributeName==null) { newElement = (Element )nodeList.item(0); 568 } else { for (int i=0;i<nodeList.getLength();i++){ 570 Element el = (Element )nodeList.item(i); 571 String name = el.getAttribute(attributeName); 572 if (name!=null && name.equals(attributeValue)){ 573 newElement = el; 574 break; 575 } 576 } 577 } 578 if (newElement==null) return false; 579 else element=newElement; 580 } 581 582 org.w3c.dom.Document doc = root.getOwnerDocument(); 584 Element newElement = doc.createElement(elementName); 585 newElement.appendChild(doc.createTextNode(elementValue)); 586 587 589 if (!element.hasChildNodes()) { element.appendChild(doc.createTextNode(getIndentBefore(elementPath.size()))); 591 element.appendChild(newElement); 592 element.appendChild(doc.createTextNode(getIndentAfter(elementPath.size()))); 593 } else { 594 NodeList list = element.getElementsByTagName(elementName); 595 if (list.getLength()==0) { Node lastChild = element.getLastChild(); 597 if (lastChild instanceof CharacterData ) { 598 element.appendChild(doc.createTextNode(" ")); element.appendChild(newElement); 600 element.appendChild(lastChild.cloneNode(false)); 602 } else { 603 element.appendChild(newElement); 604 } 605 } else { Node lastInList = list.item(list.getLength()-1); 607 Node previous = lastInList.getPreviousSibling(); 608 Node next = lastInList.getNextSibling(); 609 if (next != null) { 610 if (previous!=null && previous instanceof CharacterData ) 611 element.insertBefore(previous.cloneNode(false),next); 612 element.insertBefore(newElement,next); 613 } else { 614 if (previous!=null && previous instanceof CharacterData ) 615 element.appendChild(previous.cloneNode(false)); 616 element.appendChild(newElement); 617 } 618 } 619 } 620 return true; 621 } 622 623 629 public static boolean deleteAllElements (Element root, List elementPath, String elementName) 630 throws org.w3c.dom.DOMException { 631 if (elementPath.size()==0) return false; 632 Iterator it = elementPath.iterator(); 633 Element parent = null; 634 Element element = root; 635 while (it.hasNext()){ 636 ElementAttrInfo info = (ElementAttrInfo)it.next(); 637 String attributeName = info.getAttributeName(); 638 String attributeValue = info.getAttributeValue(); 639 NodeList nodeList = element.getElementsByTagName(info.getElementName()); 640 if (nodeList.getLength()==0) return false; 641 Element newElement=null; 642 if (attributeName==null) { newElement = (Element )nodeList.item(0); 644 } else { for (int i=0;i<nodeList.getLength();i++){ 646 Element el = (Element )nodeList.item(i); 647 String name = el.getAttribute(attributeName); 648 if (name!=null && name.equals(attributeValue)){ 649 newElement = el; 650 break; 651 } 652 } 653 } 654 if (newElement==null) return false; 655 else { 656 parent=element; 657 element=newElement; 658 } 659 } 660 661 NodeList list = element.getElementsByTagName(elementName); 662 if ( list.getLength()==0) return false; 663 664 Node beforeFirst = list.item(0).getPreviousSibling(); 666 boolean firstElement=false; 667 if (beforeFirst==null) firstElement=true; 668 else if (beforeFirst instanceof CharacterData ) { 669 if (beforeFirst.getPreviousSibling()==null) firstElement=true; 670 } 671 if (firstElement) { 673 Node next = list.item(list.getLength()-1).getNextSibling(); 674 if (next!=null && next instanceof CharacterData && next.getNextSibling()==null) 675 element.removeChild(next); 676 } 677 for(int i=list.getLength()-1;i>=0;i--){ 678 Node item = list.item(i); 679 Node previous = item.getPreviousSibling(); 680 if (previous!=null && previous instanceof CharacterData ) 681 element.removeChild(previous); 682 element.removeChild(item); 683 } 684 685 return true; 686 } 687 688 private static void showDialog(String elementName, String attrName, String attrValue){ 689 String mes = NbBundle.getMessage(XMLJ2eeUtils.class, "TXT_elementExists", 690 new Object [] { elementName, attrName, attrValue}); 691 NotifyDescriptor.Message message = new NotifyDescriptor.Message(mes); 692 DialogDisplayer.getDefault().notify(message); 693 } 694 695 private static String getIndentBefore(int level) { 696 StringBuffer sb = new StringBuffer ("\n"); for (int i=0;i<=level;i++) sb.append(" "); return sb.toString(); 699 } 700 private static String getIndentAfter(int level) { 701 StringBuffer sb = new StringBuffer ("\n"); for (int i=0;i<level;i++) sb.append(" "); return sb.toString(); 704 } 705 706 } 707 | Popular Tags |