1 19 20 package org.netbeans.modules.websvc.wsdl.xmlutils; 21 22 import java.util.*; 23 24 25 29 30 public class XMLJ2eeUtils { 31 32 37 public static void updateDocument(javax.swing.text.Document doc, String newDoc, String prefixMark) throws javax.swing.text.BadLocationException { 38 int origLen = doc.getLength(); 39 String 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 int newLen = newDoc.length(); 56 57 if (origDoc.equals(newDoc)) { 58 return; 60 } 61 62 final int granularity = 20; 63 64 int prefix = -1; 65 int postfix = -1; 66 String toInsert = newDoc; 67 68 if ((origLen > granularity) && (newLen > granularity)) { 69 int pos1 = 0; 70 int len = Math.min(origLen, newLen); 71 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 152 public static void replaceDocument(javax.swing.text.Document doc, String newDoc, String prefixMark) throws javax.swing.text.BadLocationException { 153 int origLen = doc.getLength(); 154 String 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 return; 175 } 176 177 final int granularity = 20; 178 179 int prefix = -1; 180 int postfix = -1; 181 String toInsert = newDoc; 182 183 if ((origLen > granularity) && (newLen > granularity)) { 184 int pos1 = 0; 185 int len = Math.min(origLen, newLen); 186 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 doc, String newDoc) throws javax.swing.text.BadLocationException { 266 replaceDocument(doc,newDoc,null); 267 } 268 272 public static String filterEndLines(String 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 (text, 0, pos - 1); 284 } 285 286 } 287 | Popular Tags |