This commit is contained in:
Arjen Poutsma
2008-12-13 11:58:53 +00:00
parent a9aa325bbc
commit 1d7e5239fd
2 changed files with 28 additions and 7 deletions

View File

@@ -147,13 +147,20 @@ class AxiomHandler implements ContentHandler, LexicalHandler {
}
public void startEntity(String name) throws SAXException {
charactersType = XMLStreamConstants.ENTITY_REFERENCE;
if (!isPredefinedEntityReference(name)) {
charactersType = XMLStreamConstants.ENTITY_REFERENCE;
}
}
public void endEntity(String name) throws SAXException {
charactersType = XMLStreamConstants.CHARACTERS;
}
private boolean isPredefinedEntityReference(String name) {
return "lt".equals(name) || "gt".equals(name) || "amp".equals(name) || "quot".equals(name) ||
"apos".equals(name);
}
/*
* Unsupported
*/

View File

@@ -31,17 +31,21 @@ import org.xml.sax.helpers.XMLReaderFactory;
public class AxiomHandlerTest extends XMLTestCase {
private static final String XML_1 = "<?xml version='1.0' encoding='UTF-8'?>" + "<?pi content?>" +
"<root xmlns='namespace'>" +
"<prefix:child xmlns:prefix='namespace2' xmlns:prefix2='namespace3' prefix2:attr='value'>content</prefix:child>" +
"</root>";
private static final String XML_1 =
"<?xml version='1.0' encoding='UTF-8'?>" + "<?pi content?>" + "<root xmlns='namespace'>" +
"<prefix:child xmlns:prefix='namespace2' xmlns:prefix2='namespace3' prefix2:attr='value'>content</prefix:child>" +
"</root>";
private static final String XML_2_EXPECTED = "<?xml version='1.0' encoding='UTF-8'?>" + "<root xmlns='namespace'>" +
"<child xmlns='namespace2' />" + "</root>";
private static final String XML_2_EXPECTED =
"<?xml version='1.0' encoding='UTF-8'?>" + "<root xmlns='namespace'>" + "<child xmlns='namespace2' />" +
"</root>";
private static final String XML_2_SNIPPET =
"<?xml version='1.0' encoding='UTF-8'?>" + "<child xmlns='namespace2' />";
private static final String XML_3_ENTITY =
"<predefined-entity-reference>&lt;&gt;&amp;&quot;&apos;</predefined-entity-reference>";
private AxiomHandler handler;
private OMDocument result;
@@ -87,4 +91,14 @@ public class AxiomHandlerTest extends XMLTestCase {
result.serialize(bos);
assertXMLEqual("Invalid result", XML_2_EXPECTED, bos.toString("UTF-8"));
}
public void testContentHandlerPredefinedEntityReference() throws Exception {
handler = new AxiomHandler(result, factory);
xmlReader.setContentHandler(handler);
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
xmlReader.parse(new InputSource(new StringReader(XML_3_ENTITY)));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
result.serialize(bos);
assertXMLEqual("Invalid result", XML_3_ENTITY, bos.toString("UTF-8"));
}
}