View Javadoc

1   /*$Id: TableHelper.java,v 1.4 2004/12/07 18:05:52 nw Exp $
2    * Created on 03-Dec-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.scripting;
12  
13  import org.astrogrid.applications.parameter.protocol.ExternalValue;
14  import org.astrogrid.applications.parameter.protocol.InaccessibleExternalValueException;
15  import org.astrogrid.io.Piper;
16  import org.astrogrid.scripting.table.MutableScriptStarTable;
17  import org.astrogrid.scripting.table.StarTableBuilder;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import uk.ac.starlink.table.ColumnInfo;
29  import uk.ac.starlink.table.RowListStarTable;
30  import uk.ac.starlink.table.StarTable;
31  import uk.ac.starlink.table.StarTableOutput;
32  
33  /*** Helper object for working with STIL tables <p />
34   * 
35   * See Userguide - <a href="http://www.star.bristol.ac.uk/~mbt/stil/sun252.html">http://www.star.bristol.ac.uk/~mbt/stil/sun252.html</a><br />
36   * See JavaDoc - <a href="http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/package-summary.html">http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/package-summary.html</a><br />
37   * @author Noel Winstanley nw@jb.man.ac.uk 03-Dec-2004
38   *
39   */
40  public class TableHelper {
41  
42      /*** Construct a new TableHelper
43       * 
44       */
45      public TableHelper() {
46          super();
47      }
48  
49      protected final StarTableBuilder builder = new StarTableBuilder();
50      protected final StarTableOutput output = new StarTableOutput();
51      /***
52       * @script-doc access the table builder
53       */
54      public StarTableBuilder getBuilder() {
55          return builder;
56      }
57  // output    
58      /*** @throws InaccessibleExternalValueException
59       * @throws IOException
60       * @script-doc write star table to an external location */
61      public void writeTable(ExternalValue val,StarTable table, String format) throws InaccessibleExternalValueException, IOException {
62          // ok. really inefficient, but must do for now.
63          InputStream in = this.toInputStream(table,format);
64          OutputStream os = val.write();
65          Piper.pipe(in,os);
66          in.close();
67          os.close();
68      }
69      
70      /*** @script-doc access contents of table as a string 
71       * @throws IOException*/
72      public String toString(StarTable table,String format) throws IOException {
73          InputStream in = this.toInputStream(table,format);
74          ByteArrayOutputStream bos = new ByteArrayOutputStream();
75          Piper.pipe(in,bos);
76          in.close();
77          bos.close();
78          return bos.toString();
79      }
80      /*** @script-doc access contents of a table as a stream */
81      public InputStream toInputStream(StarTable table,String format) throws IOException {
82          File f = File.createTempFile("tableHelper",null);
83          f.deleteOnExit();
84          f.createNewFile();
85          output.writeStarTable(table,f.toString(),format);
86          return new FileInputStream(f);
87      }
88  
89      /***@script-doc create a new empty mutable table */
90      public MutableScriptStarTable newMutableTable(ColumnInfo[] info) {
91          return new MutableScriptStarTable(info);
92      }
93      
94      /*** @script-doc create a new empty mutable table, with the same structure as a template table*/
95      public MutableScriptStarTable newMutableTableFromTemplate(StarTable s) {
96          return new MutableScriptStarTable(s);
97      }
98      /*** @script-doc access the STIL library object for writing out tables - see <a href="http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/StarTableOutput.html" target="alexandria_uri">http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/StarTableOutput.html">http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/StarTableOutput.html" target="alexandria_uri">http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/StarTableOutput.html</a> */
99      public StarTableOutput getOutput() {
100         return this.output;
101     }
102     /***@script-doc create a new column info object - see <a href="http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/ColumnInfo.html" target="alexandria_uri">http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/ColumnInfo.html">http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/ColumnInfo.html" target="alexandria_uri">http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/ColumnInfo.html</a> */
103     public ColumnInfo newColumnInfo(String name) {
104         return new ColumnInfo(name);
105     }
106     /***@script-doc create a new column info object - see <a href="http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/ColumnInfo.html" target="alexandria_uri">http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/ColumnInfo.html">http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/ColumnInfo.html" target="alexandria_uri">http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/ColumnInfo.html</a> */
107         
108     public ColumnInfo newColumnInfo(String name,Class contentType,String description) {
109         return new ColumnInfo(name,contentType,description);
110     }
111     
112 }
113 
114 
115 /* 
116 $Log: TableHelper.java,v $
117 Revision 1.4  2004/12/07 18:05:52  nw
118 javadoc fixes
119 
120 Revision 1.3  2004/12/07 16:50:33  jdt
121 merges from scripting-nww-805
122 
123 Revision 1.2.2.1  2004/12/07 14:47:58  nw
124 got table manipulation working.
125 
126 Revision 1.2  2004/12/06 20:03:03  clq2
127 nww_807a
128 
129 Revision 1.1.2.1  2004/12/06 13:27:47  nw
130 fixes to improvide integration with external values and starTables.
131  
132 */