public abstract class OutputCodec<E> extends Object
The objects that a codec can encode must be of the class specified by the
type parameter E. For instance, a TikzQtreeOutputCodec
will encode
objects of class Tree
as strings, whereas a
SgraphAmrOutputCodec
will encode objects of class SGraph
as
strings.
Concrete output codec implementations must implement the abstract method
write(java.lang.Object, java.io.OutputStream)
, which writes a
representation of an object to some output stream. This class then provides a
number of convenience methods that make use of the concrete implementation of
write.
Output codecs can be registered by adding them to the file
src/main/resources/META-INF/services/de.up.ling.codec.OutputCodec. Output
codecs that are suitable for a given class of objects can then be found using getOutputCodecs(java.lang.Class)
. For instance, the Alto GUI allows the user to right-click on an object to
obtain a string representation; the possible string representations are
automatically discovered from the output codecs that are available for that
object.
In order to be discovered correctly, each output codec needs to be annotated
with CodecMetadata
. Most importantly, the "type" field of that
annotation specifies the class that the codec can encode.
Constructor and Description |
---|
OutputCodec() |
Modifier and Type | Method and Description |
---|---|
void |
addOptions(String options) |
String |
asString(E object)
Returns a string representation of the given object.
|
Supplier<String> |
asStringSupplier(E object)
Returns a function that computes a string representation of the given
object.
|
static Iterable<OutputCodec> |
getAllOutputCodecs()
Returns all registered output codecs.
|
CodecMetadata |
getMetadata()
Returns the metadata associated with this output object.
|
String |
getOption(String key) |
static OutputCodec |
getOutputCodecByExtension(String extension) |
static OutputCodec |
getOutputCodecByName(String name)
Returns the registered input codec with the given name (as per the codec
metadata's
name field). |
static <T> List<OutputCodec<T>> |
getOutputCodecs(Class<T> forClass)
Returns a list of all output codecs that can encode objects of the given
class.
|
boolean |
hasTrueOption(String key) |
abstract void |
write(E object,
OutputStream ostream)
Writes a string representation of a given object to an output stream.
|
public abstract void write(E object, OutputStream ostream) throws IOException, UnsupportedOperationException
object
- ostream
- IOException
- if something went wrong with I/OUnsupportedOperationException
- if a problem occurred in computing
the representation of the objectpublic Supplier<String> asStringSupplier(E object)
object
- public String asString(E object) throws UnsupportedOperationException
write(java.lang.Object, java.io.OutputStream)
and writes a representation into a string.object
- UnsupportedOperationException
- if a problem occurred in computing
the representation of the objectpublic static Iterable<OutputCodec> getAllOutputCodecs()
META-INF/services/de.up.ling.irtg.codec.OutputCodec
. You can
find this file under src/main/resources
in the IRTG source
code repository. Note: It is usually a better idea to call getOutputCodecs(java.lang.Class)
to ensure type-safety.public static <T> List<OutputCodec<T>> getOutputCodecs(Class<T> forClass)
T
- forClass
- public static OutputCodec getOutputCodecByName(String name)
name
field). If no codec with this name can be
found, returns null.name
- public static OutputCodec getOutputCodecByExtension(String extension)
public CodecMetadata getMetadata()
public void addOptions(String options)
public boolean hasTrueOption(String key)
Copyright © 2017. All rights reserved.