Search in sources :

Example 36 with LiteralLabel

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

the class TestLiteralLabels method testHashCodesForHexBinary_1.

public void testHashCodesForHexBinary_1() {
    LiteralLabel A = node("'0123'http://www.w3.org/2001/XMLSchema#hexBinary").getLiteral();
    LiteralLabel B = node("'0123'http://www.w3.org/2001/XMLSchema#hexBinary").getLiteral();
    assertEquals(A.hashCode(), B.hashCode());
}
Also used : LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 37 with LiteralLabel

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

the class TestLiteralLabels method testEquality2.

public void testEquality2() {
    LiteralLabel A = LiteralLabelFactory.createTypedLiteral("xyz");
    LiteralLabel B = LiteralLabelFactory.createTypedLiteral("XYZ");
    assertFalse(A.equals(B));
    assertFalse(A.sameValueAs(B));
}
Also used : LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 38 with LiteralLabel

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

the class TestLiteralLabels method testEquality4.

public void testEquality4() {
    LiteralLabel A = LiteralLabelFactory.create("xyz", "en-UK");
    LiteralLabel B = LiteralLabelFactory.create("xyz", "en-uk");
    assertFalse(A.equals(B));
    assertTrue(A.sameValueAs(B));
}
Also used : LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 39 with LiteralLabel

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

the class NodeIdInline method inline$.

private static NodeId inline$(Node node) {
    if (!hasInlineDatatype(node))
        return null;
    LiteralLabel lit = node.getLiteral();
    if (node.getLiteralDatatype().equals(XSDDatatype.XSDdecimal)) {
        // Check lexical form.
        if (!XSDDatatype.XSDdecimal.isValidLiteral(lit))
            return null;
        // Not lit.getValue() because that may be a narrower type e.g. Integer.
        // .trim is how Jena does it but it rather savage. spc, \n \r \t.
        // But at this point we know it's a valid literal so the excessive
        // chopping by .trim is safe.
        BigDecimal decimal = new BigDecimal(lit.getLexicalForm().trim());
        // Does range checking.
        DecimalNode56 dn = DecimalNode56.valueOf(decimal);
        // null is "does not fit"
        if (dn != null)
            // setType
            return NodeId.createRaw(XSD_DECIMAL, dn.pack());
        else
            return null;
    } else {
        // Not decimal.
        if (XSDDatatype.XSDinteger.isValidLiteral(lit)) {
            // Quick check.
            if (lit.getLexicalForm().length() > 19)
                return null;
            // Derived types.
            NodeIdType type = derivedTypeMap.inverse().getOrDefault(lit.getDatatype(), NodeIdType.XSD_INTEGER);
            try {
                long v = ((Number) lit.getValue()).longValue();
                v = IntegerNode.pack56(v);
                // Value -1 is "does not fit"
                if (v == -1)
                    return null;
                return NodeId.createRaw(type, v);
            }// Out of range for the type, not a long etc etc.
             catch (Throwable ex) {
                return null;
            }
        }
    }
    if (XSDDatatype.XSDdouble.isValidLiteral(lit)) {
        double d = ((Number) lit.getValue()).doubleValue();
        long v = DoubleNode62.pack(d);
        if (v == DoubleNode62.NO_ENCODING)
            return null;
        // The special byte encoding of XSD_DOUBLE is handled in NodeIdFactory.encode/.decode.
        return NodeId.createRaw(XSD_DOUBLE, v);
    }
    if (XSDDatatype.XSDfloat.isValidLiteral(lit)) {
        float f = ((Number) lit.getValue()).floatValue();
        long v = FloatNode.pack(f);
        return NodeId.createRaw(XSD_FLOAT, v);
    }
    if (lit.getDatatype().equals(XSDDatatype.XSDdateTimeStamp) && XSDDatatype.XSDdateTimeStamp.isValidLiteral(lit)) {
        long v = DateTimeNode.packDateTime(lit.getLexicalForm());
        if (v == -1)
            return null;
        return NodeId.createRaw(XSD_DATETIMESTAMP, v);
    }
    if (XSDDatatype.XSDdateTime.isValidLiteral(lit)) {
        // Could use the Jena/XSDDateTime object here rather than reparse the lexical form.
        // But this works and it's close to a release ...
        long v = DateTimeNode.packDateTime(lit.getLexicalForm());
        if (v == -1)
            return null;
        return NodeId.createRaw(XSD_DATETIME, v);
    }
    if (XSDDatatype.XSDdate.isValidLiteral(lit)) {
        long v = DateTimeNode.packDate(lit.getLexicalForm());
        if (v == -1)
            return null;
        return NodeId.createRaw(XSD_DATE, v);
    }
    if (XSDDatatype.XSDboolean.isValidLiteral(lit)) {
        long v = 0;
        boolean b = (Boolean) lit.getValue();
        // return new NodeValueBoolean(b, node);
        if (b)
            v = v | 0x01;
        return NodeId.createRaw(XSD_BOOLEAN, v);
    }
    return null;
}
Also used : NodeIdType(org.apache.jena.tdb2.store.NodeIdType) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel) BigDecimal(java.math.BigDecimal)

Example 40 with LiteralLabel

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

the class NodeFactoryExtra method nodeToInt.

/**
 * Node to int
 *
 * @param node
 * @return The int value or Integer.MIN_VALUE.
 */
public static int nodeToInt(Node node) {
    LiteralLabel lit = node.getLiteral();
    if (!XSDDatatype.XSDint.isValidLiteral(lit))
        return Integer.MIN_VALUE;
    int i = ((Number) lit.getValue()).intValue();
    return i;
}
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