Skip to content

Commit

Permalink
Fix RDF issues testing 4.1.0 RC 1 #412
Browse files Browse the repository at this point in the history
This commit adds a test for ill formed XML literals, which are expected
to fail to serialize.
The issue is that ill formed literals would create ontologies that
cannot be parsed back.
  • Loading branch information
ignazio1977 committed Jun 5, 2015
1 parent 7204fb6 commit aeb9738
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ public void shouldRoundTripXMLLiteral() throws OWLOntologyStorageException {
assertTrue(string.contains(literal));
}

@Test
public void shouldFailOnMalformedXMLLiteral() throws OWLOntologyCreationException, OWLOntologyStorageException {
String literal = "<ncicp:ComplexDefinition><ncicp:def-definition>A form of cancer that begins in melanocytes (cells that make the pigment melanin). It may begin in a mole (skin melanoma), but can also begin in other pigmented tissues, such as in the eye or in the intestines.</ncicp:def-definition><ncicp:def-source>NCI-GLOSS</ncicp:def-source></ncicp:ComplexDefinition>";
OWLOntology o = m.createOntology();
OWLDataProperty p = df.getOWLDataProperty(IRI.create("urn:test#p"));
OWLLiteral l = df.getOWLLiteral(literal, OWL2Datatype.RDF_XML_LITERAL);
OWLNamedIndividual i = df.getOWLNamedIndividual(IRI.create("urn:test#i"));
o.add( df.getOWLDataPropertyAssertionAxiom(p, i, l));
expectedException.expect(OWLOntologyStorageException.class);
expectedException.expectMessage(literal);
expectedException.expectMessage("XML literal is not self contained");
saveOntology(o).toString();
}

@Test
public void shouldRoundtripPaddedLiterals() throws OWLOntologyCreationException, OWLOntologyStorageException {
String in = "Prefix(:=<urn:test#>)\n" + "Prefix(a:=<urn:test#>)\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import static org.semanticweb.owlapi.util.OWLAPIPreconditions.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.*;

import javax.annotation.Nonnull;
Expand All @@ -23,7 +25,11 @@
import org.semanticweb.owlapi.io.XMLUtils;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLRuntimeException;
import org.semanticweb.owlapi.util.SAXParsers;
import org.semanticweb.owlapi.util.StringLengthComparator;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -59,10 +65,10 @@ public class XMLWriterImpl implements XMLWriter {
* xml writer preferences instance
*/
public XMLWriterImpl(PrintWriter writer, XMLWriterNamespaceManager xmlWriterNamespaceManager, String xmlBase,
XMLWriterPreferences preferences) {
XMLWriterPreferences preferences) {
this.writer = checkNotNull(writer, "writer cannot be null");
this.xmlWriterNamespaceManager = checkNotNull(xmlWriterNamespaceManager,
"xmlWriterNamespaceManager cannot be null");
"xmlWriterNamespaceManager cannot be null");
this.xmlBase = checkNotNull(xmlBase, "xmlBase cannot be null");
xmlPreferences = checkNotNull(preferences, "preferences cannot be null");
elementStack = new Stack<>();
Expand Down Expand Up @@ -237,8 +243,8 @@ public void startDocument(IRI rootElement) {
}
for (String curPrefix : xmlWriterNamespaceManager.getPrefixes()) {
if (!curPrefix.isEmpty()) {
writeAttribute("xmlns:" + curPrefix,
verifyNotNull(xmlWriterNamespaceManager.getNamespaceForPrefix(curPrefix)));
writeAttribute("xmlns:" + curPrefix, verifyNotNull(xmlWriterNamespaceManager.getNamespaceForPrefix(
curPrefix)));
}
}
}
Expand Down Expand Up @@ -404,13 +410,23 @@ private void writeTextContent() {
if (textContent != null) {
// only escape the data if this is not an XML literal
if ("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral".equals(attributes.get("rdf:datatype"))) {
checkProperXMLLiteral(textContent);
writer.write(textContent);
} else {
writer.write(XMLUtils.escapeXML(verifyNotNull(textContent)));
}
}
}

private void checkProperXMLLiteral(String text) {
try {
SAXParsers.initParserWithOWLAPIStandards(null).parse(new InputSource(new StringReader(text)),
new DefaultHandler());
} catch (SAXException | IOException e) {
throw new OWLRuntimeException("XML literal is not self contained: \"" + text + "\"", e);
}
}

private void insertIndentation() {
if (xmlPreferences.isIndenting()) {
for (int i = 0; i < indentation * xmlPreferences.getIndentSize(); i++) {
Expand Down

0 comments on commit aeb9738

Please sign in to comment.