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.slinger.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(), user);
39 printOut = new PrintWriter(new BufferedWriter(target.resolveWriter(user)));
40 separator = variableSeperator;
41 }
42
43 /*** Closes writer
44 */
45 public void close() {
46 printOut.close();
47 }
48
49 /*** Produces text/html */
50 public String getMimeType() {
51 if (separator.trim().equals(",")) {
52 return MimeTypes.CSV;
53 }
54 else if (separator.equals("\t")) {
55 return MimeTypes.TSV;
56 }
57 else {
58 return MimeTypes.PLAINTEXT;
59 }
60 }
61
62 public void startTable(ColumnInfo[] cols) throws IOException {
63
64
65 for (int i = 0; i < cols.length; i++) {
66 if (cols[i] == null) {
67 throw new IllegalArgumentException("No information for column "+i);
68 }
69 printOut.print(cols[i].getName());
70 if (i < cols.length-1) {
71 printOut.print(separator);
72 }
73 }
74 printOut.println();
75
76
77 for (int i = 0; i < cols.length; i++) {
78 if (cols[i].getUcd("1") != null) {
79 printOut.print(cols[i].getUcd("1"));
80 }
81 if (i < cols.length-1) {
82 printOut.print(separator);
83 }
84 }
85 printOut.println();
86
87
88 for (int i = 0; i < cols.length; i++) {
89 if (cols[i].getUnits() != null) {
90 printOut.print(cols[i].getUnits().toString());
91 }
92 if (i < cols.length-1) {
93 printOut.print(separator);
94 }
95 }
96
97 printOut.println();
98
99
100 for (int i = 0; i < cols.length; i++) {
101 printOut.print(cols[i].getPublicType());
102 if (i < cols.length-1) {
103 printOut.print(separator);
104 }
105 }
106 printOut.println();
107 for (int i = 0; i < cols.length; i++) {
108 if (cols[i].getJavaType() != null) {
109 printOut.print(cols[i].getJavaType().getName());
110 }
111 if (i < cols.length-1) {
112 printOut.print(separator);
113 }
114 }
115
116 printOut.println();
117 }
118
119
120 /*** Writes the given array of values out */
121 public void writeRow(Object[] colValues) throws IOException {
122
123 for (int i=0;i<colValues.length;i++) {
124 if (colValues[i] instanceof Date) {
125 printOut.print(dateFormat.format(colValues[i]));
126 }
127 else {
128 printOut.print(colValues[i]);
129 }
130 if (i < colValues.length-1) { printOut.print(separator); }
131 }
132 printOut.println();
133
134 }
135
136 /*** Does nothing */
137 public void endTable() {
138
139 }
140
141 /*** Abort writes out a line to show the table is incomplete */
142 public void abort() {
143 printOut.println(" ------------------ Writing Aborted ----------------- ");
144 close();
145 }
146
147
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