Search in sources :

Example 26 with LiteralLabel

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

the class SpatialOperationWithCircleBase method objectToStruct.

/** Deconstruct the node or list object argument and make a NearbyMatch */
@Override
protected SpatialMatch objectToStruct(PropFuncArg argObject) {
    // EntityDefinition docDef = server.getDocDef();
    if (argObject.isNode()) {
        log.warn("Object not a List: " + argObject);
        return null;
    }
    List<Node> list = argObject.getArgList();
    if (list.size() < 3 || 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 is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Latitude is not a decimal " + list);
        return null;
    }
    Double latitude = Double.parseDouble(x.getLiteralLexicalForm());
    idx++;
    x = list.get(idx);
    if (!x.isLiteral()) {
        log.warn("Longitude is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Longitude is not a decimal " + list);
        return null;
    }
    Double longtitude = Double.parseDouble(x.getLiteralLexicalForm());
    idx++;
    x = list.get(idx);
    if (!x.isLiteral()) {
        log.warn("Radius is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Radius is not a decimal " + list);
        return null;
    }
    Double radius = Double.parseDouble(x.getLiteralLexicalForm());
    if (radius <= 0) {
        log.warn("Radius is not a correct decimal " + list);
        return null;
    }
    // Kilometres
    String units = DistanceUnitsUtils.defaultDistanceUnit;
    int limit = -1;
    idx++;
    if (idx < list.size()) {
        x = list.get(idx);
        if (!x.isLiteral()) {
            log.warn("Units or limit is not a literal " + list);
            return null;
        }
        if (x.getLiteralDatatype() == null || x.getLiteralDatatype().equals(XSDDatatype.XSDstring)) {
            String u = x.getLiteralLexicalForm();
            if (DistanceUnitsUtils.isSupportedUnits(u)) {
                idx++;
                units = u;
            } else {
                log.warn("Units are not a supported " + list);
                return null;
            }
        }
    }
    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(latitude, longtitude, radius, units, limit, this.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)

Example 27 with LiteralLabel

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

the class NodeValue method nodeToNodeValue.

// ----------------------------------------------------------------
// ---- Setting : used when a node is used to make a NodeValue
private static NodeValue nodeToNodeValue(Node node) {
    if (node.isVariable())
        Log.warn(NodeValue.class, "Variable passed to NodeValue.nodeToNodeValue");
    if (!node.isLiteral())
        // Not a literal - no value to extract
        return new NodeValueNode(node);
    boolean hasLangTag = NodeUtils.isLangString(node);
    boolean isPlainLiteral = (node.getLiteralDatatypeURI() == null && !hasLangTag);
    if (isPlainLiteral)
        return new NodeValueString(node.getLiteralLexicalForm(), node);
    if (hasLangTag) {
        // Works for RDF 1.0 and RDF 1.1
        if (node.getLiteralDatatype() != null && !RDF.dtLangString.equals(node.getLiteralDatatype())) {
            if (NodeValue.VerboseWarnings)
                Log.warn(NodeValue.class, "Lang tag and datatype (datatype ignored)");
        }
        return new NodeValueLang(node);
    }
    // Typed literal
    LiteralLabel lit = node.getLiteral();
    // Use this - already calculated when the node is formed.
    if (!node.getLiteral().isWellFormed()) {
        if (NodeValue.VerboseWarnings) {
            String tmp = FmtUtils.stringForNode(node);
            Log.warn(NodeValue.class, "Datatype format exception: " + tmp);
        }
        // Invalid lexical form.
        return new NodeValueNode(node);
    }
    NodeValue nv = _setByValue(node);
    if (nv != null)
        return nv;
    return new NodeValueNode(node);
// raise(new ExprException("NodeValue.nodeToNodeValue: Unknown Node type: "+n)) ;
}
Also used : LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 28 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 lexical form. The
 * lexical form will be parsed now and the value stored. If
 * the form is not legal this will throw an exception.
 *
 * @param lex the lexical form of the literal
 * @param typeURI the uri of the type of the literal, null for old style "plain" literals
 * @throws DatatypeFormatException if lex is not a legal form of dtype
 */
@Override
public Literal createTypedLiteral(String lex, String typeURI) {
    RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(typeURI);
    LiteralLabel ll = LiteralLabelFactory.create(lex, dt);
    return new LiteralImpl(NodeFactory.createLiteral(ll), this);
}
Also used : RDFDatatype(org.apache.jena.datatypes.RDFDatatype) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 29 with LiteralLabel

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

the class Rational method testBinary2.

public void testBinary2() {
    // Check round tripping from value
    LiteralLabel l2 = m.createTypedLiteral("DCpj", XSDDatatype.XSDbase64Binary).asNode().getLiteral();
    Object data2 = l2.getValue();
    assertTrue("binary test 3", data2 instanceof byte[]);
    byte[] data2b = (byte[]) data2;
    assertEquals("binary test 4", data2b[0], data[0]);
    assertEquals("binary test 5", data2b[1], data[1]);
    assertEquals("binary test 6", data2b[2], data[2]);
}
Also used : LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 30 with LiteralLabel

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

the class Rational method doTestRoundTrip.

/**
 * Check parse/unparse loop.
 */
public void doTestRoundTrip(String lex, RDFDatatype dt, boolean testeq) {
    LiteralLabel ll = LiteralLabelFactory.create(lex, dt);
    String lex2 = dt.unparse(ll.getValue());
    if (testeq) {
        assertEquals(lex, lex2);
    }
    LiteralLabel ll2 = LiteralLabelFactory.create(lex2, dt);
    assertTrue(ll2.isWellFormed());
}
Also used : RDFLangString(org.apache.jena.datatypes.xsd.impl.RDFLangString) 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