Search in sources :

Example 1 with InvalidPropertyURIException

use of org.apache.jena.shared.InvalidPropertyURIException in project jena by apache.

the class TestXMLFeatures method doBadPropTest.

void doBadPropTest(String lg) throws IOException {
    Model m = createMemModel();
    m.add(m.createResource(), m.createProperty("http://example/", "foo#"), "foo");
    File file = File.createTempFile("rdf", ".xml");
    // file.deleteOnExit();
    FileOutputStream fwriter = new FileOutputStream(file);
    try {
        m.write(fwriter, lg);
        fwriter.close();
        fail("Writer did not detect bad property URI");
    } catch (InvalidPropertyURIException je) {
    // as required, so nowt to do.
    }
    file.delete();
}
Also used : Model(org.apache.jena.rdf.model.Model) InvalidPropertyURIException(org.apache.jena.shared.InvalidPropertyURIException)

Example 2 with InvalidPropertyURIException

use of org.apache.jena.shared.InvalidPropertyURIException in project jena by apache.

the class TestXMLFeatures method checkPropURI.

public void checkPropURI(String s, String p, Object val, int behaviour) throws IOException {
    // create triple and graph.
    // BaseXMLWriter.dbg = true;
    // SystemOutAndErr.block();
    // TestLogger tl = new TestLogger(BaseXMLWriter.class);
    blockLogger();
    Node blank = NodeFactory.createBlankNode();
    Node prop = NodeFactory.createURI(s);
    Graph g = Factory.createGraphMem();
    g.add(Triple.create(blank, prop, blank));
    // create Model
    Model m = ModelFactory.createModelForGraph(g);
    // serialize
    RDFWriter rw = m.getWriter(lang);
    if (p != null)
        rw.setProperty(p, val);
    try (StringWriter w = new StringWriter()) {
        rw.write(m, w, "http://example.org/");
        String f = w.toString();
        switch(behaviour) {
            case BadPropURI:
                fail("Bad property URI <" + s + "> was not detected.");
                return;
            case BadURI:
                fail("Bad URI <" + s + "> was not detected.");
                return;
        }
        // read back in
        Model m2 = createMemModel();
        RDFReader rdr = m2.getReader("RDF/XML");
        rdr.setProperty("error-mode", "lax");
        try (StringReader sr = new StringReader(f)) {
            rdr.read(m2, sr, "http://example.org/");
        }
        // check
        switch(behaviour) {
            case ExtraTriples:
                assertTrue("Expecting Brickley behaviour.", m2.size() == 3);
                break;
            case NoError:
                assertTrue("Comparing Model written out and read in.", m.isIsomorphicWith(m2));
                break;
        }
    } catch (BadURIException e) {
        if (behaviour == BadURI)
            return;
        throw e;
    } catch (InvalidPropertyURIException je) {
        if (behaviour == BadPropURI)
            return;
        throw je;
    } catch (JenaException e) {
        throw e;
    } finally {
        // BaseXMLWriter.dbg = false;
        // tl.end();
        unblockLogger();
    // SystemOutAndErr.unblock();
    }
}
Also used : BadURIException(org.apache.jena.shared.BadURIException) JenaException(org.apache.jena.shared.JenaException) Model(org.apache.jena.rdf.model.Model) RDFWriter(org.apache.jena.rdf.model.RDFWriter) InvalidPropertyURIException(org.apache.jena.shared.InvalidPropertyURIException) RDFReader(org.apache.jena.rdf.model.RDFReader)

Aggregations

Model (org.apache.jena.rdf.model.Model)2 InvalidPropertyURIException (org.apache.jena.shared.InvalidPropertyURIException)2 RDFReader (org.apache.jena.rdf.model.RDFReader)1 RDFWriter (org.apache.jena.rdf.model.RDFWriter)1 BadURIException (org.apache.jena.shared.BadURIException)1 JenaException (org.apache.jena.shared.JenaException)1