Search in sources :

Example 1 with RDFWriterI

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

the class AdapterRDFWriter method write.

@Override
public void write(Writer out, Graph graph, PrefixMap prefixMap, String baseURI, Context context) {
    RDFWriterI w = create();
    setProperties(w, context);
    w.write(ModelFactory.createModelForGraph(graph), out, baseURI);
}
Also used : RDFWriterI(org.apache.jena.rdf.model.RDFWriterI)

Example 2 with RDFWriterI

use of org.apache.jena.rdf.model.RDFWriterI 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
    RDFWriterI 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) RDFWriterI(org.apache.jena.rdf.model.RDFWriterI) Test(org.junit.Test)

Example 3 with RDFWriterI

use of org.apache.jena.rdf.model.RDFWriterI 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.getInternal().readModelInternal(model, filename);
    String contents = null;
    try (StringWriter sw = new StringWriter()) {
        RDFWriterI 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) RDFWriterI(org.apache.jena.rdf.model.RDFWriterI) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 4 with RDFWriterI

use of org.apache.jena.rdf.model.RDFWriterI 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();
    RDFWriterI 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 : RDFWriterI(org.apache.jena.rdf.model.RDFWriterI) Properties(java.util.Properties) Model(org.apache.jena.rdf.model.Model) BaseXMLWriter(org.apache.jena.rdfxml.xmloutput.impl.BaseXMLWriter)

Example 5 with RDFWriterI

use of org.apache.jena.rdf.model.RDFWriterI 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()) {
        RDFWriterI 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) RDFWriterI(org.apache.jena.rdf.model.RDFWriterI)

Aggregations

RDFWriterI (org.apache.jena.rdf.model.RDFWriterI)8 Model (org.apache.jena.rdf.model.Model)4 StringWriter (java.io.StringWriter)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Properties (java.util.Properties)1 RDFReaderI (org.apache.jena.rdf.model.RDFReaderI)1 BaseXMLWriter (org.apache.jena.rdfxml.xmloutput.impl.BaseXMLWriter)1 BadURIException (org.apache.jena.shared.BadURIException)1 InvalidPropertyURIException (org.apache.jena.shared.InvalidPropertyURIException)1 JenaException (org.apache.jena.shared.JenaException)1 Test (org.junit.Test)1