1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.scripting.table;
12
13 import org.astrogrid.applications.parameter.protocol.ExternalValue;
14 import org.astrogrid.applications.parameter.protocol.InaccessibleExternalValueException;
15 import org.astrogrid.scripting.Toolbox;
16
17 import java.awt.datatransfer.Transferable;
18 import java.io.ByteArrayInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.URL;
22
23 import uk.ac.starlink.table.StarTable;
24 import uk.ac.starlink.table.StarTableFactory;
25 import uk.ac.starlink.table.TableFormatException;
26 import uk.ac.starlink.util.DataSource;
27
28 /*** Extension of the standard StarTableFactory that also allows star tables to be contructed from ExternalValues
29 * encourages factory to build random-access tables by dfault - less surprising then.
30 * <p>
31 * also wraps each returned starTable as a ScriptStarTable - which provides further script-friendly methods.
32 * @script-summary factory for StarTables
33 * @script-doc factory for ScriptStarTables - extends StarLink's StarTableFactory, but gurantees all
34 * tables returned will be instances of {@link org.astrogrid.scripting.table.ScriptStarTable}<br />
35 * StarTableFactory Javadoc - <a href="http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/StarTableFactory.html">http://www.star.bristol.ac.uk/~mbt/stil/javadocs/uk/ac/starlink/table/StarTableFactory.html</a>
36 * @author Noel Winstanley nw@jb.man.ac.uk 22-Nov-2004
37 *
38 */
39 public class StarTableBuilder extends StarTableFactory {
40
41 /*** Construct a new StarTableBuilder
42 *
43 */
44 public StarTableBuilder() {
45 super(true);
46 }
47
48
49 public StarTable makeStarTable(DataSource arg0, String arg1)
50 throws TableFormatException, IOException {
51 return new WrapperScriptStarTable( super.makeStarTable(arg0, arg1));
52 }
53 public StarTable makeStarTable(DataSource arg0)
54 throws TableFormatException, IOException {
55 return new WrapperScriptStarTable (super.makeStarTable(arg0));
56 }
57 public StarTable makeStarTable(String location, String format)
58 throws TableFormatException, IOException {
59 return new WrapperScriptStarTable (super.makeStarTable(location, format));
60 }
61 /*** load the contents of a file / url into a scriptable star table */
62 public StarTable makeStarTable(String location) throws TableFormatException,
63 IOException {
64 return new WrapperScriptStarTable (super.makeStarTable(location));
65 }
66 public StarTable makeStarTable(Transferable arg0) throws IOException {
67 return new WrapperScriptStarTable(super.makeStarTable(arg0));
68 }
69 /*** load the contents of a URL into a scriptable star table*/
70 public StarTable makeStarTable(URL location, String format)
71 throws TableFormatException, IOException {
72 return new WrapperScriptStarTable(super.makeStarTable(location, format));
73 }
74 /*** load the contents of a URL into a scriptable star table */
75 public StarTable makeStarTable(URL location) throws IOException {
76 return new WrapperScriptStarTable(super.makeStarTable(location));
77 }
78 /*** return a guaranteed random-access scriptable star table */
79 public StarTable randomTable(StarTable table) throws IOException {
80 return new WrapperScriptStarTable(super.randomTable(table));
81 }
82 /*** construct a star table from an external value
83 *
84 * @param externalValue reference to a remote location
85 * @return a star table
86 * @throws TableFormatException if table data is not in a known format
87 * @throws IOException if remote location cannot be accessed
88 * @see Toolbox#getProtocolLibrary() to create an {@link ExternalValue} *
89 */
90 public StarTable makeStarTable(ExternalValue externalValue) throws TableFormatException, IOException {
91 return this.makeStarTable(new ExternalValueDataSource(externalValue));
92 }
93
94 /*** construct a star table from an external value
95 *
96 * @param externalValue reference to a remote location
97 * @param format expected table format
98 * @return a star table
99 * @throws TableFormatException if table data is not in expected format
100 * @throws IOException if remote location cannot be accessed
101 * @see Toolbox#getProtocolLibrary() to create an {@link ExternalValue}
102 */
103 public StarTable makeStarTable(ExternalValue externalValue,String format) throws TableFormatException, IOException {
104 return this.makeStarTable(new ExternalValueDataSource(externalValue),format);
105 }
106
107 /*** construct a starTable from the contents of a string */
108 public StarTable makeStarTableFromString(String tableContent,String format) throws TableFormatException, IOException {
109 return this.makeStarTable(new InlineDataSource(tableContent),format);
110 }
111 /*** construct a starTable from the contents of a string */
112 public StarTable makeStarTableFromString(String tableContent) throws TableFormatException, IOException {
113 return this.makeStarTable(new InlineDataSource(tableContent));
114 }
115
116 /*** a helper class that wraps a string as a datasource
117 *
118 * @author Noel Winstanley nw@jb.man.ac.uk 24-Nov-2004
119 *
120 */
121 public static class InlineDataSource extends DataSource {
122 public InlineDataSource(String content) {
123 this.content = content;
124 }
125 protected final String content;
126 /***
127 * @see uk.ac.starlink.util.DataSource#getRawInputStream()
128 */
129 protected InputStream getRawInputStream() throws IOException {
130 return new ByteArrayInputStream(content.getBytes());
131 }
132 }
133
134 /***
135 * A helper class that wraps an ExternalValue as a Datasource - acts as a bridge between the
136 * Astrogrid and Starlink worlds.
137 * @see ExternalValue
138 * @author Noel Winstanley nw@jb.man.ac.uk 22-Nov-2004
139 *
140 */
141 public static class ExternalValueDataSource extends DataSource {
142 public ExternalValueDataSource(ExternalValue val) {
143 this.val = val;
144 }
145 private final ExternalValue val;
146 /***
147 * @see uk.ac.starlink.util.DataSource#getRawInputStream()
148 */
149 protected InputStream getRawInputStream() throws IOException {
150
151 try {
152 return val.read();
153 } catch (InaccessibleExternalValueException e) {
154 throw new IOException(e.getMessage());
155 }
156 }
157 }
158
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190