From: brian z. <bz...@us...> - 2001-11-20 04:55:21
|
Update of /cvsroot/jython/jython/com/ziclix/python/sql/pipe/csv In directory usw-pr-cvs1:/tmp/cvs-serv7094/com/ziclix/python/sql/pipe/csv Added Files: CSVSink.java CSVString.java Log Message: initial zxJDBC checkin --- NEW FILE: CSVSink.java --- /* * Jython Database Specification API 2.0 * * $Id: CSVSink.java,v 1.1 2001/11/20 04:55:18 bzimmer Exp $ * * Copyright (c) 2001 brian zimmer <bz...@zi...> * */ package com.ziclix.python.sql.pipe.csv; import java.io.*; import org.python.core.*; import com.ziclix.python.sql.pipe.Sink; /** * The CSVSink writes data out in a Comma Seperated Format. * * @author brian zimmer * @version $Revision: 1.1 $ */ 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() {} } --- NEW FILE: CSVString.java --- /* * Jython Database Specification API 2.0 * * $Id: CSVString.java,v 1.1 2001/11/20 04:55:18 bzimmer Exp $ * * Copyright (c) 2001 brian zimmer <bz...@zi...> * */ package com.ziclix.python.sql.pipe.csv; /** * A utility class to aide in quoting CSV strings. * * @author brian zimmer * @version $Revision: 1.1 $ */ 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; } } } |