On Mon, Mar 18, 2013 at 4:54 PM, Léonard PETNGA <[email protected]> wrote:
> Hi Jena Community,
> I'm new to Jena and I would need your help fixing some issues with my code.
> I'm trying to parse a String in duration format ie  "*PnYn MnDTnH nMn**S*" 
> into
> an *XSDDuration* datatype that I'll bind to a Node of my inferred graph (in
> a custom built-in function). To that aim, my parser extracts and stores
> integer values for Year, Month, etc... in an array int[9] of *value, *that
> is latter passed to the XML duration constructor. This works fine ...except
> for when the duration is 0 (see the error below).
>
> public static int[]  parseStringToISODurationInt(String duration)
> throwsIllegalArgumentException {
>
>      int[] value = new int[9];
>
>      StringTokenizer st = new StringTokenizer(duration);
>
>      String nullDur = "PT0H0M0S";     // from a previous converter function
>
>      if (duration == nullDur){
>
>      value[0] = 0;
>
>      value[1] = 0;
>
>      value[2] = 0;
>
>      value[3] = 0;
>
>      value[4] = 0;
>
>      value[5] = 0;
>
>      value[6] = 0;     // [7] is the Z character(zulu timezone, not needed
> here),
>
>      value[8] = 3;     // 'mscale' value
>
>    } else {
>
>      // ....rest of the code
>
>      }
>
>  return value;
>
> }
>
> In my built-in I pass the array obtained to the *XSDDuration(Object
> value)*constructor and make the new node that is binded to the graph
> as follows :
>
> Node duration = null;
>
>  duration = Node.createLiteral(
> LiteralLabelFactory.create(newXSDDuration(value)) );
> The following excerpt shows the error when running my built-in for a zero
> duration (value[i]=0 forall i =0,..6 and value[8]=3 ).
>
> Exception in thread "main"
> com.hp.hpl.jena.datatypes.DatatypeFormatException: Lexical form 'P' is not
> a legal instance of Datatype[http://www.w3.org/2001/XMLSchema#duration ->
> class com.hp.hpl.jena.datatypes.xsd.XSDDuration] null
>      [java] at
> com.hp.hpl.jena.graph.impl.LiteralLabelImpl.getValue(LiteralLabelImpl.java:300)
>
> So what what is the legal lexical form for a "zero" duration? How do I
> construct that using the *XSDDuration* constructor?
> Any other solution will be welcomed...
>
> Many thanks in advance for your prompt reaction.

The javadocs for XSDDuration [1] say of the constructor, "should only
be used by the internals but public scope because the internals spread
across multiple packages", so don't call that.  Just use
model#createTypedLiteral [2], and Literal#getValue [3], as in the
following code.  Also see the "Typed literals how-to" doc [4].

import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.datatypes.xsd.XSDDuration;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class DurationExample {
        public static void main(String[] args) {
                Model model = ModelFactory.createDefaultModel();
                Literal l = model.createTypedLiteral( "PT0H0M0S", 
XSDDatatype.XSDduration );
                XSDDuration duration = (XSDDuration) l.getValue();
                System.out.println( "days: " + duration.getDays() + "; hours: " 
+
duration.getHours() + "; ...");
        }
}

This outputs:

days: 0; hours: 0; ...

//JT

[1] 
http://jena.apache.org/documentation/javadoc/jena/com/hp/hpl/jena/datatypes/xsd/XSDDuration.html#XSDDuration(java.lang.Object)
[2] 
http://jena.apache.org/documentation/javadoc/jena/com/hp/hpl/jena/rdf/model/Model.html#createTypedLiteral(java.lang.String,
com.hp.hpl.jena.datatypes.RDFDatatype)
[3] 
http://jena.apache.org/documentation/javadoc/jena/com/hp/hpl/jena/rdf/model/Literal.html#getValue()
[4] http://jena.apache.org/documentation/notes/typed-literals.html
-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Reply via email to