use of org.eclipse.rdf4j.rio.RDFHandlerException in project rdf4j by eclipse.
the class BinaryRDFWriter method handleNamespace.
public void handleNamespace(String prefix, String uri) throws RDFHandlerException {
startRDF();
try {
out.writeByte(NAMESPACE_DECL);
writeString(prefix);
writeString(uri);
} catch (IOException e) {
throw new RDFHandlerException(e);
}
}
use of org.eclipse.rdf4j.rio.RDFHandlerException in project rdf4j by eclipse.
the class BinaryRDFWriter method endRDF.
public void endRDF() throws RDFHandlerException {
startRDF();
try {
while (!statementQueue.isEmpty()) {
writeStatement();
}
out.writeByte(END_OF_DATA);
out.flush();
writingStarted = false;
} catch (IOException e) {
throw new RDFHandlerException(e);
}
}
use of org.eclipse.rdf4j.rio.RDFHandlerException in project rdf4j by eclipse.
the class TurtleWriter method handleNamespace.
@Override
public void handleNamespace(String prefix, String name) throws RDFHandlerException {
try {
if (!namespaceTable.containsKey(name)) {
// Namespace not yet mapped to a prefix, try to give it the
// specified prefix
boolean isLegalPrefix = prefix.length() == 0 || TurtleUtil.isPN_PREFIX(prefix);
if (!isLegalPrefix || namespaceTable.containsValue(prefix)) {
if (prefix.length() == 0 || !isLegalPrefix) {
prefix = "ns";
}
int number = 1;
while (namespaceTable.containsValue(prefix + number)) {
number++;
}
prefix += number;
}
namespaceTable.put(name, prefix);
if (writingStarted) {
closePreviousStatement();
writeNamespace(prefix, name);
}
}
} catch (IOException e) {
throw new RDFHandlerException(e);
}
}
use of org.eclipse.rdf4j.rio.RDFHandlerException in project rdf4j by eclipse.
the class TurtleWriter method handleStatement.
@Override
public void handleStatement(Statement st) throws RDFHandlerException {
if (!writingStarted) {
throw new RuntimeException("Document writing has not yet been started");
}
try {
Resource subj = st.getSubject();
IRI pred = st.getPredicate();
if (inlineBNodes && (pred.equals(RDF.FIRST) || pred.equals(RDF.REST))) {
handleList(st);
} else if (inlineBNodes && !subj.equals(lastWrittenSubject) && stack.contains(subj)) {
handleInlineNode(st);
} else {
closeHangingResource();
handleStatementInternal(st, false, inlineBNodes, inlineBNodes);
}
} catch (IOException e) {
throw new RDFHandlerException(e);
}
}
use of org.eclipse.rdf4j.rio.RDFHandlerException in project rdf4j by eclipse.
the class TurtleWriter method handleStatementInternal.
/**
* Internal method that differentiates between the pretty-print and streaming writer cases.
*
* @param st
* The next statement to write
* @param endRDFCalled
* True if endRDF has been called before this method is called. This is used to buffer statements
* for pretty-printing before dumping them when all statements have been delivered to us.
* @param canShortenSubjectBNode
* True if, in the current context, we may be able to shorten the subject of this statement iff it
* is an instance of {@link BNode}.
* @param canShortenObjectBNode
* True if, in the current context, we may be able to shorten the object of this statement iff it
* is an instance of {@link BNode}.
*/
protected void handleStatementInternal(Statement st, boolean endRDFCalled, boolean canShortenSubjectBNode, boolean canShortenObjectBNode) {
Resource subj = st.getSubject();
IRI pred = st.getPredicate();
Value obj = st.getObject();
try {
if (subj.equals(lastWrittenSubject)) {
if (pred.equals(lastWrittenPredicate)) {
// Identical subject and predicate
writer.write(",");
wrapLine(prettyPrint);
} else {
// Identical subject, new predicate
writer.write(";");
writer.writeEOL();
// Write new predicate
writer.decreaseIndentation();
writePredicate(pred);
writer.increaseIndentation();
wrapLine(true);
path.removeLast();
path.addLast(pred);
lastWrittenPredicate = pred;
}
} else {
// New subject
closePreviousStatement();
stack.addLast(subj);
// Write new subject:
if (prettyPrint) {
writer.writeEOL();
}
writeResource(subj, canShortenSubjectBNode);
wrapLine(true);
writer.increaseIndentation();
lastWrittenSubject = subj;
// Write new predicate
writePredicate(pred);
wrapLine(true);
path.addLast(pred);
lastWrittenPredicate = pred;
statementClosed = false;
writer.increaseIndentation();
}
writeValue(obj, canShortenObjectBNode);
// Don't close the line just yet. Maybe the next
// statement has the same subject and/or predicate.
} catch (IOException e) {
throw new RDFHandlerException(e);
}
}
Aggregations