Search in sources :

Example 91 with Literal

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

the class RDFXMLParser method processSubjectAtts.

/**
 * processes subject attributes.
 */
private void processSubjectAtts(NodeElement nodeElt, Atts atts) throws RDFParseException, RDFHandlerException {
    Resource subject = nodeElt.getResource();
    Iterator<Att> iter = atts.iterator();
    while (iter.hasNext()) {
        Att att = iter.next();
        IRI predicate = createURI(att.getURI());
        Literal lit = createLiteral(att.getValue(), xmlLang, null);
        reportStatement(subject, predicate, lit);
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Literal(org.eclipse.rdf4j.model.Literal) Resource(org.eclipse.rdf4j.model.Resource)

Example 92 with Literal

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

the class RDFXMLWriter method handleStatement.

public void handleStatement(Statement st) throws RDFHandlerException {
    if (!writingStarted) {
        throw new RDFHandlerException("Document writing has not yet been started");
    }
    Resource subj = st.getSubject();
    IRI pred = st.getPredicate();
    Value obj = st.getObject();
    // Verify that an XML namespace-qualified name can be created for the
    // predicate
    String predString = pred.toString();
    int predSplitIdx = XMLUtil.findURISplitIndex(predString);
    if (predSplitIdx == -1) {
        throw new RDFHandlerException("Unable to create XML namespace-qualified name for predicate: " + predString);
    }
    String predNamespace = predString.substring(0, predSplitIdx);
    String predLocalName = predString.substring(predSplitIdx);
    try {
        if (!headerWritten) {
            writeHeader();
        }
        // SUBJECT
        if (!subj.equals(lastWrittenSubject)) {
            flushPendingStatements();
            // Write new subject:
            writeNewLine();
            writeStartOfStartTag(RDF.NAMESPACE, "Description");
            if (subj instanceof BNode) {
                BNode bNode = (BNode) subj;
                writeAttribute(RDF.NAMESPACE, "nodeID", getValidNodeId(bNode));
            } else if (baseIRI != null) {
                writeAttribute(RDF.NAMESPACE, "about", baseIRI.relativize(subj.stringValue()));
            } else {
                IRI uri = (IRI) subj;
                writeAttribute(RDF.NAMESPACE, "about", uri.toString());
            }
            writeEndOfStartTag();
            writeNewLine();
            lastWrittenSubject = subj;
        }
        // PREDICATE
        writeIndent();
        writeStartOfStartTag(predNamespace, predLocalName);
        // OBJECT
        if (obj instanceof Resource) {
            Resource objRes = (Resource) obj;
            if (objRes instanceof BNode) {
                BNode bNode = (BNode) objRes;
                writeAttribute(RDF.NAMESPACE, "nodeID", getValidNodeId(bNode));
            } else if (baseIRI != null) {
                writeAttribute(RDF.NAMESPACE, "resource", baseIRI.relativize(objRes.stringValue()));
            } else {
                IRI uri = (IRI) objRes;
                writeAttribute(RDF.NAMESPACE, "resource", uri.toString());
            }
            writeEndOfEmptyTag();
        } else if (obj instanceof Literal) {
            Literal objLit = (Literal) obj;
            // datatype attribute
            boolean isXMLLiteral = false;
            // language attribute
            if (Literals.isLanguageLiteral(objLit)) {
                writeAttribute("xml:lang", objLit.getLanguage().get());
            } else {
                IRI datatype = objLit.getDatatype();
                // Check if datatype is rdf:XMLLiteral
                isXMLLiteral = datatype.equals(RDF.XMLLITERAL);
                if (isXMLLiteral) {
                    writeAttribute(RDF.NAMESPACE, "parseType", "Literal");
                } else if (!datatype.equals(XMLSchema.STRING)) {
                    writeAttribute(RDF.NAMESPACE, "datatype", datatype.toString());
                }
            }
            writeEndOfStartTag();
            // label
            if (isXMLLiteral) {
                // Write XML literal as plain XML
                writer.write(objLit.getLabel());
            } else {
                writeCharacterData(objLit.getLabel());
            }
            writeEndTag(predNamespace, predLocalName);
        }
        writeNewLine();
    // Don't write </rdf:Description> yet, maybe the next statement
    // has the same subject.
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : ParsedIRI(org.eclipse.rdf4j.common.net.ParsedIRI) IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Literal(org.eclipse.rdf4j.model.Literal) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) IOException(java.io.IOException)

Example 93 with Literal

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

the class RDFXMLPrettyWriter method writeAbbreviatedPredicate.

/**
 * Write out an empty property element.
 */
private void writeAbbreviatedPredicate(IRI pred, Value obj) throws IOException, RDFHandlerException {
    writeStartOfStartTag(pred.getNamespace(), pred.getLocalName());
    if (obj instanceof Resource) {
        Resource objRes = (Resource) obj;
        if (objRes instanceof IRI) {
            IRI uri = (IRI) objRes;
            writeAttribute(RDF.NAMESPACE, "resource", uri.toString());
        } else {
            BNode bNode = (BNode) objRes;
            writeAttribute(RDF.NAMESPACE, "nodeID", getValidNodeId(bNode));
        }
        writeEndOfEmptyTag();
    } else if (obj instanceof Literal) {
        Literal objLit = (Literal) obj;
        // datatype attribute
        IRI datatype = objLit.getDatatype();
        // Check if datatype is rdf:XMLLiteral
        boolean isXmlLiteral = datatype.equals(RDF.XMLLITERAL);
        // language attribute
        if (Literals.isLanguageLiteral(objLit)) {
            writeAttribute("xml:lang", objLit.getLanguage().get());
        } else {
            if (isXmlLiteral) {
                writeAttribute(RDF.NAMESPACE, "parseType", "Literal");
            } else {
                writeAttribute(RDF.NAMESPACE, "datatype", datatype.toString());
            }
        }
        writeEndOfStartTag();
        // label
        if (isXmlLiteral) {
            // Write XML literal as plain XML
            writer.write(objLit.getLabel());
        } else {
            writeCharacterData(objLit.getLabel());
        }
        writeEndTag(pred.getNamespace(), pred.getLocalName());
    }
    writeNewLine();
}
Also used : ParsedIRI(org.eclipse.rdf4j.common.net.ParsedIRI) IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Literal(org.eclipse.rdf4j.model.Literal) Resource(org.eclipse.rdf4j.model.Resource)

Example 94 with Literal

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

the class QueryStringUtilTest method testSimpleLiteral.

@Test
public void testSimpleLiteral() {
    Literal literal = VF.createLiteral("simple \"literal\"");
    assertEquals("\"simple \\\"literal\\\"\"", QueryStringUtil.valueToString(literal));
    assertEquals("\"simple \\\"literal\\\"\"", valueToStringWithStringBuilder(literal));
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) Test(org.junit.Test)

Example 95 with Literal

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

the class RenderUtils method toSPARQL.

/**
 * Append the SPARQL query string rendering of the {@link org.eclipse.rdf4j.model.Value} to the supplied
 * {@link StringBuilder}.
 *
 * @param value
 *        the value to render
 * @param builder
 *        the {@link StringBuilder} to append to
 * @return the original {@link StringBuilder} with the value appended.
 */
public static StringBuilder toSPARQL(Value value, StringBuilder builder) {
    if (value instanceof IRI) {
        IRI aURI = (IRI) value;
        builder.append("<").append(aURI.toString()).append(">");
    } else if (value instanceof BNode) {
        builder.append("_:").append(((BNode) value).getID());
    } else if (value instanceof Literal) {
        Literal aLit = (Literal) value;
        builder.append("\"\"\"").append(escape(aLit.getLabel())).append("\"\"\"");
        if (Literals.isLanguageLiteral(aLit)) {
            aLit.getLanguage().ifPresent(lang -> builder.append("@").append(lang));
        } else {
            builder.append("^^<").append(aLit.getDatatype().toString()).append(">");
        }
    }
    return builder;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Literal(org.eclipse.rdf4j.model.Literal)

Aggregations

Literal (org.eclipse.rdf4j.model.Literal)96 Test (org.junit.Test)52 IRI (org.eclipse.rdf4j.model.IRI)34 Value (org.eclipse.rdf4j.model.Value)17 BNode (org.eclipse.rdf4j.model.BNode)14 Model (org.eclipse.rdf4j.model.Model)13 Statement (org.eclipse.rdf4j.model.Statement)13 Resource (org.eclipse.rdf4j.model.Resource)9 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)7 InputStream (java.io.InputStream)6 Date (java.util.Date)6 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 IOException (java.io.IOException)4 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)4 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)4 AbstractQueryResultIOTupleTest (org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest)4 QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)4 SPARQLResultsJSONParser (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser)4 ArrayList (java.util.ArrayList)3