Search in sources :

Example 1 with RDFWriter

use of org.apache.jena.rdf.model.RDFWriter in project jena by apache.

the class TestXMLFeatures method testRelativeAPI.

/*
	 * public void testBadProperty2() throws IOException {
	 * checkPropURI("http:/a.b/", "brickley", "http://example.org/b#",
	 * ExtraTriples); }
	 * 
	 */
public void testRelativeAPI() {
    RDFWriter w = createMemModel().getWriter(lang);
    String old = (String) w.setProperty("relativeURIs", "");
    assertEquals("default value check", old, "same-document, absolute, relative, parent");
    w.setProperty("relativeURIs", "network, grandparent,relative,  ");
    w.setProperty("relativeURIs", "  parent, same-document, network, parent, absolute ");
    // TestLogger tl = new TestLogger(URI.class);
    blockLogger();
    // will get warning
    w.setProperty("relativeURIs", "foo");
    assertTrue("A warning should have been generated.", unblockLogger());
}
Also used : RDFWriter(org.apache.jena.rdf.model.RDFWriter)

Example 2 with RDFWriter

use of org.apache.jena.rdf.model.RDFWriter in project jena by apache.

the class XMLOutputTestBase method check.

protected void check(String filename, String encoding, String regexPresent, String regexAbsent, boolean errorExpected, Change code, String base) throws IOException {
    blockLogger();
    boolean errorsFound;
    Model m = createMemModel();
    try (InputStream in = new FileInputStream(filename)) {
        m.read(in, base);
    }
    @SuppressWarnings("resource") Writer sw;
    ByteArrayOutputStream bos = null;
    if (encoding == null)
        sw = new StringWriter();
    else {
        bos = new ByteArrayOutputStream();
        sw = new OutputStreamWriter(bos, encoding);
    }
    Properties p = (Properties) System.getProperties().clone();
    RDFWriter writer = m.getWriter(lang);
    code.modify(m, writer);
    writer.write(m, sw, base);
    sw.close();
    String contents;
    if (encoding == null)
        contents = sw.toString();
    else {
        contents = bos.toString(encoding);
    }
    try {
        Model m2 = createMemModel();
        m2.read(new StringReader(contents), base);
        assertTrue("Data got changed.", m.isIsomorphicWith(m2));
        if (regexPresent != null)
            assertTrue("Should find /" + regexPresent + "/", Pattern.compile(regexPresent, Pattern.DOTALL).matcher(contents).find());
        if (regexAbsent != null)
            assertTrue("Should not find /" + regexAbsent + "/", !Pattern.compile(regexAbsent, Pattern.DOTALL).matcher(contents).find());
        contents = null;
    } finally {
        errorsFound = unblockLogger();
        System.setProperties(p);
        if (contents != null) {
            System.err.println("===================");
            System.err.println("Offending content - " + toString());
            System.err.println("===================");
            System.err.println(contents);
            System.err.println("===================");
        }
    }
    assertEquals("Errors (not) detected.", errorExpected, errorsFound);
}
Also used : Properties(java.util.Properties) Model(org.apache.jena.rdf.model.Model) RDFWriter(org.apache.jena.rdf.model.RDFWriter) RDFWriter(org.apache.jena.rdf.model.RDFWriter) BaseXMLWriter(org.apache.jena.rdfxml.xmloutput.impl.BaseXMLWriter)

Example 3 with RDFWriter

use of org.apache.jena.rdf.model.RDFWriter in project jena by apache.

the class TestWriteRDFXML method propertiesAbbrev.

@Test
public void propertiesAbbrev() {
    String name = "RDF/XML-ABBREV";
    // Write without setting properties
    StringWriter w = new StringWriter();
    model.getWriter(name).write(model, w, null);
    String x0 = w.toString();
    // Write with setting properties
    RDFWriter rdfWriter = model.getWriter(name);
    rdfWriter.setProperty("showXmlDeclaration", "true");
    rdfWriter.setProperty("showDoctypeDeclaration", "true");
    StringWriter w2 = new StringWriter();
    rdfWriter.write(model, w2, null);
    String x2 = w2.toString();
    // Did it have an effect?
    Assert.assertNotEquals(x0, x2);
}
Also used : StringWriter(java.io.StringWriter) RDFWriter(org.apache.jena.rdf.model.RDFWriter) Test(org.junit.Test)

Example 4 with RDFWriter

use of org.apache.jena.rdf.model.RDFWriter in project jena by apache.

the class TestWriterFeatures method checkReadWriteRead.

private void checkReadWriteRead(String filename, String writerName, String propertyName, String propertyValue) {
    Model model = createMemModel();
    FileManager.get().readModel(model, filename);
    String contents = null;
    try (StringWriter sw = new StringWriter()) {
        RDFWriter w = model.getWriter(writerName);
        if (propertyName != null)
            w.setProperty(propertyName, propertyValue);
        w.write(model, sw, null);
        contents = sw.toString();
    } catch (IOException ex) {
    /* ignore : StringWriter */
    }
    try (StringReader sr = new StringReader(contents)) {
        Model model2 = createMemModel();
        model2.read(new StringReader(contents), filename);
        assertTrue(model.isIsomorphicWith(model2));
    }
}
Also used : StringWriter(java.io.StringWriter) Model(org.apache.jena.rdf.model.Model) StringReader(java.io.StringReader) RDFWriter(org.apache.jena.rdf.model.RDFWriter) IOException(java.io.IOException)

Example 5 with RDFWriter

use of org.apache.jena.rdf.model.RDFWriter in project jena by apache.

the class TestXMLFeatures method relative.

private void relative(String relativeParam, String base, Collection<String> regexesPresent, Collection<String> regexesAbsent) throws IOException {
    Model m = createMemModel();
    m.read("file:testing/abbreviated/relative-uris.rdf");
    String contents;
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        RDFWriter writer = m.getWriter(lang);
        writer.setProperty("relativeURIs", relativeParam);
        writer.write(m, bos, base);
        contents = bos.toString("UTF8");
    }
    try {
        Model m2 = createMemModel();
        m2.read(new StringReader(contents), base);
        assertTrue(m.isIsomorphicWith(m2));
        Iterator<String> it = regexesPresent.iterator();
        while (it.hasNext()) {
            String regexPresent = it.next();
            assertTrue("Looking for /" + regexPresent + "/", Pattern.compile(Util.substituteStandardEntities(regexPresent), Pattern.DOTALL).matcher(contents).find());
        }
        it = regexesAbsent.iterator();
        while (it.hasNext()) {
            String regexAbsent = it.next();
            assertTrue("Looking for (not) /" + regexAbsent + "/", !Pattern.compile("[\"']" + Util.substituteStandardEntities(regexAbsent) + "[\"']", Pattern.DOTALL).matcher(contents).find());
        }
        contents = null;
    } finally {
        if (contents != null) {
            System.err.println("===================");
            System.err.println("Offending content - " + toString());
            System.err.println("===================");
            System.err.println(contents);
            System.err.println("===================");
        }
    }
}
Also used : Model(org.apache.jena.rdf.model.Model) RDFWriter(org.apache.jena.rdf.model.RDFWriter)

Aggregations

RDFWriter (org.apache.jena.rdf.model.RDFWriter)9 Model (org.apache.jena.rdf.model.Model)4 StringWriter (java.io.StringWriter)2 JenaException (org.apache.jena.shared.JenaException)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Properties (java.util.Properties)1 RDFReader (org.apache.jena.rdf.model.RDFReader)1 BaseXMLWriter (org.apache.jena.rdfxml.xmloutput.impl.BaseXMLWriter)1 BadURIException (org.apache.jena.shared.BadURIException)1 ConfigException (org.apache.jena.shared.ConfigException)1 InvalidPropertyURIException (org.apache.jena.shared.InvalidPropertyURIException)1 NoWriterForLangException (org.apache.jena.shared.NoWriterForLangException)1 Test (org.junit.Test)1