KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > 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.websvc.wsdl.xmlutils;
21
22 import java.util.*;
23
24
25 /** Static methods useful for XMLJ2eeDataObject.
26  *
27  * @author mkuchtiak
28  */

29
30 public class XMLJ2eeUtils {
31
32     /** This method updates document in editor with newDoc, but leaves the text before prefixMark.
33      * @param doc original document
34      * @param newDoc new value of whole document
35      * @param prefixMark - beginning part of the document before this mark should be preserved
36      */

37     public static void updateDocument(javax.swing.text.Document JavaDoc doc, String JavaDoc newDoc, String JavaDoc prefixMark) throws javax.swing.text.BadLocationException JavaDoc {
38         int origLen = doc.getLength();
39         String JavaDoc origDoc = doc.getText(0, origLen);;
40         int prefixInd=0;
41         if (prefixMark!=null) {
42             prefixInd = origDoc.indexOf(prefixMark);
43             if (prefixInd>0) {
44                 origLen-=prefixInd;
45                 origDoc=doc.getText(prefixInd,origLen);
46             }
47             else {
48                 prefixInd=0;
49             }
50             int prefixIndNewDoc=newDoc.indexOf(prefixMark);
51             if (prefixIndNewDoc>0)
52             newDoc=newDoc.substring(prefixIndNewDoc);
53         }
54         //newDoc=filterEndLines(newDoc);
55
int newLen = newDoc.length();
56         
57         if (origDoc.equals(newDoc)) {
58             // no change in document
59
return;
60         }
61
62         final int granularity = 20;
63         
64         int prefix = -1;
65         int postfix = -1;
66         String JavaDoc toInsert = newDoc;
67         
68         if ((origLen > granularity) && (newLen > granularity)) {
69             int pos1 = 0;
70             int len = Math.min(origLen, newLen);
71             // find the prefix which both Strings begin with
72
for (;;) {
73                 if (origDoc.regionMatches(pos1, newDoc, pos1, granularity)) {
74                     pos1 += granularity;
75                     if (pos1 + granularity >= len) {
76                         break;
77                     }
78                 }
79                 else {
80                     break;
81                 }
82             }
83             if (pos1 > 0)
84                 prefix = pos1;
85             
86             pos1 = origLen - granularity;
87             int pos2 = newLen - granularity;
88             for (;;) {
89                 if (origDoc.regionMatches(pos1, newDoc, pos2, granularity)) {
90                     pos1 -= granularity;
91                     pos2 -= granularity;
92                     if (pos1 < 0) {
93                         pos1 += granularity;
94                         break;
95                     }
96                     if (pos2 < 0) {
97                         pos2 += granularity;
98                         break;
99                     }
100                 }
101                 else {
102                     pos1 += granularity;
103                     pos2 += granularity;
104                     break;
105                 }
106             }
107             if (pos1 < origLen - granularity) {
108                 postfix = pos1;
109             }
110         }
111
112         if ((prefix != -1) && (postfix != -1)) {
113             if (postfix < prefix) {
114                 postfix = prefix;
115             }
116             
117             int delta = (prefix + (origLen - postfix) - newLen);
118             if (delta > 0) {
119                 postfix += delta;
120             }
121         }
122         
123         int removeBeginIndex = (prefix == -1) ? 0 : prefix;
124         int removeEndIndex = (postfix == -1) ? origLen - 1 : postfix;
125         
126         doc.remove(prefixInd+removeBeginIndex, removeEndIndex - removeBeginIndex);
127
128         if (toInsert.length() > 0) {
129             int p1 = (prefix == -1) ? 0 : prefix;
130             int p2 = toInsert.length();
131             if (postfix != -1)
132                 p2 = p2 - (origLen - postfix);
133
134             if (p2 > p1) {
135                 toInsert = toInsert.substring(p1, p2);
136                 doc.insertString(prefixInd+removeBeginIndex, toInsert, null);
137             }
138         }
139     }
140     /** This method update document in editor after change in beans hierarchy.
141      * It takes old document and new document in String.
142      * To avoid regeneration of whole document in text editor following steps are done:
143      * 1) compare the begin of both documents (old one and new one)
144      * - find the first position where both documents differ
145      * 2) do the same from the ends of documents
146      * 3) remove old middle part of text (modified part) and insert new one
147      *
148      * @param doc original document
149      * @param newDoc new value of whole document
150      * @param prefixMark - beginning part of the document before this mark should be preserved
151      */

152     public static void replaceDocument(javax.swing.text.Document JavaDoc doc, String JavaDoc newDoc, String JavaDoc prefixMark) throws javax.swing.text.BadLocationException JavaDoc {
153         int origLen = doc.getLength();
154         String JavaDoc origDoc = doc.getText(0, origLen);
155         int prefixInd=0;
156         if (prefixMark!=null) {
157             prefixInd = origDoc.indexOf(prefixMark);
158             if (prefixInd>0) {
159                 origLen-=prefixInd;
160                 origDoc=doc.getText(prefixInd,origLen);
161             }
162             else {
163                 prefixInd=0;
164             }
165             int prefixIndNewDoc=newDoc.indexOf(prefixMark);
166             if (prefixIndNewDoc>0)
167             newDoc=newDoc.substring(prefixIndNewDoc);
168         }
169         newDoc=filterEndLines(newDoc);
170         int newLen = newDoc.length();
171         
172         if (origDoc.equals(newDoc)) {
173             // no change in document
174
return;
175         }
176
177         final int granularity = 20;
178         
179         int prefix = -1;
180         int postfix = -1;
181         String JavaDoc toInsert = newDoc;
182         
183         if ((origLen > granularity) && (newLen > granularity)) {
184             int pos1 = 0;
185             int len = Math.min(origLen, newLen);
186             // find the prefix which both Strings begin with
187
for (;;) {
188                 if (origDoc.regionMatches(pos1, newDoc, pos1, granularity)) {
189                     pos1 += granularity;
190                     if (pos1 + granularity >= len) {
191                         break;
192                     }
193                 }
194                 else {
195                     break;
196                 }
197             }
198             if (pos1 > 0)
199                 prefix = pos1;
200             
201             pos1 = origLen - granularity;
202             int pos2 = newLen - granularity;
203             for (;;) {
204                 if (origDoc.regionMatches(pos1, newDoc, pos2, granularity)) {
205                     pos1 -= granularity;
206                     pos2 -= granularity;
207                     if (pos1 < 0) {
208                         pos1 += granularity;
209                         break;
210                     }
211                     if (pos2 < 0) {
212                         pos2 += granularity;
213                         break;
214                     }
215                 }
216                 else {
217                     pos1 += granularity;
218                     pos2 += granularity;
219                     break;
220                 }
221             }
222             if (pos1 < origLen - granularity) {
223                 postfix = pos1;
224             }
225         }
226
227         if ((prefix != -1) && (postfix != -1)) {
228             if (postfix < prefix) {
229                 postfix = prefix;
230             }
231             
232             int delta = (prefix + (origLen - postfix) - newLen);
233             if (delta > 0) {
234                 postfix += delta;
235             }
236         }
237         
238         int removeBeginIndex = (prefix == -1) ? 0 : prefix;
239         int removeEndIndex;
240         if (postfix == -1){
241             if(doc.getText(0, doc.getLength()).charAt(doc.getLength()-1) == '>'){
242                 removeEndIndex = origLen;
243             }
244             else
245                 removeEndIndex = origLen-1;
246         }
247         else
248             removeEndIndex = postfix;
249         
250         doc.remove(prefixInd+removeBeginIndex, removeEndIndex - removeBeginIndex);
251         
252         if (toInsert.length() > 0) {
253             int p1 = (prefix == -1) ? 0 : prefix;
254             int p2 = toInsert.length();
255             if (postfix != -1)
256                 p2 = p2 - (origLen - postfix);
257
258             if (p2 > p1) {
259                 toInsert = toInsert.substring(p1, p2);
260                 doc.insertString(prefixInd+removeBeginIndex, toInsert, null);
261             }
262         }
263     }
264     
265     public static void replaceDocument(javax.swing.text.Document JavaDoc doc, String JavaDoc newDoc) throws javax.swing.text.BadLocationException JavaDoc {
266         replaceDocument(doc,newDoc,null);
267     }
268     /** Filter characters #13 (CR) from the specified String
269      * @param str original string
270      * @return the string without #13 characters
271      */

272     public static String JavaDoc filterEndLines(String JavaDoc str) {
273         char[] text = str.toCharArray();
274         int pos = 0;
275         for (int i = 0; i < text.length; i++) {
276             char c = text[i];
277             if (c != 13) {
278                 if (pos != i)
279                     text[pos] = c;
280                 pos++;
281             }
282         }
283         return new String JavaDoc(text, 0, pos - 1);
284     }
285      
286 }
287
Popular Tags