-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathCSVSink.java
More file actions
134 lines (109 loc) · 2.96 KB
/
CSVSink.java
File metadata and controls
134 lines (109 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Jython Database Specification API 2.0
*
*
* Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
*
*/
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;
/**
* The CSVSink writes data out in a Comma Seperated Format.
*
* @author brian zimmer
*/
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() {
}
}