1
2
3
4
5
6
7 package org.astrogrid.tableserver.out;
8
9 import java.io.BufferedWriter;
10 import java.io.IOException;
11 import java.io.PrintWriter;
12 import java.security.Principal;
13 import java.util.Date;
14 import org.apache.commons.logging.Log;
15 import org.astrogrid.io.mime.MimeTypes;
16 import org.astrogrid.slinger.targets.TargetIdentifier;
17 import org.astrogrid.tableserver.metadata.ColumnInfo;
18
19 /***
20 * For writing out tables in something-separated form, eg comma separated or
21 * tab separated
22 *
23 * @author M Hill
24 */
25
26 public class XsvTableWriter extends AsciiTableSupport {
27
28 protected static final Log log = org.apache.commons.logging.LogFactory.getLog(HtmlTableWriter.class);
29
30 PrintWriter printOut = null;
31
32 String separator = ",";
33
34 /***
35 * Construct this wrapping the given stream. Values on a line will be separated with the given string
36 */
37 public XsvTableWriter(TargetIdentifier target, String title, String variableSeperator, Principal user) throws IOException {
38 target.setMimeType(getMimeType());
39 printOut = new PrintWriter(new BufferedWriter(target.openWriter()));
40 separator = variableSeperator;
41 }
42
43 public void open() {
44 }
45
46 /*** Closes writer
47 */
48 public void close() {
49 printOut.close();
50 }
51
52 /*** Produces text/html */
53 public String getMimeType() {
54 if (separator.trim().equals(",")) {
55 return MimeTypes.CSV;
56 }
57 else if (separator.equals("\t")) {
58 return MimeTypes.TSV;
59 }
60 else {
61 return MimeTypes.PLAINTEXT;
62 }
63 }
64
65 public void startTable(ColumnInfo[] cols) throws IOException {
66
67
68 for (int i = 0; i < cols.length; i++) {
69 if (cols[i] == null) {
70 throw new IllegalArgumentException("No information for column "+i);
71 }
72 printOut.print(cols[i].getName());
73 if (i < cols.length-1) {
74 printOut.print(separator);
75 }
76 }
77 printOut.println();
78
79
80 for (int i = 0; i < cols.length; i++) {
81 if (cols[i].getUcd("1") != null) {
82 printOut.print(cols[i].getUcd("1"));
83 }
84 if (i < cols.length-1) {
85 printOut.print(separator);
86 }
87 }
88 printOut.println();
89
90
91 for (int i = 0; i < cols.length; i++) {
92 if (cols[i].getUnits() != null) {
93 printOut.print(cols[i].getUnits().toString());
94 }
95 if (i < cols.length-1) {
96 printOut.print(separator);
97 }
98 }
99
100 printOut.println();
101
102
103 for (int i = 0; i < cols.length; i++) {
104 printOut.print(cols[i].getPublicType());
105 if (i < cols.length-1) {
106 printOut.print(separator);
107 }
108 }
109 printOut.println();
110 for (int i = 0; i < cols.length; i++) {
111 if (cols[i].getJavaType() != null) {
112 printOut.print(cols[i].getJavaType().getName());
113 }
114 if (i < cols.length-1) {
115 printOut.print(separator);
116 }
117 }
118
119 printOut.println();
120 }
121
122
123 /*** Writes the given array of values out */
124 public void writeRow(Object[] colValues) throws IOException {
125
126 for (int i=0;i<colValues.length;i++) {
127 if (colValues[i] instanceof Date) {
128 printOut.print(dateFormat.format(colValues[i]));
129 }
130 else {
131 printOut.print(colValues[i]);
132 }
133 if (i < colValues.length-1) { printOut.print(separator); }
134 }
135 printOut.println();
136
137 }
138
139 /*** Does nothing */
140 public void endTable() {
141
142 }
143
144 /*** Abort writes out a line to show the table is incomplete */
145 public void abort() {
146 printOut.println(" ------------------ Writing Aborted ----------------- ");
147 close();
148 }
149
150
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237