1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
| package de.soderer.utilities;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
/**
* Text Utilities
*
* This class does no Logging via Log4J, because it is often used before its initialisation
*/
public class TextUtilities {
/**
* A simple string for testing, which includes all ASCII characters
*/
public static final String ASCII_CHARACTERS_STRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
/**
* A simple string for testing, which includes all german characters
*/
public static final String GERMAN_TEST_STRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 äöüßÄÖܵ!?§@€$%&/\\<>(){}[]'\"´`^°²³*#.,;:=+-~_|";
/**
* Enum to represent a linebreak type of an text
*/
public enum LineBreakType {
Unknown,
Mixed,
Unix,
Mac,
Windows;
public static LineBreakType getLineBreakTypeByName(String lineBreakTypeName) {
if ("WINDOWS".equalsIgnoreCase(lineBreakTypeName)) {
return LineBreakType.Windows;
} else if ("UNIX".equalsIgnoreCase(lineBreakTypeName)) {
return LineBreakType.Unix;
} else if ("MAC".equalsIgnoreCase(lineBreakTypeName)) {
return LineBreakType.Mac;
} else {
return LineBreakType.Unknown;
}
}
}
/**
* Mac/Apple linebreak character
*/
public static String LinebreakMac = "\r";
/**
* Unix/Linux linebreak character
*/
public static String LinebreakUnix = "\n";
/**
* Windows linebreak characters
*/
public static String LinebreakWindows = "\r\n";
/**
* Trim a string to an exact length with alignment right for display purposes
* If the string underruns the length, it will be filled up with blanks on the left side.
* If the string exceeds the length, it will be cut from the left side.
*
* @param inputString
* @param length
* @return
*/
public static String trimStringToLengthAlignedRight(String inputString, int length) {
if (inputString.length() > length) {
// Takes the last characters of length
return inputString.subSequence(inputString.length() - length, inputString.length()).toString();
} else {
return String.format("%" + length + "s", inputString);
}
}
/**
* Trim a string to a exact length with alignment left for display purposes.
* If the string underruns the length, it will be filled up with blanks on the right side.
* If the string exceeds the length, it will be cut from the right side.
*
* @param inputString
* @param length
* @return
*/
public static String trimStringToLengthAlignedLeft(String inputString, int length) {
if (inputString.length() > length) {
// Takes the first characters of length
return inputString.subSequence(0, length).toString();
} else {
return String.format("%-" + length + "s", inputString);
}
}
/**
* Build a string with repetitions.
* 0 repetitions returns an empty string.
*
* @param itemString
* @param repeatTimes
* @return
*/
public static String repeatString(String itemString, int repeatTimes) {
return repeatString(itemString, repeatTimes, null);
}
/***
* Build a string with repetitions.
* 0 repetitions returns an empty string.
* In other cases there will be put a glue string between the reptitions, which can be left empty.
*
* @param itemString
* string to be repeated
* @param separatorString
* glue string
* @param repeatTimes
* Number of repetitions
* @return
*/
public static String repeatString(String itemString, int repeatTimes, String separatorString) {
StringBuilder returnStringBuilder = new StringBuilder();
for (int i = 0; i < repeatTimes; i++) {
if (separatorString != null && returnStringBuilder.length() > 0) {
returnStringBuilder.append(separatorString);
}
returnStringBuilder.append(itemString);
}
return returnStringBuilder.toString();
}
/**
* Split a string into smaller strings of a maximum size
*
* @param text
* @param chunkSize
* @return
*/
public static List<String> chopToChunks(String text, int chunkSize) {
List<String> returnList = new ArrayList<String>((text.length() + chunkSize - 1) / chunkSize);
for (int start = 0; start < text.length(); start += chunkSize) {
returnList.add(text.substring(start, Math.min(text.length(), start + chunkSize)));
}
return returnList;
}
/**
* Reduce a text to a maximum number of lines.
* The type of linebreaks in the result string will not be changed.
* If the text has less lines then maximum it will be returned unchanged.
*
* @param value
* @param maxLines
* @return
*/
public static String trimToMaxNumberOfLines(String value, int maxLines) {
String normalizedValue = normalizeLineBreaks(value, LineBreakType.Unix);
int count = 0;
int nextLinebreak = 0;
while (nextLinebreak != -1 && count < maxLines) {
nextLinebreak = normalizedValue.indexOf(LinebreakUnix, nextLinebreak + 1);
count++;
}
if (nextLinebreak != -1) {
LineBreakType originalLineBreakType = detectLinebreakType(value);
return normalizeLineBreaks(normalizedValue.substring(0, nextLinebreak + 1), originalLineBreakType) + "...";
} else {
return value;
}
}
/**
* Detect the type of linebreaks in a text.
*
* @param value
* @return
*/
public static LineBreakType detectLinebreakType(String value) {
TextPropertiesReader textPropertiesReader = new TextPropertiesReader(value);
textPropertiesReader.readProperties();
return textPropertiesReader.getLinebreakType();
}
/**
* Change all linebreaks of a text, no matter what type they are, to a given type
*
* @param value
* @param type
* @return
*/
public static String normalizeLineBreaks(String value, LineBreakType type) {
String returnString = value.replace(LinebreakWindows, LinebreakUnix).replace(LinebreakMac, LinebreakUnix);
if (type == LineBreakType.Mac) {
return returnString.replace(LinebreakUnix, LinebreakMac);
} else if (type == LineBreakType.Windows) {
return returnString.replace(LinebreakUnix, LinebreakWindows);
} else {
return returnString;
}
}
/**
* Get the text lines of a text as a list of strings
*
* @param dataString
* @return
* @throws IOException
*/
public static List<String> getLines(String dataString) throws IOException {
List<String> list = new ArrayList<String>();
LineNumberReader lineNumberReader = null;
try {
lineNumberReader = new LineNumberReader(new StringReader(dataString));
String line;
while ((line = lineNumberReader.readLine()) != null) {
list.add(line);
}
return list;
} finally {
IOUtils.closeQuietly(lineNumberReader);
}
}
/**
* Get the number of lines in a text
*
* @param dataString
* @return
* @throws IOException
*/
public static int getLineCount(String dataString) throws IOException {
if (dataString == null) {
return 0;
} else if ("".equals(dataString)) {
return 1;
} else {
LineNumberReader lineNumberReader = null;
try {
lineNumberReader = new LineNumberReader(new StringReader(dataString));
while (lineNumberReader.readLine() != null) {
// do nothing
}
return lineNumberReader.getLineNumber();
} finally {
IOUtils.closeQuietly(lineNumberReader);
}
}
}
/**
* Count the number of words by whitespaces in a text separated
*
* @param dataString
* @return
*/
public static int countWords(String dataString) {
int counter = 0;
boolean isWord = false;
int endOfLine = dataString.length() - 1;
for (int i = 0; i < dataString.length(); i++) {
if (!Character.isWhitespace(dataString.charAt(i)) && i != endOfLine) {
isWord = true;
} else if (Character.isWhitespace(dataString.charAt(i)) && isWord) {
counter++;
isWord = false;
} else if (!Character.isWhitespace(dataString.charAt(i)) && i == endOfLine) {
counter++;
}
}
return counter;
}
/**
* Get the start index of a line within a text
*
* @param dataString
* @param lineNumber
* @return
*/
public static int getStartIndexOfLine(String dataString, int lineNumber) {
if (dataString == null || lineNumber < 0) {
return -1;
} else if (lineNumber == 1) {
return 0;
} else {
int lineCount = 1;
int position = 0;
while (lineCount < lineNumber) {
int nextLineBreakMac = dataString.indexOf(LinebreakMac, position);
int nextLineBreakUnix = dataString.indexOf(LinebreakUnix, position);
int nextLineBreakWindows = dataString.indexOf(LinebreakWindows, position);
int nextPosition = -1;
int lineBreakSize = 0;
if (nextLineBreakMac >= 0 && (nextLineBreakUnix < 0 || nextLineBreakMac < nextLineBreakUnix) && (nextLineBreakWindows < 0 || nextLineBreakMac < nextLineBreakWindows)) {
nextPosition = nextLineBreakMac;
lineBreakSize = 1;
} else if (nextLineBreakUnix >= 0 && (nextLineBreakWindows < 0 || nextLineBreakUnix < nextLineBreakWindows)) {
nextPosition = nextLineBreakUnix;
lineBreakSize = 1;
} else if (nextLineBreakWindows >= 0) {
nextPosition = nextLineBreakWindows;
lineBreakSize = 2;
}
if (nextPosition >= 0) {
position = nextPosition + lineBreakSize;
if (position >= dataString.length()) {
break;
}
} else {
break;
}
lineCount++;
}
return position;
}
}
/**
* Get the startindex of the line at a given position within the text
*
* @param dataString
* @param index
* @return
*/
public static int getStartIndexOfLineAtIndex(String dataString, int index) {
if (dataString == null || index < 0) {
return -1;
} else if (index == 0) {
return 0;
} else {
int nextLineBreakMac = dataString.lastIndexOf(LinebreakMac, index);
int nextLineBreakUnix = dataString.lastIndexOf(LinebreakUnix, index);
int nextLineBreakWindows = dataString.lastIndexOf(LinebreakWindows, index);
if (nextLineBreakMac >= 0 && (nextLineBreakUnix < 0 || nextLineBreakMac < nextLineBreakUnix) && (nextLineBreakWindows < 0 || nextLineBreakMac < nextLineBreakWindows)) {
return nextLineBreakMac + LinebreakMac.length();
} else if (nextLineBreakUnix >= 0 && (nextLineBreakWindows < 0 || nextLineBreakUnix < nextLineBreakWindows)) {
return nextLineBreakUnix + LinebreakUnix.length();
} else if (nextLineBreakWindows >= 0) {
return nextLineBreakWindows + LinebreakWindows.length();
} else {
return 0;
}
}
}
/**
* Get the end index of a line within a text
*
* @param dataString
* @param index
* @return
*/
public static int getEndIndexOfLineAtIndex(String dataString, int index) {
if (dataString == null || index < 0) {
return -1;
} else if (index == 0) {
return 0;
} else {
int nextLineBreakMac = dataString.indexOf(LinebreakMac, index);
int nextLineBreakUnix = dataString.indexOf(LinebreakUnix, index);
int nextLineBreakWindows = dataString.indexOf(LinebreakWindows, index);
if (nextLineBreakMac >= 0 && (nextLineBreakUnix < 0 || nextLineBreakMac > nextLineBreakUnix) && (nextLineBreakWindows < 0 || nextLineBreakMac > nextLineBreakWindows)) {
return nextLineBreakMac;
} else if (nextLineBreakUnix >= 0 && (nextLineBreakWindows < 0 || nextLineBreakUnix - 1 > nextLineBreakWindows)) {
return nextLineBreakUnix;
} else if (nextLineBreakWindows >= 0) {
return nextLineBreakWindows;
} else {
return dataString.length();
}
}
}
/**
* Searching the next position of a string or searchPattern beginning at a startIndex.
* Searched text can be a simple string or a regex pattern, switching this with the flag searchTextIsRegularExpression.
* Search can be done caseinsensitive or caseinsensitive by the flag searchCaseSensitive.
* Search can be done from end to start, backwards by the flag searchReversely.
*
*
* @param text
* @param startPosition
* @param searchTextOrPattern
* @param searchTextIsRegularExpression
* @param searchCaseSensitive
* @param searchReversely
* @return Tuple First = Startindex, Second = Length of found substring
*/
public static Tuple<Integer, Integer> searchNextPosition(String text, int startPosition, String searchTextOrPattern, boolean searchTextIsRegularExpression, boolean searchCaseSensitive, boolean searchReversely) {
if (StringUtils.isBlank(text) || startPosition < 0 || text.length() < startPosition || StringUtils.isEmpty(searchTextOrPattern)) {
return null;
} else {
String fullText = text;
int nextPosition = -1;
int length = -1;
Pattern searchPattern;
if (searchTextIsRegularExpression) {
if (searchCaseSensitive) {
searchPattern = Pattern.compile(searchTextOrPattern, Pattern.MULTILINE);
} else {
searchPattern = Pattern.compile(searchTextOrPattern, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
}
} else {
if (searchCaseSensitive) {
searchPattern = Pattern.compile(searchTextOrPattern, Pattern.LITERAL | Pattern.MULTILINE);
} else {
searchPattern = Pattern.compile(searchTextOrPattern, Pattern.CASE_INSENSITIVE | Pattern.LITERAL | Pattern.MULTILINE);
}
}
Matcher matcher = searchPattern.matcher(fullText);
if (searchReversely) {
while (matcher.find()) {
if (matcher.start() < startPosition) {
nextPosition = matcher.start();
length = matcher.end() - nextPosition;
} else {
break;
}
}
if (nextPosition < 0) {
if (matcher.start() > 0) {
nextPosition = matcher.start();
length = matcher.end() - nextPosition;
}
while (matcher.find()) {
nextPosition = matcher.start();
length = matcher.end() - nextPosition;
}
}
} else {
if (matcher.find(startPosition)) {
nextPosition = matcher.start();
length = matcher.end() - nextPosition;
}
if (startPosition > 0 && nextPosition < 0) {
if (matcher.find(0)) {
nextPosition = matcher.start();
length = matcher.end() - nextPosition;
}
}
}
if (nextPosition < 0) {
return null;
} else {
return new Tuple<Integer, Integer>(nextPosition, length);
}
}
}
/**
* Count the occurences of a string or pattern within a text
*
* @param text
* @param searchTextOrPattern
* @param searchTextIsRegularExpression
* @param searchCaseSensitive
* @return
*/
public static int countOccurences(String text, String searchTextOrPattern, boolean searchTextIsRegularExpression, boolean searchCaseSensitive) {
if (text != null && searchTextOrPattern != null) {
String fullText = text;
int occurences = 0;
Pattern searchPattern;
if (searchTextIsRegularExpression) {
if (searchCaseSensitive) {
searchPattern = Pattern.compile(searchTextOrPattern, Pattern.MULTILINE);
} else {
searchPattern = Pattern.compile(searchTextOrPattern, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
}
} else {
if (searchCaseSensitive) {
searchPattern = Pattern.compile(searchTextOrPattern, Pattern.LITERAL | Pattern.MULTILINE);
} else {
searchPattern = Pattern.compile(searchTextOrPattern, Pattern.CASE_INSENSITIVE | Pattern.LITERAL | Pattern.MULTILINE);
}
}
Matcher matcher = searchPattern.matcher(fullText);
while (matcher.find()) {
occurences++;
}
return occurences;
} else {
return -1;
}
}
/**
* Get line number at a given text position
*
* @param dataString
* @param textPosition
* @return
*/
public static int getLineNumberOfTextposition(String dataString, int textPosition) {
if (dataString == null) {
return -1;
} else {
try {
String textPart = dataString;
if (dataString.length() > textPosition) {
textPart = dataString.substring(0, textPosition);
}
int lineNumber = getLineCount(textPart);
if (textPart.endsWith(LinebreakUnix) || textPart.endsWith(LinebreakMac)) {
lineNumber++;
}
return lineNumber;
} catch (IOException e) {
e.printStackTrace();
return -1;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}
/**
* Get the number of the first line containing a string or pattern
*
* @param dataString
* @param searchTextOrPattern
* @param searchCaseSensitive
* @param searchTextIsRegularExpression
* @return
* @throws IOException
*/
public static int getNumberOfLineContainingText(String dataString, String searchTextOrPattern, boolean searchCaseSensitive, boolean searchTextIsRegularExpression) throws IOException {
LineNumberReader lineNumberReader = null;
try {
Pattern searchPattern;
if (searchTextIsRegularExpression) {
if (searchCaseSensitive) {
searchPattern = Pattern.compile(searchTextOrPattern, Pattern.MULTILINE);
} else {
searchPattern = Pattern.compile(searchTextOrPattern, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
}
} else {
if (searchCaseSensitive) {
searchPattern = Pattern.compile(searchTextOrPattern, Pattern.LITERAL | Pattern.MULTILINE);
} else {
searchPattern = Pattern.compile(searchTextOrPattern, Pattern.CASE_INSENSITIVE | Pattern.LITERAL | Pattern.MULTILINE);
}
}
lineNumberReader = new LineNumberReader(new StringReader(dataString));
String lineContent;
while ((lineContent = lineNumberReader.readLine()) != null) {
Matcher matcher = searchPattern.matcher(lineContent);
if (matcher.find()) {
return lineNumberReader.getLineNumber();
}
}
return -1;
} finally {
IOUtils.closeQuietly(lineNumberReader);
}
}
/**
* Insert some string before a given line into a text
*
* @param dataString
* @param insertText
* @param lineNumber
* @return
* @throws IOException
*/
public static String insertTextBeforeLine(String dataString, String insertText, int lineNumber) throws IOException {
if (lineNumber > -1) {
int startIndex = TextUtilities.getStartIndexOfLine(dataString, lineNumber);
StringBuilder dataBuilder = new StringBuilder(dataString);
dataBuilder.insert(startIndex, insertText);
return dataBuilder.toString();
} else {
return dataString;
}
}
/**
* Insert some string at the end of a given line into a text
*
* @param dataString
* @param insertText
* @param lineNumber
* @return
* @throws IOException
*/
public static String insertTextAfterLine(String dataString, String insertText, int lineNumber) throws IOException {
if (lineNumber > -1) {
int startIndex = TextUtilities.getStartIndexOfLine(dataString, lineNumber + 1);
StringBuilder dataBuilder = new StringBuilder(dataString);
dataBuilder.insert(startIndex, insertText);
return dataBuilder.toString();
} else {
return dataString;
}
}
/**
* Try to detect the encoding of a byte array xml data.
*
*
* @param data
* @return
* @throws UnsupportedEncodingException
*/
public static Tuple<String, Boolean> detectEncoding(byte[] data) throws UnsupportedEncodingException {
if (data.length > 2 && data[0] == Utilities.UTF_16_BE_BOM[0] && data[1] == Utilities.UTF_16_BE_BOM[1]) {
return new Tuple<String, Boolean>("UTF-16BE", true);
} else if (data.length > 2 && data[0] == Utilities.UTF_16_LE_BOM[0] && data[1] == Utilities.UTF_16_LE_BOM[1]) {
return new Tuple<String, Boolean>("UTF-16LE", true);
} else if (data.length > 3 && data[0] == Utilities.UTF_8_BOM[0] && data[1] == Utilities.UTF_8_BOM[1] && data[2] == Utilities.UTF_8_BOM[2]) {
return new Tuple<String, Boolean>("UTF-8", true);
} else {
// Detect Xml Encoding
try {
// Use first data part only to speed up
String interimString = new String(data, 0, Math.min(data.length, 100), "UTF-8").toLowerCase();
String reducedInterimString = interimString.replace("\u0000", "");
int encodingStart = reducedInterimString.indexOf("encoding");
if (encodingStart >= 0) {
encodingStart = reducedInterimString.indexOf("=", encodingStart);
if (encodingStart >= 0) {
encodingStart++;
while (reducedInterimString.charAt(encodingStart) == ' ') {
encodingStart++;
}
if (reducedInterimString.charAt(encodingStart) == '"' || reducedInterimString.charAt(encodingStart) == '\'') {
encodingStart++;
}
StringBuilder encodingString = new StringBuilder();
while (Character.isLetter(reducedInterimString.charAt(encodingStart)) || Character.isDigit(reducedInterimString.charAt(encodingStart)) || reducedInterimString.charAt(encodingStart) == '-') {
encodingString.append(reducedInterimString.charAt(encodingStart));
encodingStart++;
}
Charset.forName(encodingString.toString());
if (encodingString.toString().startsWith("utf-16") && data[0] == 0) {
return new Tuple<String, Boolean>("UTF-16BE", false);
} else if (encodingString.toString().startsWith("utf-16") && data[1] == 0) {
return new Tuple<String, Boolean>("UTF-16LE", false);
} else {
return new Tuple<String, Boolean>(encodingString.toString().toUpperCase(), false);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
String interimString = new String(data, "UTF-8").toLowerCase();
int zeroIndex = interimString.indexOf('\u0000');
if (zeroIndex >= 0 && zeroIndex <= 100) {
if (zeroIndex % 2 == 0) {
return new Tuple<String, Boolean>("UTF-16BE", false);
} else {
return new Tuple<String, Boolean>("UTF-16LE", false);
}
}
return null;
}
}
/**
* Replace the leading indentation by double blanks of a text by some other character like tab
*
* @param data
* @param blankCount
* @param replacement
* @return
*/
public static String replaceIndentingBlanks(String data, int blankCount, String replacement) {
String result = data;
Pattern pattern = Pattern.compile("^( )+", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(result);
while (matcher.find()) {
int replacementCount = (matcher.end() - matcher.start()) / blankCount;
result = matcher.replaceFirst(StringUtils.repeat(replacement, replacementCount));
matcher = pattern.matcher(result);
}
return result;
}
/**
* Remove the first leading whitespace of a string
*
* @param data
* @return
*/
public static String removeFirstLeadingWhitespace(String data) {
Pattern pattern = Pattern.compile("^[\\t ]", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(data);
return matcher.replaceAll("");
}
/**
* Indent the lines of a string by one tab
*
* @param data
* @return
*/
public static String addLeadingTab(String data) {
return addLeadingTabs(data, 1);
}
/**
* Indent the lines of a string by some tabs
*
* @param data
* @param numberOfTabs
* @return
*/
public static String addLeadingTabs(String data, int numberOfTabs) {
Pattern pattern = Pattern.compile("^", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(data);
return matcher.replaceAll(StringUtils.repeat("\t", numberOfTabs));
}
/**
* Chop a string into pieces of maximum length
*
* @param value
* @param length
* @return
*/
public static List<String> truncate(String value, int length) {
List<String> parts = new ArrayList<String>();
int valueLength = value.length();
for (int i = 0; i < valueLength; i += length) {
parts.add(value.substring(i, Math.min(valueLength, i + length)));
}
return parts;
}
/**
* Trim all strings in an array of strings to a maximum length and add the cut off rest as new lines
*
* @param lines
* @param length
* @return
*/
public static List<String> truncate(String[] lines, int length) {
List<String> linesTruncated = new ArrayList<String>();
for (String line : lines) {
linesTruncated.addAll(truncate(line, length));
}
return linesTruncated;
}
/**
* Trim all strings in a list of strings to a maximum length and add the cut off rest as new lines
*
* @param lines
* @param length
* @return
*/
public static List<String> truncate(List<String> lines, int length) {
List<String> linesTruncated = new ArrayList<String>();
for (String line : lines) {
linesTruncated.addAll(truncate(line, length));
}
return linesTruncated;
}
/**
* Remove all leading and trailing whispaces, not only blanks like it is done by .trim()
*
* @param data
* @return
*/
public static String removeLeadingAndTrailingWhitespaces(String data) {
Pattern pattern1 = Pattern.compile("^\\s*", Pattern.MULTILINE);
Matcher matcher1 = pattern1.matcher(data);
data = matcher1.replaceAll("");
Pattern pattern2 = Pattern.compile("\\s*$", Pattern.MULTILINE);
Matcher matcher2 = pattern2.matcher(data);
data = matcher2.replaceAll("");
return data;
}
/**
* Remove all trailing whispaces
*
* @param data
* @return
*/
public static String removeTrailingWhitespaces(String data) {
Pattern pattern = Pattern.compile("\\s*$", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(data);
data = matcher.replaceAll("");
return data;
}
/**
* Scan an inputstream for start indexes of text lines
*
* @param input
* @return
* @throws IOException
*/
public static List<Long> scanLineStartIndexes(InputStream input) throws IOException {
List<Long> returnList = new ArrayList<Long>();
long position = 0;
int bufferPrevious = 0;
int bufferNext;
returnList.add(0L);
while ((bufferNext = input.read()) != -1) {
if ((char) bufferNext == '\n') {
returnList.add(position + 1);
} else if ((char) bufferPrevious == '\r') {
returnList.add(position);
}
bufferPrevious = bufferNext;
position++;
}
if ((char) bufferPrevious == '\r') {
returnList.add(position);
}
return returnList;
}
/**
* Scan an inputstream for start indexes of text lines.
* Skip the first characters up to startIndex.
*
* @param input
* @param startIndex
* @return
* @throws IOException
*/
public static List<Long> scanLineStartIndexes(InputStream input, long startIndex) throws IOException {
List<Long> returnList = new ArrayList<Long>();
long position = 0;
int bufferPrevious = 0;
int bufferNext;
if (startIndex <= 0) {
returnList.add(0L);
} else {
input.skip(startIndex);
position = startIndex;
}
while ((bufferNext = input.read()) != -1) {
if ((char) bufferNext == '\n') {
returnList.add(position + 1);
} else if ((char) bufferPrevious == '\r') {
returnList.add(position);
}
bufferPrevious = bufferNext;
position++;
}
if ((char) bufferPrevious == '\r') {
returnList.add(position);
}
return returnList;
}
/**
* Scan an inputstream for positions of linebreaks.
*
* @param dataInputStream
* @return
* @throws IOException
*/
public static List<Long> scanLinebreakIndexes(InputStream dataInputStream) throws IOException {
List<Long> returnList = new ArrayList<Long>();
long position = 0;
int bufferPrevious = 0;
int bufferNext;
while ((bufferNext = dataInputStream.read()) != -1) {
if ((char) bufferNext == '\n' && (char) bufferPrevious != '\r') {
returnList.add(position);
} else if ((char) bufferNext == '\r') {
returnList.add(position);
}
bufferPrevious = bufferNext;
position++;
}
return returnList;
}
/**
* Break all lines after a maximum number of characters.
*
* @param dataString
* @param maximumLength
* @return
*/
public static String breakLinesAfterMaximumLength(String dataString, int maximumLength) {
return StringUtils.join(dataString.split("(?<=\\G.{" + maximumLength + "})"), "\n");
}
/**
* Scan a string for the characters used in it
*
* @param data
* @return
*/
public static List<Character> getUsedChars(String data) {
List<Character> usedCharsList = new ArrayList<Character>();
for (char item : data.toCharArray()) {
if (!usedCharsList.contains(item)) {
usedCharsList.add(item);
}
}
return usedCharsList;
}
/**
* Check if a string ends with a stringpart caseinsensitive
*
* @param str
* @param suffix
* @return
*/
public static boolean endsWithIgnoreCase(String str, String suffix) {
if (str == null || suffix == null) {
return (str == null && suffix == null);
} else if (suffix.length() > str.length()) {
return false;
} else {
int strOffset = str.length() - suffix.length();
return str.regionMatches(true, strOffset, suffix, 0, suffix.length());
}
}
/**
* Check if a string starts with a stringpart caseinsensitive
*
* @param str
* @param prefix
* @return
*/
public static boolean startsWithIgnoreCase(String str, String prefix) {
if (str == null || prefix == null) {
return (str == null && prefix == null);
} else if (prefix.length() > str.length()) {
return false;
} else {
return str.regionMatches(true, 0, prefix, 0, prefix.length());
}
}
/**
* Trim a string to a maximum number of characters and add a sign if it was cut off.
*
* @param inputString
* @param laenge
* @param interLeave
* @return
*/
public static String trimStringToMaximumLength(String inputString, int laenge, String interLeave) {
if (inputString == null || inputString.length() <= laenge) {
return inputString;
} else {
int takeFirstLength = (laenge - interLeave.length()) / 2;
int takeLastLength = laenge - interLeave.length() - takeFirstLength;
return inputString.substring(0, takeFirstLength) + interLeave + inputString.substring(inputString.length() - takeLastLength);
}
}
/**
* Check if a string contains a stringpart caseinsensitive
*
* @param str
* @param substr
* @return
*/
public static boolean containsIgnoreCase(String str, String substr) {
if (str == substr) {
return true;
} else if (str == null) {
return false;
} else if (substr == null) {
return true;
} else if (substr.length() > str.length()) {
return false;
} else {
return str.regionMatches(true, 0, substr, 0, str.length());
}
}
}
|
54053(+83) Besucher