org.eclipse.rdf4j.rio.ParserConfig Java Examples

The following examples show how to use org.eclipse.rdf4j.rio.ParserConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RDFImportServiceImpl.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
private void importInputStream(RepositoryConnection conn, ImportServiceConfig config, @Nonnull InputStream stream,
                               @Nonnull RDFFormat format) throws IOException {
    RDFParser parser = Rio.createParser(format);
    ParserConfig parserConfig = new ParserConfig();
    if (config.getContinueOnError()) {
        parserConfig.addNonFatalError(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES);
        parserConfig.addNonFatalError(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES);
        parserConfig.addNonFatalError(BasicParserSettings.NORMALIZE_DATATYPE_VALUES);
    }
    parserConfig.addNonFatalError(BasicParserSettings.VERIFY_URI_SYNTAX);
    parser.setParserConfig(parserConfig);
    BatchInserter inserter = new BatchInserter(conn, transformer, config.getBatchSize());
    if (config.getLogOutput()) {
        inserter.setLogger(LOGGER);
    }
    if (config.getPrintOutput()) {
        inserter.setPrintToSystem(true);
    }
    parser.setRDFHandler(inserter);
    parser.parse(stream, "");
}
 
Example #2
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void test780IRISpace() throws Exception {
	String ttl = "_:b25978837	a <http://purl.bioontology.org/ontology/UATC/\\u0020SERINE\\u0020\\u0020> .";
	try {
		Rio.parse(new StringReader(ttl), "", RDFFormat.TURTLE);
		fail();
	} catch (RDFParseException e) {
		// Invalid IRI
	}
	Model model = Rio.parse(new StringReader(ttl), "", RDFFormat.TURTLE,
			new ParserConfig().set(BasicParserSettings.VERIFY_URI_SYNTAX, false), SimpleValueFactory.getInstance(),
			new ParseErrorLogger());
	assertEquals(1, model.size());
	model.filter(null, RDF.TYPE, null)
			.objects()
			.forEach(obj -> assertEquals("http://purl.bioontology.org/ontology/UATC/ SERINE  ", obj.stringValue()));
}
 
Example #3
Source File: SPARQLXMLParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test with unrelated ParserConfig settings
 *
 * @throws Exception
 */
@Test
public void testEntityExpansionUnrelatedSettings() throws Exception {
	ParserConfig config = new ParserConfig();
	QueryResultCollector handler = new QueryResultCollector();
	ParseErrorCollector errorCollector = new ParseErrorCollector();
	QueryResultParser aParser = QueryResultIO.createTupleParser(TupleQueryResultFormat.SPARQL)
			.setQueryResultHandler(handler)
			.setParserConfig(config)
			.setParseErrorListener(errorCollector);

	try {
		// this should trigger a SAX parse exception that will blow up at
		// the 64k entity limit rather than OOMing
		aParser.parseQueryResult(this.getClass().getResourceAsStream("/sparqlxml/bad-entity-expansion-limit.srx"));
		fail("Parser did not throw an exception");
	} catch (QueryResultParseException e) {
		// assertTrue(e.getMessage().contains(
		// "The parser has encountered more than \"64,000\" entity
		// expansions in this document; this is the limit imposed by the
		// "));
	}
	assertEquals(0, errorCollector.getWarnings().size());
	assertEquals(0, errorCollector.getErrors().size());
	assertEquals(1, errorCollector.getFatalErrors().size());
}
 
Example #4
Source File: SPARQLConnectionTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void setParserConfigPassesToProtocolSession() throws Exception {
	ParserConfig config = new ParserConfig();

	subject.setParserConfig(config);
	verify(client, times(1)).setParserConfig(config);
}
 
Example #5
Source File: RDFParserHelperTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
	parserConfig = new ParserConfig();
	// By default we wipe out the SPI loaded datatype and language handlers
	parserConfig.set(BasicParserSettings.DATATYPE_HANDLERS, Collections.<DatatypeHandler>emptyList());
	parserConfig.set(BasicParserSettings.LANGUAGE_HANDLERS, Collections.<LanguageHandler>emptyList());
	// Ensure that the set of non-fatal errors is empty by default
	parserConfig.setNonFatalErrors(new HashSet<>());
	errListener = new ParseErrorCollector();
	valueFactory = SimpleValueFactory.getInstance();
}
 
Example #6
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
	vf = ValueFactoryImpl.getInstance();
	settingsNoVerifyLangTag = new ParserConfig();
	settingsNoVerifyLangTag.set(BasicParserSettings.VERIFY_LANGUAGE_TAGS, false);
	errors = new ParseErrorCollector();
	parser = Rio.createParser(RDFFormat.TURTLE);
	statementCollector = new StatementCollector(new LinkedHashModel());
	parser.setRDFHandler(statementCollector);
}
 
Example #7
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testParsingNamespacesWithOption() throws Exception {
	ParserConfig aConfig = new ParserConfig();

	aConfig.set(BasicParserSettings.NAMESPACES,
			Collections.<Namespace>singleton(new NamespaceImpl("foo", SKOS.NAMESPACE)));

	Model model = Rio.parse(new StringReader("<urn:a> foo:broader <urn:b>."), "", RDFFormat.TURTLE, aConfig, vf,
			new ParseErrorLogger());

	assertEquals(1, model.size());
	assertTrue(model.contains(vf.createURI("urn:a"), SKOS.BROADER, vf.createURI("urn:b")));
}
 
Example #8
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testParsingNamespacesWithOverride() throws Exception {
	ParserConfig aConfig = new ParserConfig();

	aConfig.set(BasicParserSettings.NAMESPACES,
			Collections.<Namespace>singleton(new NamespaceImpl("foo", SKOS.NAMESPACE)));

	Model model = Rio.parse(new StringReader("@prefix skos : <urn:not_skos:> ." + "<urn:a> skos:broader <urn:b>."),
			"", RDFFormat.TURTLE, aConfig, vf, new ParseErrorLogger());

	assertEquals(1, model.size());
	assertTrue(model.contains(vf.createIRI("urn:a"), vf.createIRI("urn:not_skos:broader"), vf.createIRI("urn:b")));
}
 
Example #9
Source File: AbstractRDFParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a new RDFParserBase that will use the supplied ValueFactory to create RDF model objects.
 *
 * @param valueFactory A ValueFactory.
 */
protected AbstractRDFParser(ValueFactory valueFactory) {
	try {
		md5 = MessageDigest.getInstance("MD5");
	} catch (NoSuchAlgorithmException e) {
		throw new RuntimeException(e);
	}

	namespaceTable = new HashMap<>(16);
	nextBNodePrefix = createUniqueBNodePrefix();
	setValueFactory(valueFactory);
	setParserConfig(new ParserConfig());
}
 
Example #10
Source File: TriGParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
	vf = SimpleValueFactory.getInstance();
	settingsNoVerifyLangTag = new ParserConfig();
	settingsNoVerifyLangTag.set(BasicParserSettings.VERIFY_LANGUAGE_TAGS, false);
	errors = new ParseErrorCollector();
	parser = Rio.createParser(RDFFormat.TRIG);
	statementCollector = new StatementCollector(new LinkedHashModel());
	parser.setRDFHandler(statementCollector);
}
 
Example #11
Source File: RDFXMLParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test with unrelated ParserConfig settings
 *
 * @throws Exception
 */
@Test
public void testEntityExpansionUnrelatedSettings() throws Exception {
	final Model aGraph = new LinkedHashModel();
	ParseErrorCollector errorCollector = new ParseErrorCollector();
	ParserConfig config = new ParserConfig();
	RDFParser aParser = Rio.createParser(RDFFormat.RDFXML)
			.setRDFHandler(new StatementCollector(aGraph))
			.setParserConfig(config)
			.setParseErrorListener(errorCollector);

	try {
		// this should trigger a SAX parse exception that will blow up at the
		// 64k entity limit rather than OOMing
		aParser.parse(
				this.getClass().getResourceAsStream("/testcases/rdfxml/openrdf/bad-entity-expansion-limit.rdf"),
				"http://example.org");
		fail("Parser did not throw an exception");
	} catch (RDFParseException e) {
		// assertTrue(e.getMessage().contains(
		// "The parser has encountered more than \"64,000\" entity expansions in this document; this is the limit
		// imposed by the "));
	}
	assertEquals(0, errorCollector.getWarnings().size());
	assertEquals(0, errorCollector.getErrors().size());
	assertEquals(1, errorCollector.getFatalErrors().size());
}
 
Example #12
Source File: HTTPRepositoryConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public HTTPRepositoryConnection(HTTPRepository repository, RDF4JProtocolSession client) {
	super(repository);

	this.client = client;

	// parser used for locally processing input data to be sent to the server
	// should be strict, and should preserve bnode ids.
	setParserConfig(new ParserConfig());
	getParserConfig().set(BasicParserSettings.VERIFY_DATATYPE_VALUES, true);
	getParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true);
	getParserConfig().set(HTTPRepositorySettings.MAX_STATEMENT_BUFFER_SIZE,
			HTTPRepositorySettings.MAX_STATEMENT_BUFFER_SIZE.getDefaultValue());
}
 
Example #13
Source File: JSONLDInternalTripleCallback.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public JSONLDInternalTripleCallback(RDFHandler nextHandler, ValueFactory vf, ParserConfig parserConfig,
		ParseErrorListener parseErrorListener, Function<String, Resource> namedBNodeCreator,
		Supplier<Resource> anonymousBNodeCreator) {
	this.handler = nextHandler;
	this.vf = vf;
	this.parserConfig = parserConfig;
	this.parseErrorListener = parseErrorListener;
	this.namedBNodeCreator = namedBNodeCreator;
	this.anonymousBNodeCreator = anonymousBNodeCreator;
}
 
Example #14
Source File: RepositoryConnectionWrapper.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void setParserConfig(ParserConfig parserConfig) {
	super.setParserConfig(parserConfig);
	if (getDelegate() != null) {
		getDelegate().setParserConfig(parserConfig);
	}
}
 
Example #15
Source File: JSONLDInternalTripleCallbackTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void triplesTest() throws JsonLdError, IOException {
	// String inputstring =
	// "{\"@id\":{\"@id\":\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/machine/DVC-1_8\"},\"http://igreen-projekt.de/ontologies/isoxml#deviceElement\":\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceelement/DET-1_8\",\"http://igreen-projekt.de/ontologies/isoxml#deviceID\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"DVC-1\"},\"http://igreen-projekt.de/ontologies/isoxml#deviceLocalizationLabel\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"FF000000406564\"},\"http://igreen-projekt.de/ontologies/isoxml#deviceProcessData\":[\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/13_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/6_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/14_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/11_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/8_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/4_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/5_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/10_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/2_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/21_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/15_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/16_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/19_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/17_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/3_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/12_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/7_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/18_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/9_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/22_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/20_8\"],\"http://igreen-projekt.de/ontologies/isoxml#deviceSerialNumber\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"12345\"},\"http://igreen-projekt.de/ontologies/isoxml#deviceSoftwareVersion\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"01.009\"},\"http://igreen-projekt.de/ontologies/isoxml#deviceStructureLabel\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"31303030303030\"},\"http://igreen-projekt.de/ontologies/isoxml#workingSetMasterNAME\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"A000860020800001\"},\"http://www.w3.org/1999/02/22-rdf-syntax-ns#type\":{\"@iri\":\"http://www.agroxml.de/rdfs#Machine\"},\"http://www.w3.org/2000/01/rdf-schema#label\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"Krone
	// Device\"}}";
	final String inputstring = "{ \"@id\":\"http://nonexistent.com/abox#Document1823812\", \"@type\":\"http://nonexistent.com/tbox#Document\" }";
	final String expectedString = "(http://nonexistent.com/abox#Document1823812, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://nonexistent.com/tbox#Document) [null]";
	final Object input = JsonUtils.fromString(inputstring);

	final Model graph = new LinkedHashModel();
	final ParseErrorCollector parseErrorListener = new ParseErrorCollector();
	final ParserConfig parserConfig = new ParserConfig();
	final JSONLDInternalTripleCallback callback = new JSONLDInternalTripleCallback(new StatementCollector(graph),
			SimpleValueFactory.getInstance(), parserConfig, parseErrorListener,
			nodeID -> SimpleValueFactory.getInstance().createBNode(nodeID),
			() -> SimpleValueFactory.getInstance().createBNode());

	JsonLdProcessor.toRDF(input, callback);

	final Iterator<Statement> statements = graph.iterator();

	// contains only one statement (type)
	while (statements.hasNext()) {
		final Statement stmt = statements.next();

		System.out.println(stmt.toString());
		assertEquals("Output was not as expected", stmt.toString(), expectedString);
	}

	assertEquals(0, parseErrorListener.getFatalErrors().size());
	assertEquals(0, parseErrorListener.getErrors().size());
	assertEquals(0, parseErrorListener.getWarnings().size());
}
 
Example #16
Source File: JSONLDWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testRoundTripNamespaces() throws Exception {
	IRI uri1 = vf.createIRI(exNs, "uri1");
	IRI uri2 = vf.createIRI(exNs, "uri2");
	Literal plainLit = vf.createLiteral("plain", XMLSchema.STRING);

	Statement st1 = vf.createStatement(uri1, uri2, plainLit);

	ByteArrayOutputStream out = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(out);
	rdfWriter.getWriterConfig().set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT);
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(st1);
	rdfWriter.endRDF();

	ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	ParserConfig config = new ParserConfig();
	config.set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, true);
	config.set(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES, true);
	rdfParser.setParserConfig(config);
	rdfParser.setValueFactory(vf);
	Model model = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(model));

	rdfParser.parse(in, "foo:bar");

	assertEquals("Unexpected number of statements, found " + model.size(), 1, model.size());

	assertTrue("missing namespaced statement", model.contains(st1));

	if (rdfParser.getRDFFormat().supportsNamespaces()) {
		assertTrue("Expected at least one namespace, found " + model.getNamespaces().size(),
				model.getNamespaces().size() >= 1);
		assertEquals(exNs, model.getNamespace("ex").get().getName());
	}
}
 
Example #17
Source File: TestStatementsController.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void shouldUseTimeoutParameterForUpdateQueries() throws Exception {
	// prepare
	StatementsController controller = new StatementsController();

	final int maxExecution = 1;
	final MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod(HttpMethod.POST.name());
	request.setContentType(Protocol.SPARQL_UPDATE_MIME_TYPE);
	request.addParameter(Protocol.TIMEOUT_PARAM_NAME, String.valueOf(maxExecution));
	final String updateString = "delete where { <monkey:pod> ?p ?o . }";
	request.setContent(updateString.getBytes(StandardCharsets.UTF_8));

	// prepare mocks
	final Repository repMock = Mockito.mock(Repository.class);
	final RepositoryConnection connectionMock = Mockito.mock(RepositoryConnection.class);
	final ParserConfig parserConfigMock = Mockito.mock(ParserConfig.class);
	final Update updateMock = Mockito.mock(Update.class);
	Mockito.when(repMock.getConnection()).thenReturn(connectionMock);
	Mockito.when(connectionMock.prepareUpdate(QueryLanguage.SPARQL, updateString, null)).thenReturn(updateMock);
	Mockito.when(connectionMock.getParserConfig()).thenReturn(parserConfigMock);

	// repository interceptor uses this attribute
	request.setAttribute("repository", repMock);

	// act
	controller.handleRequest(request, new MockHttpServletResponse());

	Mockito.verify(updateMock).setMaxExecutionTime(maxExecution);
}
 
Example #18
Source File: Transaction.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Obtains a {@link RepositoryConnection} through the {@link ExecutorService}.
 *
 * @return A new {@link RepositoryConnection} to use for this Transaction.
 * @throws InterruptedException If the execution of the task was interrupted.
 * @throws ExecutionException   If the execution of the task failed for any reason.
 */
private RepositoryConnection getTransactionConnection() throws InterruptedException, ExecutionException {
	// create a new RepositoryConnection with correct parser settings
	Future<RepositoryConnection> result = submit(() -> {
		RepositoryConnection conn = rep.getConnection();
		ParserConfig config = conn.getParserConfig();
		config.set(BasicParserSettings.PRESERVE_BNODE_IDS, true);
		config.addNonFatalError(BasicParserSettings.VERIFY_DATATYPE_VALUES);
		config.addNonFatalError(BasicParserSettings.VERIFY_LANGUAGE_TAGS);

		return conn;
	});
	return getFromFuture(result);
}
 
Example #19
Source File: SailConnectionUpdate.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public SailConnectionUpdate(ParsedUpdate parsedUpdate, SailConnection con, ValueFactory vf,
		ParserConfig parserConfig) {
	super(parsedUpdate);
	this.con = con;
	this.vf = vf;
	this.parserConfig = parserConfig;
}
 
Example #20
Source File: HalyardStatsTest.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatsTargetPartial() throws Exception {
    final HBaseSail sail = new HBaseSail(HBaseServerTestInstance.getInstanceConfig(), "statsTable3", true, -1, true, 0, null, null);
    sail.initialize();
    try (InputStream ref = HalyardStatsTest.class.getResourceAsStream("testData.trig")) {
        RDFParser p = Rio.createParser(RDFFormat.TRIG);
        p.setPreserveBNodeIDs(true);
        p.setRDFHandler(new AbstractRDFHandler() {
            @Override
            public void handleStatement(Statement st) throws RDFHandlerException {
                sail.addStatement(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext());
            }
        }).parse(ref, "");
    }
    sail.commit();
    sail.close();

    File root = File.createTempFile("test_stats", "");
    root.delete();
    root.mkdirs();

    assertEquals(0, ToolRunner.run(HBaseServerTestInstance.getInstanceConfig(), new HalyardStats(),
            new String[]{"-s", "statsTable3", "-t", root.toURI().toURL().toString() + "stats{0}.trig", "-r", "100", "-g", "http://whatever/myStats", "-c", "http://whatever/graph0"}));

    File stats = new File(root, "stats0.trig");
    assertTrue(stats.isFile());
    try (InputStream statsStream = new FileInputStream(stats)) {
        try (InputStream refStream = HalyardStatsTest.class.getResourceAsStream("testStatsTargetPartial.trig")) {
            Model statsM = Rio.parse(statsStream, "", RDFFormat.TRIG, new ParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true), SimpleValueFactory.getInstance(), new ParseErrorLogger());
            Model refM = Rio.parse(refStream, "", RDFFormat.TRIG, new ParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true), SimpleValueFactory.getInstance(), new ParseErrorLogger(), SimpleValueFactory.getInstance().createIRI("http://whatever/myStats"));
            assertEqualModels(refM, statsM);
        }
    }
}
 
Example #21
Source File: HalyardStatsTest.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatsTarget() throws Exception {
    final HBaseSail sail = new HBaseSail(HBaseServerTestInstance.getInstanceConfig(), "statsTable", true, -1, true, 0, null, null);
    sail.initialize();
    try (InputStream ref = HalyardStatsTest.class.getResourceAsStream("testData.trig")) {
        RDFParser p = Rio.createParser(RDFFormat.TRIG);
        p.setPreserveBNodeIDs(true);
        p.setRDFHandler(new AbstractRDFHandler() {
            @Override
            public void handleStatement(Statement st) throws RDFHandlerException {
                sail.addStatement(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext());
            }
        }).parse(ref, "");
    }
    sail.commit();
    sail.close();

    File root = File.createTempFile("test_stats", "");
    root.delete();
    root.mkdirs();

    assertEquals(0, ToolRunner.run(HBaseServerTestInstance.getInstanceConfig(), new HalyardStats(),
            new String[]{"-s", "statsTable", "-t", root.toURI().toURL().toString() + "stats{0}.trig", "-r", "100", "-g", "http://whatever/myStats"}));

    File stats = new File(root, "stats0.trig");
    assertTrue(stats.isFile());
    try (InputStream statsStream = new FileInputStream(stats)) {
        try (InputStream refStream = HalyardStatsTest.class.getResourceAsStream("testStatsTarget.trig")) {
            Model statsM = Rio.parse(statsStream, "", RDFFormat.TRIG, new ParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true), SimpleValueFactory.getInstance(), new ParseErrorLogger());
            Model refM = Rio.parse(refStream, "", RDFFormat.TRIG, new ParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true), SimpleValueFactory.getInstance(), new ParseErrorLogger(), SimpleValueFactory.getInstance().createIRI("http://whatever/myStats"));
            assertEqualModels(refM, statsM);
        }
    }
}
 
Example #22
Source File: JSONLDWriterBackgroundTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testRoundTripNamespaces() throws Exception {
	String exNs = "http://example.org/";
	IRI uri1 = vf.createIRI(exNs, "uri1");
	IRI uri2 = vf.createIRI(exNs, "uri2");
	Literal plainLit = vf.createLiteral("plain", XMLSchema.STRING);

	Statement st1 = vf.createStatement(uri1, uri2, plainLit);

	ByteArrayOutputStream out = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(out);
	rdfWriter.getWriterConfig().set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT);
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(st1);
	rdfWriter.endRDF();

	ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	ParserConfig config = new ParserConfig();
	config.set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, true);
	config.set(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES, true);
	rdfParser.setParserConfig(config);
	rdfParser.setValueFactory(vf);
	Model model = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(model));

	rdfParser.parse(in, "foo:bar");

	assertEquals("Unexpected number of statements, found " + model.size(), 1, model.size());

	assertTrue("missing namespaced statement", model.contains(st1));

	if (rdfParser.getRDFFormat().supportsNamespaces()) {
		assertTrue("Expected at least one namespace, found " + model.getNamespaces().size(),
				model.getNamespaces().size() >= 1);
		assertEquals(exNs, model.getNamespace("ex").get().getName());
	}
}
 
Example #23
Source File: DatasetRepository.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Inspects if the dataset at the supplied URL location has been modified since the last load into this repository
 * and if so loads it into the supplied context.
 *
 * @param url     the location of the dataset
 * @param context the context in which to load the dataset
 * @param config  parser configuration to use for processing the dataset
 * @throws RepositoryException if an error occurred while loading the dataset.
 */
public void loadDataset(URL url, IRI context, ParserConfig config) throws RepositoryException {
	try {
		Long since = lastModified.get(url);
		URLConnection urlCon = url.openConnection();
		if (since != null) {
			urlCon.setIfModifiedSince(since);
		}
		if (since == null || since < urlCon.getLastModified()) {
			load(url, urlCon, context, config);
		}
	} catch (RDFParseException | IOException e) {
		throw new RepositoryException(e);
	}
}
 
Example #24
Source File: DatasetRepository.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private synchronized void load(URL url, URLConnection urlCon, IRI context, ParserConfig config)
		throws RepositoryException, RDFParseException, IOException {
	long modified = urlCon.getLastModified();
	if (lastModified.containsKey(url) && lastModified.get(url) >= modified) {
		return;
	}

	// Try to determine the data's MIME type
	String mimeType = urlCon.getContentType();
	int semiColonIdx = mimeType.indexOf(';');
	if (semiColonIdx >= 0) {
		mimeType = mimeType.substring(0, semiColonIdx);
	}
	RDFFormat format = Rio.getParserFormatForMIMEType(mimeType)
			.orElse(Rio.getParserFormatForFileName(url.getPath()).orElseThrow(Rio.unsupportedFormat(mimeType)));

	try (InputStream stream = urlCon.getInputStream()) {
		try (RepositoryConnection repCon = super.getConnection()) {

			repCon.setParserConfig(config);
			repCon.begin();
			repCon.clear(context);
			repCon.add(stream, url.toExternalForm(), format, context);
			repCon.commit();
			lastModified.put(url, modified);
		}
	}
}
 
Example #25
Source File: JSONLDWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupParserConfig(ParserConfig config) {
	super.setupParserConfig(config);
	config.set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, true);
	config.set(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES, true);
}
 
Example #26
Source File: JSONLDInternalTripleCallback.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public JSONLDInternalTripleCallback(RDFHandler nextHandler, ValueFactory vf) {
	this(nextHandler, vf, new ParserConfig(), new ParseErrorLogger(), nodeID -> vf.createBNode(nodeID),
			() -> vf.createBNode());
}
 
Example #27
Source File: JSONLDInternalTripleCallback.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @return the parserConfig
 */
public ParserConfig getParserConfig() {
	return parserConfig;
}
 
Example #28
Source File: JSONLDParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Get an instance of JsonFactory configured using the settings from {@link #getParserConfig()}.
 *
 * @return A newly configured JsonFactory based on the currently enabled settings
 */
private JsonFactory configureNewJsonFactory() {
	final JsonFactory nextJsonFactory = new JsonFactory(JSON_MAPPER);
	ParserConfig parserConfig = getParserConfig();

	if (parserConfig.isSet(JSONSettings.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER)) {
		nextJsonFactory.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER,
				parserConfig.get(JSONSettings.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER));
	}
	if (parserConfig.isSet(JSONSettings.ALLOW_COMMENTS)) {
		nextJsonFactory.configure(JsonParser.Feature.ALLOW_COMMENTS,
				parserConfig.get(JSONSettings.ALLOW_COMMENTS));
	}
	if (parserConfig.isSet(JSONSettings.ALLOW_NON_NUMERIC_NUMBERS)) {
		nextJsonFactory.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS,
				parserConfig.get(JSONSettings.ALLOW_NON_NUMERIC_NUMBERS));
	}
	if (parserConfig.isSet(JSONSettings.ALLOW_NUMERIC_LEADING_ZEROS)) {
		nextJsonFactory.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS,
				parserConfig.get(JSONSettings.ALLOW_NUMERIC_LEADING_ZEROS));
	}
	if (parserConfig.isSet(JSONSettings.ALLOW_SINGLE_QUOTES)) {
		nextJsonFactory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES,
				parserConfig.get(JSONSettings.ALLOW_SINGLE_QUOTES));
	}
	if (parserConfig.isSet(JSONSettings.ALLOW_UNQUOTED_CONTROL_CHARS)) {
		nextJsonFactory.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
				parserConfig.get(JSONSettings.ALLOW_UNQUOTED_CONTROL_CHARS));
	}
	if (parserConfig.isSet(JSONSettings.ALLOW_UNQUOTED_FIELD_NAMES)) {
		nextJsonFactory.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES,
				parserConfig.get(JSONSettings.ALLOW_UNQUOTED_FIELD_NAMES));
	}
	if (parserConfig.isSet(JSONSettings.ALLOW_YAML_COMMENTS)) {
		nextJsonFactory.configure(JsonParser.Feature.ALLOW_YAML_COMMENTS,
				parserConfig.get(JSONSettings.ALLOW_YAML_COMMENTS));
	}
	if (parserConfig.isSet(JSONSettings.ALLOW_TRAILING_COMMA)) {
		nextJsonFactory.configure(JsonParser.Feature.ALLOW_TRAILING_COMMA,
				parserConfig.get(JSONSettings.ALLOW_TRAILING_COMMA));
	}
	if (parserConfig.isSet(JSONSettings.INCLUDE_SOURCE_IN_LOCATION)) {
		nextJsonFactory.configure(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION,
				parserConfig.get(JSONSettings.INCLUDE_SOURCE_IN_LOCATION));
	}
	if (parserConfig.isSet(JSONSettings.STRICT_DUPLICATE_DETECTION)) {
		nextJsonFactory.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION,
				parserConfig.get(JSONSettings.STRICT_DUPLICATE_DETECTION));
	}
	return nextJsonFactory;
}
 
Example #29
Source File: JSONLDInternalTripleCallback.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @param parserConfig the parserConfig to set
 */
public void setParserConfig(ParserConfig parserConfig) {
	this.parserConfig = parserConfig;
}
 
Example #30
Source File: JSONParser.java    From Halyard with Apache License 2.0 4 votes vote down vote up
@Override
public RDFParser setParserConfig(ParserConfig config) {
    this.config = config;
    return this;
}