uk.ac.starlink.table
Class StarTableFactory

java.lang.Object
  extended byuk.ac.starlink.table.StarTableFactory

public class StarTableFactory
extends Object

Manufactures StarTable objects from generic inputs. This factory delegates the actual table creation to external TableBuilder objects, each of which knows how to read a particular table format. Various makeStarTable methods are offered, which construct StarTables from different types of object, such as URL and DataSource. Each of these comes in two types: automatic format detection and named format.

In the case of a named format, a specifier must be given for the format in which the table to be read is held. This may be one of the following:

In the case of automatic format detection (no format specified), the factory hands the table location to each of the handlers in the default handler list in turn, and if any of them can make a table out of it, it is returned.

In either case, failure to make a table will usually result in a TableFormatException, though if an error in actual I/O is encountered an IOException may be thrown instead.

By default, if the corresponding classes are present, the following TableBuilders are installed in the default handler list (used by default in automatic format detection):

The following additional handlers are installed in the known handler list (not used by default but available by specifying the format name):

Additionally, any classes named in the startable.readers system property (as a colon-separated list) which implement the TableBuilder interface and have a no-arg constructor will be instantiated and added to the known handler list.

The factory has a flag requireRandom which determines whether the makeStarTable methods are guaranteed to return tables which provide random access (StarTable.isRandom()==true). NOTE the meaning (and name) of this flag has changed as of STIL version 2.1. Previously it was only a hint that random tables were preferred. Now setting it true guarantees that all tables returned by the factory are random.


Field Summary
static String AUTO_HANDLER
          Special handler identifier which signifies automatic format detection.
static String KNOWN_BUILDERS_PROPERTY
          System property which can contain a list of TableBuilder classes for addition to the known (non-automatically detected) handler list.
 
Constructor Summary
StarTableFactory()
          Constructs a StarTableFactory with a default list of builders which is not guaranteed to construct random-access tables.
StarTableFactory(boolean requireRandom)
          Constructs a StarTableFactory with a default list of builders specifying whether it will return random-access tables.
 
Method Summary
 boolean canImport(DataFlavor[] flavors)
          Indicates whether a particular set of DataFlavor ojects offered by a Transferable is suitable for attempting to turn the Transferable into a StarTable.
 List getDefaultBuilders()
          Gets the list of builders which are used for automatic format detection.
 JDBCHandler getJDBCHandler()
          Returns the JDBC handler object used by this factory.
 List getKnownBuilders()
          Gets the list of builders which are available for selection by format name.
 List getKnownFormats()
          Returns the list of format names, one for each of the handlers returned by getKnownBuilders().
 StoragePolicy getStoragePolicy()
          Returns the current storage policy.
 TableBuilder getTableBuilder(String name)
          Returns a table handler with a given name.
 StarTable makeStarTable(DataSource datsrc)
          Constructs a readable StarTable from a DataSource object using automatic format detection.
 StarTable makeStarTable(DataSource datsrc, String handler)
          Constructs a readable StarTable from a DataSource using a named table input handler.
 StarTable makeStarTable(InputStream in, TableBuilder builder)
          Attempts to read and return a StarTable from an input stream.
 StarTable makeStarTable(String location)
          Constructs a readable StarTable from a location string using automatic format detection.
 StarTable makeStarTable(String location, String handler)
          Constructs a readable StarTable from a location string using a named table input handler.
 StarTable makeStarTable(Transferable trans)
          Constructs a StarTable from a Transferable object using automatic format detection.
 StarTable makeStarTable(URL url)
          Deprecated. Use makeStarTable(new URLDataSource(url))
 StarTable makeStarTable(URL url, String handler)
          Deprecated. Use makeStarTable(new URLDataSource(url),handler)
 StarTable[] makeStarTables(DataSource datsrc)
          Constructs an array of StarTables from a DataSource using automatic format detection.
 StarTable[] makeStarTables(DataSource datsrc, String handler)
          Constructs an array of StarTables from a DataSource using a named table input handler.
 StarTable randomTable(StarTable table)
          Returns a table based on a given table and guaranteed to have random access.
 boolean requireRandom()
          Returns the requireRandom flag.
 void setDefaultBuilders(TableBuilder[] builders)
          Sets the list of builders which actually do the table construction.
 void setJDBCHandler(JDBCHandler handler)
          Sets the JDBC handler object used by this factory.
 void setKnownBuilders(TableBuilder[] builders)
          Sets the list of builders which are available for selection by format name.
 void setRequireRandom(boolean requireRandom)
          Sets whether random-access tables will be constructed by this factory.
 void setStoragePolicy(StoragePolicy policy)
          Sets the storage policy.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KNOWN_BUILDERS_PROPERTY

public static final String KNOWN_BUILDERS_PROPERTY
System property which can contain a list of TableBuilder classes for addition to the known (non-automatically detected) handler list.

See Also:
Constant Field Values

AUTO_HANDLER

public static final String AUTO_HANDLER
Special handler identifier which signifies automatic format detection.

See Also:
Constant Field Values
Constructor Detail

StarTableFactory

public StarTableFactory()
Constructs a StarTableFactory with a default list of builders which is not guaranteed to construct random-access tables.


StarTableFactory

public StarTableFactory(boolean requireRandom)
Constructs a StarTableFactory with a default list of builders specifying whether it will return random-access tables.

Parameters:
requireRandom - whether random-access tables will be constructed
Method Detail

getDefaultBuilders

public List getDefaultBuilders()
Gets the list of builders which are used for automatic format detection. Builders earlier in the list are given a chance to make the table before ones later in the list. This list can be modified to change the behaviour of the factory.

Returns:
a mutable list of TableBuilder objects used to construct StarTables

setDefaultBuilders

public void setDefaultBuilders(TableBuilder[] builders)
Sets the list of builders which actually do the table construction. Builders earlier in the list are given a chance to make the table before ones later in the list.

Parameters:
builders - an array of TableBuilder objects used to construct StarTables

getKnownBuilders

public List getKnownBuilders()
Gets the list of builders which are available for selection by format name. This is initially set to the list of default builders plus a few others. This list can be modified to change the behaviour of the factory.

Returns:
a mutable list of TableBuilder objects which may be specified for table building

setKnownBuilders

public void setKnownBuilders(TableBuilder[] builders)
Sets the list of builders which are available for selection by format name. This is initially set to the list of default builders plus a few others.

Parameters:
builders - an array of TableBuilder objects used to construct StarTables

getKnownFormats

public List getKnownFormats()
Returns the list of format names, one for each of the handlers returned by getKnownBuilders().

Returns:
list of format name strings

setRequireRandom

public void setRequireRandom(boolean requireRandom)
Sets whether random-access tables will be constructed by this factory. If this flag is set true then any table returned by the various makeStarTable methods is guaranteed to provide random access (its StarTable.isRandom() method will return true). If the flag is false, then returned tables may or may not be random-access.

Parameters:
requireRandom - whether this factory will create random-access tables

requireRandom

public boolean requireRandom()
Returns the requireRandom flag. If this flag is set true then any table returned by the various makeStarTable methods is guaranteed to provide random access (its StarTable.isRandom() method will return true). If the flag is false, then returned tables may or may not be random-access.

Returns:
whether this factory will create random-access tables

setStoragePolicy

public void setStoragePolicy(StoragePolicy policy)
Sets the storage policy. This may be used to determine what kind of scratch storage is used when constructing tables.

Parameters:
policy - the new storage policy object

getStoragePolicy

public StoragePolicy getStoragePolicy()
Returns the current storage policy. This may be used to determine what kind of scratch storage is used when constructing tables. If it has not been set explicitly, the default policy is used (StoragePolicy.getDefaultPolicy()).

Returns:
storage policy object

randomTable

public StarTable randomTable(StarTable table)
                      throws IOException
Returns a table based on a given table and guaranteed to have random access. If the original table table has random access then it is returned, otherwise a new random access table is built using its data.

This convenience method is equivalent to getStoragePolicy().randomTable(table).

Parameters:
table - original table
Returns:
a table with the same data as table and with isRandom()==true
Throws:
IOException

makeStarTable

public StarTable makeStarTable(DataSource datsrc)
                        throws TableFormatException,
                               IOException
Constructs a readable StarTable from a DataSource object using automatic format detection.

Parameters:
datsrc - the data source containing the table data
Returns:
a new StarTable view of the resource datsrc
Throws:
TableFormatException - if none of the default handlers could turn datsrc into a table
IOException - if an I/O error is encountered

makeStarTables

public StarTable[] makeStarTables(DataSource datsrc)
                           throws TableFormatException,
                                  IOException
Constructs an array of StarTables from a DataSource using automatic format detection. Only certain formats (those whose handlers implement MultiTableBuilder will be capable of returning an array having more than one element.

Parameters:
datsrc - the data source containing the table data
Returns:
an array of tables loaded from datsrc
Throws:
TableFormatException - if none of the default handlers could turn datsrc into a table
IOException - if an I/O error is encountered

makeStarTable

public StarTable makeStarTable(String location)
                        throws TableFormatException,
                               IOException
Constructs a readable StarTable from a location string using automatic format detection. The location string can represent a filename or URL, including a jdbc: protocol URL if an appropriate JDBC driver is installed.

Parameters:
location - the name of the table resource
Returns:
a new StarTable view of the resource at location
Throws:
TableFormatException - if no handler capable of turning location into a table is available
IOException - if one of the handlers encounters an error constructing a table

makeStarTable

public StarTable makeStarTable(URL url)
                        throws IOException
Deprecated. Use makeStarTable(new URLDataSource(url))

Constructs a readable StarTable from a URL using automatic format detection.

Parameters:
url - the URL where the table lives
Returns:
a new StarTable view of the resource at url
Throws:
TableFormatException - if no handler capable of turning datsrc into a table is available
IOException - if one of the handlers encounters an error constructing a table

makeStarTable

public StarTable makeStarTable(DataSource datsrc,
                               String handler)
                        throws TableFormatException,
                               IOException
Constructs a readable StarTable from a DataSource using a named table input handler. The input handler may be named either using its format name (as returned from the TableBuilder.getFormatName() method) or by giving the full class name of the handler. In the latter case this factory does not need to have been informed about the handler previously. If null or the empty string or the special value AUTO_HANDLER is supplied for handler, it will fall back on automatic format detection.

Parameters:
datsrc - the data source containing the table data
handler - specifier for the handler which can handle tables of the right format
Returns:
a new StarTable view of the resource datsrc
Throws:
TableFormatException - if datsrc does not contain a table in the format named by handler
IOException - if an I/O error is encountered

makeStarTables

public StarTable[] makeStarTables(DataSource datsrc,
                                  String handler)
                           throws TableFormatException,
                                  IOException
Constructs an array of StarTables from a DataSource using a named table input handler. The input handler may be named either using its format name (as returned from the TableBuilder.getFormatName() method) or by giving the full class name of the handler. In the latter case this factory does not need to have been informed about the handler previously. If null or the empty string or the special value AUTO_HANDLER is supplied for handler, it will fall back on automatic format detection.

If the handler does not implement the MultiTableBuilder interface, then the returned array will have a single element.

Parameters:
datsrc - the data source containing the table data
handler - specifier for the handler which can handle tables of the right format
Returns:
an array of StarTables loaded from datsrc
Throws:
TableFormatException - if datsrc does not contain a table in the format named by handler
IOException - if an I/O error is encountered

makeStarTable

public StarTable makeStarTable(String location,
                               String handler)
                        throws TableFormatException,
                               IOException
Constructs a readable StarTable from a location string using a named table input handler. The input handler may be named either using its format name (as returned from the TableBuilder.getFormatName() method) or by giving the full class name of the handler. In the latter case this factory does not need to have been informed about the handler previously. If null or the empty string or the special value AUTO_HANDLER is supplied for handler, it will fall back on automatic format detection. A location of "-" means standard input - in this case the handler must be specified.

Parameters:
location - the name of the table resource
handler - specifier for the handler which can handle tables of the right format
Returns:
a new StarTable view of the resource at location
Throws:
TableFormatException - if location does not point to a table in the format named by handler
IOException - if an I/O error is encountered

makeStarTable

public StarTable makeStarTable(URL url,
                               String handler)
                        throws TableFormatException,
                               IOException
Deprecated. Use makeStarTable(new URLDataSource(url),handler)

Constructs a readable StarTable from a URL using a named table input handler. The input handler may be named either using its format name (as returned from the TableBuilder.getFormatName() method) or by giving the full class name of the handler. In the latter case this factory does not need to have been informed about the handler previously. If null or the empty string or the special value AUTO_HANDLER is supplied for handler, it will fall back on automatic format detection.

Parameters:
url - the URL where the table lives
handler - specifier for the handler which can handle tables of the right format
Returns:
a new StarTable view of the resource at url
Throws:
TableFormatException - if the resource at url cannot be turned into a table by handler
IOException - if an I/O error is encountered

makeStarTable

public StarTable makeStarTable(InputStream in,
                               TableBuilder builder)
                        throws TableFormatException,
                               IOException
Attempts to read and return a StarTable from an input stream. This is not always possible, since certain table handlers may required more than one pass through the input data. The handler must be specified (automatic format detection cannot be used on a stream). The input stream will be decompressed and buffered if necessary.

Parameters:
in - input stream
builder - handler which understands the data in in
Returns:
a table read from the stream if it could be done
Throws:
TableFormatException - if builder needs more than one pass of the data, or the stream is in some way malformed
IOException - for other I/O errors
See Also:
TableBuilder.streamStarTable(java.io.InputStream, uk.ac.starlink.table.TableSink, java.lang.String)

makeStarTable

public StarTable makeStarTable(Transferable trans)
                        throws IOException
Constructs a StarTable from a Transferable object using automatic format detection. In conjunction with a suitable TransferHandler this makes it easy to accept drop of an object representing a table which has been dragged from another application.

The implementation of this method currently tries the following on a given transferable to turn it into a table:

Parameters:
trans - the Transferable object to construct a table from
Returns:
a new StarTable constructed from the Transferable
Throws:
TableFormatException - if no table can be constructed
IOException
See Also:
canImport(java.awt.datatransfer.DataFlavor[])

canImport

public boolean canImport(DataFlavor[] flavors)
Indicates whether a particular set of DataFlavor ojects offered by a Transferable is suitable for attempting to turn the Transferable into a StarTable.

Each of the builder objects is queried about whether it can import the given flavour, and if one says it can, a true value is returned. A true value is also returned if one of the flavours has a representation class of URL.

Parameters:
flavors - the data flavours offered

getJDBCHandler

public JDBCHandler getJDBCHandler()
Returns the JDBC handler object used by this factory.

Returns:
the JDBC handler

setJDBCHandler

public void setJDBCHandler(JDBCHandler handler)
Sets the JDBC handler object used by this factory.

Parameters:
handler - the JDBC handler

getTableBuilder

public TableBuilder getTableBuilder(String name)
                             throws TableFormatException
Returns a table handler with a given name. This name may be either its format name (as returned from the TableBuilder.getFormatName() method) or by giving the full class name of the handler. In the latter case this factory does not need to have been informed about the handler previously.

Parameters:
name - specification of the handler required
Returns:
TableBuilder specified by name
Throws:
TableFormatException - if name doesn't name any available handler

Copyright © 2004 CLRC: Central Laboratory of the Research Councils. All rights reserved.