001: /*
002: * Jython Database Specification API 2.0
003: *
004: * $Id: CSVSink.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: import com.ziclix.python.sql.pipe.Sink;
012: import org.python.core.Py;
013: import org.python.core.PyObject;
014:
015: import java.io.PrintWriter;
016:
017: /**
018: * The CSVSink writes data out in a Comma Seperated Format.
019: *
020: * @author brian zimmer
021: * @version $Revision: 2414 $
022: */
023: public class CSVSink implements Sink {
024:
025: /**
026: * Field header
027: */
028: protected boolean header;
029:
030: /**
031: * Field delimiter
032: */
033: protected String delimiter;
034:
035: /**
036: * Field writer
037: */
038: protected PrintWriter writer;
039:
040: /**
041: * Field converters
042: */
043: protected PyObject converters;
044:
045: /**
046: * All data will be written to the given PrintWriter.
047: *
048: * @param writer the PrintWriter to which data will be written
049: */
050: public CSVSink(PrintWriter writer) {
051: this (writer, Py.None);
052: }
053:
054: /**
055: * All data will be written to the given PrintWriter. If
056: * the converters param is not None, then an attempt will
057: * be made to convert the object using the given converter.
058: *
059: * @param writer the PrintWriter to which data will be written
060: * @param converters an indexed dictionary of callable objects used for converting objects to strings
061: */
062: public CSVSink(PrintWriter writer, PyObject converters) {
063:
064: this .header = false;
065: this .writer = writer;
066: this .converters = converters;
067: this .delimiter = ",";
068: }
069:
070: /**
071: * Handle the data callback and write the row out.
072: */
073: public void row(PyObject row) {
074:
075: String[] values = new String[row.__len__()];
076:
077: if (this .header) {
078: for (int i = 0; i < row.__len__(); i++) {
079: values[i] = this .convert(Py.newInteger(i), row
080: .__getitem__(i));
081: }
082: } else {
083: for (int i = 0; i < row.__len__(); i++) {
084: values[i] = row.__getitem__(i).__getitem__(0)
085: .toString();
086: }
087:
088: this .header = true;
089: }
090:
091: this .println(values);
092: }
093:
094: /**
095: * Convert the object at index to a String.
096: */
097: protected String convert(PyObject index, PyObject object) {
098:
099: if (this .converters != Py.None) {
100: PyObject converter = this .converters.__finditem__(index);
101:
102: if (converter != Py.None) {
103: object = converter.__call__(object);
104: }
105: }
106:
107: if ((object == Py.None) || (object == null)) {
108: return "";
109: }
110:
111: return CSVString.toCSV(object.toString());
112: }
113:
114: /**
115: * Print the row of Strings.
116: */
117: protected void println(String[] row) {
118:
119: for (int i = 0; i < row.length - 1; i++) {
120: this .writer.print(row[i]);
121: this .writer.print(this .delimiter);
122: }
123:
124: this .writer.println(row[row.length - 1]);
125: }
126:
127: /**
128: * Method start
129: */
130: public void start() {
131: }
132:
133: /**
134: * Method end
135: */
136: public void end() {
137: }
138: }
|