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);
}
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);
}
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));
}
}
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);
}
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("===================");
}
}
}
Aggregations