/** * 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. */ @Override public Object getValue() { return asNode().getLiteralValue(); }
@Override public byte getByte() { if (isPlainLiteral()) { return Byte.parseByte(getLexicalForm()); } else { return byteValue( asNumber( getValue() ) ); } }
private Number asNumber(Object value) { if (value instanceof Number) { return ((Number)value); } else { String message = "Error converting typed value to a number. \n"; message += "Datatype is: " + getDatatypeURI(); if ( getDatatypeURI() == null || ! getDatatypeURI().startsWith(XSDDatatype.XSD)) { message +=" which is not an xsd type."; } message += " \n"; String type = message += "Java representation type is " + (value == null ? "null" : value.getClass().toString()); throw new DatatypeFormatException(message); } } private byte byteValue( Number n )
@Override public Literal createPlainLiteral( String string ) { return new LiteralImpl( NodeFactory.createLiteral( string, "", false ), null ); }
@Override public boolean getBoolean() { Object value = asNode().getLiteralValue(); if (isPlainLiteral()) { // old style plain literal - try parsing the string if (value.equals("true")) { return true; } else if (value.equals("false")) { return false; } else { throw new BadBooleanException( value.toString() ); } } else { // typed literal if (value instanceof Boolean) { return ((Boolean)value).booleanValue(); } else { throw new DatatypeFormatException(this.toString() + " is not a Boolean"); } } }
private int intValue( Number n ) { return (int) getIntegralValueInRange( Integer.MIN_VALUE, n, Integer.MAX_VALUE ); }
/** Answer the model this literal was created in, if any, otherwise null. */ @Override public Model getModel() { return (ModelCom) getGraph(); }
private Literal literal( String lex, RDFDatatype datatype) { return new LiteralImpl( NodeFactory.createLiteral( lex, "", datatype), this ); }
private byte byteValue( Number n ) { return (byte) getIntegralValueInRange( Byte.MIN_VALUE, n, Byte.MAX_VALUE ); }
private Literal literal( String s, String lang, boolean wellFormed ) { return new LiteralImpl( NodeFactory.createLiteral( s, lang, wellFormed), this ); }
@Override public int getInt() { if (isPlainLiteral()) { return Integer.parseInt(getLexicalForm()); } else { return intValue( asNumber( getValue() ) ); } }
/** * Return the datatype of the literal. This will be null in the * case of plain literals. */ @Override public RDFDatatype getDatatype() { return asNode().getLiteralDatatype(); }
private short shortValue( Number n ) { return (short) getIntegralValueInRange( Short.MIN_VALUE, n, Short.MAX_VALUE ); }
@Override public Literal createTypedLiteral( String string , RDFDatatype dType) { return new LiteralImpl(NodeFactory.createLiteral(string, "", dType), null) ; }
@Override public long getLong() { if (isPlainLiteral()) { return Long.parseLong(getLexicalForm()); } else { return asNumber(getValue()).longValue(); } }
/** * Return true if this is a "plain" (i.e. old style, not typed) literal. */ public boolean isPlainLiteral() { return asNode().getLiteralDatatype() == null; }
@Override public Literal createLangLiteral( String string , String lang ) { return new LiteralImpl( NodeFactory.createLiteral( string, lang, false ), null ); }
@Override public double getDouble() { if (isPlainLiteral()) { return Double.parseDouble(getLexicalForm()); } else { return asNumber(getValue()).doubleValue(); } }
@Override public Resource asResource() { throw new ResourceRequiredException( asNode() ); }