001: package com.technoetic.xplanner.util;
002:
003: import java.io.BufferedReader;
004: import java.io.IOException;
005: import java.io.StringReader;
006:
007: import com.opensymphony.util.TextUtils;
008:
009: public class StringUtilities {
010: public static String computeSuffix(String description,
011: String boundaryText, int maxSuffixLength) {
012: int index = description.toLowerCase().indexOf(
013: boundaryText.toLowerCase());
014: String wholeSuffix = description.substring(index
015: + boundaryText.length());
016: String matchSuffix = getShortSuffix(wholeSuffix,
017: maxSuffixLength);
018: if (matchSuffix.length() < wholeSuffix.length()) {
019: matchSuffix += "...";
020: }
021: return matchSuffix;
022: }
023:
024: public static String computePrefix(String description,
025: String boundaryText, int maxSuffixLength) {
026: int index = description.toLowerCase().indexOf(
027: boundaryText.toLowerCase());
028: String wholePrefix = description.substring(0, index);
029: String matchPrefix = getShortPrefix(wholePrefix,
030: maxSuffixLength);
031: if (matchPrefix.length() < wholePrefix.length()) {
032: matchPrefix = "..." + matchPrefix;
033: }
034: return matchPrefix;
035: }
036:
037: public static String replaceQuotationMarks(String text) {
038: if (text == null)
039: return "";
040: String newText = text.replaceAll("\"", "''");
041: newText = newText.replaceAll("'", "\\\\'");
042: return newText;
043: }
044:
045: public static String getShortSuffix(String wholeSuffix,
046: int maxSuffixLength) {
047: int count = 0;
048: int pos = 0;
049: while (pos < wholeSuffix.length() && count < maxSuffixLength) {
050: count = adjustCountBasedOnChar(wholeSuffix.charAt(pos),
051: count);
052: pos++;
053: }
054: return wholeSuffix.substring(0, pos);
055: }
056:
057: private static int adjustCountBasedOnChar(char currentChar,
058: int count) {
059: if (currentChar != ' ' && currentChar != '\n') {
060: count++;
061: }
062: return count;
063: }
064:
065: public static String getShortPrefix(String wholePrefix,
066: int maxPrefixLength) {
067: int pos = wholePrefix.length() - 1;
068: int count = 0;
069: while (pos >= 0 && count < maxPrefixLength) {
070: count = adjustCountBasedOnChar(wholePrefix.charAt(pos),
071: count);
072: pos--;
073: }
074: return wholePrefix.substring(pos + 1);
075: }
076:
077: public static String getFirstNLines(String text,
078: int desiredLineCount) {
079: BufferedReader bufReader = new BufferedReader(new StringReader(
080: text));
081: StringBuffer stringBuffer = new StringBuffer();
082: int i = 1;
083: try {
084: String str = bufReader.readLine();
085: while (str != null && i <= desiredLineCount) {
086: stringBuffer.append(str);
087: if (i < desiredLineCount) {
088: stringBuffer.append(" ");
089: }
090: i++;
091: str = bufReader.readLine();
092: }
093: } catch (IOException e) {
094: //Don't care
095: }
096: return stringBuffer.toString();
097: }
098:
099: public static String htmlEncode(String s) {
100: if (!TextUtils.stringSet(s))
101: return "";
102:
103: StringBuffer str = new StringBuffer();
104:
105: for (int j = 0; j < s.length(); j++) {
106: str.append(htmlEncode(s.charAt(j)));
107: }
108:
109: return str.toString();
110: }
111:
112: public static String htmlEncode(char c) {
113: switch (c) {
114: case '"':
115: return """;
116: case '&':
117: return "&";
118: case '<':
119: return "<";
120: case '>':
121: return ">";
122: default:
123: return "" + c;
124: }
125: }
126:
127: }
|