Skip to content
Snippets Groups Projects
Commit 73189913 authored by ignazio's avatar ignazio
Browse files

Specify RioSetting values for Rio renderers #614

parent c54f6deb
No related branches found
No related tags found
No related merge requests found
......@@ -40,10 +40,12 @@ import static org.semanticweb.owlapi.util.OWLAPIPreconditions.checkNotNull;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
......@@ -62,6 +64,7 @@ import org.eclipse.rdf4j.rio.RDFHandlerException;
import org.eclipse.rdf4j.rio.RDFParseException;
import org.eclipse.rdf4j.rio.RDFParser;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.RioSetting;
import org.eclipse.rdf4j.rio.UnsupportedRDFormatException;
import org.eclipse.rdf4j.rio.helpers.BasicParserSettings;
import org.eclipse.rdf4j.rio.helpers.StatementCollector;
......@@ -224,6 +227,7 @@ public class RioParserImpl extends AbstractOWLParser implements RioParser {
createParser.getParserConfig().addNonFatalError(BasicParserSettings.VERIFY_LANGUAGE_TAGS);
createParser.getParserConfig().addNonFatalError(XMLParserSettings.DISALLOW_DOCTYPE_DECL);
createParser.getParserConfig().set(XMLParserSettings.DISALLOW_DOCTYPE_DECL, Boolean.FALSE);
addParametersIfPresent(documentSource, createParser);
createParser.setRDFHandler(handler);
long rioParseStart = System.currentTimeMillis();
if (owlFormatFactory.isTextual() && documentSource.isReaderAvailable()) {
......@@ -247,6 +251,22 @@ public class RioParserImpl extends AbstractOWLParser implements RioParser {
}
}
// These warnings are suppressed because the types cannot be easily determined here without
// forcing constraints that might need to be updated when Rio introduces new settings.
@SuppressWarnings({"null", "rawtypes", "unchecked"})
protected void addParametersIfPresent(OWLOntologyDocumentSource documentSource,
RDFParser createParser) {
Collection<RioSetting<?>> supportedSettings = createParser.getSupportedSettings();
if (documentSource.getFormat() != null && !supportedSettings.isEmpty()) {
for (RioSetting r : supportedSettings) {
Serializable v = documentSource.getFormat().getParameter(r, null);
if (v != null) {
createParser.getParserConfig().set(r, v);
}
}
}
}
@Override
public String toString() {
return getClass().getName() + " : " + owlFormatFactory;
......
......@@ -39,8 +39,11 @@ import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.io.Writer;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collection;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
......@@ -51,7 +54,11 @@ import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFHandler;
import org.eclipse.rdf4j.rio.RDFWriter;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.RioSetting;
import org.eclipse.rdf4j.rio.UnsupportedRDFormatException;
import org.eclipse.rdf4j.rio.helpers.BasicWriterSettings;
import org.eclipse.rdf4j.rio.helpers.JSONLDSettings;
import org.eclipse.rdf4j.rio.helpers.NTriplesWriterSettings;
import org.eclipse.rdf4j.rio.helpers.StatementCollector;
import org.semanticweb.owlapi.formats.RioRDFDocumentFormat;
import org.semanticweb.owlapi.formats.RioRDFDocumentFormatFactory;
......@@ -212,6 +219,9 @@ public class RioStorer extends AbstractOWLStorer {
}
}
try {
// if this is a writer rather than a statement collector, set its config from the format
// parameters, if any
addSettingsIfPresent(format);
final RioRenderer ren = new RioRenderer(ontology, rioHandler, format, contexts);
ren.render();
} catch (final IOException e) {
......@@ -248,10 +258,53 @@ public class RioStorer extends AbstractOWLStorer {
}
}
try {
// if this is a writer rather than a statement collector, set its config from the format
// parameters, if any
addSettingsIfPresent(format);
final RioRenderer ren = new RioRenderer(ontology, rioHandler, format, contexts);
ren.render();
} catch (final IOException e) {
throw new OWLOntologyStorageException(e);
}
}
// These warnings are suppressed because the types cannot be easily determined here without
// forcing constraints that might need to be updated when Rio introduces new settings.
@SuppressWarnings({"rawtypes", "null", "unchecked"})
protected void addSettingsIfPresent(OWLDocumentFormat format) {
if (rioHandler instanceof RDFWriter) {
RDFWriter w = (RDFWriter) rioHandler;
Collection<RioSetting<?>> supportedSettings = knownSettings(w);
for (RioSetting r : supportedSettings) {
Serializable v = format.getParameter(r, null);
if (v != null) {
w.getWriterConfig().set(r, v);
}
}
}
}
protected Collection<RioSetting<?>> knownSettings(RDFWriter w) {
try {
return w.getSupportedSettings();
} catch (UnsupportedOperationException e) {
LOGGER.debug(
"Bug in RIO means this exception is thrown in some formats where an unmodifiable class is modified. As a workaround, OWLAPI will try all the known settings - relying on the caller to only use supported settings.",
e);
return Arrays.asList(JSONLDSettings.COMPACT_ARRAYS, JSONLDSettings.HIERARCHICAL_VIEW,
JSONLDSettings.JSONLD_MODE, JSONLDSettings.OPTIMIZE,
JSONLDSettings.USE_NATIVE_TYPES, JSONLDSettings.USE_RDF_TYPE,
BasicWriterSettings.BASE_DIRECTIVE, BasicWriterSettings.INLINE_BLANK_NODES,
BasicWriterSettings.PRETTY_PRINT,
BasicWriterSettings.RDF_LANGSTRING_TO_LANG_LITERAL,
BasicWriterSettings.XSD_STRING_TO_PLAIN_LITERAL,
NTriplesWriterSettings.ESCAPE_UNICODE);
// parsing only settings, not used here
// NTriplesParserSettings
// RDFJSONParserSettings
// XMLParserSettings
// TriXParserSettings
// TurtleParserSettings
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment