001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.util;
014:
015: import java.util.StringTokenizer;
016:
017: /**
018: * Some string manipulation utilities.
019: *
020: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
021: */
022: public class StringUtil {
023: /**
024: * replaces substrings with content 'toFind' with 'replace' in
025: * s and returns the result ('s/$toFind/$replace/g')
026: *
027: * @param s The String the substrings should be replaced in.
028: * @param toFind The substring to be replaced
029: * @param replace The replacement.
030: * @return the string with all replacements.
031: */
032: public static final String replace(String s, String toFind,
033: String replace) {
034: SStringBuilder erg = new SStringBuilder();
035:
036: int lastindex = 0;
037: int indexOf = s.indexOf(toFind);
038: if (indexOf == -1)
039: return s;
040: while (indexOf != -1) {
041: erg.append(s.substring(lastindex, indexOf)).append(replace);
042: lastindex = indexOf + toFind.length();
043: indexOf = s.indexOf(toFind, lastindex);
044: }
045:
046: erg.append(s.substring(lastindex));
047:
048: return erg.toString();
049: }
050:
051: /* slower ..
052: ist langsamer, mit jit 1/3, weniger als halb so schnell
053: public static String replaceNew(String s, String toFind, String replace) {
054: SStringBuilder erg = new SStringBuilder();
055:
056: StringTokenizer t = new StringTokenizer(s, toFind);
057: while ( t.hasMoreTokens() ) {
058: erg.append(t.nextToken().trim()).append(replace);
059: }
060: return erg.toString();
061: }
062: */
063:
064: /**
065: * replaces all newlines in the given String 's' with the replacement
066: * string 'r'. Each line is trimmed from leading and trailing whitespaces,
067: * then the new line-delimiter is added.
068: *
069: * @param s the source string.
070: * @param r the new line delimiter
071: * @return the resulting string.
072: */
073: public static final String replaceNewLines(String s, String r) {
074: SStringBuilder result = new SStringBuilder();
075:
076: StringTokenizer t = new StringTokenizer(s, "\n");
077: while (t.hasMoreTokens()) {
078: result.append(t.nextToken().trim()).append(r);
079: }
080: return result.toString();
081: }
082:
083: /**
084: * concatenates two arrays of strings.
085: *
086: * @param s1 the first array of strings.
087: * @param s2 the second array of strings.
088: * @return the resulting array with all strings in s1 and s2
089: */
090: public static final String[] concat(String[] s1, String[] s2) {
091: String[] erg = new String[s1.length + s2.length];
092:
093: System.arraycopy(s1, 0, erg, 0, s1.length);
094: System.arraycopy(s2, 0, erg, s1.length, s2.length);
095:
096: return erg;
097: }
098:
099: private final static char[] ALPHAS = { 'a', 'b', 'c', 'd', 'e',
100: 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
101: 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', };
102:
103: /**
104: * All possible digits for representing a number as a String
105: * This is conservative and does not include 'special'
106: * characters since some browsers don't handle them right.
107: * The IE for instance seems to be case insensitive in class
108: * names for CSSs. Grrr.
109: */
110: private final static char[] DIGITS = { '0', '1', '2', '3', '4',
111: '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
112: 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
113: 't', 'u', 'v', 'w', 'x', 'y', 'z',
114: /* This %@&!-IE is case insensitive for certain
115: * URLs and IDs
116: * 'A' , 'B' ,
117: * 'C' , 'D' , 'E' , 'F' , 'G' , 'H' ,
118: * 'I' , 'J' , 'K' , 'L' , 'M' , 'N' ,
119: * 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' ,
120: * 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z'
121: */
122: };
123:
124: public static final int MAX_RADIX = DIGITS.length;
125:
126: /**
127: * Codes number up to radix 62.
128: * Note, this method is only public for backward compatiblity. don't
129: * use it.
130: *
131: * @param minDigits returns a string with a least minDigits digits
132: */
133: public static String toString(long i, int radix, int minDigits) {
134: char[] buf = new char[65];
135:
136: radix = Math.min(Math.abs(radix), MAX_RADIX);
137: minDigits = Math.min(buf.length - 1, Math.abs(minDigits));
138:
139: int charPos = buf.length - 1;
140:
141: boolean negative = (i < 0);
142: if (negative) {
143: i = -i;
144: }
145:
146: while (i >= radix) {
147: buf[charPos--] = DIGITS[(int) (i % radix)];
148: i /= radix;
149: }
150: buf[charPos] = DIGITS[(int) i];
151:
152: // if minimum length of the result string is set, pad it with the
153: // zero-representation (that is: '0')
154: while (charPos > buf.length - minDigits)
155: buf[--charPos] = DIGITS[0];
156:
157: if (negative) {
158: buf[--charPos] = '-';
159: }
160:
161: return new String(buf, charPos, buf.length - charPos);
162: }
163:
164: /**
165: * creates a shortest possible string representation of the given
166: * long number that qualifies as an identifier in common programming
167: * languages (and HTML-id's :-)
168: * That is, it must start with a letter.
169: *
170: * @param val the long value to be encoded
171: * @return a string represantation of the given value that qualifies
172: * as an identifier.
173: */
174: public static String toIdentifierString(long val) {
175: char buf[] = new char[14];
176: int i = 0;
177: if (val < 0) {
178: buf[i++] = '_';
179: val = -(val + 1);
180: }
181: buf[i++] = ALPHAS[(int) (val % ALPHAS.length)];
182: val /= ALPHAS.length;
183: while (val != 0 && i < buf.length) {
184: buf[i++] = DIGITS[(int) (val % DIGITS.length)];
185: val /= DIGITS.length;
186: }
187: return new String(buf, 0, i);
188: }
189:
190: /**
191: * generates a shortest representation as string for the given long.
192: * This is used to generate session or resource IDs.
193: */
194: public static String toShortestAlphaNumericString(long i) {
195: return toString(i, MAX_RADIX, 0);
196: }
197:
198: /**
199: * generates a shortest representation as string for the given with
200: * at least minDigits digits. Unused digits are padded with zero.
201: */
202: public static String toShortestAlphaNumericString(long i,
203: int minDigits) {
204: return toString(i, MAX_RADIX, minDigits);
205: }
206:
207: public static String delimitedString(Object[] array) {
208: if (array == null)
209: return null;
210: if (array.length == 0)
211: return "";
212:
213: SStringBuilder buffer = new SStringBuilder("" + array[0]);
214: for (int i = 1; i < array.length; i++) {
215: if (array[i] == null)
216: buffer.append(", null");
217: else {
218: buffer.append(", ");
219: buffer.append(array[i]);
220: }
221: }
222: return buffer.toString();
223: }
224:
225: /*
226: public static void main(String args[]) {
227: System.out.println(StringUtil.toString(9124, 10, 0));
228:
229: System.out.println(StringUtil.toShortestAlphaNumericString(9124));
230: System.out.println(StringUtil.toShortestAlphaNumericString(-9124));
231:
232: System.out.println(StringUtil.toString(1, MAX_RADIX, 4));
233:
234: System.out.println(StringUtil.toString(9124, MAX_RADIX, 1));
235: System.out.println(StringUtil.toString(9124, MAX_RADIX, 4));
236: System.out.println(StringUtil.toString(9124, MAX_RADIX, 5));
237:
238: System.out.println(StringUtil.toString(-1, MAX_RADIX, 64));
239:
240: System.out.println("\"" + StringUtil.toShortestAlphaNumericString(3843,
241: 2));
242: for (int i=-10; i < 300; ++i) {
243: System.out.println(toIdentifierString(i));
244: }
245: System.out.println(toIdentifierString(Long.MIN_VALUE));
246: System.out.println(toIdentifierString(Long.MAX_VALUE));
247: }
248: */
249: }
|