Search in sources :

Example 31 with RDFDatatype

use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.

the class JSONInput method parseOneTerm.

private Node parseOneTerm(JsonObject term) {
    checkContains(term, false, false, kType, kValue, kXmlLang, kDatatype);
    String type = stringOrNull(term, kType);
    String v = stringOrNull(term, kValue);
    if (kUri.equals(type)) {
        checkContains(term, false, true, kType, kValue);
        String uri = v;
        Node n = NodeFactory.createURI(v);
        return n;
    }
    if (kLiteral.equals(type) || kTypedLiteral.equals(type)) {
        String lang = stringOrNull(term, kXmlLang);
        String dtStr = stringOrNull(term, kDatatype);
        if (lang != null && dtStr != null)
            throw new ResultSetException("Both language and datatype defined: " + term);
        RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(dtStr);
        return NodeFactory.createLiteral(v, lang, dt);
    }
    if (kBnode.equals(type))
        return labelMap.get(null, v);
    throw new ResultSetException("Object key not recognized as valid for an RDF term: " + term);
}
Also used : LabelToNode(org.apache.jena.riot.lang.LabelToNode) Node(org.apache.jena.graph.Node) RDFDatatype(org.apache.jena.datatypes.RDFDatatype)

Example 32 with RDFDatatype

use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.

the class JsonLDReader method createNode.

private Node createNode(Map<String, Object> map) {
    String type = (String) map.get("type");
    String lex = (String) map.get("value");
    if (type.equals(IRI))
        return createURI(lex);
    else if (type.equals(BLANK_NODE))
        return createBlankNode(lex);
    else if (type.equals(LITERAL)) {
        String lang = (String) map.get("language");
        String datatype = (String) map.get("datatype");
        if (Objects.equals(xsdString, datatype))
            // In RDF 1.1, simple literals and xsd:string are the same.
            // During migration, we prefer simple literals to xsd:strings. 
            datatype = null;
        if (lang == null && datatype == null)
            return profile.createStringLiteral(lex, -1, -1);
        if (lang != null)
            return profile.createLangLiteral(lex, lang, -1, -1);
        RDFDatatype dt = NodeFactory.getType(datatype);
        return profile.createTypedLiteral(lex, dt, -1, -1);
    } else
        throw new InternalErrorException("Node is not a IRI, bNode or a literal: " + type);
}
Also used : InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) RDFDatatype(org.apache.jena.datatypes.RDFDatatype)

Example 33 with RDFDatatype

use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.

the class NodeFormatterBase method formatLiteral.

@Override
public void formatLiteral(AWriter w, Node n) {
    RDFDatatype dt = n.getLiteralDatatype();
    String lang = n.getLiteralLanguage();
    String lex = n.getLiteralLexicalForm();
    if (lang != null && !lang.equals("")) {
        formatLitLang(w, lex, lang);
    } else if (dt == null) {
        // RDF 1.0, simple literal.
        formatLitString(w, lex);
    } else if (JenaRuntime.isRDF11 && dt.equals(XSDDatatype.XSDstring)) {
        // RDF 1.1, xsd:string - output as short string.
        formatLitString(w, lex);
    } else {
        // Datatype, no language tag, not short string.
        formatLitDT(w, lex, dt.getURI());
    }
}
Also used : RDFDatatype(org.apache.jena.datatypes.RDFDatatype)

Example 34 with RDFDatatype

use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.

the class ThriftConvert method convert.

public static Node convert(RDF_Term term, PrefixMap pmap) {
    if (term.isSetPrefixName()) {
        String x = expand(term.getPrefixName(), pmap);
        if (x != null)
            return NodeFactory.createURI(x);
        throw new RiotThriftException("Failed to expand " + term);
    //                Log.warn(BinRDF.class, "Failed to expand "+term) ;
    //                return NodeFactory.createURI(prefix+":"+localname) ; 
    }
    if (term.isSetIri())
        return NodeFactory.createURI(term.getIri().getIri());
    if (term.isSetBnode())
        return NodeFactory.createBlankNode(term.getBnode().getLabel());
    if (term.isSetLiteral()) {
        RDF_Literal lit = term.getLiteral();
        String lex = lit.getLex();
        String dtString = null;
        if (lit.isSetDatatype())
            dtString = lit.getDatatype();
        else if (lit.isSetDtPrefix()) {
            String x = expand(lit.getDtPrefix(), pmap);
            if (x == null)
                throw new RiotThriftException("Failed to expand datatype prefix name:" + term);
            dtString = x;
        }
        RDFDatatype dt = NodeFactory.getType(dtString);
        String lang = lit.getLangtag();
        return NodeFactory.createLiteral(lex, lang, dt);
    }
    if (term.isSetValInteger()) {
        long x = term.getValInteger();
        String lex = Long.toString(x, 10);
        RDFDatatype dt = XSDDatatype.XSDinteger;
        return NodeFactory.createLiteral(lex, dt);
    }
    if (term.isSetValDouble()) {
        double x = term.getValDouble();
        String lex = Double.toString(x);
        RDFDatatype dt = XSDDatatype.XSDdouble;
        return NodeFactory.createLiteral(lex, dt);
    }
    if (term.isSetValDecimal()) {
        long value = term.getValDecimal().getValue();
        int scale = term.getValDecimal().getScale();
        BigDecimal d = BigDecimal.valueOf(value, scale);
        String lex = d.toPlainString();
        RDFDatatype dt = XSDDatatype.XSDdecimal;
        return NodeFactory.createLiteral(lex, dt);
    }
    if (term.isSetVariable())
        return Var.alloc(term.getVariable().getName());
    if (term.isSetAny())
        return Node.ANY;
    if (term.isSetUndefined())
        return null;
    throw new RiotThriftException("No conversion to a Node: " + term.toString());
}
Also used : RDFLangString(org.apache.jena.datatypes.xsd.impl.RDFLangString) RDFDatatype(org.apache.jena.datatypes.RDFDatatype) BigDecimal(java.math.BigDecimal)

Example 35 with RDFDatatype

use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.

the class ThriftConvert method toThriftValue.

/** Attempt to encode a node by value (integer, decimal, double) into an RDF_term.
     * @param node
     * @param term
     * @return true if the term was set, else false.
     */
public static boolean toThriftValue(Node node, RDF_Term term) {
    if (!node.isLiteral())
        return false;
    // Value cases : Integer, Double, Decimal
    String lex = node.getLiteralLexicalForm();
    RDFDatatype rdt = node.getLiteralDatatype();
    if (rdt == null)
        return false;
    if (rdt.equals(XSDDatatype.XSDdecimal)) {
        if (rdt.isValid(lex)) {
            BigDecimal decimal = new BigDecimal(lex.trim());
            int scale = decimal.scale();
            BigInteger bigInt = decimal.unscaledValue();
            if (bigInt.compareTo(MAX_I) <= 0 && bigInt.compareTo(MIN_I) >= 0) {
                // This check makes sure that bigInt.longValue() is safe
                RDF_Decimal d = new RDF_Decimal(bigInt.longValue(), scale);
                RDF_Term literal = new RDF_Term();
                term.setValDecimal(d);
                return true;
            }
        }
    } else if (rdt.equals(XSDDatatype.XSDinteger) || rdt.equals(XSDDatatype.XSDlong) || rdt.equals(XSDDatatype.XSDint) || rdt.equals(XSDDatatype.XSDshort) || rdt.equals(XSDDatatype.XSDbyte)) {
        // Conservative - no derived types.
        if (rdt.isValid(lex)) {
            try {
                long v = ((Number) node.getLiteralValue()).longValue();
                term.setValInteger(v);
                return true;
            }// Out of range for the type, not a long etc etc.
             catch (Throwable ex) {
            }
        }
    } else if (rdt.equals(XSDDatatype.XSDdouble)) {
        // XSDfloat??
        if (rdt.isValid(lex)) {
            try {
                double v = ((Double) node.getLiteralValue()).doubleValue();
                term.setValDouble(v);
                return true;
            }// Out of range for the type, ...
             catch (Throwable ex) {
            }
        }
    }
    return false;
}
Also used : BigInteger(java.math.BigInteger) RDFLangString(org.apache.jena.datatypes.xsd.impl.RDFLangString) RDFDatatype(org.apache.jena.datatypes.RDFDatatype) BigDecimal(java.math.BigDecimal)

Aggregations

RDFDatatype (org.apache.jena.datatypes.RDFDatatype)35 Node (org.apache.jena.graph.Node)9 LiteralLabel (org.apache.jena.graph.impl.LiteralLabel)6 TypeMapper (org.apache.jena.datatypes.TypeMapper)4 RDFLangString (org.apache.jena.datatypes.xsd.impl.RDFLangString)4 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)2 DatatypeFormatException (org.apache.jena.datatypes.DatatypeFormatException)2 RDFNode (org.apache.jena.rdf.model.RDFNode)2 ARQInternalErrorException (org.apache.jena.sparql.ARQInternalErrorException)2 FileReader (java.io.FileReader)1 Duration (javax.xml.datatype.Duration)1 InternalErrorException (org.apache.jena.atlas.lib.InternalErrorException)1 BaseDatatype (org.apache.jena.datatypes.BaseDatatype)1 XSDDateTime (org.apache.jena.datatypes.xsd.XSDDateTime)1 EnhNode (org.apache.jena.enhanced.EnhNode)1 RiotException (org.apache.jena.riot.RiotException)1 LabelToNode (org.apache.jena.riot.lang.LabelToNode)1 Token (org.apache.jena.riot.tokens.Token)1 Item (org.apache.jena.sparql.sse.Item)1