1
2
3
4
5
6
7
8
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
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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132