View Javadoc

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