Interface Literal

All Superinterfaces:
FrontsNode, RDFNode
All Known Implementing Classes:
LiteralImpl

public interface Literal extends RDFNode
An RDF Literal.

In RDF2003 literals can be typed. If typed then the literal comprises a datatype, a lexical form and a value (together with an optional xml:lang string). Old style literals have no type and are termed "plain" literals.

Implementations of this interface should be able to support both plain and typed literals. In the case of typed literals the primitive accessor methods such as getInt() determine if the literal value can be coerced to an appropriate java wrapper class. If so then the class is unwrapped to extract the primitive value returned. If the coercion fails then a runtime DatatypeFormatException is thrown.

In the case of plain literals then the primitive accessor methods duplicate the behaviour of jena1. The literal is internally stored in lexical form but the accessor methods such as getInt will attempt to parse the lexical form and if successful will return the primitive value.

Object (i.e. non-primitive) values are supported. In the case of typed literals then a global TypeMapper registry determines what datatype representation to use for a given Object type. In the case of plain literals then the object will be stored in the lexical form given by its toString method. Factory objects, provided by the application, are needed in that case to covert the lexical form back into the appropriate object type.

  • Method Details

    • inModel

      Literal inModel(Model m)
      Answer a version of this literal in the model m. If this literal is already in m, answer itself rather than create a new literal. This method covariantly overrides the method in RDFNode.
      Specified by:
      inModel in interface RDFNode
      Parameters:
      m - a model to move the node to
      Returns:
      this, if it's already in m (or no model), a copy in m otherwise
    • getValue

      Object getValue()
      Return the value of the literal. In the case of plain literals this will return the literal string. In the case of typed literals it will return a java object representing the value. In the case of typed literals representing a java primitive then the appropriate java wrapper class (Integer etc) will be returned. A Long value may be converted to its equivalent Integer value if there is no loss of precision (i.e. if the value is within the Integer minimum and maximum values).
    • getDatatype

      RDFDatatype getDatatype()
      Return the datatype of the literal.
    • getDatatypeURI

      String getDatatypeURI()
      Return the uri of the datatype of the literal.
    • getLexicalForm

      String getLexicalForm()
      Return the lexical form of the literal.
    • getBoolean

      boolean getBoolean()
      If the literal is interpretable as a Boolean return its value as a boolean. Plain literals are interpreted by parsing their lexical representation, typed literals are interpreted by coercion of the java object representing their value.
      Returns:
      the literal interpreted as a boolean
    • getByte

      byte getByte()
      If the literal is interpretable as a Byte return its value. Plain literals are interpreted by parsing their lexical representation, typed literals are interpreted by coercion of the java object representing their value.
      Returns:
      the literal interpreted as a byte
    • getShort

      short getShort()
      If the literal is interpretable as a Short return its value. Plain literals are interpreted by parsing their lexical representation, typed literals are interpreted by coercion of the java object representing their value.
      Returns:
      the literal interpreted as a short
    • getInt

      int getInt()
      If the literal is interpretable as a Integer return its value. Plain literals are interpreted by parsing their lexical representation, typed literals are interpreted by coercion of the java object representing their value.
      Returns:
      the literal interpreted as an int
    • getLong

      long getLong()
      If the literal is interpretable as a Long return its value. Plain literals are interpreted by parsing their lexical representation, typed literals are interpreted by coercion of the java object representing their value.
      Returns:
      the literal interpreted as a long
    • getChar

      char getChar()
      If the literal is interpretable as a Char return its value. Plain literals are interpreted by parsing their lexical representation, typed literals are interpreted by coercion of the java object representing their value.
      Returns:
      the literal interpreted as a char
    • getFloat

      float getFloat()
      If the literal is interpretable as a Float return its value. Plain literals are interpreted by parsing their lexical representation, typed literals are interpreted by coercion of the java object representing their value.
      Returns:
      the literal interpreted as a float
    • getDouble

      double getDouble()
      If the literal is interpretable as a Double return its value. Plain literals are interpreted by parsing their lexical representation, typed literals are interpreted by coercion of the java object representing their value.
      Returns:
      the literal interpreted as a double
    • getString

      String getString()
      If the literal is interpretable as a string return its value. For typed literals this will throw an error for non string literals and one needs to use getLexicalForm to return the string form of other datatypes.
      Returns:
      the literal string
    • getLanguage

      String getLanguage()
      If a language is defined for this literal return it
      Returns:
      the language for this literal if it exists, or empty string if none
    • isWellFormedXML

      boolean isWellFormedXML()
      Answer true iff this literal is (or claims to be) well-formed XML.
    • equals

      boolean equals(Object o)
      Test whether another object is equal to this object.

      A plain Literal is equal to another object only if the object is also a plain Literal and the string value and language of both literals are equal. In the case of a typed literal equality is defined by the datatype equality function on the value spaces and may or may not include comparison of the lang strings.

      Overrides:
      equals in class Object
      Parameters:
      o - The object to test against
      Returns:
      true if the the objects are equal, false otherwise
    • sameValueAs

      boolean sameValueAs(Literal other)
      Test that two literals are semantically equivalent. In some cases this may be the sames as equals, in others equals is stricter. For example, two xsd:int literals with the same value but different language tag are semantically equivalent but distinguished by the java equality function in order to support round tripping.