View Javadoc

1   /*
2    * $Id: StilStarTableWriter.java,v 1.3 2005/03/30 19:01:08 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
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  // RowListStarTable starTable = null;
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  //      starWriter.writeStarTable(this, out);
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  $Log: StilStarTableWriter.java,v $
187  Revision 1.3  2005/03/30 19:01:08  mch
188  fixes to results format
189 
190  Revision 1.2  2005/03/30 18:25:45  mch
191  fix for sql-server jdbc problem
192 
193  Revision 1.1  2005/03/21 18:45:55  mch
194  Naughty big lump of changes
195 
196  Revision 1.3  2005/03/10 15:13:48  mch
197  Seperating out fits, table and xdb servers
198 
199  Revision 1.2  2005/03/10 13:49:52  mch
200  Updating metadata
201 
202  Revision 1.1.1.1  2005/02/17 18:37:34  mch
203  Initial checkin
204 
205  Revision 1.1.1.1  2005/02/16 17:11:24  mch
206  Initial checkin
207 
208  Revision 1.1.2.3  2005/01/13 18:57:31  mch
209  Fixes to metadata mostly
210 
211  Revision 1.1.2.2  2004/12/08 18:36:40  mch
212  Added Vizier, rationalised SqlWriters etc, separated out TableResults from QueryResults
213 
214  Revision 1.1.2.1  2004/12/03 11:54:52  mch
215  added adaptor to STIL table-writing library
216 
217  Revision 1.1.2.3  2004/11/30 02:32:18  mch
218  fix to 0-base of writerows
219 
220  Revision 1.1.2.2  2004/11/30 01:04:02  mch
221  Rationalised tablewriters, reverted AxisDataService06 to string
222 
223  Revision 1.1.2.1  2004/11/25 18:33:43  mch
224  more status (incl persisting) more tablewriting lots of fixes
225 
226  Revision 1.1.2.1  2004/11/25 08:29:41  mch
227  added table writers modelled on STIL
228 
229 
230  */
231 
232 
233 
234 
235 
236 
237