View Javadoc

1   /*$Id: StarTableBuilder.java,v 1.4 2004/12/07 18:05:52 nw Exp $
2    * Created on 22-Nov-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.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 $Log: StarTableBuilder.java,v $
164 Revision 1.4  2004/12/07 18:05:52  nw
165 javadoc fixes
166 
167 Revision 1.3  2004/12/07 16:50:33  jdt
168 merges from scripting-nww-805
169 
170 Revision 1.2.2.1  2004/12/07 14:47:58  nw
171 got table manipulation working.
172 
173 Revision 1.2  2004/12/06 20:03:03  clq2
174 nww_807a
175 
176 Revision 1.1.2.1  2004/12/06 13:27:47  nw
177 fixes to improvide integration with external values and starTables.
178 
179 Revision 1.3  2004/11/30 15:39:56  clq2
180 scripting-nww-777
181 
182 Revision 1.1.2.1.2.1  2004/11/25 00:34:54  nw
183 configured to build random-access tables,
184 and added methods to build tables from string
185 
186 Revision 1.1.2.1  2004/11/22 15:54:51  nw
187 deprecated existing scripting interface (which includes service lists).
188 produced new scripting interface, with more helpler objects.
189  
190 */