View Javadoc

1   /*
2    * $Id: XsvTableWriter.java,v 1.8 2005/05/27 16:21:02 clq2 Exp $
3    *
4    * (C) Copyright Astrogrid...
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        //column names, line 1
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        //ucds, line 2
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        //units, line 3
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       //types
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();//new line
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  $Log: XsvTableWriter.java,v $
155  Revision 1.8  2005/05/27 16:21:02  clq2
156  mchv_1
157 
158  Revision 1.7.8.2  2005/05/13 16:56:32  mch
159  'some changes'
160 
161  Revision 1.7.8.1  2005/04/21 17:20:51  mch
162  Fixes to output types
163 
164  Revision 1.7  2005/03/31 15:06:16  mch
165  Fixes and workarounds for null values, misisng metadoc columns
166 
167  Revision 1.6  2005/03/31 12:10:28  mch
168  Fixes and workarounds for null values, misisng metadoc columns
169 
170  Revision 1.5  2005/03/30 21:51:25  mch
171  Fix to return Votable fits list for url list
172 
173  Revision 1.4  2005/03/30 18:25:45  mch
174  fix for sql-server jdbc problem
175 
176  Revision 1.3  2005/03/30 15:52:15  mch
177  debug etc for bad sql types
178 
179  Revision 1.2  2005/03/30 15:18:55  mch
180  debug etc for bad sql types
181 
182  Revision 1.1  2005/03/21 18:45:55  mch
183  Naughty big lump of changes
184 
185  Revision 1.4  2005/03/10 22:39:17  mch
186  Fixed tests more metadata fixes
187 
188  Revision 1.3  2005/03/10 15:13:48  mch
189  Seperating out fits, table and xdb servers
190 
191  Revision 1.2  2005/03/10 13:49:52  mch
192  Updating metadata
193 
194  Revision 1.1.1.1  2005/02/17 18:37:34  mch
195  Initial checkin
196 
197  Revision 1.1.1.1  2005/02/16 17:11:24  mch
198  Initial checkin
199 
200  Revision 1.1.2.7  2005/01/24 12:14:28  mch
201  Fixes to VizieR proxy and resource stuff
202 
203  Revision 1.1.2.6  2005/01/13 18:57:31  mch
204  Fixes to metadata mostly
205 
206  Revision 1.1.2.5  2004/12/13 21:53:14  mch
207  Made the java types the intermediate types, added types to Xsv and html output
208 
209  Revision 1.1.2.4  2004/12/08 18:36:40  mch
210  Added Vizier, rationalised SqlWriters etc, separated out TableResults from QueryResults
211 
212  Revision 1.1.2.3  2004/12/06 02:50:30  mch
213  a few bug fixes
214 
215  Revision 1.1.2.2  2004/11/30 02:32:18  mch
216  fix to 0-base of writerows
217 
218  Revision 1.1.2.1  2004/11/30 01:26:42  mch
219  added tablewriters
220 
221  Revision 1.1.2.1  2004/11/25 18:33:43  mch
222  more status (incl persisting) more tablewriting lots of fixes
223 
224  Revision 1.1.2.1  2004/11/25 08:29:41  mch
225  added table writers modelled on STIL
226 
227 
228  */
229 
230 
231 
232 
233 
234 
235 
236 
237