001: /*
002: * Jython Database Specification API 2.0
003: *
004: * $Id: CSVString.java 2414 2005-02-23 04:26:23Z bzimmer $
005: *
006: * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
007: *
008: */
009: package com.ziclix.python.sql.pipe.csv;
010:
011: /**
012: * A utility class to aide in quoting CSV strings.
013: *
014: * @author brian zimmer
015: * @version $Revision: 2414 $
016: */
017: public class CSVString {
018:
019: /**
020: * The default delimiter.
021: */
022: public static final String DELIMITER = ",";
023:
024: private CSVString() {
025: }
026:
027: /**
028: * Escape the string as needed using the default delimiter.
029: */
030: public static String toCSV(String string) {
031: return toCSV(string, CSVString.DELIMITER);
032: }
033:
034: /**
035: * Escape the string as needed using the given delimiter.
036: */
037: public static String toCSV(String string, String delimiter) {
038:
039: String res = replace(string, "\"", "\"\"");
040:
041: if ((res.indexOf("\"") >= 0)
042: || (string.indexOf(delimiter) >= 0)) {
043: res = "\"" + res + "\"";
044: }
045:
046: return res;
047: }
048:
049: /**
050: * Returns a new string resulting from replacing the first occurrence, or all occurrences,
051: * of search string in this string with replace string.
052: * If the string search does not occur in the character sequence represented by this object,
053: * then this string is returned.
054: *
055: * @param search the old string
056: * @param replace the new string
057: * @param all=true all occurrences of the search string are replaced
058: * @param all=false only the first occurrence of the search string is replaced
059: * @return a string derived from this string by replacing the first occurrence,
060: * or every occurrence of search with replace.
061: */
062: public static String replace(String original, String search,
063: String replace, boolean all) {
064:
065: String valReturn = new String("");
066: int l = original.length();
067: int lo = search.length();
068: int i = 0;
069: int j;
070:
071: while (i <= l) {
072: j = original.indexOf(search, i);
073:
074: if (j == -1) {
075: valReturn = valReturn.concat(original.substring(i, l));
076: i = l + 1; // Stop, no more occurrence.
077: } else {
078: valReturn = valReturn.concat(original.substring(i, j));
079: valReturn = valReturn.concat(replace);
080: i = j + lo;
081:
082: if (!all) { // Stop, replace the first occurrence only.
083: valReturn = valReturn.concat(original.substring(i,
084: l));
085: i = l + 1; // Stop, replace the first occurrence only.
086: }
087: }
088: }
089:
090: return valReturn;
091: }
092:
093: /**
094: * Returns a new string resulting from replacing all occurrences,
095: * of search string in this string with replace string.
096: * If the string search does not occur in the character sequence represented by this object,
097: * then this string is returned.
098: *
099: * @param search the old string
100: * @param replace the new string
101: * @return a string derived from this string by replacing every occurrence of search with replace.
102: */
103: public static String replace(String original, String search,
104: String replace) {
105: return replace(original, search, replace, true);
106: }
107:
108: /**
109: * Returns a new string resulting from replacing the end of this String
110: * from oldSuffix to newSuffix.
111: * The original string is returned if it does not end with oldSuffix.
112: *
113: * @param oldSuffix the old suffix
114: * @param newSuffix the new suffix
115: * @return a string derived from this string by replacing the end oldSuffix by newSuffix
116: */
117: public static String replaceEndWith(String original,
118: String oldSuffix, String newSuffix) {
119:
120: if (original.endsWith(oldSuffix)) {
121: String st = original.substring(0, original.length()
122: - oldSuffix.length());
123:
124: return st.concat(newSuffix);
125: } else {
126: return original;
127: }
128: }
129: }
|