use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class RDFXMLParser method processNodeElt.
/*------------------------*
* RDF processing methods *
*------------------------*/
/* Process a node element (can be both subject and object) */
private void processNodeElt(String namespaceURI, String localName, String qName, Atts atts, boolean isEmptyElt) throws RDFParseException, RDFHandlerException {
if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
// Check the element name
checkNodeEltName(namespaceURI, localName, qName);
}
Resource nodeResource = getNodeResource(atts);
NodeElement nodeElement = new NodeElement(nodeResource);
if (!elementStack.isEmpty()) {
// node can be object of a statement, or part of an rdf:List
NodeElement subject = (NodeElement) peekStack(1);
PropertyElement predicate = (PropertyElement) peekStack(0);
if (predicate.parseCollection()) {
Resource lastListRes = predicate.getLastListResource();
Resource newListRes = createNode();
if (lastListRes == null) {
// first element in the list
reportStatement(subject.getResource(), predicate.getURI(), newListRes);
handleReification(newListRes);
} else {
// not the first element in the list
reportStatement(lastListRes, RDF.REST, newListRes);
}
reportStatement(newListRes, RDF.FIRST, nodeResource);
predicate.setLastListResource(newListRes);
} else {
reportStatement(subject.getResource(), predicate.getURI(), nodeResource);
handleReification(nodeResource);
}
}
if (!localName.equals("Description") || !namespaceURI.equals(RDF.NAMESPACE)) {
// element name is uri's type
IRI className = null;
if ("".equals(namespaceURI)) {
// No namespace, use base URI
className = buildResourceFromLocalName(localName);
} else {
className = createURI(namespaceURI + localName);
}
reportStatement(nodeResource, RDF.TYPE, className);
}
Att type = atts.removeAtt(RDF.NAMESPACE, "type");
if (type != null) {
// rdf:type attribute, value is a URI-reference
IRI className = resolveURI(type.getValue());
reportStatement(nodeResource, RDF.TYPE, className);
}
if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
checkRDFAtts(atts);
}
processSubjectAtts(nodeElement, atts);
if (!isEmptyElt) {
elementStack.push(nodeElement);
}
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class RDFJSONWriter method modelToRdfJsonInternal.
public static void modelToRdfJsonInternal(final Model graph, final WriterConfig writerConfig, final JsonGenerator jg) throws IOException, JsonGenerationException {
if (writerConfig.get(BasicWriterSettings.PRETTY_PRINT)) {
// SES-2011: Always use \n for consistency
Indenter indenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
// By default Jackson does not pretty print, so enable this unless
// PRETTY_PRINT setting is disabled
DefaultPrettyPrinter pp = new DefaultPrettyPrinter().withArrayIndenter(indenter).withObjectIndenter(indenter);
jg.setPrettyPrinter(pp);
}
jg.writeStartObject();
for (final Resource nextSubject : graph.subjects()) {
jg.writeObjectFieldStart(RDFJSONWriter.resourceToString(nextSubject));
for (final IRI nextPredicate : graph.filter(nextSubject, null, null).predicates()) {
jg.writeArrayFieldStart(nextPredicate.stringValue());
for (final Value nextObject : graph.filter(nextSubject, nextPredicate, null).objects()) {
// contexts are optional, so this may return empty in some
// scenarios depending on the interpretation of the way contexts
// work
final Set<Resource> contexts = graph.filter(nextSubject, nextPredicate, nextObject).contexts();
RDFJSONWriter.writeObject(nextObject, contexts, jg);
}
jg.writeEndArray();
}
jg.writeEndObject();
}
jg.writeEndObject();
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class RDFJSONWriter method writeObject.
/**
* Helper method to reduce complexity of the JSON serialisation algorithm Any null contexts will only be
* serialised to JSON if there are also non-null contexts in the contexts array
*
* @param object
* The RDF value to serialise
* @param contexts
* The set of contexts that are relevant to this object, including null contexts as they are found.
* @param jg
* the {@link JsonGenerator} to write to.
* @throws IOException
* @throws JsonGenerationException
* @throws JSONException
*/
public static void writeObject(final Value object, final Set<Resource> contexts, final JsonGenerator jg) throws JsonGenerationException, IOException {
jg.writeStartObject();
if (object instanceof Literal) {
jg.writeObjectField(RDFJSONUtility.VALUE, object.stringValue());
jg.writeObjectField(RDFJSONUtility.TYPE, RDFJSONUtility.LITERAL);
final Literal l = (Literal) object;
if (Literals.isLanguageLiteral(l)) {
jg.writeObjectField(RDFJSONUtility.LANG, l.getLanguage().orElse(null));
} else {
jg.writeObjectField(RDFJSONUtility.DATATYPE, l.getDatatype().stringValue());
}
} else if (object instanceof BNode) {
jg.writeObjectField(RDFJSONUtility.VALUE, resourceToString((BNode) object));
jg.writeObjectField(RDFJSONUtility.TYPE, RDFJSONUtility.BNODE);
} else if (object instanceof IRI) {
jg.writeObjectField(RDFJSONUtility.VALUE, resourceToString((IRI) object));
jg.writeObjectField(RDFJSONUtility.TYPE, RDFJSONUtility.URI);
}
if (contexts != null && !contexts.isEmpty() && !(contexts.size() == 1 && contexts.iterator().next() == null)) {
jg.writeArrayFieldStart(RDFJSONUtility.GRAPHS);
for (final Resource nextContext : contexts) {
if (nextContext == null) {
jg.writeNull();
} else {
jg.writeString(resourceToString(nextContext));
}
}
jg.writeEndArray();
}
jg.writeEndObject();
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class TriGParser method parseGraph.
protected void parseGraph() throws IOException, RDFParseException, RDFHandlerException {
int c = readCodePoint();
int c2 = peekCodePoint();
Resource contextOrSubject = null;
boolean foundContextOrSubject = false;
if (c == '[') {
skipWSC();
c2 = readCodePoint();
if (c2 == ']') {
contextOrSubject = createNode();
foundContextOrSubject = true;
skipWSC();
} else {
unread(c2);
unread(c);
}
c = readCodePoint();
} else if (c == '<' || TurtleUtil.isPrefixStartChar(c) || (c == ':' && c2 != '-') || (c == '_' && c2 == ':')) {
unread(c);
Value value = parseValue();
if (value instanceof Resource) {
contextOrSubject = (Resource) value;
foundContextOrSubject = true;
} else {
// NOTE: If a user parses Turtle using TriG, then the following
// could actually be "Illegal subject name", but it should still
// hold
reportFatalError("Illegal graph name: " + value);
}
skipWSC();
c = readCodePoint();
} else {
setContext(null);
}
if (c == '{') {
setContext(contextOrSubject);
c = skipWSC();
if (c != '}') {
parseTriples();
c = skipWSC();
while (c == '.') {
readCodePoint();
c = skipWSC();
if (c == '}') {
break;
}
parseTriples();
c = skipWSC();
}
verifyCharacterOrFail(c, "}");
}
} else {
setContext(null);
// parse from here to triples
if (foundContextOrSubject) {
subject = contextOrSubject;
unread(c);
parsePredicateObjectList();
} else // Or if we didn't recognise anything, just parse as Turtle
{
unread(c);
parseTriples();
}
}
readCodePoint();
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class JSONLDInternalRDFParser method handleStatement.
public void handleStatement(RDFDataset result, Statement nextStatement) {
// TODO: from a basic look at the code it seems some of these could be
// null
// null values for IRIs will probably break things further down the line
// and i'm not sure yet if this should be something handled later on, or
// something that should be checked here
final String subject = getResourceValue(nextStatement.getSubject());
final String predicate = getResourceValue(nextStatement.getPredicate());
final Value object = nextStatement.getObject();
final String graphName = getResourceValue(nextStatement.getContext());
if (object instanceof Literal) {
final Literal literal = (Literal) object;
final String value = literal.getLabel();
String datatype = getResourceValue(literal.getDatatype());
// rdf:langString
if (literal.getLanguage().isPresent() && datatype == null) {
datatype = RDF.LANGSTRING.stringValue();
}
// type xsd:String
if (!literal.getLanguage().isPresent() && datatype == null) {
datatype = XMLSchema.STRING.stringValue();
}
result.addQuad(subject, predicate, value, datatype, literal.getLanguage().orElse(null), graphName);
} else {
result.addQuad(subject, predicate, getResourceValue((Resource) object), graphName);
}
}
Aggregations