1 19 20 package org.netbeans.editor; 21 22 import java.io.Reader ; 23 import java.io.Writer ; 24 import java.io.File ; 25 import java.io.FileReader ; 26 import java.io.IOException ; 27 import java.util.List ; 28 import java.util.Iterator ; 29 30 import javax.swing.text.BadLocationException ; 31 import javax.swing.text.Segment ; 32 33 39 40 public class Analyzer { 41 42 43 private static Object platformLS; 44 45 46 public static final char[] EMPTY_CHAR_ARRAY = new char[0]; 47 48 49 private static char spacesBuffer[] = new char[] { ' ' }; 50 51 52 private static char tabsBuffer[] = new char[] { '\t' }; 53 54 55 private static final int MAX_CACHED_SPACES_STRING_LENGTH = 50; 56 57 58 private static final String [] spacesStrings 59 = new String [MAX_CACHED_SPACES_STRING_LENGTH + 1]; 60 61 static { 62 spacesStrings[0] = ""; 63 spacesStrings[MAX_CACHED_SPACES_STRING_LENGTH] 64 = new String (getSpacesBuffer(MAX_CACHED_SPACES_STRING_LENGTH), 65 0, MAX_CACHED_SPACES_STRING_LENGTH); 66 } 67 68 private Analyzer() { 69 } 71 72 73 public static Object getPlatformLS() { 74 if (platformLS == null) { 75 platformLS = System.getProperty("line.separator"); } 77 return platformLS; 78 } 79 80 85 public static String testLS(char chars[], int len) { 86 for (int i = 0; i < len; i++) { 87 switch (chars[i]) { 88 case '\r': 89 if (i + 1 < len && chars[i + 1] == '\n') { 90 return BaseDocument.LS_CRLF; 91 } else { 92 return BaseDocument.LS_CR; 93 } 94 95 case '\n': 96 return BaseDocument.LS_LF; 97 } 98 } 99 return null; } 101 102 110 public static int convertLSToLF(char chars[], int len) { 111 int tgtOffset = 0; 112 short lsLen = 0; int moveStart = 0; int moveLen; 116 for (int i = 0; i < len; i++) { 117 if (chars[i] == '\r') { if (i + 1 < len && chars[i + 1] == '\n') { lsLen = 2; } else { 122 lsLen = 1; } 124 } 125 126 if (lsLen > 0) { 127 moveLen = i - moveStart; 128 if (moveLen > 0) { 129 if (tgtOffset != moveStart) { System.arraycopy(chars, moveStart, chars, tgtOffset, moveLen); 131 } 132 tgtOffset += moveLen; 133 } 134 chars[tgtOffset++] = '\n'; 135 moveStart += moveLen + lsLen; i += lsLen - 1; lsLen = 0; } 139 } 140 141 moveLen = len - moveStart; 143 if (moveLen > 0) { 144 if (tgtOffset != moveStart) { 145 System.arraycopy(chars, moveStart, chars, tgtOffset, moveLen); 146 } 147 tgtOffset += moveLen; 148 } 149 150 return tgtOffset; } 152 153 157 public static String convertLSToLF(String text) { 158 char[] tgtChars = null; 159 int tgtOffset = 0; 160 short lsLen = 0; int moveStart = 0; int moveLen; int textLen = text.length(); 164 165 for (int i = 0; i < textLen; i++) { 166 if (text.charAt(i) == '\r') { if (i + 1 < textLen && text.charAt(i + 1) == '\n') { lsLen = 2; } else { 171 lsLen = 1; } 173 } 174 175 if (lsLen > 0) { 176 if (tgtChars == null) { 177 tgtChars = new char[textLen]; 178 text.getChars(0, textLen, tgtChars, 0); } 180 moveLen = i - moveStart; 181 if (moveLen > 0) { 182 if (tgtOffset != moveStart) { text.getChars(moveStart, moveStart + moveLen, tgtChars, tgtOffset); 184 } 185 tgtOffset += moveLen; 186 } 187 tgtChars[tgtOffset++] = '\n'; 188 moveStart += moveLen + lsLen; i += lsLen - 1; lsLen = 0; } 192 } 193 194 moveLen = textLen - moveStart; 196 if (moveLen > 0) { 197 if (tgtOffset != moveStart) { 198 text.getChars(moveStart, moveStart + moveLen, tgtChars, tgtOffset); 199 } 200 tgtOffset += moveLen; 201 } 202 203 return (tgtChars == null) ? text : new String (tgtChars, 0, tgtOffset); 204 } 205 206 public static boolean isSpace(String s) { 207 int len = s.length(); 208 for (int i = 0; i < len; i++) { 209 if (s.charAt(i) != ' ') { 210 return false; 211 } 212 } 213 return true; 214 } 215 216 217 public static boolean isSpace(char[] chars, int offset, int len) { 218 while (len > 0) { 219 if (chars[offset++] != ' ') { 220 return false; 221 } 222 len--; 223 } 224 return true; 225 } 226 227 228 public static boolean isWhitespace(char[] chars, int offset, int len) { 229 while (len > 0) { 230 if (!Character.isWhitespace(chars[offset])) { 231 return false; 232 } 233 offset++; 234 len--; 235 } 236 return true; 237 } 238 239 240 public static int findFirstNonTab(char[] chars, int offset, int len) { 241 while (len > 0) { 242 if (chars[offset] != '\t') { 243 return offset; 244 } 245 offset++; 246 len--; 247 } 248 return -1; 249 } 250 251 252 public static int findFirstNonSpace(char[] chars, int offset, int len) { 253 while (len > 0) { 254 if (chars[offset] != ' ') { 255 return offset; 256 } 257 offset++; 258 len--; 259 } 260 return -1; 261 } 262 263 264 public static int findFirstNonWhite(char[] chars, int offset, int len) { 265 while (len > 0) { 266 if (!Character.isWhitespace(chars[offset])) { 267 return offset; 268 } 269 offset++; 270 len--; 271 } 272 return -1; 273 } 274 275 276 public static int findLastNonWhite(char[] chars, int offset, int len) { 277 int i = offset + len - 1; 278 while (i >= offset) { 279 if (!Character.isWhitespace(chars[i])) { 280 return i; 281 } 282 i--; 283 } 284 return -1; 285 } 286 287 290 public static int getLFCount(char chars[]) { 291 return getLFCount(chars, 0, chars.length); 292 } 293 294 public static int getLFCount(char chars[], int offset, int len) { 295 int lfCount = 0; 296 while (len > 0) { 297 if (chars[offset++] == '\n') { 298 lfCount++; 299 } 300 len--; 301 } 302 return lfCount; 303 } 304 305 public static int getLFCount(String s) { 306 int lfCount = 0; 307 int len = s.length(); 308 for (int i = 0; i < len; i++) { 309 if (s.charAt(i) == '\n') { 310 lfCount++; 311 } 312 } 313 return lfCount; 314 } 315 316 public static int findFirstLFOffset(char[] chars, int offset, int len) { 317 while (len > 0) { 318 if (chars[offset++] == '\n') { 319 return offset - 1; 320 } 321 len--; 322 } 323 return -1; 324 } 325 326 public static int findFirstLFOffset(String s) { 327 int len = s.length(); 328 for (int i = 0; i < len; i++) { 329 if (s.charAt(i) == '\n') { 330 return i; 331 } 332 } 333 return -1; 334 } 335 336 public static int findFirstTab(char[] chars, int offset, int len) { 337 while (len > 0) { 338 if (chars[offset++] == '\t') { 339 return offset - 1; 340 } 341 len--; 342 } 343 return -1; 344 } 345 346 public static int findFirstTabOrLF(char[] chars, int offset, int len) { 347 while (len > 0) { 348 switch (chars[offset++]) { 349 case '\t': 350 case '\n': 351 return offset - 1; 352 } 353 len--; 354 } 355 return -1; 356 } 357 358 361 public static void reverse(char[] chars, int len) { 362 for (int i = ((--len - 1) >> 1); i >= 0; --i) { 363 char ch = chars[i]; 364 chars[i] = chars[len - i]; 365 chars[len - i] = ch; 366 } 367 } 368 369 public static boolean equals(String s, char[] chars) { 370 return equals(s, chars, 0, chars.length); 371 } 372 373 public static boolean equals(String s, char[] chars, int offset, int len) { 374 if (s.length() != len) { 375 return false; 376 } 377 for (int i = 0; i < len; i++) { 378 if (s.charAt(i) != chars[offset + i]) { 379 return false; 380 } 381 } 382 return true; 383 } 384 385 399 public static void initialRead(BaseDocument doc, Reader reader, boolean testLS) 400 throws IOException { 401 if (doc.getLength() > 0) { 403 return; 404 } 405 406 if (reader != null) { 408 int readBufferSize = ((Integer )doc.getProperty( 410 SettingsNames.READ_BUFFER_SIZE)).intValue(); 411 412 if (testLS) { 413 reader = new LineSeparatorConversion.InitialSeparatorReader(reader); 415 } 416 417 418 LineSeparatorConversion.ToLineFeed toLF = new LineSeparatorConversion.ToLineFeed(reader, readBufferSize); 419 420 boolean firstRead = true; int pos = 0; int line = 0; int maxLineLength = 0; int lineStartPos = 0; int markCount = 0; 427 448 449 Segment text = toLF.nextConverted(); 451 while (text != null) { 452 try { 453 doc.insertString(pos, new String (text.array, text.offset, text.count), null); 454 } catch (BadLocationException e) { 455 throw new IllegalStateException (e.toString()); 456 } 457 pos += text.count; 458 text = toLF.nextConverted(); 459 } 460 461 if (testLS) { 462 doc.putProperty(BaseDocument.READ_LINE_SEPARATOR_PROP, 463 ((LineSeparatorConversion.InitialSeparatorReader)reader).getInitialSeparator()); 464 } 469 470 490 491 494 } 495 } 496 497 498 static void read(BaseDocument doc, Reader reader, int pos) 499 throws BadLocationException , IOException { 500 int readBufferSize = ((Integer )doc.getProperty( 501 SettingsNames.READ_BUFFER_SIZE)).intValue(); 502 LineSeparatorConversion.ToLineFeed toLF 503 = new LineSeparatorConversion.ToLineFeed(reader, readBufferSize); 504 505 Segment text = toLF.nextConverted(); 506 while (text != null) { 507 doc.insertString(pos, new String (text.array, text.offset, text.count), null); 508 pos += text.count; 509 text = toLF.nextConverted(); 510 } 511 } 512 513 514 static void write(BaseDocument doc, Writer writer, int pos, int len) 515 throws BadLocationException , IOException { 516 String lsType = (String )doc.getProperty(BaseDocument.WRITE_LINE_SEPARATOR_PROP); 517 if (lsType == null) { 518 lsType = (String )doc.getProperty(BaseDocument.READ_LINE_SEPARATOR_PROP); 519 if (lsType == null) { 520 lsType = BaseDocument.LS_LF; 521 } 522 } 523 int writeBufferSize = ((Integer )doc.getProperty( 524 SettingsNames.WRITE_BUFFER_SIZE)).intValue(); 525 char[] getBuf = new char[writeBufferSize]; 526 char[] writeBuf = new char[2 * writeBufferSize]; 527 int actLen = 0; 528 529 while (len > 0) { 530 actLen = Math.min(len, writeBufferSize); 531 doc.getChars(pos, getBuf, 0, actLen); 532 int tgtLen = convertLFToLS(getBuf, actLen, writeBuf, lsType); 533 writer.write(writeBuf, 0, tgtLen); 534 pos += actLen; 535 len -= actLen; 536 } 537 538 543 544 } 545 546 547 public static int getColumn(char buffer[], int offset, 548 int len, int tabSize, int startCol) { 549 int col = startCol; 550 int endOffset = offset + len; 551 552 if (tabSize <= 0) { 554 new Exception ("Wrong tab size=" + tabSize).printStackTrace(); tabSize = 8; 556 } 557 558 while (offset < endOffset) { 559 switch (buffer[offset++]) { 560 case '\t': 561 col = (col + tabSize) / tabSize * tabSize; 562 break; 563 default: 564 col++; 565 } 566 } 567 return col; 568 } 569 570 574 public static synchronized char[] getSpacesBuffer(int numSpaces) { 575 while (numSpaces > spacesBuffer.length) { 577 char tmpBuf[] = new char[spacesBuffer.length * 2]; System.arraycopy(spacesBuffer, 0, tmpBuf, 0, spacesBuffer.length); 579 System.arraycopy(spacesBuffer, 0, tmpBuf, spacesBuffer.length, spacesBuffer.length); 580 spacesBuffer = tmpBuf; 581 } 582 583 return spacesBuffer; 584 } 585 586 590 public static synchronized String getSpacesString(int numSpaces) { 591 if (numSpaces <= MAX_CACHED_SPACES_STRING_LENGTH) { String ret = spacesStrings[numSpaces]; 593 if (ret == null) { 594 ret = spacesStrings[MAX_CACHED_SPACES_STRING_LENGTH].substring(0, numSpaces); 595 spacesStrings[numSpaces] = ret; 596 } 597 598 return ret; 599 600 } else { return new String (getSpacesBuffer(numSpaces), 0, numSpaces); 602 } 603 } 604 605 608 public static char[] createSpacesBuffer(int numSpaces) { 609 char[] ret = new char[numSpaces]; 610 System.arraycopy(getSpacesBuffer(numSpaces), 0, ret, 0, numSpaces); 611 return ret; 612 } 613 614 618 public static char[] getTabsBuffer(int numTabs) { 619 if (numTabs > tabsBuffer.length) { 621 char tmpBuf[] = new char[numTabs * 2]; 623 for (int i = 0; i < tmpBuf.length; i += tabsBuffer.length) { 625 System.arraycopy(tabsBuffer, 0, tmpBuf, i, 626 Math.min(tabsBuffer.length, tmpBuf.length - i)); 627 } 628 tabsBuffer = tmpBuf; 629 } 630 631 return tabsBuffer; 632 } 633 634 639 public static String getIndentString(int indent, boolean expandTabs, int tabSize) { 640 return getWhitespaceString(0, indent, expandTabs, tabSize); 641 } 642 643 648 public static String getWhitespaceString(int startCol, int endCol, 649 boolean expandTabs, int tabSize) { 650 return (expandTabs || tabSize <= 0) 651 ? getSpacesString(endCol - startCol) 652 : new String (createWhiteSpaceFillBuffer(startCol, endCol, tabSize)); 653 } 654 655 658 public static char[] createWhiteSpaceFillBuffer(int startCol, int endCol, 659 int tabSize) { 660 return createWhitespaceFillBuffer(startCol, endCol, tabSize); 661 } 662 663 669 public static char[] createWhitespaceFillBuffer(int startCol, int endCol, 670 int tabSize) { 671 if (startCol >= endCol) { 672 return EMPTY_CHAR_ARRAY; 673 } 674 675 if (tabSize <= 0) { 677 new Exception ("Wrong tab size=" + tabSize).printStackTrace(); tabSize = 8; 679 } 680 681 int tabs = 0; 682 int spaces = 0; 683 int nextTab = (startCol + tabSize) / tabSize * tabSize; 684 if (nextTab > endCol) { spaces += endCol - startCol; 686 } else { tabs++; int endSpaces = endCol - endCol / tabSize * tabSize; 689 tabs += (endCol - endSpaces - nextTab) / tabSize; 690 spaces += endSpaces; 691 } 692 693 char[] ret = new char[tabs + spaces]; 694 if (tabs > 0) { 695 System.arraycopy(getTabsBuffer(tabs), 0, ret, 0, tabs); 696 } 697 if (spaces > 0) { 698 System.arraycopy(getSpacesBuffer(spaces), 0, ret, tabs, spaces); 699 } 700 return ret; 701 } 702 703 708 public static char[] loadFile(String fileName) throws IOException { 709 File file = new File (fileName); 710 char chars[] = new char[(int)file.length()]; 711 FileReader reader = new FileReader (file); 712 reader.read(chars); 713 reader.close(); 714 int len = Analyzer.convertLSToLF(chars, chars.length); 715 if (len != chars.length) { 716 char copyChars[] = new char[len]; 717 System.arraycopy(chars, 0, copyChars, 0, len); 718 chars = copyChars; 719 } 720 return chars; 721 } 722 723 736 public static int convertLFToLS(char[] src, int len, char[] tgt, String lsType) { 737 if (lsType.equals(BaseDocument.LS_CR)) { System.arraycopy(src, 0, tgt, 0, len); 739 740 if (lsType == BaseDocument.LS_CR) { char chars[] = tgt; 743 for (int i = 0; i < len; i++) { 744 if (chars[i] == '\n') { 745 chars[i] = '\r'; 746 } 747 } 748 } 749 return len; 750 } else if (lsType.equals(BaseDocument.LS_CRLF)) { 751 int tgtLen = 0; 752 int moveStart = 0; int moveLen; 755 for (int i = 0; i < len; i++) { 756 if (src[i] == '\n') { moveLen = i - moveStart; 758 if (moveLen > 0) { System.arraycopy(src, moveStart, tgt, tgtLen, moveLen); 760 tgtLen += moveLen; 761 } 762 tgt[tgtLen++] = '\r'; 763 tgt[tgtLen++] = '\n'; 764 moveStart = i + 1; } 766 } 767 768 moveLen = len - moveStart; 770 if (moveLen > 0) { 771 System.arraycopy(src, moveStart, tgt, tgtLen, moveLen); 772 tgtLen += moveLen; 773 } 774 return tgtLen; 775 } else { System.arraycopy(src,0, tgt, 0, len); 777 return len; 778 } 779 } 780 781 public static boolean startsWith(char[] chars, char[] prefix) { 782 if (chars == null || chars.length < prefix.length) { 783 return false; 784 } 785 for (int i = 0; i < prefix.length; i++) { 786 if (chars[i] != prefix[i]) { 787 return false; 788 } 789 } 790 return true; 791 } 792 793 public static boolean endsWith(char[] chars, char[] suffix) { 794 if (chars == null || chars.length < suffix.length) { 795 return false; 796 } 797 for (int i = chars.length - suffix.length; i < chars.length; i++) { 798 if (chars[i] != suffix[i]) { 799 return false; 800 } 801 } 802 return true; 803 } 804 805 public static char[] concat(char[] chars1, char[] chars2) { 806 if (chars1 == null || chars1.length == 0) { 807 return chars2; 808 } 809 if (chars2 == null || chars2.length == 0) { 810 return chars1; 811 } 812 char[] ret = new char[chars1.length + chars2.length]; 813 System.arraycopy(chars1, 0, ret, 0, chars1.length); 814 System.arraycopy(chars2, 0, ret, chars1.length, chars2.length); 815 return ret; 816 } 817 818 public static char[] extract(char[] chars, int offset, int len) { 819 char[] ret = new char[len]; 820 System.arraycopy(chars, offset, ret, 0, len); 821 return ret; 822 } 823 824 public static boolean blocksHit(int[] blocks, int startPos, int endPos) { 825 return (blocksIndex(blocks, startPos, endPos) >= 0); 826 } 827 828 public static int blocksIndex(int[] blocks, int startPos, int endPos) { 829 if (blocks.length > 0) { 830 int onlyEven = ~1; 831 int low = 0; 832 int high = blocks.length - 2; 833 834 while (low <= high) { 835 int mid = ((low + high) / 2) & onlyEven; 836 837 if (blocks[mid + 1] <= startPos) { 838 low = mid + 2; 839 } else if (blocks[mid] >= endPos) { 840 high = mid - 2; 841 } else { 842 return low; } 844 } 845 } 846 847 return -1; 848 } 849 850 854 public static String removeSpaces(String s) { 855 int spcInd = s.indexOf(' '); 856 if (spcInd >= 0) { 857 StringBuffer sb = new StringBuffer (s.substring(0, spcInd)); 858 int sLen = s.length(); 859 for (int i = spcInd + 1; i < sLen; i++) { 860 char ch = s.charAt(i); 861 if (ch != ' ') { 862 sb.append(ch); 863 } 864 } 865 return sb.toString(); 866 } 867 return s; 868 } 869 870 } 871 | Popular Tags |