From: brian z. <bz...@us...> - 2005-02-23 04:27:02
|
Update of /cvsroot/jython/jython/com/ziclix/python/sql/pipe/csv In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18519/com/ziclix/python/sql/pipe/csv Modified Files: CSVSink.java CSVString.java Log Message: for the sake of better javadocs, fixed all warnings and updated ant build.xml; removed all tabs; removed Makefiles Index: CSVSink.java =================================================================== RCS file: /cvsroot/jython/jython/com/ziclix/python/sql/pipe/csv/CSVSink.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** CSVSink.java 20 Nov 2001 04:55:18 -0000 1.1 --- CSVSink.java 23 Feb 2005 04:26:19 -0000 1.2 *************** *** 1,3 **** - /* * Jython Database Specification API 2.0 --- 1,2 ---- *************** *** 10,16 **** package com.ziclix.python.sql.pipe.csv; - import java.io.*; - import org.python.core.*; import com.ziclix.python.sql.pipe.Sink; /** --- 9,17 ---- package com.ziclix.python.sql.pipe.csv; import com.ziclix.python.sql.pipe.Sink; + import org.python.core.Py; + import org.python.core.PyObject; + + import java.io.PrintWriter; /** *************** *** 22,127 **** public class CSVSink implements Sink { ! /** Field header */ ! protected boolean header; ! /** Field delimiter */ ! protected String delimiter; ! /** Field writer */ ! protected PrintWriter writer; ! /** Field converters */ ! protected PyObject converters; ! /** ! * All data will be written to the given PrintWriter. ! * ! * @param writer the PrintWriter to which data will be written ! */ ! public CSVSink(PrintWriter writer) { ! this(writer, Py.None); ! } ! /** ! * All data will be written to the given PrintWriter. If ! * the converters param is not None, then an attempt will ! * be made to convert the object using the given converter. ! * ! * @param writer the PrintWriter to which data will be written ! * @param converters an indexed dictionary of callable objects used for converting objects to strings ! */ ! public CSVSink(PrintWriter writer, PyObject converters) { ! this.header = false; ! this.writer = writer; ! this.converters = converters; ! this.delimiter = ","; ! } ! /** ! * Handle the data callback and write the row out. ! */ ! public void row(PyObject row) { ! String[] values = new String[row.__len__()]; ! if (this.header) { ! for (int i = 0; i < row.__len__(); i++) { ! values[i] = this.convert(Py.newInteger(i), row.__getitem__(i)); ! } ! } else { ! for (int i = 0; i < row.__len__(); i++) { ! values[i] = row.__getitem__(i).__getitem__(0).toString(); ! } ! this.header = true; ! } ! this.println(values); ! } ! /** ! * Convert the object at index to a String. ! */ ! protected String convert(PyObject index, PyObject object) { ! if (this.converters != Py.None) { ! PyObject converter = this.converters.__finditem__(index); ! if (converter != Py.None) { ! object = converter.__call__(object); ! } ! } ! if ((object == Py.None) || (object == null)) { ! return ""; ! } ! return CSVString.toCSV(object.toString()); ! } ! /** ! * Print the row of Strings. ! */ ! protected void println(String[] row) { ! for (int i = 0; i < row.length - 1; i++) { ! this.writer.print(row[i]); ! this.writer.print(this.delimiter); ! } ! this.writer.println(row[row.length - 1]); ! } ! /** ! * Method start ! * ! */ ! public void start() {} ! /** ! * Method end ! * ! */ ! public void end() {} } --- 23,136 ---- public class CSVSink implements Sink { ! /** ! * Field header ! */ ! protected boolean header; ! /** ! * Field delimiter ! */ ! protected String delimiter; ! /** ! * Field writer ! */ ! protected PrintWriter writer; ! /** ! * Field converters ! */ ! protected PyObject converters; ! /** ! * All data will be written to the given PrintWriter. ! * ! * @param writer the PrintWriter to which data will be written ! */ ! public CSVSink(PrintWriter writer) { ! this(writer, Py.None); ! } ! /** ! * All data will be written to the given PrintWriter. If ! * the converters param is not None, then an attempt will ! * be made to convert the object using the given converter. ! * ! * @param writer the PrintWriter to which data will be written ! * @param converters an indexed dictionary of callable objects used for converting objects to strings ! */ ! public CSVSink(PrintWriter writer, PyObject converters) { ! this.header = false; ! this.writer = writer; ! this.converters = converters; ! this.delimiter = ","; ! } ! /** ! * Handle the data callback and write the row out. ! */ ! public void row(PyObject row) { ! String[] values = new String[row.__len__()]; ! if (this.header) { ! for (int i = 0; i < row.__len__(); i++) { ! values[i] = this.convert(Py.newInteger(i), row.__getitem__(i)); ! } ! } else { ! for (int i = 0; i < row.__len__(); i++) { ! values[i] = row.__getitem__(i).__getitem__(0).toString(); ! } ! this.header = true; ! } ! this.println(values); ! } ! /** ! * Convert the object at index to a String. ! */ ! protected String convert(PyObject index, PyObject object) { ! if (this.converters != Py.None) { ! PyObject converter = this.converters.__finditem__(index); ! if (converter != Py.None) { ! object = converter.__call__(object); ! } ! } ! if ((object == Py.None) || (object == null)) { ! return ""; ! } ! return CSVString.toCSV(object.toString()); ! } ! /** ! * Print the row of Strings. ! */ ! protected void println(String[] row) { ! for (int i = 0; i < row.length - 1; i++) { ! this.writer.print(row[i]); ! this.writer.print(this.delimiter); ! } ! this.writer.println(row[row.length - 1]); ! } ! /** ! * Method start ! */ ! public void start() { ! } ! /** ! * Method end ! */ ! public void end() { ! } } Index: CSVString.java =================================================================== RCS file: /cvsroot/jython/jython/com/ziclix/python/sql/pipe/csv/CSVString.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** CSVString.java 20 Nov 2001 04:55:18 -0000 1.1 --- CSVString.java 23 Feb 2005 04:26:19 -0000 1.2 *************** *** 1,3 **** - /* * Jython Database Specification API 2.0 --- 1,2 ---- *************** *** 18,121 **** public class CSVString { ! /** ! * The default delimiter. ! */ ! public static final String DELIMITER = ","; ! private CSVString() {} ! /** ! * Escape the string as needed using the default delimiter. ! */ ! public static String toCSV(String string) { ! return toCSV(string, CSVString.DELIMITER); ! } ! /** ! * Escape the string as needed using the given delimiter. ! */ ! public static String toCSV(String string, String delimiter) { ! String res = replace(string, "\"", "\"\""); ! if ((res.indexOf("\"") >= 0) || (string.indexOf(delimiter) >= 0)) { ! res = "\"" + res + "\""; ! } ! return res; ! } ! /** ! * Returns a new string resulting from replacing the first occurrence, or all occurrences, ! * of search string in this string with replace string. ! * If the string search does not occur in the character sequence represented by this object, ! * then this string is returned. ! * @param search the old string ! * @param replace the new string ! * @param all=true all occurrences of the search string are replaced ! * @param all=false only the first occurrence of the search string is replaced ! * @return a string derived from this string by replacing the first occurrence, ! * or every occurrence of search with replace. ! */ ! public static String replace(String original, String search, String replace, boolean all) { ! String valReturn = new String(""); ! int l = original.length(); ! int lo = search.length(); ! int ln = replace.length(); ! int i = 0; ! int j; ! while (i <= l) { ! j = original.indexOf(search, i); ! if (j == -1) { ! valReturn = valReturn.concat(original.substring(i, l)); ! i = l + 1; // Stop, no more occurrence. ! } else { ! valReturn = valReturn.concat(original.substring(i, j)); ! valReturn = valReturn.concat(replace); ! i = j + lo; ! if (!all) { // Stop, replace the first occurrence only. ! valReturn = valReturn.concat(original.substring(i, l)); ! i = l + 1; // Stop, replace the first occurrence only. ! } ! } ! } ! return valReturn; ! } ! /** ! * Returns a new string resulting from replacing all occurrences, ! * of search string in this string with replace string. ! * If the string search does not occur in the character sequence represented by this object, ! * then this string is returned. ! * @param search the old string ! * @param replace the new string ! * @return a string derived from this string by replacing every occurrence of search with replace. ! */ ! public static String replace(String original, String search, String replace) { ! return replace(original, search, replace, true); ! } ! /** ! * Returns a new string resulting from replacing the end of this String ! * from oldSuffix to newSuffix. ! * The original string is returned if it does not end with oldSuffix. ! * @param oldSuffix the old suffix ! * @param newSuffix the new suffix ! * @return a string derived from this string by replacing the end oldSuffix by newSuffix ! */ ! public static String replaceEndWith(String original, String oldSuffix, String newSuffix) { ! if (original.endsWith(oldSuffix)) { ! String st = original.substring(0, original.length() - oldSuffix.length()); ! return st.concat(newSuffix); ! } else { ! return original; ! } ! } } --- 17,123 ---- public class CSVString { ! /** ! * The default delimiter. ! */ ! public static final String DELIMITER = ","; ! private CSVString() { ! } ! /** ! * Escape the string as needed using the default delimiter. ! */ ! public static String toCSV(String string) { ! return toCSV(string, CSVString.DELIMITER); ! } ! /** ! * Escape the string as needed using the given delimiter. ! */ ! public static String toCSV(String string, String delimiter) { ! String res = replace(string, "\"", "\"\""); ! if ((res.indexOf("\"") >= 0) || (string.indexOf(delimiter) >= 0)) { ! res = "\"" + res + "\""; ! } ! return res; ! } ! /** ! * Returns a new string resulting from replacing the first occurrence, or all occurrences, ! * of search string in this string with replace string. ! * If the string search does not occur in the character sequence represented by this object, ! * then this string is returned. ! * ! * @param search the old string ! * @param replace the new string ! * @param all=true all occurrences of the search string are replaced ! * @param all=false only the first occurrence of the search string is replaced ! * @return a string derived from this string by replacing the first occurrence, ! * or every occurrence of search with replace. ! */ ! public static String replace(String original, String search, String replace, boolean all) { ! String valReturn = new String(""); ! int l = original.length(); ! int lo = search.length(); ! int i = 0; ! int j; ! while (i <= l) { ! j = original.indexOf(search, i); ! if (j == -1) { ! valReturn = valReturn.concat(original.substring(i, l)); ! i = l + 1; // Stop, no more occurrence. ! } else { ! valReturn = valReturn.concat(original.substring(i, j)); ! valReturn = valReturn.concat(replace); ! i = j + lo; ! if (!all) { // Stop, replace the first occurrence only. ! valReturn = valReturn.concat(original.substring(i, l)); ! i = l + 1; // Stop, replace the first occurrence only. ! } ! } ! } ! return valReturn; ! } ! /** ! * Returns a new string resulting from replacing all occurrences, ! * of search string in this string with replace string. ! * If the string search does not occur in the character sequence represented by this object, ! * then this string is returned. ! * ! * @param search the old string ! * @param replace the new string ! * @return a string derived from this string by replacing every occurrence of search with replace. ! */ ! public static String replace(String original, String search, String replace) { ! return replace(original, search, replace, true); ! } ! /** ! * Returns a new string resulting from replacing the end of this String ! * from oldSuffix to newSuffix. ! * The original string is returned if it does not end with oldSuffix. ! * ! * @param oldSuffix the old suffix ! * @param newSuffix the new suffix ! * @return a string derived from this string by replacing the end oldSuffix by newSuffix ! */ ! public static String replaceEndWith(String original, String oldSuffix, String newSuffix) { ! if (original.endsWith(oldSuffix)) { ! String st = original.substring(0, original.length() - oldSuffix.length()); ! return st.concat(newSuffix); ! } else { ! return original; ! } ! } } |