KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: CSVSink.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 import com.ziclix.python.sql.pipe.Sink;
12 import org.python.core.Py;
13 import org.python.core.PyObject;
14
15 import java.io.PrintWriter JavaDoc;
16
17 /**
18  * The CSVSink writes data out in a Comma Seperated Format.
19  *
20  * @author brian zimmer
21  * @version $Revision: 1.2 $
22  */

23 public class CSVSink implements Sink {
24
25     /**
26      * Field header
27      */

28     protected boolean header;
29
30     /**
31      * Field delimiter
32      */

33     protected String JavaDoc delimiter;
34
35     /**
36      * Field writer
37      */

38     protected PrintWriter JavaDoc writer;
39
40     /**
41      * Field converters
42      */

43     protected PyObject converters;
44
45     /**
46      * All data will be written to the given PrintWriter.
47      *
48      * @param writer the PrintWriter to which data will be written
49      */

50     public CSVSink(PrintWriter JavaDoc writer) {
51         this(writer, Py.None);
52     }
53
54     /**
55      * All data will be written to the given PrintWriter. If
56      * the converters param is not None, then an attempt will
57      * be made to convert the object using the given converter.
58      *
59      * @param writer the PrintWriter to which data will be written
60      * @param converters an indexed dictionary of callable objects used for converting objects to strings
61      */

62     public CSVSink(PrintWriter JavaDoc writer, PyObject converters) {
63
64         this.header = false;
65         this.writer = writer;
66         this.converters = converters;
67         this.delimiter = ",";
68     }
69
70     /**
71      * Handle the data callback and write the row out.
72      */

73     public void row(PyObject row) {
74
75         String JavaDoc[] values = new String JavaDoc[row.__len__()];
76
77         if (this.header) {
78             for (int i = 0; i < row.__len__(); i++) {
79                 values[i] = this.convert(Py.newInteger(i), row.__getitem__(i));
80             }
81         } else {
82             for (int i = 0; i < row.__len__(); i++) {
83                 values[i] = row.__getitem__(i).__getitem__(0).toString();
84             }
85
86             this.header = true;
87         }
88
89         this.println(values);
90     }
91
92     /**
93      * Convert the object at index to a String.
94      */

95     protected String JavaDoc convert(PyObject index, PyObject object) {
96
97         if (this.converters != Py.None) {
98             PyObject converter = this.converters.__finditem__(index);
99
100             if (converter != Py.None) {
101                 object = converter.__call__(object);
102             }
103         }
104
105         if ((object == Py.None) || (object == null)) {
106             return "";
107         }
108
109         return CSVString.toCSV(object.toString());
110     }
111
112     /**
113      * Print the row of Strings.
114      */

115     protected void println(String JavaDoc[] row) {
116
117         for (int i = 0; i < row.length - 1; i++) {
118             this.writer.print(row[i]);
119             this.writer.print(this.delimiter);
120         }
121
122         this.writer.println(row[row.length - 1]);
123     }
124
125     /**
126      * Method start
127      */

128     public void start() {
129     }
130
131     /**
132      * Method end
133      */

134     public void end() {
135     }
136 }
137
Popular Tags