01: /**
02: Copyright (C) 2002-2003 Together
03:
04: This library is free software; you can redistribute it and/or
05: modify it under the terms of the GNU Lesser General Public
06: License as published by the Free Software Foundation; either
07: version 2.1 of the License, or (at your option) any later version.
08:
09: This library is distributed in the hope that it will be useful,
10: but WITHOUT ANY WARRANTY; without even the implied warranty of
11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: Lesser General Public License for more details.
13:
14: You should have received a copy of the GNU Lesser General Public
15: License along with this library; if not, write to the Free Software
16: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17:
18: */package org.webdocwf.util.loader.generator;
19:
20: /**
21: * Utility methods for csv jdbc.
22: *
23: * @author Zoran Milakovic
24: */
25:
26: public class Utils {
27:
28: /**
29: * Replace all occurence of forReplace with replaceWith in input string.
30: * @param input
31: * @param forReplace
32: * @param replaceWith
33: * @return new string with replaced values
34: */
35: public static String replaceAll(String input, String forReplace,
36: String replaceWith) {
37: StringBuffer result = new StringBuffer();
38: boolean hasMore = true;
39: while (hasMore) {
40: int start = input.indexOf(forReplace);
41: int end = start + forReplace.length();
42: if (start != -1) {
43: result.append(input.substring(0, start) + replaceWith);
44: input = input.substring(end);
45: } else {
46: hasMore = false;
47: result.append(input);
48: }
49: }
50: if (result.toString().equals(""))
51: return input; //nothing is changed
52: else
53: return result.toString();
54: }
55:
56: public static String handleQuotedString(String quotedString) {
57: String retVal = quotedString;
58: if ((retVal.startsWith("'") && retVal.endsWith("'"))) {
59: if (!retVal.equals("''")) {
60: retVal = retVal.substring(retVal.indexOf("'") + 1,
61: retVal.lastIndexOf("'"));
62: } else {
63: retVal = "";
64: }
65: }
66: return retVal;
67: }
68:
69: public static String[] replaceLineBrakesAndCarrReturn(
70: String[] toReplace, String lineBreakEscape,
71: String carriageReturnEscape) {
72: String[] retVal = new String[toReplace.length];
73: for (int i = 0; i < toReplace.length; i++) {
74: if (toReplace[i] != null) {
75: retVal[i] = replaceAll(toReplace[i], "\n",
76: lineBreakEscape);
77: retVal[i] = replaceAll(retVal[i], "\r",
78: carriageReturnEscape);
79: }
80: }
81: return retVal;
82: }
83:
84: }
|