use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class TriGWriter method handleStatementInternal.
protected void handleStatementInternal(Statement st, boolean endRDFCalled, boolean canShortenSubject, boolean canShortenObject) {
// them if they are sent here
if (prettyPrintModel != null && !endRDFCalled) {
prettyPrintModel.add(st);
return;
}
try {
Resource context = st.getContext();
if (inActiveContext && !contextsEquals(context, currentContext)) {
closePreviousStatement();
closeActiveContext();
}
if (!inActiveContext) {
writer.writeEOL();
if (context != null) {
boolean canShortenContext = false;
if (context instanceof BNode) {
if (prettyPrintModel != null && !prettyPrintModel.contains(context, null, null) && !prettyPrintModel.contains(null, null, context)) {
canShortenContext = true;
}
}
writeResource(context, canShortenContext);
writer.write(" ");
}
writer.write("{");
writer.increaseIndentation();
currentContext = context;
inActiveContext = true;
}
} catch (IOException e) {
throw new RDFHandlerException(e);
}
// If we get to this point, switch endRDFCalled to true so writing occurs
super.handleStatementInternal(st, true, canShortenSubject, canShortenObject);
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class RDFXMLPrettyWriter method handleStatement.
@Override
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();
try {
if (!headerWritten) {
writeHeader();
}
if (!nodeStack.isEmpty() && !subj.equals(nodeStack.peek().getValue())) {
// Different subject than we had before, empty the stack
// until we find it
popStacks(subj);
}
if (nodeStack.isEmpty()) {
// Push subject
nodeStack.push(new Node(subj));
}
// Stack now contains at least one element
Node topSubject = nodeStack.peek();
// for the type URI
if (pred.equals(RDF.TYPE) && obj instanceof IRI && !topSubject.hasType() && !topSubject.isWritten()) {
// Use typed node element
topSubject.setType((IRI) obj);
} else {
if (!nodeStack.isEmpty() && pred.equals(nodeStack.peek().nextLi())) {
pred = RDF.LI;
nodeStack.peek().incrementNextLi();
}
// Push predicate and object
predicateStack.push(pred);
nodeStack.push(new Node(obj));
}
} catch (IOException e) {
throw new RDFHandlerException(e);
}
}
use of org.eclipse.rdf4j.model.Resource 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();
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class TurtleParser method parseCollection.
/**
* Parses a collection, e.g. <tt>( item1 item2 item3 )</tt>.
*/
protected Resource parseCollection() throws IOException, RDFParseException, RDFHandlerException {
verifyCharacterOrFail(readCodePoint(), "(");
int c = skipWSC();
if (c == ')') {
// Empty list
readCodePoint();
if (subject != null) {
reportStatement(subject, predicate, RDF.NIL);
}
return RDF.NIL;
} else {
Resource listRoot = createNode();
if (subject != null) {
reportStatement(subject, predicate, listRoot);
}
// Remember current subject and predicate
Resource oldSubject = subject;
IRI oldPredicate = predicate;
// generated bNode becomes subject, predicate becomes rdf:first
subject = listRoot;
predicate = RDF.FIRST;
parseObject();
Resource bNode = listRoot;
while (skipWSC() != ')') {
// Create another list node and link it to the previous
Resource newNode = createNode();
reportStatement(bNode, RDF.REST, newNode);
// New node becomes the current
subject = bNode = newNode;
parseObject();
}
// Skip ')'
readCodePoint();
// Close the list
reportStatement(bNode, RDF.REST, RDF.NIL);
// Restore previous subject and predicate
subject = oldSubject;
predicate = oldPredicate;
return listRoot;
}
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class BinaryRDFParser method readStatement.
private void readStatement() throws RDFParseException, IOException, RDFHandlerException {
Value v = readValue();
Resource subj = null;
if (v instanceof Resource) {
subj = (Resource) v;
} else {
reportFatalError("Invalid subject type: " + v);
}
v = readValue();
IRI pred = null;
if (v instanceof IRI) {
pred = (IRI) v;
} else {
reportFatalError("Invalid predicate type: " + v);
}
Value obj = readValue();
if (obj == null) {
reportFatalError("Invalid object type: null");
}
v = readValue();
Resource context = null;
if (v == null || v instanceof Resource) {
context = (Resource) v;
} else {
reportFatalError("Invalid context type: " + v);
}
Statement st = createStatement(subj, pred, obj, context);
if (rdfHandler != null) {
rdfHandler.handleStatement(st);
}
}
Aggregations