public class RDFSerializer extends Object
addTriple(Statement)
. This method must be overridden in a subclass
to provide the actual representation output.
For example, to output to Turtle, you might use the OpenRDF TurtleWriter to form a representation of the RDF graph:
StringWriter sw = new StringWriter();
final TurtleWriter tw = new TurtleWriter( sw );
RDFSerializer rs = new RDFSerializer()
{
public void addTriple( Statement s )
{
tw.handleStatement( s );
}
};
rs.serialize( myObject );
System.out.println( sw.toString() );
By default the class will only produce triples for fields which have been
annotated with the Predicate
annotation. The annotation gives the URI
used to link the object to its field in the triple. If you wish to attempt to
serialise unannotated fields, then you should use the constructor that takes
a boolean, passing true: RDFSerializer(boolean)
. This will then
create predicates based on the field name, so field member
will become
predicate hasMember
. Complex fields are serialised into subgraphs and those
graphs have URIs automatically generated for them based on the URI of the object
in which they exist and their name. For example:
http://example.com/MyObject
http://example.com/MyObject_hasMember
http://example.com/MyObject_member.
The serialize(Object, String)
method requires the URI of the object
to be serialised. This must be decided by the caller and passed in. It is
used to construct URIs for complex fields and predicates (if not given). So,
an object with URI http://example.com/object
and a complex field
called field
will end up with a triple that links the object to a
subgraph representing the complex object as so:
The name of the subgraph is based on the URI of the object and the name of
the field. The predicate is also automatically generated from the name of the
field. Note that this means you may need to be careful about the names
of the fields. For example, if an object had a complex field
http://example.com/object :hasField http://example.com/object_field
name_first
but also had a complex field name
that
itself had a complex field first
it's possible the same URI may be
generated for both subgraphs.
Primitive types will be typed with XSD datatypes. For example:
example:field example:hasNumber "20"^^xsd:integer
Lists and collections are output in one of two ways. By default they are
output as separate triples in an unordered way:
Modifier and Type | Field and Description |
---|---|
protected boolean |
autoPredicate
Whether to try to create predicates for unannotated fields
|
protected boolean |
outputClassNames
Whether to output class names
|
static String |
RDF_OPENIMAJ_P_CLASSNAME
Predicate for giving the class name
|
static String |
RDF_OPENIMAJ_P_COLLECTIONITEM
Predicate for unnamed collections
|
static String |
RDF_OPENIMAJ_TMP_GRAPH
URI used for temporary graph when loading into a store
|
Constructor and Description |
---|
RDFSerializer()
Default constructor
|
RDFSerializer(boolean autoPredicate)
Constructor that determines whether to create predicates automatically
when the
Predicate annotation does not exist. |
Modifier and Type | Method and Description |
---|---|
void |
addTriple(org.openrdf.model.Statement t)
Adds a single triple to some RDF serializer.
|
org.openrdf.model.impl.URIImpl |
getObjectURI(Object obj,
org.openrdf.model.impl.URIImpl defaultURI)
Returns a URI for the given object.
|
org.openrdf.model.URI |
serialize(Object objectToSerialize,
String uri)
Serialize the given object as RDF.
|
org.openrdf.model.URI |
serializeAux(Object objectToSerialize,
String uri)
Serialize the given object as RDF.
|
org.openrdf.model.URI |
serializeAux(Object objectToSerialize,
String uri,
boolean outputCollectionObjects)
Serialize the given object as RDF.
|
void |
setAutoPredicate(boolean tf)
Set whether to attempt to output all fields from the objects, not just
those annotated with
Predicate . |
void |
setOutputClassNames(boolean tf)
Set whether to output class names as triples.
|
<T> T |
unserialize(T objectToUnserialize,
String objectRootURI,
org.openrdf.repository.Repository repo)
Unserializes an object from an RDF graph that is rooted at the given URI.
|
<T> T |
unserialize(T objectToUnserialize,
String objectRootURI,
String rdf,
org.openrdf.rio.RDFFormat rdfFormat)
Unserializes an object from the given RDF string (with the given format)
into the given object.
|
public static final String RDF_OPENIMAJ_TMP_GRAPH
public static final String RDF_OPENIMAJ_P_CLASSNAME
public static final String RDF_OPENIMAJ_P_COLLECTIONITEM
protected boolean autoPredicate
protected boolean outputClassNames
public RDFSerializer()
public RDFSerializer(boolean autoPredicate)
Predicate
annotation does not exist.autoPredicate
- Whether to automatically create predicatespublic org.openrdf.model.URI serialize(Object objectToSerialize, String uri)
objectToSerialize
- The object to serialize.uri
- The URI of the object to serialize.public org.openrdf.model.URI serializeAux(Object objectToSerialize, String uri)
objectToSerialize
- The object to serialize.uri
- The URI of the object to serialize.public org.openrdf.model.URI serializeAux(Object objectToSerialize, String uri, boolean outputCollectionObjects)
objectToSerialize
- The object to serialize.uri
- The URI of the object to serialize.outputCollectionObjects
- Whether to output items in a collectionpublic void setOutputClassNames(boolean tf)
tf
- TRUE to output class name triples.public void setAutoPredicate(boolean tf)
Predicate
.tf
- TRUE to attempt to find predicates for all members.public <T> T unserialize(T objectToUnserialize, String objectRootURI, String rdf, org.openrdf.rio.RDFFormat rdfFormat)
T
- Type of object being unserialisedobjectToUnserialize
- The object to populateobjectRootURI
- The URI that gives the root of the object graphrdf
- The RDF stringrdfFormat
- The format of the RDF in the stringpublic <T> T unserialize(T objectToUnserialize, String objectRootURI, org.openrdf.repository.Repository repo)
T
- Type of object being unserialisedobjectToUnserialize
- The object to populateobjectRootURI
- The URI that gives the root of the object graphrepo
- The repository storing the RDF graphpublic org.openrdf.model.impl.URIImpl getObjectURI(Object obj, org.openrdf.model.impl.URIImpl defaultURI)
obj
- The objectdefaultURI
- A default value for the URIpublic void addTriple(org.openrdf.model.Statement t)
t
- The triple to add