Search in sources :

Example 1 with RDFDatatype

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

the class ParserProfileStd method create.

@Override
public Node create(Node currentGraph, Token token) {
    // Dispatches to the underlying ParserFactory operation
    long line = token.getLine();
    long col = token.getColumn();
    String str = token.getImage();
    switch(token.getType()) {
        case BNODE:
            return createBlankNode(currentGraph, str, line, col);
        case IRI:
            return createURI(str, line, col);
        case PREFIXED_NAME:
            {
                String prefix = str;
                String suffix = token.getImage2();
                String expansion = expandPrefixedName(prefix, suffix, token);
                return createURI(expansion, line, col);
            }
        case DECIMAL:
            return createTypedLiteral(str, XSDDatatype.XSDdecimal, line, col);
        case DOUBLE:
            return createTypedLiteral(str, XSDDatatype.XSDdouble, line, col);
        case INTEGER:
            return createTypedLiteral(str, XSDDatatype.XSDinteger, line, col);
        case LITERAL_DT:
            {
                Token tokenDT = token.getSubToken2();
                String uriStr;
                switch(tokenDT.getType()) {
                    case IRI:
                        uriStr = tokenDT.getImage();
                        break;
                    case PREFIXED_NAME:
                        {
                            String prefix = tokenDT.getImage();
                            String suffix = tokenDT.getImage2();
                            uriStr = expandPrefixedName(prefix, suffix, tokenDT);
                            break;
                        }
                    default:
                        throw new RiotException("Expected IRI for datatype: " + token);
                }
                uriStr = resolveIRI(uriStr, tokenDT.getLine(), tokenDT.getColumn());
                RDFDatatype dt = NodeFactory.getType(uriStr);
                return createTypedLiteral(str, dt, line, col);
            }
        case LITERAL_LANG:
            return createLangLiteral(str, token.getImage2(), line, col);
        case STRING:
            return createStringLiteral(str, line, col);
        default:
            {
                Node x = createNodeFromToken(currentGraph, token, line, col);
                if (x != null)
                    return x;
                errorHandler.fatal("Not a valid token for an RDF term: " + token, line, col);
                return null;
            }
    }
}
Also used : RiotException(org.apache.jena.riot.RiotException) Node(org.apache.jena.graph.Node) Token(org.apache.jena.riot.tokens.Token) RDFDatatype(org.apache.jena.datatypes.RDFDatatype)

Example 2 with RDFDatatype

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

the class CanonicalizeLiteral method apply.

@Override
public Node apply(Node node) {
    if (!node.isLiteral())
        return node;
    if (!node.getLiteralDatatype().isValid(node.getLiteralLexicalForm()))
        // Invalid lexical form for the datatype - do nothing.
        return node;
    RDFDatatype dt = node.getLiteralDatatype();
    Node n2;
    if (NodeUtils.isLangString(node)) {
        // RDF 1.0, no datatype ; RDF 1.1 : datatype is rdf:langString 
        if (node.getLiteralLanguage().equals(""))
            //n2 = NormalizeValue.dtSimpleLiteral.handle(node, node.getLiteralLexicalForm(), null) ;
            return node;
        else
            n2 = canonicalLangtag(node.getLiteralLexicalForm(), node.getLiteralLanguage());
    } else if (dt == null) {
        // RDF 1.0 / no lang.
        n2 = NormalizeValue.dtSimpleLiteral.handle(node, node.getLiteralLexicalForm(), null);
    } else {
        // Dataype, not rdf:langString (RDF 1.1). 
        DatatypeHandler handler = dispatch.get(dt);
        if (handler == null)
            return node;
        n2 = handler.handle(node, node.getLiteralLexicalForm(), dt);
    }
    if (n2 == null)
        return node;
    return n2;
}
Also used : Node(org.apache.jena.graph.Node) RDFDatatype(org.apache.jena.datatypes.RDFDatatype)

Example 3 with RDFDatatype

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

the class XSDDatatype method isBaseTypeCompatible.

/**
     * Test if the given typed value is in the right partition of the XSD type space.
     * If this test passes then if the typed value has a legal lexical form for
     * this type then it is a legal instance.
     */
public boolean isBaseTypeCompatible(LiteralLabel lit) {
    XSTypeDefinition base = getFoundingType();
    RDFDatatype litDT = lit.getDatatype();
    if (litDT instanceof XSDDatatype) {
        XSTypeDefinition litBase = ((XSDDatatype) litDT).getFoundingType();
        return base.equals(litBase);
    } else if (litDT == null && lit.language().equals("")) {
        // Special RDF case, a plain literal is type compatible with and xsd:string-based type
        return base.equals(XSDstring.typeDeclaration);
    } else {
        return false;
    }
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) RDFDatatype(org.apache.jena.datatypes.RDFDatatype)

Example 4 with RDFDatatype

use of org.apache.jena.datatypes.RDFDatatype 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 5 with RDFDatatype

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

the class NodeUtils method stringLiteral.

/** Get lexical for of anything that looks like a string literal.
     * Returns the string value of plain literal (simple literal
     * or lang string) or XSD string.
     */
public static String stringLiteral(Node literal) {
    if (!literal.isLiteral())
        return null;
    RDFDatatype dType = literal.getLiteralDatatype();
    String langTag = literal.getLiteralLanguage();
    // Language?
    if (langTag != null && !langTag.equals(""))
        return literal.getLiteralLexicalForm();
    if (dType == null || dType.equals(XSDDatatype.XSDstring))
        return literal.getLiteralLexicalForm();
    return null;
}
Also used : RDFDatatype(org.apache.jena.datatypes.RDFDatatype)

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