KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > common > xmlutils > XMLJ2eeUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ddloaders.common.xmlutils;
21
22 import org.w3c.dom.Element JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25 import org.w3c.dom.CharacterData JavaDoc;
26 import java.util.*;
27
28 import org.openide.NotifyDescriptor;
29 import org.openide.util.NbBundle;
30 import org.openide.DialogDisplayer;
31
32 /** Static methods useful for XMLJ2eeDataObject.
33  *
34  * @author mkuchtiak
35  */

36
37 public class XMLJ2eeUtils {
38
39     /** This method updates document in editor with newDoc, but leaves the text before prefixMark.
40      * @param doc original document
41      * @param newDoc new value of whole document
42      * @param prefixMark - beginning part of the document before this mark should be preserved
43      */

44     public static void updateDocument(javax.swing.text.Document JavaDoc doc, String JavaDoc newDoc, String JavaDoc prefixMark) throws javax.swing.text.BadLocationException JavaDoc {
45         int origLen = doc.getLength();
46         String JavaDoc 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         //newDoc=filterEndLines(newDoc);
62
int newLen = newDoc.length();
63         
64         if (origDoc.equals(newDoc)) {
65             // no change in document
66
return;
67         }
68
69         final int granularity = 20;
70         
71         int prefix = -1;
72         int postfix = -1;
73         String JavaDoc toInsert = newDoc;
74         
75         if ((origLen > granularity) && (newLen > granularity)) {
76             int pos1 = 0;
77             int len = Math.min(origLen, newLen);
78             // find the prefix which both Strings begin with
79
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     /** This method update document in editor after change in beans hierarchy.
148      * It takes old document and new document in String.
149      * To avoid regeneration of whole document in text editor following steps are done:
150      * 1) compare the begin of both documents (old one and new one)
151      * - find the first position where both documents differ
152      * 2) do the same from the ends of documents
153      * 3) remove old middle part of text (modified part) and insert new one
154      *
155      * @param doc original document
156      * @param newDoc new value of whole document
157      * @param prefixMark - beginning part of the document before this mark should be preserved
158      */

159     public static void replaceDocument(javax.swing.text.Document JavaDoc doc, String JavaDoc newDoc, String JavaDoc prefixMark) throws javax.swing.text.BadLocationException JavaDoc {
160         int origLen = doc.getLength();
161         String JavaDoc 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             // no change in document
181
return;
182         }
183
184         final int granularity = 20;
185         
186         int prefix = -1;
187         int postfix = -1;
188         String JavaDoc toInsert = newDoc;
189         
190         if ((origLen > granularity) && (newLen > granularity)) {
191             int pos1 = 0;
192             int len = Math.min(origLen, newLen);
193             // find the prefix which both Strings begin with
194
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 JavaDoc doc, String JavaDoc newDoc) throws javax.swing.text.BadLocationException JavaDoc {
273         replaceDocument(doc,newDoc,null);
274     }
275     /** Filter characters #13 (CR) from the specified String
276      * @param str original string
277      * @return the string without #13 characters
278      */

279     public static String JavaDoc filterEndLines(String JavaDoc 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 JavaDoc(text, 0, pos - 1);
291     }
292     /** This method updates the attribute specified by the elementPath in DOM tree
293      * @param root Root element of the DOM graph
294      * @param elementPath List defining the element path to the target element
295      * @param attrName Attribute name of the target attribute
296      * @param attrValue New value for the attribute defined by attrName
297      * @return true if method was succesful, false otherwise
298      */

299     public static boolean changeAttribute (Element JavaDoc root, List JavaDoc elementPath, String JavaDoc attrName, String JavaDoc attrValue)
300     throws org.w3c.dom.DOMException JavaDoc {
301        
302         //if (elementPath.size()==0) return false;
303
if (elementPath==null) return false;
304         Iterator it = elementPath.iterator();
305         Element JavaDoc element = root;
306         String JavaDoc keyAttributeName=null;
307         NodeList JavaDoc lastNodeList=null;
308         int elementIndex=-1;
309         while (it.hasNext()){
310             elementIndex=-1;
311             ElementAttrInfo info = (ElementAttrInfo)it.next();
312             String JavaDoc attributeName = info.getAttributeName();
313             String JavaDoc attributeValue = info.getAttributeValue();
314             NodeList JavaDoc nodeList = element.getElementsByTagName(info.getElementName());
315             if (nodeList.getLength()==0) return false;
316             lastNodeList = nodeList;
317             Element JavaDoc newElement=null;
318             if (attributeName==null) { // there is only one element
319
newElement = (Element JavaDoc)nodeList.item(0);
320             } else { // element need to be found by element value
321
for (int i=0;i<nodeList.getLength();i++){
322                     Element JavaDoc el = (Element JavaDoc)nodeList.item(i);
323                     String JavaDoc 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             // test if there is no such a keyAttribute - avoid a duplicity
339
if (attrName!=null && elementIndex>=0 && attrName.equals(keyAttributeName)) {
340                 for (int i=0;i<lastNodeList.getLength();i++) {
341                     if (elementIndex!=i) {
342                         Element JavaDoc el = (Element JavaDoc)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 JavaDoc root, List JavaDoc elementPath, String JavaDoc elementName, int index, String JavaDoc attrName, String JavaDoc attrValue)
355     throws org.w3c.dom.DOMException JavaDoc {
356         if (elementPath.size()==0) return false;
357         Iterator it = elementPath.iterator();
358         Element JavaDoc element = root;
359         //String keyAttributeName=null;
360
NodeList JavaDoc lastNodeList=null;
361         int elementIndex=-1;
362         while (it.hasNext()){
363             elementIndex=-1;
364             ElementAttrInfo info = (ElementAttrInfo)it.next();
365             String JavaDoc attributeName = info.getAttributeName();
366             String JavaDoc attributeValue = info.getAttributeValue();
367             NodeList JavaDoc nodeList = element.getElementsByTagName(info.getElementName());
368             if (nodeList.getLength()==0) return false;
369             lastNodeList = nodeList;
370             Element JavaDoc newElement=null;
371             if (attributeName==null) { // there is only one element
372
newElement = (Element JavaDoc)nodeList.item(0);
373             } else { // element need to be found by element value
374
for (int i=0;i<nodeList.getLength();i++){
375                     Element JavaDoc el = (Element JavaDoc)nodeList.item(i);
376                     String JavaDoc value = el.getAttribute(attributeName);
377                     if (value!=null && value.equals(attributeValue)){
378                         newElement = el;
379                         //keyAttributeName=attributeName;
380
elementIndex=i;
381                         break;
382                     }
383                 }
384             }
385             if (newElement==null) return false;
386             else element=newElement;
387         }
388         NodeList JavaDoc nodeList = element.getElementsByTagName(elementName);
389         element = (Element JavaDoc)nodeList.item(index);
390         if (attrValue==null)
391             element.removeAttribute(attrName);
392         else {
393             element.setAttribute(attrName,attrValue);
394         }
395         return true;
396     }
397     /** This method deletes the element specified by elementPath from the DOM tree
398      * @param root Root element of the DOM graph
399      * @param elementPath List defining the element path to the target element
400      * @return true if method was succesful, false otherwise
401      */

402     public static boolean deleteElement (Element JavaDoc root, List JavaDoc elementPath)
403     throws org.w3c.dom.DOMException JavaDoc {
404         if (elementPath.size()==0) return false;
405         Iterator it = elementPath.iterator();
406         Element JavaDoc parent = null;
407         Element JavaDoc element = root;
408         while (it.hasNext()){
409             ElementAttrInfo info = (ElementAttrInfo)it.next();
410             String JavaDoc attributeName = info.getAttributeName();
411             String JavaDoc attributeValue = info.getAttributeValue();
412             NodeList JavaDoc nodeList = element.getElementsByTagName(info.getElementName());
413             if (nodeList.getLength()==0) return false;
414             Element JavaDoc newElement=null;
415             if (attributeName==null) { // there is only one element
416
newElement = (Element JavaDoc)nodeList.item(0);
417             } else { // element need to be found by element value
418
for (int i=0;i<nodeList.getLength();i++){
419                     Element JavaDoc el = (Element JavaDoc)nodeList.item(i);
420                     String JavaDoc 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         // removing the previous text element and test if it is the first element in parent
434
Node JavaDoc previous = element.getPreviousSibling();
435         boolean firstElement=false;
436         if (previous==null) firstElement=true;
437         else if (previous instanceof CharacterData JavaDoc) {
438             if (previous.getPreviousSibling()==null) firstElement=true;
439             parent.removeChild(previous);
440         }
441         // removing the next text element (only if this element was the last non text element in parent)
442
if (firstElement) {
443             Node JavaDoc next = element.getNextSibling();
444             if (next!=null && next instanceof CharacterData JavaDoc && next.getNextSibling()==null)
445                      parent.removeChild(next);
446         }
447      
448         parent.removeChild(element);
449         
450         return true;
451     }
452     /** This method adds the element specified by elementName and element attributes into the element
453      * specified by the elementPath
454      * @param root Root element of the DOM graph
455      * @param elementPath List defining the element path to the target element
456      * @param elementName name of the new Element
457      * @param keyAttribute key Attribute of the new Element
458      * @param attrNames names of the attributes that will be initialized
459      * @param attrValues initial values for attrNames
460      * @return true if method was succesful, false otherwise
461      */

462     public static boolean addElement (Element JavaDoc root, List JavaDoc elementPath, String JavaDoc elementName, String JavaDoc keyAttribute, String JavaDoc[] attrNames, String JavaDoc[] attrValues){
463         Iterator it = elementPath.iterator();
464         Element JavaDoc element = root;
465         while (it.hasNext()){
466             ElementAttrInfo info = (ElementAttrInfo)it.next();
467             String JavaDoc attributeName = info.getAttributeName();
468             String JavaDoc attributeValue = info.getAttributeValue();
469             NodeList JavaDoc nodeList = element.getElementsByTagName(info.getElementName());
470             if (nodeList.getLength()==0) return false;
471             Element JavaDoc newElement=null;
472             if (attributeName==null) { // there is only one element
473
newElement = (Element JavaDoc)nodeList.item(0);
474             } else { // element need to be found by element value
475
for (int i=0;i<nodeList.getLength();i++){
476                     Element JavaDoc el = (Element JavaDoc)nodeList.item(i);
477                     String JavaDoc 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         // creating new newElement
489
org.w3c.dom.Document JavaDoc doc = root.getOwnerDocument();
490         Element JavaDoc newElement = doc.createElement(elementName);
491         // adding list of the attributes
492
for (int i=0;i<attrNames.length;i++)
493             newElement.setAttribute(attrNames[i],attrValues[i]);
494         
495         // test if there is no such an element already
496
if (keyAttribute!=null) {
497             String JavaDoc newKeyValue = newElement.getAttribute(keyAttribute);
498             if (newKeyValue!=null) {
499                 NodeList JavaDoc nodeList = element.getElementsByTagName(elementName);
500                 if (nodeList!=null) {
501                     for (int i=0;i<nodeList.getLength();i++){
502                         if (newKeyValue.equals(((Element JavaDoc)nodeList.item(i)).getAttribute(keyAttribute))){
503                             showDialog(elementName,keyAttribute,newKeyValue);
504                             return false;
505                         }
506                     }
507                 }
508             }
509         }
510         
511         // appending newElement into DOM graph
512

513         if (!element.hasChildNodes()) { //element contains no elements until now
514
element.appendChild(doc.createTextNode(getIndentBefore(elementPath.size())));
515             element.appendChild(newElement);
516             element.appendChild(doc.createTextNode(getIndentAfter(elementPath.size())));
517         } else {
518             NodeList JavaDoc list = element.getElementsByTagName(elementName);
519             if (list.getLength()==0) { // that will be the first Element
520
Node JavaDoc lastChild = element.getLastChild();
521                 if (lastChild instanceof CharacterData JavaDoc) {
522                     element.appendChild(doc.createTextNode(" ")); // NOI18N
523
element.appendChild(newElement);
524                     //Node textEl = (CharacterData)lastChild.cloneNode(false);
525
element.appendChild(lastChild.cloneNode(false));
526                 } else {
527                     element.appendChild(newElement);
528                 }
529             } else { // new Element will be added to other Elements
530
Node JavaDoc lastInList = list.item(list.getLength()-1);
531                 Node JavaDoc previous = lastInList.getPreviousSibling();
532                 Node JavaDoc next = lastInList.getNextSibling();
533                 if (next != null) {
534                     if (previous!=null && previous instanceof CharacterData JavaDoc)
535                         element.insertBefore(previous.cloneNode(false),next);
536                     element.insertBefore(newElement,next);
537                 } else {
538                     if (previous!=null && previous instanceof CharacterData JavaDoc)
539                         element.appendChild(previous.cloneNode(false));
540                     element.appendChild(newElement);
541                 }
542             }
543         }
544         return true;
545     }
546     
547     /** This method adds the element specified by elementName and elementValue into the element
548      * specified by the elementPath
549      * @param root Root element of the DOM graph
550      * @param elementPath List defining the element path to the target element
551      * @param elementName name of the new Element
552      * @param elementValue value of te new Element
553      * @return true if method was succesful, false otherwise
554      */

555     public static boolean addStringElement (Element JavaDoc root, List JavaDoc elementPath, String JavaDoc elementName, String JavaDoc elementValue){
556         Iterator it = elementPath.iterator();
557         Element JavaDoc element = root;
558         
559         while (it.hasNext()){
560             ElementAttrInfo info = (ElementAttrInfo)it.next();
561             String JavaDoc attributeName = info.getAttributeName();
562             String JavaDoc attributeValue = info.getAttributeValue();
563             NodeList JavaDoc nodeList = element.getElementsByTagName(info.getElementName());
564             if (nodeList.getLength()==0) return false;
565             Element JavaDoc newElement=null;
566             if (attributeName==null) { // there is only one element
567
newElement = (Element JavaDoc)nodeList.item(0);
568             } else { // element need to be found by element value
569
for (int i=0;i<nodeList.getLength();i++){
570                     Element JavaDoc el = (Element JavaDoc)nodeList.item(i);
571                     String JavaDoc 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         // creating new newElement
583
org.w3c.dom.Document JavaDoc doc = root.getOwnerDocument();
584         Element JavaDoc newElement = doc.createElement(elementName);
585         newElement.appendChild(doc.createTextNode(elementValue));
586
587         // appending newElement into DOM graph
588

589         if (!element.hasChildNodes()) { //element contains no elements until now
590
element.appendChild(doc.createTextNode(getIndentBefore(elementPath.size())));
591             element.appendChild(newElement);
592             element.appendChild(doc.createTextNode(getIndentAfter(elementPath.size())));
593         } else {
594             NodeList JavaDoc list = element.getElementsByTagName(elementName);
595             if (list.getLength()==0) { // that will be the first Element
596
Node JavaDoc lastChild = element.getLastChild();
597                 if (lastChild instanceof CharacterData JavaDoc) {
598                     element.appendChild(doc.createTextNode(" ")); // NOI18N
599
element.appendChild(newElement);
600                     //Node textEl = (CharacterData)lastChild.cloneNode(false);
601
element.appendChild(lastChild.cloneNode(false));
602                 } else {
603                     element.appendChild(newElement);
604                 }
605             } else { // new Element will be added to other Elements
606
Node JavaDoc lastInList = list.item(list.getLength()-1);
607                 Node JavaDoc previous = lastInList.getPreviousSibling();
608                 Node JavaDoc next = lastInList.getNextSibling();
609                 if (next != null) {
610                     if (previous!=null && previous instanceof CharacterData JavaDoc)
611                         element.insertBefore(previous.cloneNode(false),next);
612                     element.insertBefore(newElement,next);
613                 } else {
614                     if (previous!=null && previous instanceof CharacterData JavaDoc)
615                         element.appendChild(previous.cloneNode(false));
616                     element.appendChild(newElement);
617                 }
618             }
619         }
620         return true;
621     }
622     
623     /** This method deletes the all elements specified by elementName from the DOM tree
624      * @param root Root element of the DOM graph
625      * @param elementPath List defining the element path to the parent element
626      * @param elementName String specifying the elements for deleting
627      * @return true if method was succesful, false otherwise
628      */

629     public static boolean deleteAllElements (Element JavaDoc root, List JavaDoc elementPath, String JavaDoc elementName)
630     throws org.w3c.dom.DOMException JavaDoc {
631         if (elementPath.size()==0) return false;
632         Iterator it = elementPath.iterator();
633         Element JavaDoc parent = null;
634         Element JavaDoc element = root;
635         while (it.hasNext()){
636             ElementAttrInfo info = (ElementAttrInfo)it.next();
637             String JavaDoc attributeName = info.getAttributeName();
638             String JavaDoc attributeValue = info.getAttributeValue();
639             NodeList JavaDoc nodeList = element.getElementsByTagName(info.getElementName());
640             if (nodeList.getLength()==0) return false;
641             Element JavaDoc newElement=null;
642             if (attributeName==null) { // there is only one element
643
newElement = (Element JavaDoc)nodeList.item(0);
644             } else { // element need to be found by element value
645
for (int i=0;i<nodeList.getLength();i++){
646                     Element JavaDoc el = (Element JavaDoc)nodeList.item(i);
647                     String JavaDoc 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 JavaDoc list = element.getElementsByTagName(elementName);
662         if ( list.getLength()==0) return false;
663         
664         // removing the previous text element and test if it is the first element in parent
665
Node JavaDoc beforeFirst = list.item(0).getPreviousSibling();
666         boolean firstElement=false;
667         if (beforeFirst==null) firstElement=true;
668         else if (beforeFirst instanceof CharacterData JavaDoc) {
669             if (beforeFirst.getPreviousSibling()==null) firstElement=true;
670         }
671         // removing the next text element (only if this element was the last non text element in parent)
672
if (firstElement) {
673             Node JavaDoc next = list.item(list.getLength()-1).getNextSibling();
674             if (next!=null && next instanceof CharacterData JavaDoc && next.getNextSibling()==null)
675                      element.removeChild(next);
676         }
677         for(int i=list.getLength()-1;i>=0;i--){
678             Node JavaDoc item = list.item(i);
679             Node JavaDoc previous = item.getPreviousSibling();
680             if (previous!=null && previous instanceof CharacterData JavaDoc)
681                 element.removeChild(previous);
682             element.removeChild(item);
683         }
684         
685         return true;
686     }
687     
688     private static void showDialog(String JavaDoc elementName, String JavaDoc attrName, String JavaDoc attrValue){
689      String JavaDoc mes = NbBundle.getMessage(XMLJ2eeUtils.class, "TXT_elementExists",
690         new Object JavaDoc [] { elementName, attrName, attrValue});
691      NotifyDescriptor.Message message = new NotifyDescriptor.Message(mes);
692      DialogDisplayer.getDefault().notify(message);
693     }
694     
695     private static String JavaDoc getIndentBefore(int level) {
696         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("\n"); //NOI18N
697
for (int i=0;i<=level;i++) sb.append(" "); //NOI18N
698
return sb.toString();
699     }
700     private static String JavaDoc getIndentAfter(int level) {
701         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("\n"); //NOI18N
702
for (int i=0;i<level;i++) sb.append(" "); //NOI18N
703
return sb.toString();
704     }
705      
706 }
707
Popular Tags