下面列出了javax.xml.bind.annotation.adapters.HexBinaryAdapter#org.eclipse.rdf4j.rio.RDFParseException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Load a dataset. Note: the repositories are cleared before loading data
*
* @param rep
* @param datasetFile
* @throws RDFParseException
* @throws RepositoryException
* @throws IOException
*/
protected void loadDataSet(Repository rep, String datasetFile)
throws RDFParseException, RepositoryException, IOException {
logger.debug("loading dataset...");
InputStream dataset = SPARQLServiceEvaluationTest.class.getResourceAsStream(datasetFile);
if (dataset == null) {
throw new IllegalArgumentException("Datasetfile " + datasetFile + " not found.");
}
RepositoryConnection con = rep.getConnection();
try {
con.clear();
con.add(dataset, "",
Rio.getParserFormatForFileName(datasetFile).orElseThrow(Rio.unsupportedFormat(datasetFile)));
} finally {
dataset.close();
con.close();
}
logger.debug("dataset loaded.");
}
protected void loadTestData(String dataFile, Resource... contexts)
throws RDFParseException, IOException, SailException {
logger.debug("loading dataset {}", dataFile);
InputStream dataset = this.getClass().getResourceAsStream(dataFile);
SailConnection con = store.getConnection();
try {
con.begin();
for (Statement nextStatement : Rio.parse(dataset, "", RDFFormat.TURTLE, contexts)) {
con.addStatement(nextStatement.getSubject(), nextStatement.getPredicate(), nextStatement.getObject(),
nextStatement.getContext());
}
} finally {
con.commit();
con.close();
dataset.close();
}
logger.debug("dataset loaded.");
}
@Test
public void testWriteSiteLinks() throws RDFHandlerException, IOException,
RDFParseException {
this.sites.setSiteInformation("enwiki", "wikipedia", "en", "mediawiki",
"http://en.wikipedia.org/w/$1",
"http://en.wikipedia.org/wiki/$1");
this.sites.setSiteInformation("dewiki", "wikipedia", "de", "mediawiki",
"http://de.wikipedia.org/w/$1",
"http://de.wikipedia.org/wiki/$1");
Map<String, SiteLink> siteLinks = objectFactory.createSiteLinks();
this.rdfConverter.writeSiteLinks(this.resource, siteLinks);
this.rdfWriter.finish();
Model model = RdfTestHelpers.parseRdf(out.toString());
assertEquals(model, RdfTestHelpers.parseRdf(RdfTestHelpers
.getResourceFromFile("SiteLinks.rdf")));
}
protected int parsePredicate(int c) throws IOException, RDFParseException {
StringBuilder sb = new StringBuilder(100);
// predicate must be an uriref (<foo://bar>)
if (c == '<') {
// predicate is an uriref
c = parseUriRef(c, sb);
predicate = createURI(sb.toString());
} else if (c == -1) {
throwEOFException();
} else {
throw new RDFParseException("Expected '<', found: " + new String(Character.toChars(c)), lineNo, c);
}
return c;
}
/**
* Tests N-Quads parsing with literal and datatype.
*/
@Test
public void testParseBasicLiteralDatatype() throws RDFHandlerException, IOException, RDFParseException {
final ByteArrayInputStream bais = new ByteArrayInputStream(
("<http://www.v/dat/4b2-21> " + "<http://www.w3.org/20/ica#dtend> "
+ "\"2010\"^^<http://www.w3.org/2001/XMLSchema#integer> " + "<http://sin.siteserv.org/def/>.")
.getBytes());
final TestRDFHandler rdfHandler = new TestRDFHandler();
parser.setRDFHandler(rdfHandler);
parser.parse(bais, "http://test.base.uri");
final Statement statement = rdfHandler.getStatements().iterator().next();
Assert.assertEquals("http://www.v/dat/4b2-21", statement.getSubject().stringValue());
Assert.assertEquals("http://www.w3.org/20/ica#dtend", statement.getPredicate().stringValue());
Assert.assertTrue(statement.getObject() instanceof Literal);
Literal object = (Literal) statement.getObject();
Assert.assertEquals("2010", object.stringValue());
Assert.assertFalse(object.getLanguage().isPresent());
Assert.assertEquals("http://www.w3.org/2001/XMLSchema#integer", object.getDatatype().toString());
Assert.assertEquals("http://sin.siteserv.org/def/", statement.getContext().stringValue());
}
@Test
public void testExceptionHandlingWithDefaultSettings() throws Exception {
String data = "invalid nt";
RDFParser ntriplesParser = createRDFParser();
ntriplesParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
Model model = new LinkedHashModel();
ntriplesParser.setRDFHandler(new StatementCollector(model));
try {
ntriplesParser.parse(new StringReader(data), NTRIPLES_TEST_URL);
fail("expected RDFParseException due to invalid data");
} catch (RDFParseException expected) {
assertEquals(expected.getLineNumber(), 1);
}
}
/**
* Tests N-Quads parsing with literal and datatype using a prefix, which is illegal in NQuads, but legal in
* N3/Turtle that may otherwise look like NQuads
*/
@Test
public void testParseBasicLiteralDatatypePrefix() throws RDFHandlerException, IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(
("<http://www.v/dat/4b2-21> " + "<http://www.w3.org/20/ica#dtend> " + "\"2010\"^^xsd:integer "
+ "<http://sin.siteserv.org/def/>.").getBytes());
final TestRDFHandler rdfHandler = new TestRDFHandler();
parser.setRDFHandler(rdfHandler);
try {
parser.parse(bais, "http://test.base.uri");
Assert.fail("Expected exception when passing in a datatype using an N3 style prefix");
} catch (RDFParseException rdfpe) {
Assert.assertEquals(1, rdfpe.getLineNumber());
// FIXME: Enable column numbers when parser supports them
// Assert.assertEquals(69, rdfpe.getColumnNumber());
}
}
/**
* @param insertDataExpr
* @param uc
* @throws SailException
*/
protected void executeInsertData(InsertData insertDataExpr, UpdateContext uc, int maxExecutionTime)
throws SailException {
SPARQLUpdateDataBlockParser parser = new SPARQLUpdateDataBlockParser(vf);
RDFHandler handler = new RDFSailInserter(con, vf, uc);
if (maxExecutionTime > 0) {
handler = new TimeLimitRDFHandler(handler, 1000L * maxExecutionTime);
}
parser.setRDFHandler(handler);
parser.setLineNumberOffset(insertDataExpr.getLineNumberOffset());
parser.getParserConfig().addNonFatalError(BasicParserSettings.VERIFY_DATATYPE_VALUES);
parser.getParserConfig().addNonFatalError(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES);
parser.getParserConfig().set(BasicParserSettings.SKOLEMIZE_ORIGIN, null);
try {
// TODO process update context somehow? dataset, base URI, etc.
parser.parse(new StringReader(insertDataExpr.getDataBlock()), "");
} catch (RDFParseException | RDFHandlerException | IOException e) {
throw new SailException(e);
}
}
@Test
public void testNTriplesFile() throws Exception {
RDFParser ntriplesParser = createRDFParser();
ntriplesParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
Model model = new LinkedHashModel();
ntriplesParser.setRDFHandler(new StatementCollector(model));
try (InputStream in = this.getClass().getResourceAsStream(NTRIPLES_TEST_FILE)) {
ntriplesParser.parse(in, NTRIPLES_TEST_URL);
} catch (RDFParseException e) {
fail("Failed to parse N-Triples test document: " + e.getMessage());
}
assertEquals(30, model.size());
assertEquals(28, model.subjects().size());
assertEquals(1, model.predicates().size());
assertEquals(23, model.objects().size());
}
/**
* Adds the data that can be read from the supplied InputStream or Reader to this repository.
*
* @param inputStreamOrReader An {@link InputStream} or {@link Reader} containing RDF data that must be added to the
* repository.
* @param baseURI The base URI for the data.
* @param dataFormat The file format of the data.
* @param rdfHandler handles all data from all documents
* @throws IOException
* @throws UnsupportedRDFormatException
* @throws RDFParseException
* @throws RDFHandlerException
*/
private void loadInputStreamOrReader(Object inputStreamOrReader, String baseURI, RDFFormat dataFormat,
RDFHandler rdfHandler) throws IOException, RDFParseException, RDFHandlerException {
RDFParser rdfParser = Rio.createParser(dataFormat, vf);
rdfParser.setParserConfig(config);
rdfParser.setParseErrorListener(new ParseErrorLogger());
rdfParser.setRDFHandler(rdfHandler);
if (inputStreamOrReader instanceof InputStream) {
rdfParser.parse((InputStream) inputStreamOrReader, baseURI);
} else if (inputStreamOrReader instanceof Reader) {
rdfParser.parse((Reader) inputStreamOrReader, baseURI);
} else {
throw new IllegalArgumentException(
"Must be an InputStream or a Reader, is a: " + inputStreamOrReader.getClass());
}
}
@Override
public void add(File file, String baseURI, RDFFormat dataFormat, Resource... contexts)
throws IOException, RDFParseException, RepositoryException {
if (baseURI == null) {
// default baseURI to file
baseURI = file.toURI().toString();
}
if (dataFormat == null) {
dataFormat = Rio.getParserFormatForFileName(file.getName())
.orElseThrow(Rio.unsupportedFormat(file.getName()));
}
try (InputStream in = new FileInputStream(file)) {
add(in, baseURI, dataFormat, contexts);
}
}
/**
* Verifies that there is only whitespace or comments until the end of the line.
*/
protected int assertLineTerminates(int c) throws IOException, RDFParseException {
c = readCodePoint();
c = skipWhitespace(c);
if (c == '#') {
// c = skipToEndOfLine(c);
} else {
if (c != -1 && c != '\r' && c != '\n') {
reportFatalError("Content after '.' is not allowed");
}
}
return c;
}
@Test(expected = RDFParseException.class)
public void testBlankNodeIdentifiersWithOtherCharactersAsFirstCharacter() throws Exception {
// The characters -, U+00B7, U+0300 to U+036F and U+203F to U+2040 are permitted anywhere except the first
// character.
List<Character> charactersList = new ArrayList<>();
charactersList.add('-');
charactersList.add('\u00B7');
charactersList.add('\u0300');
charactersList.add('\u036F');
charactersList.add('\u0301');
charactersList.add('\u203F');
for (Character character : charactersList) {
RDFParser ntriplesParser = new NTriplesParser();
Model model = new LinkedHashModel();
ntriplesParser.setRDFHandler(new StatementCollector(model));
try {
ntriplesParser.parse(
new StringReader("<urn:test:subject> <urn:test:predicate> _:" + character + "1 . "),
NTRIPLES_TEST_URL);
} catch (RDFParseException e) {
assertEquals(0, model.size());
assertEquals(0, model.subjects().size());
assertEquals(0, model.predicates().size());
assertEquals(0, model.objects().size());
throw e;
}
fail("Should have failed to parse invalid N-Triples bnode with '" + character
+ "' at the begining of the bnode label");
}
}
@Override
protected Literal createLiteral(String label, String lang, IRI datatype) throws RDFParseException {
Locator locator = saxFilter.getLocator();
if (locator != null) {
return createLiteral(label, lang, datatype, locator.getLineNumber(), locator.getColumnNumber());
} else {
return createLiteral(label, lang, datatype, -1, -1);
}
}
/**
* Tests the behaviour with non-whitespace characters after a period character without a context.
*
* @throws RDFHandlerException
* @throws IOException
* @throws RDFParseException
*/
@Test
public void testNonWhitespaceAfterPeriodWithContext() throws RDFHandlerException, IOException, RDFParseException {
final ByteArrayInputStream bais = new ByteArrayInputStream(
"<http://www.wrong.com> <http://wrong.com/1.1/tt> \"x\"^^<http://xxx.net/int> <http://path.to.graph> . <thisisnotlegal> "
.getBytes());
try {
parser.parse(bais, "http://base-uri");
Assert.fail("Expected exception when there is non-whitespace characters after a period.");
} catch (RDFParseException rdfpe) {
Assert.assertEquals(1, rdfpe.getLineNumber());
// FIXME: Enable column numbers when parser supports them
// Assert.assertEquals(44, rdfpe.getColumnNumber());
}
}
@Test
public void testTripleInPredicate() throws IOException {
String data = "@prefix ex: <http://example.com/>.\nex:Example << <urn:a> <urn:b> <urn:c> >> \"foo\" .";
try (Reader r = new StringReader(data)) {
parser.parse(r, baseURI);
fail("Must fail with RDFParseException");
} catch (RDFParseException e) {
assertEquals("Illegal predicate value: <<urn:a urn:b urn:c>> [line 2]", e.getMessage());
}
}
private static void addTriples(final SailRepository repo, final InputStream triplesStream, final RDFFormat rdfFormat) throws RDFParseException, RepositoryException, IOException {
SailRepositoryConnection conn = null;
try {
conn = repo.getConnection();
conn.begin();
conn.add(triplesStream, "", rdfFormat);
conn.commit();
} finally {
closeQuietly(conn);
}
}
public void testNQuadsFile() throws Exception {
RDFParser nquadsParser = createRDFParser();
nquadsParser.setRDFHandler(new AbstractRDFHandler() {
});
try (InputStream in = AbstractNQuadsParserUnitTest.class.getResourceAsStream(NQUADS_TEST_FILE)) {
nquadsParser.parse(in, NQUADS_TEST_URL);
} catch (RDFParseException e) {
fail("NQuadsParser failed to parse N-Quads test document: " + e.getMessage());
}
}
private IRI readTurtle(Model manifests, URL url, String baseURI, String... excludedSubdirs)
throws IOException, RDFParseException {
if (baseURI == null) {
baseURI = url.toExternalForm();
}
ParsedIRI baseIRI = ParsedIRI.create(baseURI);
SimpleValueFactory vf = SimpleValueFactory.getInstance();
IRI manifest = vf.createIRI(baseIRI.toString());
int before = manifests.size();
try (InputStream in = url.openStream()) {
RDFParser rdfParser = new TurtleParser();
ContextStatementCollector collection = new ContextStatementCollector(manifests, vf, manifest);
rdfParser.setRDFHandler(collection);
rdfParser.parse(in, baseIRI.toString());
for (Map.Entry<String, String> e : collection.getNamespaces().entrySet()) {
manifests.setNamespace(e.getKey(), e.getValue());
}
}
if (before < manifests.size()) {
for (Value included : new LinkedHashSet<>(
manifests.filter(manifest, vf.createIRI(MF_INCLUDE), null).objects())) {
String subManifestFile = included.stringValue();
if (includeSubManifest(subManifestFile, excludedSubdirs)) {
readTurtle(manifests, new URL(subManifestFile), subManifestFile, excludedSubdirs);
}
}
}
return manifest;
}
/**
* Overrides {@link AbstractRDFParser#reportError(String, RioSetting)}, adding line- and column number information
* to the error.
*/
@Override
protected void reportError(String msg, RioSetting<Boolean> setting) throws RDFParseException {
Locator locator = saxFilter.getLocator();
if (locator != null) {
reportError(msg, locator.getLineNumber(), locator.getColumnNumber(), setting);
} else {
reportError(msg, -1, -1, setting);
}
}
/**
* Creates a new {@link Statement} object with the supplied components.
*/
protected Statement createStatement(Resource subj, IRI pred, Value obj, Resource context) throws RDFParseException {
try {
return valueFactory.createStatement(subj, pred, obj, context);
} catch (Exception e) {
reportFatalError(e);
return null; // required by compiler
}
}
@Test
public void testAllowYamlCommentsDisabled() throws Exception {
thrown.expect(RDFParseException.class);
thrown.expectMessage("Could not parse JSONLD");
parser.set(JSONSettings.ALLOW_YAML_COMMENTS, false);
parser.parse(new StringReader(YAML_COMMENTS_TEST_STRING), "");
}
/**
* Creates a {@link BNode} object for the specified identifier.
*/
@Deprecated
protected BNode createBNode(String nodeID) throws RDFParseException {
// If we are preserving blank node ids then we do not prefix them to
// make them globally unique
if (preserveBNodeIDs()) {
return valueFactory.createBNode(nodeID);
} else {
// Prefix the node ID with a unique UUID prefix to reduce
// cross-document clashes
// This is consistent as long as nextBNodePrefix is not modified
// between parser runs
String toAppend = nodeID;
if (nodeID.length() > 32) {
// we only hash the node ID if it is longer than the hash string
// itself would be.
byte[] chars = nodeID.getBytes(StandardCharsets.UTF_8);
// we use an MD5 hash rather than the node ID itself to get a
// fixed-length generated id, rather than
// an ever-growing one (see SES-2171)
toAppend = (new HexBinaryAdapter()).marshal(md5.digest(chars));
}
return valueFactory.createBNode("genid-" + nextBNodePrefix + toAppend);
}
}
/**
* Checks whether 'atts' is empty. If this is not the case, a warning is generated for each attribute that is still
* present.
*/
private void checkNoMoreAtts(Atts atts) throws RDFParseException {
if (atts.size() > 0) {
Iterator<Att> iter = atts.iterator();
while (iter.hasNext()) {
Att att = iter.next();
reportError("unexpected attribute '" + att.getQName() + "'",
XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES);
iter.remove();
}
}
}
@Test
public void testUnicodeLiteralDecoding() throws RDFHandlerException, IOException, RDFParseException {
TestRDFHandler rdfHandler = new TestRDFHandler();
parser.setRDFHandler(rdfHandler);
final String INPUT_LITERAL_PLAIN = "[は]";
final String INPUT_LITERAL_ENCODED = "[\\u306F]";
final String INPUT_STRING = String.format("<http://a> <http://b> \"%s\" <http://c> .", INPUT_LITERAL_ENCODED);
final ByteArrayInputStream bais = new ByteArrayInputStream(INPUT_STRING.getBytes());
parser.parse(bais, "http://base-uri");
rdfHandler.assertHandler(1);
final Literal obj = (Literal) rdfHandler.getStatements().iterator().next().getObject();
Assert.assertEquals(INPUT_LITERAL_PLAIN, obj.getLabel());
}
/**
* The N-Quads parser must be able to parse the N-Triples test file without error.
*/
public void testNTriplesFile() throws Exception {
RDFParser nquadsParser = createRDFParser();
nquadsParser.setRDFHandler(new AbstractRDFHandler() {
});
try (InputStream in = AbstractNQuadsParserUnitTest.class.getResourceAsStream(NTRIPLES_TEST_FILE)) {
nquadsParser.parse(in, NTRIPLES_TEST_URL);
} catch (RDFParseException e) {
fail("NQuadsParser failed to parse N-Triples test document: " + e.getMessage());
}
}
/**
* Parses a quoted string, which is either a "normal string" or a """long string""".
*
* @return string
* @throws IOException
* @throws RDFParseException
*/
protected String parseQuotedString() throws IOException, RDFParseException {
String result = null;
int c1 = readCodePoint();
// First character should be '"' or "'"
verifyCharacterOrFail(c1, "\"\'");
// Check for long-string, which starts and ends with three double quotes
int c2 = readCodePoint();
int c3 = readCodePoint();
if ((c1 == '"' && c2 == '"' && c3 == '"') || (c1 == '\'' && c2 == '\'' && c3 == '\'')) {
// Long string
result = parseLongString(c2);
} else {
// Normal string
unread(c3);
unread(c2);
result = parseString(c1);
}
// Unescape any escape sequences
try {
result = TurtleUtil.decodeString(result);
} catch (IllegalArgumentException e) {
reportError(e.getMessage(), BasicParserSettings.VERIFY_DATATYPE_VALUES);
}
return result;
}
protected void parseStatement() throws IOException, RDFParseException, RDFHandlerException {
StringBuilder sb = new StringBuilder(8);
int codePoint;
// longest valid directive @prefix
do {
codePoint = readCodePoint();
if (codePoint == -1 || TurtleUtil.isWhitespace(codePoint)) {
unread(codePoint);
break;
}
appendCodepoint(sb, codePoint);
} while (sb.length() < 8);
String directive = sb.toString();
if (directive.startsWith("@") || directive.equalsIgnoreCase("prefix") || directive.equalsIgnoreCase("base")) {
parseDirective(directive);
skipWSC();
// SPARQL BASE and PREFIX lines do not end in .
if (directive.startsWith("@")) {
verifyCharacterOrFail(readCodePoint(), ".");
}
} else {
unread(directive);
parseTriples();
skipWSC();
verifyCharacterOrFail(readCodePoint(), ".");
}
}
private void assertStatements(String ttl)
throws RDFParseException, RDFHandlerException, IOException, RepositoryException {
StatementCollector expected = new StatementCollector();
RDFParser parser = Rio.createParser(RDFFormat.TURTLE);
parser.setRDFHandler(expected);
URL url = getClass().getResource(BASE_DIR + ttl);
try (InputStream rdfStream = url.openStream()) {
parser.parse(rdfStream, url.toString());
}
for (Statement stmt : expected.getStatements()) {
assertTrue("Expected statement: " + stmt, conn.hasStatement(stmt, true));
}
}
/**
* Overrides {@link AbstractRDFParser#reportError(String, RioSetting)}, adding line- and column number information
* to the error.
*/
@Override
protected void reportError(String msg, RioSetting<Boolean> setting) throws RDFParseException {
Locator locator = saxParser.getLocator();
if (locator != null) {
reportError(msg, locator.getLineNumber(), locator.getColumnNumber(), setting);
} else {
reportError(msg, -1, -1, setting);
}
}