Search in sources :

Example 1 with ParserConfig

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

the class JSONLDWriterBackgroundTest 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)

Example 2 with ParserConfig

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

the class TriGParserCustomTest method setUp.

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

Example 3 with ParserConfig

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

the class CustomTurtleParserTest method test780IRISpace.

@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/%20SERINE%20%20", obj.stringValue()));
}
Also used : ParseErrorLogger(org.eclipse.rdf4j.rio.helpers.ParseErrorLogger) StringReader(java.io.StringReader) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 4 with ParserConfig

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

the class CustomTurtleParserTest method setUp.

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

Example 5 with ParserConfig

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

the class CustomTurtleParserTest method testParsingNamespacesWithOption.

@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")));
}
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)

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