1
2
3
4
5
6
7 package org.astrogrid.tableserver.out;
8
9 import java.io.IOException;
10 import java.io.OutputStream;
11 import java.util.Vector;
12 import org.astrogrid.slinger.mime.MimeNames;
13 import org.astrogrid.tableserver.metadata.ColumnInfo;
14 import uk.ac.starlink.table.AbstractStarTable;
15 import uk.ac.starlink.table.RowSequence;
16 import uk.ac.starlink.table.StarTableWriter;
17
18 /***
19 * Adaptor to the STIL (Starlink Library) StarTableWriter,
20 * which can then be used to create fits files, votables, etc.
21 * However it's a non streaming build (the whole table is built) so use with care
22 * <p>
23 * An alternative, if there is a suitable StarTable representation of the data
24 * source, is to just create the appropriate StarTableWriter around the appropriate StarTable
25 * (essentially a table reader) and write it out.
26 *
27 * @author M Hill
28 */
29
30 public class StilStarTableWriter extends AbstractStarTable implements TableWriter {
31
32 StarTableWriter starWriter = null;
33
34 OutputStream out = null;
35
36 ColumnInfo[] cols = null;
37 Vector rows = new Vector();
38
39 /***
40 * Constructor for a table with the given anme
41 */
42 public StilStarTableWriter(StarTableWriter writer, OutputStream target) throws IOException {
43 starWriter = writer;
44 this.out = target;
45 }
46
47 /*** Closes writer - does nothing
48 */
49 public void close() throws IOException {
50 }
51
52 /*** try and return mime type from starWriters' format name */
53 public String getMimeType() {
54 return MimeNames.getMimeType(starWriter.getFormatName());
55 }
56
57 /*** Starts table - stores column info for write */
58 public void startTable(ColumnInfo[] newCols) throws IOException {
59 cols = newCols;
60 }
61
62
63 /*** Writes the given array of values out */
64 public void writeRow(Object[] colValues) throws IOException {
65 rows.add(colValues);
66 }
67
68 public void endTable() throws IOException {
69 throw new UnsupportedOperationException();
70
71 }
72
73
74 /*** Aborting does nothing as nothing happens until endtable anyway */
75 public void abort() {
76 }
77
78 /***
79 * StarTable implementation, returns the number of rows built
80 */
81 public long getRowCount() {
82 return rows.size();
83 }
84
85 /***
86 * StarTable implementation, returns the number of columns stored
87 */
88 public int getColumnCount() {
89 return cols.length;
90 }
91
92 /***
93 * StarTable implementation, returns the columinfo suitable for startable
94 */
95 public uk.ac.starlink.table.ColumnInfo getColumnInfo(int icol) {
96 uk.ac.starlink.table.ColumnInfo starColumn = new uk.ac.starlink.table.ColumnInfo(cols[icol].getName());
97 starColumn.setUnitString(cols[icol].getUnits().toString());
98 starColumn.setUCD(cols[icol].getUcd("1"));
99 starColumn.setContentClass(cols[icol].getJavaType());
100 return starColumn;
101 }
102
103 /***
104 * StarTable implementation, returns access to the rows
105 */
106 public RowSequence getRowSequence() throws IOException {
107 return new RowAdaptor();
108 }
109
110 private class RowAdaptor implements RowSequence {
111
112 int rowCursor = -1;
113 /***
114 * Indicates whether this table contains any more rows after the current
115 * one.
116 */
117 public boolean hasNext() {
118 return (rowCursor<rows.size());
119 }
120
121 /***
122 * Returns the contents of the current table row, as an array
123 * with the same number of elements as there are columns in this
124 * table.
125 * An unchecked exception will be thrown if there is no current
126 * row (<tt>next</tt> has not yet been called).
127 *
128 * @return an array of the objects in each cell in row <tt>irow</tt>
129 * @throws IOException if there is an error reading the data
130 */
131 public Object[] getRow() throws IOException {
132 if (rowCursor == -1) {
133 throw new IllegalStateException("Next not yet called");
134 }
135 return (Object[]) rows.get(rowCursor);
136 }
137
138 /***
139 * Indicates that this sequence will not be required any more.
140 * This should release resources associated with this object.
141 * The effect of calling any of the other methods following a
142 * <code>close</code> is undefined.
143 */
144 public void close() throws IOException {
145 cols = null;
146 rows = null;
147 }
148
149 /***
150 * Advances the current row to the next one.
151 * Since the initial position of a RowSequence is before the first row,
152 * this method must be called before current row data
153 * can be accessed using the
154 * {@link #getCell(int)} or {@link #getRow()} methods.
155 * An unchecked exception such as <tt>NoSuchElementException</tt>
156 * will be thrown if {@link #hasNext} returns <tt>false</tt>.
157 *
158 * @throws IOException if there is some error in the positioning
159 */
160 public void next() throws IOException {
161 rowCursor++;
162 }
163
164 /***
165 * Returns the contents of a cell in the current row.
166 * The class of the returned object should be the same as,
167 * or a subclass of, the class returned by
168 * <tt>getColumnInfo(icol).getContentClass()</tt>.
169 * An unchecked exception will be thrown if there is no current
170 * row (<tt>next</tt> has not yet been called).
171 *
172 * @return the contents of cell <tt>icol</tt> in the current row
173 * @throws IOException if there is an error reading the data
174 * @throws IllegalStateException if there is no current row (before the
175 * start of the table)
176 */
177 public Object getCell(int icol) throws IOException {
178 return getRow()[icol];
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