use of org.eclipse.rdf4j.rio.RDFParseException in project rdf4j by eclipse.
the class TestTurtleParser method testParseBooleanLiteralWhitespaceComma.
@Test
public void testParseBooleanLiteralWhitespaceComma() throws Exception {
String data = "<urn:a> <urn:b> true , false .";
Reader r = new StringReader(data);
try {
parser.parse(r, baseURI);
assertTrue(statementCollector.getStatements().size() == 2);
} catch (RDFParseException e) {
fail("parse error on correct data: " + e.getMessage());
}
}
use of org.eclipse.rdf4j.rio.RDFParseException in project rdf4j by eclipse.
the class TestTurtleParser method testParseBooleanLiteralSemicolumn.
@Test
public void testParseBooleanLiteralSemicolumn() throws Exception {
String data = "<urn:a> <urn:b> true; <urn:c> false .";
Reader r = new StringReader(data);
try {
parser.parse(r, baseURI);
assertTrue(statementCollector.getStatements().size() == 2);
} catch (RDFParseException e) {
fail("parse error on correct data: " + e.getMessage());
}
}
use of org.eclipse.rdf4j.rio.RDFParseException in project rdf4j by eclipse.
the class TestTurtleParser method testParseIllegalURIFatal.
@Test
public void testParseIllegalURIFatal() throws Exception {
String data = " <urn:foo_bar\\r> <urn:foo> <urn:bar> ; <urn:foo2> <urn:bar2> . <urn:foobar> <urn:food> <urn:barf> . ";
try {
parser.parse(new StringReader(data), baseURI);
fail("default config should result in fatal error / parse exception");
} catch (RDFParseException e) {
// expected
}
}
use of org.eclipse.rdf4j.rio.RDFParseException in project rdf4j by eclipse.
the class RDFXMLParser method parse.
private void parse(InputSource inputSource) throws IOException, RDFParseException, RDFHandlerException {
clear();
try {
documentURI = inputSource.getSystemId();
saxFilter.setParseStandAloneDocuments(getParserConfig().get(XMLParserSettings.PARSE_STANDALONE_DOCUMENTS));
// saxFilter.clear();
saxFilter.setDocumentURI(documentURI);
XMLReader xmlReader;
if (getParserConfig().isSet(XMLParserSettings.CUSTOM_XML_READER)) {
xmlReader = getParserConfig().get(XMLParserSettings.CUSTOM_XML_READER);
} else {
xmlReader = XMLReaderFactory.createXMLReader();
}
xmlReader.setContentHandler(saxFilter);
xmlReader.setErrorHandler(this);
// not explicitly set
for (RioSetting<Boolean> aSetting : getCompulsoryXmlFeatureSettings()) {
try {
xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting));
} catch (SAXNotRecognizedException e) {
reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey()));
} catch (SAXNotSupportedException e) {
reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey()));
}
}
// not explicitly set
for (RioSetting<?> aSetting : getCompulsoryXmlPropertySettings()) {
try {
xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting));
} catch (SAXNotRecognizedException e) {
reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey()));
} catch (SAXNotSupportedException e) {
reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey()));
}
}
// the parser config
for (RioSetting<Boolean> aSetting : getOptionalXmlFeatureSettings()) {
try {
if (getParserConfig().isSet(aSetting)) {
xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting));
}
} catch (SAXNotRecognizedException e) {
reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey()));
} catch (SAXNotSupportedException e) {
reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey()));
}
}
// the parser config
for (RioSetting<?> aSetting : getOptionalXmlPropertySettings()) {
try {
if (getParserConfig().isSet(aSetting)) {
xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting));
}
} catch (SAXNotRecognizedException e) {
reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey()));
} catch (SAXNotSupportedException e) {
reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey()));
}
}
xmlReader.parse(inputSource);
} catch (SAXParseException e) {
Exception wrappedExc = e.getException();
if (wrappedExc == null) {
reportFatalError(e, e.getLineNumber(), e.getColumnNumber());
} else {
reportFatalError(wrappedExc, e.getLineNumber(), e.getColumnNumber());
}
} catch (SAXException e) {
Exception wrappedExc = e.getException();
if (wrappedExc == null) {
reportFatalError(e);
} else if (wrappedExc instanceof RDFParseException) {
throw (RDFParseException) wrappedExc;
} else if (wrappedExc instanceof RDFHandlerException) {
throw (RDFHandlerException) wrappedExc;
} else {
reportFatalError(wrappedExc);
}
} finally {
// Clean up
saxFilter.clear();
xmlLang = null;
elementStack.clear();
usedIDs.clear();
clear();
}
}
use of org.eclipse.rdf4j.rio.RDFParseException in project rdf4j by eclipse.
the class SAXFilter method endElement.
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
try {
if (rdfParser.getParserConfig().get(XMLParserSettings.FAIL_ON_MISMATCHED_TAGS) && !parseLiteralMode) {
// Verify that the end tag matches the start tag.
ElementInfo elInfo;
if (deferredElement != null) {
elInfo = deferredElement;
} else {
elInfo = peekStack();
}
if (!qName.equals(elInfo.qName)) {
rdfParser.reportError("expected end tag </'" + elInfo.qName + ">, found </" + qName + ">", XMLParserSettings.FAIL_ON_MISMATCHED_TAGS);
}
}
if (!inRDFContext) {
elInfoStack.pop();
charBuf.setLength(0);
return;
}
if (deferredElement == null && rdfContextStackHeight == 0) {
// This end tag removes the element that signaled the start
// of the RDF context (i.e. <rdf:RDF>) from the stack.
inRDFContext = false;
elInfoStack.pop();
charBuf.setLength(0);
return;
}
if (parseLiteralMode && xmlLiteralStackHeight > 0) {
appendEndTag(qName);
xmlLiteralStackHeight--;
return;
}
// Check for any deferred start elements
if (deferredElement != null) {
// Start element still deferred, this is an empty element
rdfParser.setBaseURI(deferredElement.baseURI.toString());
rdfParser.setXMLLang(deferredElement.xmlLang);
rdfParser.emptyElement(deferredElement.namespaceURI, deferredElement.localName, deferredElement.qName, deferredElement.atts);
deferredElement = null;
} else {
if (parseLiteralMode) {
// Insert any used namespace prefixes from the XML literal's
// context that are not defined in the XML literal itself.
insertUsedContextPrefixes();
rdfParser.text(charBuf.toString());
parseLiteralMode = false;
} else {
String s = charBuf.toString();
// ignore whitespace-only nodes
if (s.trim().length() > 0) {
rdfParser.text(s);
}
}
charBuf.setLength(0);
// Handle the end tag
elInfoStack.pop();
rdfContextStackHeight--;
rdfParser.endElement(namespaceURI, localName, qName);
}
} catch (RDFParseException e) {
throw new SAXException(e);
} catch (RDFHandlerException e) {
throw new SAXException(e);
}
}
Aggregations