001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.util;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.StringTokenizer;
025:
026: public class StringUtils {
027:
028: // returns null if no file exists in the subtree otherwise
029: public static String concat(String s1, String s2, String s3) {
030: StringBuffer buffer = new StringBuffer();
031:
032: buffer.append(s1).append(s2).append(s3);
033:
034: return buffer.toString();
035: }
036:
037: public static String concat(String s1, String s2, String s3,
038: String s4) {
039: StringBuffer buffer = new StringBuffer();
040:
041: buffer.append(s1).append(s2).append(s3).append(s4);
042:
043: return buffer.toString();
044: }
045:
046: public static String concat(String s1, String s2, String s3,
047: String s4, String s5) {
048: StringBuffer buffer = new StringBuffer();
049:
050: buffer.append(s1).append(s2).append(s3).append(s4).append(s5);
051:
052: return buffer.toString();
053: }
054:
055: public static String concat(String s1, String s2, String s3,
056: String s4, String s5, String s6) {
057: StringBuffer buffer = new StringBuffer();
058:
059: buffer.append(s1).append(s2).append(s3).append(s4).append(s5)
060: .append(s6);
061:
062: return buffer.toString();
063: }
064:
065: public static String concat(String s1, String s2, String s3,
066: String s4, String s5, String s6, String s7) {
067: StringBuffer buffer = new StringBuffer();
068:
069: buffer.append(s1).append(s2).append(s3).append(s4).append(s5)
070: .append(s6).append(s7);
071:
072: return buffer.toString();
073: }
074:
075: public static String concat(String s1, String s2, String s3,
076: String s4, String s5, String s6, String s7, String s8) {
077: StringBuffer buffer = new StringBuffer();
078:
079: buffer.append(s1).append(s2).append(s3).append(s4).append(s5)
080: .append(s6).append(s7).append(s8);
081:
082: return buffer.toString();
083: }
084:
085: public static String concat(String s1, String s2, String s3,
086: String s4, String s5, String s6, String s7, String s8,
087: String s9) {
088: StringBuffer buffer = new StringBuffer();
089:
090: buffer.append(s1).append(s2).append(s3).append(s4).append(s5)
091: .append(s6).append(s7).append(s8).append(s9);
092:
093: return buffer.toString();
094: }
095:
096: public static String concat(String s1, String s2, String s3,
097: String s4, String s5, String s6, String s7, String s8,
098: String s9, String s10) {
099: StringBuffer buffer = new StringBuffer();
100:
101: buffer.append(s1).append(s2).append(s3).append(s4).append(s5)
102: .append(s6).append(s7).append(s8).append(s9)
103: .append(s10);
104:
105: return buffer.toString();
106: }
107:
108: public static String concat(String s1, String s2, String s3,
109: String s4, String s5, String s6, String s7, String s8,
110: String s9, String s10, String s11) {
111: StringBuffer buffer = new StringBuffer();
112:
113: buffer.append(s1).append(s2).append(s3).append(s4).append(s5)
114: .append(s6).append(s7).append(s8).append(s9)
115: .append(s10).append(s11);
116:
117: return buffer.toString();
118: }
119:
120: public static String concat(String s1, String s2, String s3,
121: String s4, String s5, String s6, String s7, String s8,
122: String s9, String s10, String s11, String s12) {
123: StringBuffer buffer = new StringBuffer();
124:
125: buffer.append(s1).append(s2).append(s3).append(s4).append(s5)
126: .append(s6).append(s7).append(s8).append(s9)
127: .append(s10).append(s11).append(s12);
128:
129: return buffer.toString();
130: }
131:
132: /**
133: * Create a String containing the string representation of all objects in
134: * the given collection, separated by the given character.
135: */
136: public static String createCSV(Collection coll, char sep) {
137: StringBuffer buffer = new StringBuffer();
138: Iterator iterator = coll.iterator();
139:
140: while (iterator.hasNext()) {
141: buffer.append(iterator.next()).append(sep);
142: }
143: if (buffer.length() != 0) {
144: buffer.deleteCharAt(buffer.length() - 1);
145: }
146: return buffer.toString();
147: }
148:
149: /**
150: * Create a String containing all strings of the given array concatenated,
151: * separated by the given character.
152: */
153: public static String createCSV(String[] strings, char sep) {
154: StringBuffer buffer = new StringBuffer();
155: for (int i = 0; i < strings.length; i++)
156: buffer.append(strings[i]).append(sep);
157: if (buffer.length() != 0) {
158: buffer.deleteCharAt(buffer.length() - 1);
159: }
160: return buffer.toString();
161: }
162:
163: /**
164: * Create a String containing the string representation of all objects in
165: * the given collection, separated by the given character.
166: */
167: public static String createCSV(long[] values, char sep) {
168: StringBuffer buffer = new StringBuffer();
169:
170: for (int i = 0; i < values.length; i++) {
171: buffer.append(values[i]).append(sep);
172: }
173: buffer.deleteCharAt(buffer.length() - 1);
174: return buffer.toString();
175: }
176:
177: /**
178: * Create a String containing the string representation of all objects in
179: * the given collection, separated by the given character.
180: */
181: public static String createCSV(int[] values, char sep) {
182: StringBuffer buffer = new StringBuffer();
183:
184: for (int i = 0; i < values.length; i++) {
185: buffer.append(values[i]).append(sep);
186: }
187: buffer.deleteCharAt(buffer.length() - 1);
188: return buffer.toString();
189: }
190:
191: /**
192: * Create a String containing the string representation of all objects in
193: * the given collection quoted by the given quote character, separated by
194: * the given separator.
195: */
196: public static String createQuotedCSV(Collection coll, char quote,
197: char sep) {
198: StringBuffer buffer = new StringBuffer();
199: Iterator iterator = coll.iterator();
200:
201: while (iterator.hasNext()) {
202: buffer.append(quote).append(iterator.next()).append(quote)
203: .append(sep);
204: }
205: if (buffer.length() > 0) {
206: buffer.deleteCharAt(buffer.length() - 1);
207: }
208: return buffer.toString();
209: }
210:
211: public static List tokenizeCSV(String string, char sep) {
212: StringTokenizer tokenizer = new StringTokenizer(string,
213: new String(new char[] { sep }));
214: List answer = new ArrayList();
215:
216: while (tokenizer.hasMoreTokens()) {
217: answer.add(tokenizer.nextToken().trim());
218: }
219: return answer;
220: }
221:
222: public static Collection tokenizeQuotedCSV(String string, char sep) {
223: StringTokenizer tokenizer = new StringTokenizer(string,
224: new String(new char[] { sep }));
225: Collection answer = new ArrayList();
226:
227: while (tokenizer.hasMoreTokens()) {
228: String token = tokenizer.nextToken().trim();
229: if (token.length() > 2) {
230: answer.add(token.substring(1, token.length() - 1));
231: }
232: }
233: return answer;
234: }
235:
236: public static boolean contains(String[] array, String s) {
237: if (array != null) {
238: for (int i = 0; i < array.length; i++)
239: if (array[i].equals(s))
240: return true;
241: }
242: return false;
243: }
244:
245: public static String toHexString(byte[] bytes) {
246: StringBuffer buffer = new StringBuffer();
247: for (int i = 0; i < bytes.length; i++) {
248: int n = bytes[i] + 128;
249: if (n < 16) {
250: buffer.append("0");
251: }
252: buffer.append(Integer.toHexString(n));
253: }
254: return buffer.toString();
255: }
256:
257: public static byte[] fromHexString(String value) {
258: byte[] bytes = new byte[value.length() / 2];
259:
260: for (int i = 0; i < bytes.length; i++) {
261: String hex = value.substring(i * 2, i * 2 + 2);
262: bytes[i] = (byte) (Integer.valueOf(hex, 16).intValue() - 128);
263: }
264: return bytes;
265: }
266:
267: public static String replaceColonBySpace(String line) {
268: StringBuffer buffer = new StringBuffer();
269:
270: for (int i = 0; i < line.length(); i++) {
271: char c = line.charAt(i);
272: if (c == ':') {
273: buffer.append(' ');
274: } else {
275: buffer.append(c);
276: }
277: }
278: return buffer.toString();
279: }
280: }
|