Class StarTableFactory
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 from an input data stream.
Various makeStarTable
methods
are offered, which construct StarTable
s from different
types of object, such as URL
and
DataSource
. Each of these comes in
two types: automatic format detection and named format.
Additionally, a list of TableScheme
objects is maintained,
each of which can produce a table from an opaque specification string
of the form :<scheme-name>:<spec>
.
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:
- The format name - this is a short mnemonic string like "fits"
which is returned by the TableBuilder's
getFormatName
method - it is matched case insensitively. This must be one of the builders known to the factory. - The classname of a suitable TableBuilder (the class must
implement
TableBuilder
and have no-arg constructor). Such a class must be on the classpath, but need not have been specified previously to the factory. - The empty string or
null
orAUTO_HANDLER
- in this case automatic format detection is used.
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):
-
invalid reference
uk.ac.starlink.votable.FitsPlusTableBuilder
-
invalid reference
uk.ac.starlink.votable.ColFitsPlusTableBuilder
-
invalid reference
uk.ac.starlink.fits.ColFitsTableBuilder
-
invalid reference
uk.ac.starlink.fits.FitsTableBuilder
-
invalid reference
uk.ac.starlink.votable.VOTableBuilder
-
invalid reference
uk.ac.starlink.cdf.CdfTableBuilder
-
invalid reference
uk.ac.starlink.ecsv.EcsvTableBuilder
-
invalid reference
uk.ac.starlink.pds4.Pds4TableBuilder
-
MrtTableBuilder
(format name="mrt") -
invalid reference
uk.ac.starlink.parquet.ParquetTableBuilder
-
invalid reference
uk.ac.starlink.feather.FeatherTableBuilder
The following additional handlers are installed in the known handler list (not used by default but available by specifying the format name):
-
AsciiTableBuilder
(format name="ascii") -
CsvTableBuilder
(format name="csv") -
TstTableBuilder
(format name="tst") -
IpacTableBuilder
(format name="ipac") -
invalid reference
uk.ac.starlink.hapi.HapiTableBuilder
-
invalid reference
uk.ac.starlink.gbin.GbinTableBuilder
-
WDCTableBuilder
(format name="wdc")
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.
Some makeStarTable
methods
take a location String rather than an input stream or DataSource;
these may either give a URL or filename, or a
scheme-based location of the form
:<scheme-name>:<scheme-specification>
,
for instance "jdbc://localhost/db1#SELECT id FROM gsc
".
There is a theoretical risk of a namespace clash between
input-yielding URLs, or even filenames, and scheme-based locations,
but if scheme names avoid obvious values like "http" and "C"
this is not likely to cause problems in practice.
The following TableSchemes are installed by default:
JDBCTableScheme
(scheme name="jdbc")LoopTableScheme
(scheme name="loop")TestTableScheme
(scheme name="test")ClassTableScheme
(scheme name="class")-
invalid reference
uk.ac.starlink.hapi.HapiTableScheme
Additionally, any classes named in the startable.schemes
system property (as a colon-separated list) which implement the
TableScheme
interface and have a no-arg constructor
will be instantiated and added to the known scheme 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.
- Author:
- Mark Taylor (Starlink)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Special handler identifier which signifies automatic format detection.static final String
System property which can contain a list ofTableBuilder
classnames for addition to the known (non-automatically detected) handler list.static final String
System property which can contain a list ofTableScheme
classnames for addition to the default list. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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.Constructs a StarTableFactory which is a copy of an existing one. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addScheme
(TableScheme scheme) Safely adds a table scheme for use by this factory.boolean
canImport
(DataFlavor[] flavors) Indicates whether a particular set ofDataFlavor
ojects offered by aTransferable
is suitable for attempting to turn theTransferable
into a StarTable.Gets the list of builders which are used for automatic format detection.Predicate
<uk.ac.starlink.util.DataSource> Returns the object controlling access to input data.Returns the JDBC handler object used by this factory.Gets the list of builders which are available for selection by format name.Returns the list of format names, one for each of the handlers returned bygetKnownBuilders()
.Returns the current table preparation object, if any.Returns a schemeName->scheme map indicating the TableSchemes in use by this factory.Returns the current storage policy.getTableBuilder
(String name) Returns a table handler with a given name.makeStarTable
(Transferable trans) Constructs a StarTable from aTransferable
object using automatic format detection.makeStarTable
(InputStream in, TableBuilder builder) Attempts to read and return a StarTable from an input stream.makeStarTable
(String location) Constructs aStarTable
from a location string without format specification.makeStarTable
(String location, String handler) Constructs aStarTable
from a location string using a named table input handler.makeStarTable
(URL url) Deprecated.makeStarTable
(URL url, String handler) Deprecated.UsemakeStarTable(new URLDataSource(url),handler)
makeStarTable
(uk.ac.starlink.util.DataSource datsrc) Constructs aStarTable
from aDataSource
object using automatic format detection.makeStarTable
(uk.ac.starlink.util.DataSource datsrc, String handler) Constructs aStarTable
from aDataSource
using a named table input handler.makeStarTables
(String location, String handler) Constructs a sequence ofStarTable
s from a location string using a named table input handler.makeStarTables
(uk.ac.starlink.util.DataSource datsrc) Constructs a sequence of StarTables from a DataSource using automatic format detection.makeStarTables
(uk.ac.starlink.util.DataSource datsrc, String handler) Constructs a sequence of StarTables from a DataSource using a named table input handler.static String[]
parseSchemeLocation
(String location) Parses a scheme-format table specification as a scheme name and a scheme-specific part.randomTable
(StarTable table) Returns a table based on a given table and guaranteed to have random access.boolean
Returns therequireRandom
flag.void
setDefaultBuilders
(TableBuilder[] builders) Sets the list of builders which actually do the table construction.void
setInputRestriction
(Predicate<uk.ac.starlink.util.DataSource> restriction) Sets an object that can control access to input data.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
setPreparation
(TablePreparation tablePrep) Sets a table preparation object that is invoked on each table created by this factory.void
setRequireRandom
(boolean requireRandom) Sets whether random-access tables will be constructed by this factory.void
setStoragePolicy
(StoragePolicy policy) Sets the storage policy.
-
Field Details
-
KNOWN_BUILDERS_PROPERTY
System property which can contain a list ofTableBuilder
classnames for addition to the known (non-automatically detected) handler list.- See Also:
-
SCHEMES_PROPERTY
System property which can contain a list ofTableScheme
classnames for addition to the default list.- See Also:
-
AUTO_HANDLER
Special handler identifier which signifies automatic format detection.- See Also:
-
-
Constructor Details
-
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
-
StarTableFactory
Constructs a StarTableFactory which is a copy of an existing one.- Parameters:
fact
- instance to copy
-
-
Method Details
-
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 constructStarTable
s
-
setDefaultBuilders
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 constructStarTable
s
-
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
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 constructStarTable
s
-
getKnownFormats
Returns the list of format names, one for each of the handlers returned bygetKnownBuilders()
.- Returns:
- list of format name strings
-
getSchemes
Returns a schemeName->scheme map indicating the TableSchemes in use by this factory. This map is mutable, so entries may be added or removed, but NOTE that the map keys are assumed to be equivalent to the getSchemeName return value for their value, so modify it with care. Consider using theaddScheme(uk.ac.starlink.table.TableScheme)
method for adding entries.- Returns:
- map of scheme names to schemes
-
addScheme
Safely adds a table scheme for use by this factory. It is equivalent togetSchemes().put(scheme.getSchemeName(),scheme)
.- Parameters:
scheme
- new scheme for use
-
setRequireRandom
public void setRequireRandom(boolean requireRandom) Sets whether random-access tables will be constructed by this factory. If this flag is settrue
then any table returned by the variousmakeStarTable
methods is guaranteed to provide random access (itsStarTable.isRandom()
method will returntrue
). 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 therequireRandom
flag. If this flag is settrue
then any table returned by the variousmakeStarTable
methods is guaranteed to provide random access (itsStarTable.isRandom()
method will returntrue
). 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
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
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
-
setPreparation
Sets a table preparation object that is invoked on each table created by this factory. Any previous value is overwritten. Null is allowed.- Parameters:
tablePrep
- new table preparation, or null
-
getPreparation
Returns the current table preparation object, if any. By default, null is returned.- Returns:
- table preparation, or null
-
setInputRestriction
Sets an object that can control access to input data. If a non-null value is set, then any attempt to read a table from a resource such as a file or URL will first test it using the supplied predicate. If itstest
method returns false, the table read attempt will fail with an IOException.- Parameters:
restriction
- policy for restricting DataSource access, or null for no restrictions
-
getInputRestriction
Returns the object controlling access to input data. By default this returns null, meaning no access controls.- Returns:
- policy for restricting DataSource access, or null
- See Also:
-
randomTable
Returns a table based on a given table and guaranteed to have random access. If the original tabletable
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 withisRandom()==true
- Throws:
IOException
-
makeStarTable
public StarTable makeStarTable(uk.ac.starlink.util.DataSource datsrc) throws TableFormatException, IOException Constructs aStarTable
from aDataSource
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 turndatsrc
into a tableIOException
- if an I/O error is encountered
-
makeStarTables
public TableSequence makeStarTables(uk.ac.starlink.util.DataSource datsrc) throws TableFormatException, IOException Constructs a sequence of StarTables from a DataSource using automatic format detection. Only certain formats (those whose handlers implementMultiTableBuilder
) will be capable of returning a sequence having more than one element.- Parameters:
datsrc
- the data source containing the table data- Returns:
- a sequence of tables loaded from
datsrc
- Throws:
TableFormatException
- if none of the default handlers could turndatsrc
into a tableIOException
- if an I/O error is encountered
-
makeStarTable
Constructs aStarTable
from a location string without format specification. The location string can represent a filename or URL, or a scheme-based specification of the form<scheme>:<scheme-spec>
corresponding to one of the installedschemes
.- 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 turninglocation
into a table is availableIOException
- if one of the handlers encounters an error constructing a table
-
makeStarTable
Deprecated.UsemakeStarTable(new URLDataSource(url))
Constructs aStarTable
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 turningdatsrc
into a table is availableIOException
- if one of the handlers encounters an error constructing a table
-
makeStarTable
public StarTable makeStarTable(uk.ac.starlink.util.DataSource datsrc, String handler) throws TableFormatException, IOException Constructs aStarTable
from aDataSource
using a named table input handler. The input handler may be named either using its format name (as returned from theTableBuilder.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. Ifnull
or the empty string or the special valueAUTO_HANDLER
is supplied forhandler
, it will fall back on automatic format detection.- Parameters:
datsrc
- the data source containing the table datahandler
- specifier for the handler which can handle tables of the right format- Returns:
- a new StarTable view of the resource
datsrc
- Throws:
TableFormatException
- ifdatsrc
does not contain a table in the format named byhandler
IOException
- if an I/O error is encountered
-
makeStarTables
public TableSequence makeStarTables(uk.ac.starlink.util.DataSource datsrc, String handler) throws TableFormatException, IOException Constructs a sequence 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 theTableBuilder.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. Ifnull
or the empty string or the special valueAUTO_HANDLER
is supplied forhandler
, it will fall back on automatic format detection.If the handler does not implement the
MultiTableBuilder
interface, then the returned sequence will contain a single table.- Parameters:
datsrc
- the data source containing the table datahandler
- specifier for the handler which can handle tables of the right format- Returns:
- a sequence of StarTables loaded from
datsrc
- Throws:
TableFormatException
- ifdatsrc
does not contain a table in the format named byhandler
IOException
- if an I/O error is encountered
-
makeStarTables
public TableSequence makeStarTables(String location, String handler) throws TableFormatException, IOException Constructs a sequence ofStarTable
s from a location string using a named table input handler. The input handler may be named either using its format name (as returned from theTableBuilder.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. Ifnull
or the empty string or the special valueAUTO_HANDLER
is supplied forhandler
, it will fall back on automatic format detection.Alternatively, the location string can be a scheme-based specification, in which case the
handler
is ignored.- Parameters:
location
- the name of the table resourcehandler
- specifier for the handler which can handle tables of the right format- Returns:
- a new StarTable view of the resource at
location
- Throws:
TableFormatException
- iflocation
does not point to a table in the format named byhandler
IOException
- if an I/O error is encountered
-
makeStarTable
public StarTable makeStarTable(String location, String handler) throws TableFormatException, IOException Constructs aStarTable
from a location string using a named table input handler. The input handler may be named either using its format name (as returned from theTableBuilder.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. Ifnull
or the empty string or the special valueAUTO_HANDLER
is supplied forhandler
, it will fall back on automatic format detection.A location of "-" means standard input - in this case the handler must be specified.
Alternatively, the location string can be a scheme-based specification, in which case the
handler
is ignored.- Parameters:
location
- the name of the table resourcehandler
- specifier for the handler which can handle tables of the right format- Returns:
- a new StarTable view of the resource at
location
- Throws:
TableFormatException
- iflocation
does not point to a table in the format named byhandler
IOException
- if an I/O error is encountered
-
makeStarTable
@Deprecated public StarTable makeStarTable(URL url, String handler) throws TableFormatException, IOException Deprecated.UsemakeStarTable(new URLDataSource(url),handler)
Constructs aStarTable
from a URL using a named table input handler. The input handler may be named either using its format name (as returned from theTableBuilder.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. Ifnull
or the empty string or the special valueAUTO_HANDLER
is supplied forhandler
, it will fall back on automatic format detection.- Parameters:
url
- the URL where the table liveshandler
- 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 aturl
cannot be turned into a table byhandler
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 streambuilder
- handler which understands the data inin
- Returns:
- a table read from the stream if it could be done
- Throws:
TableFormatException
- ifbuilder
needs more than one pass of the data, or the stream is in some way malformedIOException
- for other I/O errors- See Also:
-
makeStarTable
Constructs a StarTable from aTransferable
object using automatic format detection. In conjunction with a suitableTransferHandler
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:
- If it finds a
URL
object, passes that to the URL factory method - If it finds a transferable that will supply an
InputStream
, turns it into aDataSource
and passes that to theDataSource
constructor
- 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 constructedIOException
- See Also:
- If it finds a
-
canImport
Indicates whether a particular set ofDataFlavor
ojects offered by aTransferable
is suitable for attempting to turn theTransferable
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
Returns the JDBC handler object used by this factory.- Returns:
- the JDBC handler
-
setJDBCHandler
Sets the JDBC handler object used by this factory.- Parameters:
handler
- the JDBC handler
-
getTableBuilder
Returns a table handler with a given name. This name may be either its format name (as returned from theTableBuilder.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
- ifname
doesn't name any available handler
-
parseSchemeLocation
Parses a scheme-format table specification as a scheme name and a scheme-specific part. Normally schemes are of the form ":<scheme-name>:<scheme-specific-part>", but as a special case the initial colon may be omitted for JDBC (backward compatibility).- Parameters:
location
- table specification- Returns:
- if the location is syntactically a scheme, a 2-element array giving [scheme-name,scheme-specific-part]; otherwise null
-
makeStarTable(new URLDataSource(url))