Search in sources :

Example 36 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class HTTPRepositoryConnection method getContextIDs.

public RepositoryResult<Resource> getContextIDs() throws RepositoryException {
    try {
        List<Resource> contextList = new ArrayList<Resource>();
        TupleQueryResult contextIDs = client.getContextIDs();
        try {
            while (contextIDs.hasNext()) {
                BindingSet bindingSet = contextIDs.next();
                Value context = bindingSet.getValue("contextID");
                if (context instanceof Resource) {
                    contextList.add((Resource) context);
                }
            }
        } finally {
            contextIDs.close();
        }
        return createRepositoryResult(contextList);
    } catch (QueryEvaluationException e) {
        throw new RepositoryException(e);
    } catch (IOException e) {
        throw new RepositoryException(e);
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Resource(org.eclipse.rdf4j.model.Resource) ArrayList(java.util.ArrayList) Value(org.eclipse.rdf4j.model.Value) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Example 37 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class RDFXMLParser method getPropertyResource.

/**
 * Retrieves the object resource of a property element using relevant attributes (rdf:resource and
 * rdf:nodeID) from its attributes list.
 *
 * @return a resource or a bNode.
 */
private Resource getPropertyResource(Atts atts) throws RDFParseException {
    Att resource = atts.removeAtt(RDF.NAMESPACE, "resource");
    Att nodeID = atts.removeAtt(RDF.NAMESPACE, "nodeID");
    if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
        int definedAttsCount = 0;
        if (resource != null) {
            definedAttsCount++;
        }
        if (nodeID != null) {
            definedAttsCount++;
        }
        if (definedAttsCount > 1) {
            reportError("Only one of the attributes rdf:resource or rdf:nodeID can be used here", XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES);
        }
    }
    Resource result = null;
    if (resource != null) {
        result = resolveURI(resource.getValue());
    } else if (nodeID != null) {
        result = createNode(nodeID.getValue());
    } else {
        // No resource specified, generate a bNode
        result = createNode();
    }
    return result;
}
Also used : Resource(org.eclipse.rdf4j.model.Resource)

Example 38 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class RDFXMLParser method processPropertyElt.

private void processPropertyElt(String namespaceURI, String localName, String qName, Atts atts, boolean isEmptyElt) throws RDFParseException, RDFHandlerException {
    if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
        checkPropertyEltName(namespaceURI, localName, qName, XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES);
    }
    // Get the URI of the property
    IRI propURI = null;
    if (namespaceURI.equals("")) {
        // no namespace URI
        reportError("unqualified property element <" + qName + "> not allowed", XMLParserSettings.FAIL_ON_INVALID_QNAME);
        // Use base URI as namespace:
        propURI = buildResourceFromLocalName(localName);
    } else {
        propURI = createURI(namespaceURI + localName);
    }
    // List expansion rule
    if (propURI.equals(RDF.LI)) {
        NodeElement subject = (NodeElement) peekStack(0);
        propURI = createURI(RDF.NAMESPACE + "_" + subject.getNextLiCounter());
    }
    // Push the property on the stack.
    PropertyElement predicate = new PropertyElement(propURI);
    elementStack.push(predicate);
    // Check if property has a reification ID
    Att id = atts.removeAtt(RDF.NAMESPACE, "ID");
    if (id != null) {
        IRI reifURI = buildURIFromID(id.getValue());
        predicate.setReificationURI(reifURI);
    }
    // Check for presence of rdf:parseType attribute
    Att parseType = atts.removeAtt(RDF.NAMESPACE, "parseType");
    if (parseType != null) {
        if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
            checkNoMoreAtts(atts);
        }
        String parseTypeValue = parseType.getValue();
        if (parseTypeValue.equals("Resource")) {
            Resource objectResource = createNode();
            NodeElement subject = (NodeElement) peekStack(1);
            reportStatement(subject.getResource(), propURI, objectResource);
            if (isEmptyElt) {
                handleReification(objectResource);
            } else {
                NodeElement object = new NodeElement(objectResource);
                object.setIsVolatile(true);
                elementStack.push(object);
            }
        } else if (parseTypeValue.equals("Collection")) {
            if (isEmptyElt) {
                NodeElement subject = (NodeElement) peekStack(1);
                reportStatement(subject.getResource(), propURI, RDF.NIL);
                handleReification(RDF.NIL);
            } else {
                predicate.setParseCollection(true);
            }
        } else {
            // other parseType
            if (!parseTypeValue.equals("Literal")) {
                reportWarning("unknown parseType: " + parseType.getValue());
            }
            if (isEmptyElt) {
                NodeElement subject = (NodeElement) peekStack(1);
                Literal lit = createLiteral("", null, RDF.XMLLITERAL);
                reportStatement(subject.getResource(), propURI, lit);
                handleReification(lit);
            } else {
                // The next string is an rdf:XMLLiteral
                predicate.setDatatype(RDF.XMLLITERAL);
                saxFilter.setParseLiteralMode();
            }
        }
    } else // parseType == null
    if (isEmptyElt) {
        // empty element without an rdf:parseType attribute
        // Note: we handle rdf:datatype attributes here to allow datatyped
        // empty strings in documents. The current spec does have a
        // production rule that matches this, which is likely to be an
        // omission on its part.
        Att datatype = atts.getAtt(RDF.NAMESPACE, "datatype");
        if (atts.size() == 0 || atts.size() == 1 && datatype != null) {
            // element had no attributes, or only the optional
            // rdf:ID and/or rdf:datatype attributes.
            NodeElement subject = (NodeElement) peekStack(1);
            IRI dtURI = null;
            if (datatype != null) {
                dtURI = createURI(datatype.getValue());
            }
            Literal lit = createLiteral("", xmlLang, dtURI);
            reportStatement(subject.getResource(), propURI, lit);
            handleReification(lit);
        } else {
            // Create resource for the statement's object.
            Resource resourceRes = getPropertyResource(atts);
            // All special rdf attributes have been checked/removed.
            if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
                checkRDFAtts(atts);
            }
            NodeElement resourceElt = new NodeElement(resourceRes);
            NodeElement subject = (NodeElement) peekStack(1);
            reportStatement(subject.getResource(), propURI, resourceRes);
            handleReification(resourceRes);
            Att type = atts.removeAtt(RDF.NAMESPACE, "type");
            if (type != null) {
                // rdf:type attribute, value is a URI-reference
                IRI className = resolveURI(type.getValue());
                reportStatement(resourceRes, RDF.TYPE, className);
            }
            processSubjectAtts(resourceElt, atts);
        }
    } else {
        // Not an empty element, sub elements will follow.
        // Check for rdf:datatype attribute
        Att datatype = atts.removeAtt(RDF.NAMESPACE, "datatype");
        if (datatype != null) {
            IRI dtURI = resolveURI(datatype.getValue());
            predicate.setDatatype(dtURI);
        }
        // No more attributes are expected.
        if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
            checkNoMoreAtts(atts);
        }
    }
    if (isEmptyElt) {
        // Empty element has been pushed on the stack
        // at the start of this method, remove it.
        elementStack.pop();
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Literal(org.eclipse.rdf4j.model.Literal) Resource(org.eclipse.rdf4j.model.Resource)

Example 39 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class RDFXMLParser method getNodeResource.

/**
 * Retrieves the resource of a node element (subject or object) using relevant attributes (rdf:ID,
 * rdf:about and rdf:nodeID) from its attributes list.
 *
 * @return a resource or a bNode.
 */
private Resource getNodeResource(Atts atts) throws RDFParseException {
    Att id = atts.removeAtt(RDF.NAMESPACE, "ID");
    Att about = atts.removeAtt(RDF.NAMESPACE, "about");
    Att nodeID = atts.removeAtt(RDF.NAMESPACE, "nodeID");
    if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
        int definedAttsCount = 0;
        if (id != null) {
            definedAttsCount++;
        }
        if (about != null) {
            definedAttsCount++;
        }
        if (nodeID != null) {
            definedAttsCount++;
        }
        if (definedAttsCount > 1) {
            reportError("Only one of the attributes rdf:ID, rdf:about or rdf:nodeID can be used here", XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES);
        }
    }
    Resource result = null;
    if (id != null) {
        result = buildURIFromID(id.getValue());
    } else if (about != null) {
        result = resolveURI(about.getValue());
    } else if (nodeID != null) {
        result = createNode(nodeID.getValue());
    } else {
        // No resource specified, generate a bNode
        result = createNode();
    }
    return result;
}
Also used : Resource(org.eclipse.rdf4j.model.Resource)

Example 40 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class RDFXMLParser method endElement.

void endElement(String namespaceURI, String localName, String qName) throws RDFParseException, RDFHandlerException {
    Object topElement = peekStack(0);
    if (topElement instanceof NodeElement) {
        // start- and end element associated with it.
        if (((NodeElement) topElement).isVolatile()) {
            elementStack.pop();
        }
    } else {
        // topElement instanceof PropertyElement
        PropertyElement predicate = (PropertyElement) topElement;
        if (predicate.parseCollection()) {
            Resource lastListResource = predicate.getLastListResource();
            if (lastListResource == null) {
                // no last list resource, list must have been empty.
                NodeElement subject = (NodeElement) peekStack(1);
                reportStatement(subject.getResource(), predicate.getURI(), RDF.NIL);
                handleReification(RDF.NIL);
            } else {
                // Generate the final tail of the list.
                reportStatement(lastListResource, RDF.REST, RDF.NIL);
            }
        }
    }
    elementStack.pop();
}
Also used : Resource(org.eclipse.rdf4j.model.Resource)

Aggregations

Resource (org.eclipse.rdf4j.model.Resource)90 IRI (org.eclipse.rdf4j.model.IRI)37 Value (org.eclipse.rdf4j.model.Value)30 Test (org.junit.Test)16 Statement (org.eclipse.rdf4j.model.Statement)15 Model (org.eclipse.rdf4j.model.Model)12 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)12 BNode (org.eclipse.rdf4j.model.BNode)11 IOException (java.io.IOException)9 Literal (org.eclipse.rdf4j.model.Literal)9 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)7 StringWriter (java.io.StringWriter)6 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)6 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)5 ArrayList (java.util.ArrayList)4