1 11 package org.eclipse.jface.text; 12 13 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.ListIterator ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.core.runtime.Assert; 23 24 25 31 public class TextUtilities { 32 33 36 public final static String [] DELIMITERS= new String [] { "\n", "\r", "\r\n" }; 38 43 public final static String [] fgDelimiters= DELIMITERS; 44 45 46 47 55 public static String determineLineDelimiter(String text, String hint) { 56 try { 57 int[] info= indexOf(DELIMITERS, text, 0); 58 return DELIMITERS[info[1]]; 59 } catch (ArrayIndexOutOfBoundsException x) { 60 } 61 return hint; 62 } 63 64 75 public static int[] indexOf(String [] searchStrings, String text, int offset) { 76 77 int[] result= { -1, -1 }; 78 int zeroIndex= -1; 79 80 for (int i= 0; i < searchStrings.length; i++) { 81 82 int length= searchStrings[i].length(); 83 84 if (length == 0) { 85 zeroIndex= i; 86 continue; 87 } 88 89 int index= text.indexOf(searchStrings[i], offset); 90 if (index >= 0) { 91 92 if (result[0] == -1) { 93 result[0]= index; 94 result[1]= i; 95 } else if (index < result[0]) { 96 result[0]= index; 97 result[1]= i; 98 } else if (index == result[0] && length > searchStrings[result[1]].length()) { 99 result[0]= index; 100 result[1]= i; 101 } 102 } 103 } 104 105 if (zeroIndex > -1 && result[0] == -1) { 106 result[0]= 0; 107 result[1]= zeroIndex; 108 } 109 110 return result; 111 } 112 113 121 public static int endsWith(String [] searchStrings, String text) { 122 123 int index= -1; 124 125 for (int i= 0; i < searchStrings.length; i++) { 126 if (text.endsWith(searchStrings[i])) { 127 if (index == -1 || searchStrings[i].length() > searchStrings[index].length()) 128 index= i; 129 } 130 } 131 132 return index; 133 } 134 135 143 public static int startsWith(String [] searchStrings, String text) { 144 145 int index= -1; 146 147 for (int i= 0; i < searchStrings.length; i++) { 148 if (text.startsWith(searchStrings[i])) { 149 if (index == -1 || searchStrings[i].length() > searchStrings[index].length()) 150 index= i; 151 } 152 } 153 154 return index; 155 } 156 157 165 public static int equals(String [] compareStrings, String text) { 166 for (int i= 0; i < compareStrings.length; i++) { 167 if (text.equals(compareStrings[i])) 168 return i; 169 } 170 return -1; 171 } 172 173 183 public static DocumentEvent mergeUnprocessedDocumentEvents(IDocument unprocessedDocument, List documentEvents) throws BadLocationException { 184 185 if (documentEvents.size() == 0) 186 return null; 187 188 final Iterator iterator= documentEvents.iterator(); 189 final DocumentEvent firstEvent= (DocumentEvent) iterator.next(); 190 191 final IDocument document= unprocessedDocument; 193 int offset= firstEvent.getOffset(); 194 int length= firstEvent.getLength(); 195 final StringBuffer text= new StringBuffer (firstEvent.getText() == null ? "" : firstEvent.getText()); 197 while (iterator.hasNext()) { 198 199 final int delta= text.length() - length; 200 201 final DocumentEvent event= (DocumentEvent) iterator.next(); 202 final int eventOffset= event.getOffset(); 203 final int eventLength= event.getLength(); 204 final String eventText= event.getText() == null ? "" : event.getText(); 206 if (eventOffset > offset + length + delta) { 208 final String string= document.get(offset + length, (eventOffset - delta) - (offset + length)); 209 text.append(string); 210 text.append(eventText); 211 212 length= (eventOffset - delta) + eventLength - offset; 213 214 } else if (eventOffset + eventLength < offset) { 216 final String string= document.get(eventOffset + eventLength, offset - (eventOffset + eventLength)); 217 text.insert(0, string); 218 text.insert(0, eventText); 219 220 length= offset + length - eventOffset; 221 offset= eventOffset; 222 223 } else { 225 final int start= Math.max(0, eventOffset - offset); 226 final int end= Math.min(text.length(), eventLength + eventOffset - offset); 227 text.replace(start, end, eventText); 228 229 offset= Math.min(offset, eventOffset); 230 final int totalDelta= delta + eventText.length() - eventLength; 231 length= text.length() - totalDelta; 232 } 233 } 234 235 return new DocumentEvent(document, offset, length, text.toString()); 236 } 237 238 248 public static DocumentEvent mergeProcessedDocumentEvents(List documentEvents) throws BadLocationException { 249 250 if (documentEvents.size() == 0) 251 return null; 252 253 final ListIterator iterator= documentEvents.listIterator(documentEvents.size()); 254 final DocumentEvent firstEvent= (DocumentEvent) iterator.previous(); 255 256 final IDocument document= firstEvent.getDocument(); 258 int offset= firstEvent.getOffset(); 259 int length= firstEvent.getLength(); 260 int textLength= firstEvent.getText() == null ? 0 : firstEvent.getText().length(); 261 262 while (iterator.hasPrevious()) { 263 264 final int delta= length - textLength; 265 266 final DocumentEvent event= (DocumentEvent) iterator.previous(); 267 final int eventOffset= event.getOffset(); 268 final int eventLength= event.getLength(); 269 final int eventTextLength= event.getText() == null ? 0 : event.getText().length(); 270 271 if (eventOffset > offset + textLength + delta) { 273 length= (eventOffset - delta) - (offset + textLength) + length + eventLength; 274 textLength= (eventOffset - delta) + eventTextLength - offset; 275 276 } else if (eventOffset + eventTextLength < offset) { 278 length= offset - (eventOffset + eventTextLength) + length + eventLength; 279 textLength= offset + textLength - eventOffset; 280 offset= eventOffset; 281 282 } else { 284 final int start= Math.max(0, eventOffset - offset); 285 final int end= Math.min(length, eventTextLength + eventOffset - offset); 286 length += eventLength - (end - start); 287 288 offset= Math.min(offset, eventOffset); 289 final int totalDelta= delta + eventLength - eventTextLength; 290 textLength= length - totalDelta; 291 } 292 } 293 294 final String text= document.get(offset, textLength); 295 return new DocumentEvent(document, offset, length, text); 296 } 297 298 306 public static Map removeDocumentPartitioners(IDocument document) { 307 Map partitioners= new HashMap (); 308 if (document instanceof IDocumentExtension3) { 309 IDocumentExtension3 extension3= (IDocumentExtension3) document; 310 String [] partitionings= extension3.getPartitionings(); 311 for (int i= 0; i < partitionings.length; i++) { 312 IDocumentPartitioner partitioner= extension3.getDocumentPartitioner(partitionings[i]); 313 if (partitioner != null) { 314 extension3.setDocumentPartitioner(partitionings[i], null); 315 partitioner.disconnect(); 316 partitioners.put(partitionings[i], partitioner); 317 } 318 } 319 } else { 320 IDocumentPartitioner partitioner= document.getDocumentPartitioner(); 321 if (partitioner != null) { 322 document.setDocumentPartitioner(null); 323 partitioner.disconnect(); 324 partitioners.put(IDocumentExtension3.DEFAULT_PARTITIONING, partitioner); 325 } 326 } 327 return partitioners; 328 } 329 330 338 public static void addDocumentPartitioners(IDocument document, Map partitioners) { 339 if (document instanceof IDocumentExtension3) { 340 IDocumentExtension3 extension3= (IDocumentExtension3) document; 341 Iterator e= partitioners.keySet().iterator(); 342 while (e.hasNext()) { 343 String partitioning= (String ) e.next(); 344 IDocumentPartitioner partitioner= (IDocumentPartitioner) partitioners.get(partitioning); 345 partitioner.connect(document); 346 extension3.setDocumentPartitioner(partitioning, partitioner); 347 } 348 partitioners.clear(); 349 } else { 350 IDocumentPartitioner partitioner= (IDocumentPartitioner) partitioners.get(IDocumentExtension3.DEFAULT_PARTITIONING); 351 partitioner.connect(document); 352 document.setDocumentPartitioner(partitioner); 353 } 354 } 355 356 369 public static String getContentType(IDocument document, String partitioning, int offset, boolean preferOpenPartitions) throws BadLocationException { 370 if (document instanceof IDocumentExtension3) { 371 IDocumentExtension3 extension3= (IDocumentExtension3) document; 372 try { 373 return extension3.getContentType(partitioning, offset, preferOpenPartitions); 374 } catch (BadPartitioningException x) { 375 return IDocument.DEFAULT_CONTENT_TYPE; 376 } 377 } 378 379 return document.getContentType(offset); 380 } 381 382 396 public static ITypedRegion getPartition(IDocument document, String partitioning, int offset, boolean preferOpenPartitions) throws BadLocationException { 397 if (document instanceof IDocumentExtension3) { 398 IDocumentExtension3 extension3= (IDocumentExtension3) document; 399 try { 400 return extension3.getPartition(partitioning, offset, preferOpenPartitions); 401 } catch (BadPartitioningException x) { 402 return new TypedRegion(0, document.getLength(), IDocument.DEFAULT_CONTENT_TYPE); 403 } 404 } 405 406 return document.getPartition(offset); 407 } 408 409 424 public static ITypedRegion[] computePartitioning(IDocument document, String partitioning, int offset, int length, boolean includeZeroLengthPartitions) throws BadLocationException { 425 if (document instanceof IDocumentExtension3) { 426 IDocumentExtension3 extension3= (IDocumentExtension3) document; 427 try { 428 return extension3.computePartitioning(partitioning, offset, length, includeZeroLengthPartitions); 429 } catch (BadPartitioningException x) { 430 return new ITypedRegion[0]; 431 } 432 } 433 434 return document.computePartitioning(offset, length); 435 } 436 437 445 public static String [] computePartitionManagingCategories(IDocument document) { 446 if (document instanceof IDocumentExtension3) { 447 IDocumentExtension3 extension3= (IDocumentExtension3) document; 448 String [] partitionings= extension3.getPartitionings(); 449 if (partitionings != null) { 450 Set categories= new HashSet (); 451 for (int i= 0; i < partitionings.length; i++) { 452 IDocumentPartitioner p= extension3.getDocumentPartitioner(partitionings[i]); 453 if (p instanceof IDocumentPartitionerExtension2) { 454 IDocumentPartitionerExtension2 extension2= (IDocumentPartitionerExtension2) p; 455 String [] c= extension2.getManagingPositionCategories(); 456 if (c != null) { 457 for (int j= 0; j < c.length; j++) 458 categories.add(c[j]); 459 } 460 } 461 } 462 String [] result= new String [categories.size()]; 463 categories.toArray(result); 464 return result; 465 } 466 } 467 return null; 468 } 469 470 479 public static String getDefaultLineDelimiter(IDocument document) { 480 481 if (document instanceof IDocumentExtension4) 482 return ((IDocumentExtension4)document).getDefaultLineDelimiter(); 483 484 String lineDelimiter= null; 485 486 try { 487 lineDelimiter= document.getLineDelimiter(0); 488 } catch (BadLocationException x) { 489 } 490 491 if (lineDelimiter != null) 492 return lineDelimiter; 493 494 String sysLineDelimiter= System.getProperty("line.separator"); String [] delimiters= document.getLegalLineDelimiters(); 496 Assert.isTrue(delimiters.length > 0); 497 for (int i= 0; i < delimiters.length; i++) { 498 if (delimiters[i].equals(sysLineDelimiter)) { 499 lineDelimiter= sysLineDelimiter; 500 break; 501 } 502 } 503 504 if (lineDelimiter == null) 505 lineDelimiter= delimiters[0]; 506 507 return lineDelimiter; 508 } 509 510 519 public static boolean overlaps(IRegion left, IRegion right) { 520 521 if (left == null || right == null) 522 return false; 523 524 int rightEnd= right.getOffset() + right.getLength(); 525 int leftEnd= left.getOffset()+ left.getLength(); 526 527 if (right.getLength() > 0) { 528 if (left.getLength() > 0) 529 return left.getOffset() < rightEnd && right.getOffset() < leftEnd; 530 return right.getOffset() <= left.getOffset() && left.getOffset() < rightEnd; 531 } 532 533 if (left.getLength() > 0) 534 return left.getOffset() <= right.getOffset() && right.getOffset() < leftEnd; 535 536 return left.getOffset() == right.getOffset(); 537 } 538 539 546 public static String [] copy(String [] array) { 547 if (array != null) { 548 String [] copy= new String [array.length]; 549 System.arraycopy(array, 0, copy, 0, array.length); 550 return copy; 551 } 552 return null; 553 } 554 555 562 public static int[] copy(int[] array) { 563 if (array != null) { 564 int[] copy= new int[array.length]; 565 System.arraycopy(array, 0, copy, 0, array.length); 566 return copy; 567 } 568 return null; 569 } 570 } 571 | Popular Tags |