in jena-arq/src/main/java/org/apache/jena/riot/lang/ReaderTriX.java [132:312]
private void read(XMLStreamReader parser, String baseURI, StreamRDF output) {
State state = OUTER;
Node g = null;
Deque<Node> terms = new ArrayDeque<>();
// Depth of triple term nesting. 0 - not in a triple term
int tripleTermDepth = 0;
try {
while(parser.hasNext()) {
int event = parser.next();
switch (event) {
case XMLStreamConstants.NAMESPACE:
break;
case XMLStreamConstants.START_DOCUMENT :
break;
case XMLStreamConstants.END_DOCUMENT :
if ( state != OUTER )
staxError(parser.getLocation(), "End of document while processing XML element");
return;
case XMLStreamConstants.START_ELEMENT : {
String tag = parser.getLocalName();
switch (tag) {
case TriX.tagTriX:
case TriX.tagTriXAlt:
if ( state != OUTER )
staxErrorOutOfPlaceElement(parser);
state = TRIX;
break;
// structure
case TriX.tagGraph:
if ( state != TRIX )
staxErrorOutOfPlaceElement(parser);
// URI?
state = GRAPH;
break;
case TriX.tagTriple: {
switch (state) {
// Asserted triple
case GRAPH :
state = TRIPLE;
push(terms, nAssertedTriple);
break;
// If TRIPLE_TERM, nested triple term
case TRIPLE:
state = TRIPLE_TERM;
// fall through.
case TRIPLE_TERM:
tripleTermDepth++;
push(terms, nTripleTerm);
break;
default: staxErrorOutOfPlaceElement(parser);
}
break;
}
// Can occur in <graph> and <triple>
case TriX.tagId:
case TriX.tagQName:
case TriX.tagURI: {
if ( state != GRAPH && state != TRIPLE && state != TRIPLE_TERM )
staxErrorOutOfPlaceElement(parser);
Node n = term(parser, profile);
if ( state == GRAPH ) {
if ( g != null )
staxError(parser.getLocation(), "Duplicate graph name");
g = n;
if ( g.isLiteral() )
staxError(parser.getLocation(), "graph name is a literal");
}
else
// state TRIPLE
push(terms, n);
break;
}
case TriX.tagPlainLiteral:
case TriX.tagTypedLiteral: {
if ( state != TRIPLE )
staxErrorOutOfPlaceElement(parser);
Node n = term(parser, profile);
push(terms, n);
break;
}
default:
staxError(parser.getLocation(), "Unrecognized XML element: "+qnameAsString(parser.getName()));
break;
}
break;
}
case XMLStreamConstants.END_ELEMENT : {
String tag = parser.getLocalName();
switch(tag) {
case TriX.tagTriple: {
int line = parser.getLocation().getLineNumber();
int col = parser.getLocation().getColumnNumber();
try {
// Reverse order
Node o = pop(terms);
if ( o instanceof NodeMarker )
staxError(parser.getLocation(), "Too few terms (zero) for a triple.");
Node p = pop(terms);
if ( p instanceof NodeMarker )
staxError(parser.getLocation(), "Too few terms (one) for a triple.");
Node s = pop(terms);
if ( s instanceof NodeMarker )
staxError(parser.getLocation(), "Too few terms (two) for a triple.");
Node marker = pop(terms);
if ( ! ( marker instanceof NodeMarker ) )
staxError(parser.getLocation(), "Too many terms for a triple.");
if ( p.isLiteral() )
staxError(parser.getLocation(), "Predicate is a literal");
if ( s.isLiteral() )
staxError(parser.getLocation(), "Subject is a literal");
switch(state) {
case TRIPLE: {
if ( ! nAssertedTriple.equals(marker) )
// Internal error.
staxError(parser.getLocation(), "Misaligned triple");
//System.out.println("Asserted: Terms: "+terms.size());
// Emit asserted quad or triple.
if ( g == null ) {
Triple t = profile.createTriple(s, p, o, line, col);
output.triple(t);
}
else {
if ( g.isLiteral() )
staxError(parser.getLocation(), "graph name is a literal");
Quad q = profile.createQuad(g, s, p, o, line, col);
output.quad(q);
}
// Next is either end of <graph> or another <triple>
state = GRAPH;
break;
}
case TRIPLE_TERM: {
if ( ! nTripleTerm.equals(marker) )
// Internal error.
staxError(parser.getLocation(), "Misaligned triple term.");
// Triple term.
Node nt = profile.createTripleTerm(s, p, o, line, col);
push(terms, nt);
tripleTermDepth--;
if ( tripleTermDepth == 0 )
state = TRIPLE;
break;
}
default:
staxError(parser.getLocation(), "Internal error");
}
break;
} catch (NoSuchElementException ex) {
staxError(parser.getLocation(), "Too few terms for a triple.");
}
}
case TriX.tagGraph:
state = TRIX;
g = null;
break;
case TriX.tagTriX:
case TriX.tagTriXAlt:
// We don't worry about mismatched tags.
state = OUTER;
break;
}
break;
}
}
}
staxError("Premature end of file");
return ;
} catch (XMLStreamException ex) {
staxError(parser.getLocation(), "XML error: "+ex.getMessage());
}
}