001: package org.julp.util.common;
002:
003: public class Utils {
004:
005: public final static char UPPER = 'U';
006: public final static char LOWER = 'L';
007: public final static char NO_SPACE = 'N';
008: public final static char ONE_SPACE = 'S';
009:
010: private Utils() {
011: }
012:
013: /**
014: * Breaking long String to lines.
015: * Line does not break words, only white space
016: * Lines could be separated with System.getProperty(\"line.separator\") - default
017: * or \"<br>\" or any given String.
018: *
019: */
020: public static String breakString(String source, String separator,
021: int offset, boolean preserveWhiteSpace) {
022: StringBuffer sb = new StringBuffer();
023: String[] z = source.split(" ");
024: String line = "";
025: for (int i = 0; i < z.length; i++) {
026: if (preserveWhiteSpace) {
027: line = line + z[i] + " ";
028: } else {
029: line = line + z[i].trim();
030: if (!z[i].equals("")) {
031: line = line + " ";
032: }
033: }
034: if (line.length() >= offset) {
035: sb.append(line).append(separator);
036: line = "";
037: }
038: if (i == z.length - 1) { // last line
039: if (line.length() < offset) {
040: sb.append(line);
041: }
042: }
043: }
044: return sb.toString();
045: }
046:
047: public static String breakString(String source, String separator,
048: int offset) {
049: return breakString(source, separator, offset, false);
050: }
051:
052: public static String breakString(String source, int offset) {
053: String lineSeparator;
054: try {
055: lineSeparator = System.getProperty("line.separator");
056: } catch (SecurityException se) {
057: lineSeparator = "\n"; //??
058: }
059: return breakString(source, lineSeparator, offset, false);
060: }
061:
062: public static String normalizeString(String source, char caseType,
063: char spaceType, char replaceWith) {
064: if (source == null || source.trim().length() == 0) {
065: return "";
066: }
067: source = source.trim();
068: StringBuffer sb = new StringBuffer();
069:
070: for (int i = 0; i < source.length(); i++) {
071: char c = source.charAt(i);
072: if (Character.isWhitespace(c)) {
073: if (spaceType == NO_SPACE) {
074: if (!Character.isWhitespace(replaceWith)) {
075: sb.append(replaceWith);
076: }
077: } else if (spaceType == ONE_SPACE) {
078: if (i > 0
079: && !Character.isWhitespace(source
080: .charAt(i - 1))) {
081: if (!Character.isWhitespace(replaceWith)) {
082: sb.append(replaceWith);
083: } else {
084: sb.append(c);
085: }
086: }
087: }
088: } else {
089: sb.append(c);
090: }
091: }
092: String result = null;
093: if (caseType == UPPER) {
094: result = sb.toString().toUpperCase();
095: }
096: if (caseType == LOWER) {
097: result = sb.toString().toLowerCase();
098: }
099: return result;
100: }
101: }
|