Search in sources :

Example 6 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class NodeValue method _setByValue.

// Returns null for unrecognized literal.
private static NodeValue _setByValue(Node node) {
    // nodeToNodeValue should have dealt with it.
    if (NodeUtils.hasLang(node))
        return new NodeValueLang(node);
    LiteralLabel lit = node.getLiteral();
    String lex = lit.getLexicalForm();
    RDFDatatype datatype = lit.getDatatype();
    // Quick check.
    // Only XSD supported.
    // And (for testing) roman numerals.
    String datatypeURI = datatype.getURI();
    if (!datatypeURI.startsWith(xsdNamespace) && !SystemARQ.EnableRomanNumerals) {
        // Not XSD.
        return null;
    }
    try {
        // DatatypeFormatException - should not happen
        if (XSDstring.isValidLiteral(lit))
            // String - plain or xsd:string, or derived datatype.
            return new NodeValueString(lit.getLexicalForm(), node);
        // Otherwise xsd:string is like any other unknown datatype.
        // Ditto literals with language tags (which are handled by nodeToNodeValue)
        // isValidLiteral is a value test - not a syntactic test.
        // This makes a difference in that "1"^^xsd:decimal" is a
        // valid literal for xsd:integer (all other cases are subtypes of xsd:integer)
        // which we want to become integer anyway).
        // Order here is promotion order integer-decimal-float-double
        // XSD allows whitespace. Java String.trim removes too much
        // so must test for validity on the untrimmed lexical form.
        String lexTrimmed = lex.trim();
        if (!datatype.equals(XSDdecimal)) {
            // XSD integer and derived types
            if (XSDinteger.isValidLiteral(lit)) {
                // BigInteger does not accept such whitespace.
                String s = lexTrimmed;
                if (s.startsWith("+"))
                    // BigInteger does not accept leading "+"
                    s = s.substring(1);
                // Includes subtypes (int, byte, postiveInteger etc).
                // NB Known to be valid for type by now
                BigInteger integer = new BigInteger(s);
                return new NodeValueInteger(integer, node);
            }
        }
        if (datatype.equals(XSDdecimal) && XSDdecimal.isValidLiteral(lit)) {
            BigDecimal decimal = new BigDecimal(lexTrimmed);
            return new NodeValueDecimal(decimal, node);
        }
        if (datatype.equals(XSDfloat) && XSDfloat.isValidLiteral(lit)) {
            // NB If needed, call to floatValue, then assign to double.
            // Gets 1.3f != 1.3d right
            float f = ((Number) lit.getValue()).floatValue();
            return new NodeValueFloat(f, node);
        }
        if (datatype.equals(XSDdouble) && XSDdouble.isValidLiteral(lit)) {
            double d = ((Number) lit.getValue()).doubleValue();
            return new NodeValueDouble(d, node);
        }
        if (datatype.equals(XSDboolean) && XSDboolean.isValidLiteral(lit)) {
            boolean b = (Boolean) lit.getValue();
            return new NodeValueBoolean(b, node);
        }
        if ((datatype.equals(XSDdateTime) || datatype.equals(XSDdateTimeStamp)) && XSDdateTime.isValid(lex)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDdate) && XSDdate.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDtime) && XSDtime.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDgYear) && XSDgYear.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDgYearMonth) && XSDgYearMonth.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDgMonth) && XSDgMonth.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDgMonthDay) && XSDgMonthDay.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDgDay) && XSDgDay.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDduration) && XSDduration.isValid(lex)) {
            Duration duration = xmlDatatypeFactory.newDuration(lexTrimmed);
            return new NodeValueDuration(duration, node);
        }
        if (datatype.equals(XSDyearMonthDuration) && XSDyearMonthDuration.isValid(lex)) {
            Duration duration = xmlDatatypeFactory.newDuration(lexTrimmed);
            return new NodeValueDuration(duration, node);
        }
        if (datatype.equals(XSDdayTimeDuration) && XSDdayTimeDuration.isValid(lex)) {
            Duration duration = xmlDatatypeFactory.newDuration(lexTrimmed);
            return new NodeValueDuration(duration, node);
        }
        // Not wired in
        if (SystemARQ.EnableRomanNumerals) {
            if (lit.getDatatypeURI().equals(RomanNumeralDatatype.get().getURI())) {
                Object obj = RomanNumeralDatatype.get().parse(lexTrimmed);
                if (obj instanceof Integer)
                    return new NodeValueInteger(((Integer) obj).longValue());
                if (obj instanceof RomanNumeral)
                    return new NodeValueInteger(((RomanNumeral) obj).intValue());
                throw new ARQInternalErrorException("DatatypeFormatException: Roman numeral is unknown class");
            }
        }
    } catch (DatatypeFormatException ex) {
        // Should have been caught earlier by special test in nodeToNodeValue
        throw new ARQInternalErrorException("DatatypeFormatException: " + lit, ex);
    }
    return null;
}
Also used : DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) Duration(javax.xml.datatype.Duration) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) RDFDatatype(org.apache.jena.datatypes.RDFDatatype) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 7 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class ModelCom method createTypedLiteral.

/**
 * Build a typed literal from its value form.
 *
 * @param value the value of the literal
 * @param typeURI the URI of the type of the literal, null for old style "plain" literals
 */
@Override
public Literal createTypedLiteral(Object value, String typeURI) {
    RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(typeURI);
    LiteralLabel ll = LiteralLabelFactory.createByValue(value, "", dt);
    return new LiteralImpl(NodeFactory.createLiteral(ll), this);
}
Also used : RDFDatatype(org.apache.jena.datatypes.RDFDatatype) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 8 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class ModelCom method createTypedLiteral.

/**
 * Create a typed literal xsd:dateTime from a Calendar object.
 */
@Override
public Literal createTypedLiteral(Calendar cal) {
    Object value = new XSDDateTime(cal);
    LiteralLabel ll = LiteralLabelFactory.createByValue(value, "", XSDDatatype.XSDdateTime);
    return new LiteralImpl(NodeFactory.createLiteral(ll), this);
}
Also used : XSDDateTime(org.apache.jena.datatypes.xsd.XSDDateTime) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 9 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class Converters method makeLiteral.

/**
 * Creates a literal from the value and type URI. There are several possible
 * outcomes:
 * <ul>
 * <li>If the URI is registered with TypeMapper and the value is the proper
 * lexical form for the type, the registered TypeMapper is used and calling
 * {@code getLiteralValue()} on the returned node will return a proper object.
 * </li>
 * <li>If the URI is unregistered a Datatype is created but not registered with
 * the TypeMapper. The resulting node is properly constructed for used in output
 * serialization, queries, or updates. Calling {@code getLiteralValue()} on the
 * returned node will throw DatatypeFormatException. Note that if
 * {@code JenaParameters.enableEagerLiteralValidation} is true the
 * DatatypeFormatException will be thrown by this method.</li>
 * <li>If the URI is registered but the value is not a proper lexical form a
 * DatatypeFormatException will be thrown by this method.</li>
 * </ul>
 *
 * @param value the value for the literal
 * @param typeUri the type URI for the literal node.
 * @return the literal node.
 * @throws DatatypeFormatException on errors noted above
 */
public static Node makeLiteral(String value, String typeUri) {
    Object oValue = value;
    RDFDatatype dt = TypeMapper.getInstance().getTypeByName(typeUri);
    if (dt == null) {
        dt = new BaseDatatype(typeUri) {

            @Override
            public boolean isValidValue(Object valueForm) {
                return false;
            }

            @Override
            public Object parse(String lexicalForm) throws DatatypeFormatException {
                RDFDatatype dt = TypeMapper.getInstance().getTypeByName(uri);
                if (dt == null) {
                    throw new DatatypeFormatException("no registered Datatype for " + uri);
                }
                return dt.parse(lexicalForm);
            }
        };
    } else {
        oValue = dt.parse(value);
    }
    LiteralLabel ll = LiteralLabelFactory.createByValue(oValue, null, dt);
    return NodeFactory.createLiteral(ll);
}
Also used : DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) BaseDatatype(org.apache.jena.datatypes.BaseDatatype) RDFDatatype(org.apache.jena.datatypes.RDFDatatype) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 10 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class NodeFactoryExtra method nodeToDouble.

/**
 * Node to double
 *
 * @param node
 * @return The double value or Double.NaN
 */
public static double nodeToDouble(Node node) {
    LiteralLabel lit = node.getLiteral();
    if (!XSDDatatype.XSDdouble.isValidLiteral(lit))
        return Double.NaN;
    double d = ((Number) lit.getValue()).doubleValue();
    return d;
}
Also used : LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Aggregations

LiteralLabel (org.apache.jena.graph.impl.LiteralLabel)43 Node (org.apache.jena.graph.Node)8 RDFDatatype (org.apache.jena.datatypes.RDFDatatype)7 DatatypeFormatException (org.apache.jena.datatypes.DatatypeFormatException)5 NodeFactory.createLiteral (org.apache.jena.graph.NodeFactory.createLiteral)5 Test (org.junit.Test)5 RDFLangString (org.apache.jena.datatypes.xsd.impl.RDFLangString)4 BigDecimal (java.math.BigDecimal)3 BaseDatatype (org.apache.jena.datatypes.BaseDatatype)3 SpatialIndexException (org.apache.jena.query.spatial.SpatialIndexException)3 Var (org.apache.jena.sparql.core.Var)3 UUID (java.util.UUID)2 FrontsNode (org.apache.jena.graph.FrontsNode)2 Triple (org.apache.jena.graph.Triple)2 PrefixMapping (org.apache.jena.shared.PrefixMapping)2 ExprVar (org.apache.jena.sparql.expr.ExprVar)2 FileReader (java.io.FileReader)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Duration (javax.xml.datatype.Duration)1