public abstract class InputCodec<E> extends Object
The codec will attempt to decode the representation into an object
of the class specified
by the type parameter E. For instance, a IrtgInputCodec
will decode a string representation into an InterpretedTreeAutomaton
,
whereas a TreeAutomatonInputCodec
will decode a string into
a TreeAutomaton
.
Concrete input codec implementations must implement the abstract method
read(java.io.InputStream)
, which reads data from an input stream
and decodes it into an object. This class then
provides a number of convenience methods that make use of the concrete
implementation of read.
Input codecs can be registered by adding them to the file
src/main/resources/META-INF/services/de.up.ling.codec.InputCodec.
Input codecs that are suitable for a given class of objects can then
be found using getInputCodecs(java.lang.Class)
.
In order to be discovered correctly, each input codec needs to be annotated
with CodecMetadata
, such as the type of E and a filename extension.
Constructor and Description |
---|
InputCodec() |
Modifier and Type | Method and Description |
---|---|
void |
addOptions(String options) |
static Iterable<InputCodec> |
getAllInputCodecs()
Returns all registered input codecs.
|
static InputCodec |
getInputCodecByExtension(String extension)
Returns the registered input codec for the given
filename extension (as per the codec metadata's
extension field). |
static InputCodec |
getInputCodecByName(String name)
Returns the registered input codec with the given name (as per
the codec metadata's
name field). |
static InputCodec |
getInputCodecByNameOrExtension(String filename,
String codecName)
Returns the input codec specified by the given codec name and/or
filename.
|
static <T> List<InputCodec<T>> |
getInputCodecs(Class<T> forClass)
Returns all registered input codecs that can produce objects
of class
T (or a subtype). |
CodecMetadata |
getMetadata()
Returns the metadata for this input codec.
|
String |
getOption(String key) |
boolean |
hasTrueOption(String key) |
static void |
main(String[] args) |
abstract E |
read(InputStream is)
Reads an object from an input stream.
|
E |
read(String s)
Reads an object from a string representation.
|
void |
setOption(String option,
String value) |
void |
setProgressListener(ProgressListener listener)
Sets the progress listener for this codec.
|
public abstract E read(InputStream is) throws CodecParseException, IOException
is
- CodecParseException
- if an error occurred while decoding
the input stream into an objectIOException
- if an error occurred while reading
data from the input streampublic E read(String s) throws CodecParseException
String.getBytes()
to decode
the string into a sequence of bytes using the
platform's default encoding, and then reads
an object from this byte stream using read(java.io.InputStream)
.
It is provided as a convenience method for the
frequent special case where the object is represented
as a string of some kind.s
- CodecParseException
public CodecMetadata getMetadata()
public static Iterable<InputCodec> getAllInputCodecs()
META-INF/services/de.up.ling.irtg.codec.InputCodec
.
You can find this file under src/main/resources
in the IRTG source code repository. Note: It is usually a better idea
to call getInputCodecs(java.lang.Class)
to ensure type-safety.public static <T> List<InputCodec<T>> getInputCodecs(Class<T> forClass)
T
(or a subtype). For instance, call
InputCodec.getInputCodecs(Tree.class)
to obtain
all input codecs for trees.
The method takes the
information about what class an input codec reads from the
type
field in the codec's metadata annotation.T
- forClass
- public static InputCodec getInputCodecByName(String name)
name
field). If
no codec with this name can be found, returns null.name
- public static InputCodec getInputCodecByExtension(String extension)
extension
field). If no codec for
this extension can be found, returns null.extension
- public static InputCodec getInputCodecByNameOrExtension(String filename, String codecName) throws Exception
filename
- codecName
- Exception
public void setProgressListener(ProgressListener listener)
listener
- public void addOptions(String options)
public boolean hasTrueOption(String key)
Copyright © 2017. All rights reserved.