Search in sources :

Example 21 with LiteralLabel

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

the class Rational method testBinary1.

/**
 * Test binary types base64 and hexbinary
 */
public void testBinary1() {
    // Check byte[] maps onto a binary type  - specifically base64Binary.
    byte[] data = new byte[] { 12, 42, 99 };
    Literal l = m.createTypedLiteral(data);
    LiteralLabel ll = l.asNode().getLiteral();
    assertTrue("binary test 1", ll.getDatatype() instanceof XSDbinary);
    // base64 is registered for byte[]
    // hexBinary is not registered as a type for byte[]
    assertTrue("binary test 1a", ll.getDatatype() instanceof XSDbase64Binary);
    assertEquals("binary test 1b", "DCpj", ll.getLexicalForm());
}
Also used : NodeFactory.createLiteral(org.apache.jena.graph.NodeFactory.createLiteral) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 22 with LiteralLabel

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

the class Rational method testBinary3.

public void testBinary3() {
    // Check hexBinary
    Literal l = m.createTypedLiteral(data, XSDDatatype.XSDhexBinary);
    LiteralLabel ll = l.asNode().getLiteral();
    assertEquals("binary test 1b", ll.getDatatype(), XSDDatatype.XSDhexBinary);
    assertEquals("binary test 2b", Hex.encodeHexString(data, false), ll.getLexicalForm());
    // Check round tripping from value
    LiteralLabel l2 = m.createTypedLiteral(ll.getLexicalForm(), XSDDatatype.XSDhexBinary).asNode().getLiteral();
    Object data2 = l2.getValue();
    assertTrue("binary test 3b", data2 instanceof byte[]);
    byte[] data2b = ((byte[]) data2);
    assertEquals("binary test 4b", data2b[0], data[0]);
    assertEquals("binary test 5b", data2b[1], data[1]);
    assertEquals("binary test 6b", data2b[2], data[2]);
    assertEquals(l2, ll);
}
Also used : NodeFactory.createLiteral(org.apache.jena.graph.NodeFactory.createLiteral) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 23 with LiteralLabel

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

the class AbstractQueryBuilderTest method testMakeValueNodes.

@Test
public void testMakeValueNodes() {
    List<Object> list = new ArrayList<Object>();
    list.add(null);
    list.add(RDF.type);
    Node n2 = NodeFactory.createBlankNode();
    list.add(n2);
    builder.addPrefix("demo", "http://example.com/");
    list.add("demo:type");
    list.add("<one>");
    list.add(Integer.valueOf(5));
    Collection<Node> result = builder.makeValueNodes(list.iterator());
    assertEquals(6, result.size());
    assertTrue(result.contains(null));
    assertTrue(result.contains(RDF.type.asNode()));
    assertTrue(result.contains(n2));
    assertTrue(result.contains(NodeFactory.createURI("http://example.com/type")));
    assertTrue(result.contains(NodeFactory.createURI("one")));
    LiteralLabel ll = LiteralLabelFactory.createTypedLiteral(Integer.valueOf(5));
    assertTrue(result.contains(NodeFactory.createLiteral(ll)));
}
Also used : Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel) Test(org.junit.Test)

Example 24 with LiteralLabel

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

the class AbstractQueryBuilderTest method testMakeNode.

@Test
public void testMakeNode() {
    Node n = builder.makeNode(null);
    assertEquals(Node.ANY, n);
    n = builder.makeNode(RDF.type);
    assertEquals(RDF.type.asNode(), n);
    Node n2 = NodeFactory.createBlankNode();
    n = builder.makeNode(n2);
    assertEquals(n2, n);
    builder.addPrefix("demo", "http://example.com/");
    n = builder.makeNode("demo:type");
    assertEquals(NodeFactory.createURI("http://example.com/type"), n);
    n = builder.makeNode("<one>");
    assertEquals(NodeFactory.createURI("one"), n);
    try {
        n = builder.makeNode(builder);
        fail("Should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // do nothing
    }
    n = builder.makeNode(Integer.valueOf(5));
    assertTrue(n.isLiteral());
    LiteralLabel ll = LiteralLabelFactory.createTypedLiteral(Integer.valueOf(5));
    assertEquals(NodeFactory.createLiteral(ll), n);
    n = builder.makeNode(NodeFactory.createVariable("foo"));
    assertTrue(n.isVariable());
    assertEquals("foo", n.getName());
    assertTrue(n instanceof Var);
    n = builder.makeNode("'text'@en");
    assertTrue(n.isLiteral());
    assertEquals("text", n.getLiteralLexicalForm());
    assertEquals("en", n.getLiteralLanguage());
}
Also used : Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel) Test(org.junit.Test)

Example 25 with LiteralLabel

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

the class SpatialOperationWithBoxPFBase method objectToStruct.

/** Deconstruct the node or list object argument and make a SpatialMatch */
@Override
protected SpatialMatch objectToStruct(PropFuncArg argObject) {
    if (argObject.isNode()) {
        log.warn("Object not a List: " + argObject);
        return null;
    }
    List<Node> list = argObject.getArgList();
    if (list.size() < 4 || list.size() > 5)
        throw new SpatialIndexException("Change in object list size");
    int idx = 0;
    Node x = list.get(idx);
    if (!x.isLiteral()) {
        log.warn("Latitude 1 is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Latitude 1 is not a decimal " + list);
        return null;
    }
    Double latitude1 = Double.parseDouble(x.getLiteralLexicalForm());
    idx++;
    x = list.get(idx);
    if (!x.isLiteral()) {
        log.warn("Longitude 1 is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Longitude 1 is not a decimal " + list);
        return null;
    }
    Double longtitude1 = Double.parseDouble(x.getLiteralLexicalForm());
    idx++;
    x = list.get(idx);
    if (!x.isLiteral()) {
        log.warn("Latitude 2 is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Latitude 2 is not a decimal " + list);
        return null;
    }
    Double latitude2 = Double.parseDouble(x.getLiteralLexicalForm());
    idx++;
    x = list.get(idx);
    if (!x.isLiteral()) {
        log.warn("Longitude 2 is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Longitude 2 is not a decimal " + list);
        return null;
    }
    Double longtitude2 = Double.parseDouble(x.getLiteralLexicalForm());
    idx++;
    int limit = -1;
    if (idx < list.size()) {
        x = list.get(idx);
        if (!x.isLiteral()) {
            log.warn("Limit is not a literal " + list);
            return null;
        }
        LiteralLabel lit = x.getLiteral();
        if (!XSDDatatype.XSDinteger.isValidLiteral(lit)) {
            log.warn("Limit is not an integer " + list);
            return null;
        }
        int v = NodeFactoryExtra.nodeToInt(x);
        limit = (v < 0) ? -1 : v;
        idx++;
        if (idx < list.size()) {
            log.warn("Limit is not the last parameter " + list);
            return null;
        }
    }
    SpatialMatch match = new SpatialMatch(latitude1, longtitude1, latitude2, longtitude2, limit, getSpatialOperation());
    if (log.isDebugEnabled())
        log.debug("Trying SpatialMatch: " + match.toString());
    return match;
}
Also used : Node(org.apache.jena.graph.Node) SpatialIndexException(org.apache.jena.query.spatial.SpatialIndexException) 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