Search in sources :

Example 6 with ParserConfig

use of org.eclipse.rdf4j.rio.ParserConfig in project rdf4j by eclipse.

the class CustomTurtleParserTest method testParsingNamespacesWithOverride.

@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")));
}
Also used : ParseErrorLogger(org.eclipse.rdf4j.rio.helpers.ParseErrorLogger) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) StringReader(java.io.StringReader) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) NamespaceImpl(org.eclipse.rdf4j.model.impl.NamespaceImpl) Test(org.junit.Test)

Example 7 with ParserConfig

use of org.eclipse.rdf4j.rio.ParserConfig in project rdf4j by eclipse.

the class SPARQLXMLParserCustomTest method testEntityExpansionUnrelatedSettings.

/**
 * 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());
}
Also used : QueryResultParser(org.eclipse.rdf4j.query.resultio.QueryResultParser) QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) ParseErrorCollector(org.eclipse.rdf4j.rio.helpers.ParseErrorCollector) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) QueryResultCollector(org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector) Test(org.junit.Test)

Example 8 with ParserConfig

use of org.eclipse.rdf4j.rio.ParserConfig in project rdf4j by eclipse.

the class RDFXMLParserCustomTest method testEntityExpansionUnrelatedSettings.

/**
 * 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());
}
Also used : StatementCollector(org.eclipse.rdf4j.rio.helpers.StatementCollector) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Model(org.eclipse.rdf4j.model.Model) ParseErrorCollector(org.eclipse.rdf4j.rio.helpers.ParseErrorCollector) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) RDFParser(org.eclipse.rdf4j.rio.RDFParser) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 9 with ParserConfig

use of org.eclipse.rdf4j.rio.ParserConfig in project rdf4j by eclipse.

the class RDFParserHelperTest method setUp.

/**
 * @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<RioSetting<?>>());
    errListener = new ParseErrorCollector();
    valueFactory = SimpleValueFactory.getInstance();
}
Also used : RioSetting(org.eclipse.rdf4j.rio.RioSetting) ParseErrorCollector(org.eclipse.rdf4j.rio.helpers.ParseErrorCollector) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) Before(org.junit.Before)

Example 10 with ParserConfig

use of org.eclipse.rdf4j.rio.ParserConfig in project rdf4j by eclipse.

the class JSONLDWriterTest method testRoundTripNamespaces.

@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.handleNamespace("ex", exNs);
    rdfWriter.startRDF();
    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());
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) ByteArrayInputStream(java.io.ByteArrayInputStream) Statement(org.eclipse.rdf4j.model.Statement) StatementCollector(org.eclipse.rdf4j.rio.helpers.StatementCollector) Literal(org.eclipse.rdf4j.model.Literal) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) RDFParser(org.eclipse.rdf4j.rio.RDFParser) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) RDFWriterTest(org.eclipse.rdf4j.rio.RDFWriterTest) Test(org.junit.Test)

Aggregations

ParserConfig (org.eclipse.rdf4j.rio.ParserConfig)11 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)9 Test (org.junit.Test)8 Model (org.eclipse.rdf4j.model.Model)7 ParseErrorCollector (org.eclipse.rdf4j.rio.helpers.ParseErrorCollector)6 StatementCollector (org.eclipse.rdf4j.rio.helpers.StatementCollector)6 StringReader (java.io.StringReader)3 Statement (org.eclipse.rdf4j.model.Statement)3 RDFParser (org.eclipse.rdf4j.rio.RDFParser)3 ParseErrorLogger (org.eclipse.rdf4j.rio.helpers.ParseErrorLogger)3 Before (org.junit.Before)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IRI (org.eclipse.rdf4j.model.IRI)2 Literal (org.eclipse.rdf4j.model.Literal)2 NamespaceImpl (org.eclipse.rdf4j.model.impl.NamespaceImpl)2 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)2 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)2 RDFWriterTest (org.eclipse.rdf4j.rio.RDFWriterTest)2 QueryResultParseException (org.eclipse.rdf4j.query.resultio.QueryResultParseException)1