KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ziclix > python > sql > pipe > csv > CSVString


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: CSVString.java,v 1.2 2005/02/23 04:26:19 bzimmer Exp $
5  *
6  * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
7  *
8  */

9 package com.ziclix.python.sql.pipe.csv;
10
11 /**
12  * A utility class to aide in quoting CSV strings.
13  *
14  * @author brian zimmer
15  * @version $Revision: 1.2 $
16  */

17 public class CSVString {
18
19     /**
20      * The default delimiter.
21      */

22     public static final String JavaDoc DELIMITER = ",";
23
24     private CSVString() {
25     }
26
27     /**
28      * Escape the string as needed using the default delimiter.
29      */

30     public static String JavaDoc toCSV(String JavaDoc string) {
31         return toCSV(string, CSVString.DELIMITER);
32     }
33
34     /**
35      * Escape the string as needed using the given delimiter.
36      */

37     public static String JavaDoc toCSV(String JavaDoc string, String JavaDoc delimiter) {
38
39         String JavaDoc res = replace(string, "\"", "\"\"");
40
41         if ((res.indexOf("\"") >= 0) || (string.indexOf(delimiter) >= 0)) {
42             res = "\"" + res + "\"";
43         }
44
45         return res;
46     }
47
48     /**
49      * Returns a new string resulting from replacing the first occurrence, or all occurrences,
50      * of search string in this string with replace string.
51      * If the string search does not occur in the character sequence represented by this object,
52      * then this string is returned.
53      *
54      * @param search the old string
55      * @param replace the new string
56      * @param all=true all occurrences of the search string are replaced
57      * @param all=false only the first occurrence of the search string is replaced
58      * @return a string derived from this string by replacing the first occurrence,
59      * or every occurrence of search with replace.
60      */

61     public static String JavaDoc replace(String JavaDoc original, String JavaDoc search, String JavaDoc replace, boolean all) {
62
63         String JavaDoc valReturn = new String JavaDoc("");
64         int l = original.length();
65         int lo = search.length();
66         int i = 0;
67         int j;
68
69         while (i <= l) {
70             j = original.indexOf(search, i);
71
72             if (j == -1) {
73                 valReturn = valReturn.concat(original.substring(i, l));
74                 i = l + 1; // Stop, no more occurrence.
75
} else {
76                 valReturn = valReturn.concat(original.substring(i, j));
77                 valReturn = valReturn.concat(replace);
78                 i = j + lo;
79
80                 if (!all) { // Stop, replace the first occurrence only.
81
valReturn = valReturn.concat(original.substring(i, l));
82                     i = l + 1; // Stop, replace the first occurrence only.
83
}
84             }
85         }
86
87         return valReturn;
88     }
89
90     /**
91      * Returns a new string resulting from replacing all occurrences,
92      * of search string in this string with replace string.
93      * If the string search does not occur in the character sequence represented by this object,
94      * then this string is returned.
95      *
96      * @param search the old string
97      * @param replace the new string
98      * @return a string derived from this string by replacing every occurrence of search with replace.
99      */

100     public static String JavaDoc replace(String JavaDoc original, String JavaDoc search, String JavaDoc replace) {
101         return replace(original, search, replace, true);
102     }
103
104     /**
105      * Returns a new string resulting from replacing the end of this String
106      * from oldSuffix to newSuffix.
107      * The original string is returned if it does not end with oldSuffix.
108      *
109      * @param oldSuffix the old suffix
110      * @param newSuffix the new suffix
111      * @return a string derived from this string by replacing the end oldSuffix by newSuffix
112      */

113     public static String JavaDoc replaceEndWith(String JavaDoc original, String JavaDoc oldSuffix, String JavaDoc newSuffix) {
114
115         if (original.endsWith(oldSuffix)) {
116             String JavaDoc st = original.substring(0, original.length() - oldSuffix.length());
117
118             return st.concat(newSuffix);
119         } else {
120             return original;
121         }
122     }
123 }
124
Popular Tags