1 7 8 package org.dom4j; 9 10 import junit.textui.TestRunner; 11 12 import java.util.List ; 13 14 import javax.xml.parsers.SAXParser ; 15 import javax.xml.parsers.SAXParserFactory ; 16 17 import org.dom4j.io.SAXContentHandler; 18 19 import org.xml.sax.XMLReader ; 20 21 public class SAXContentHandlerTest extends AbstractTestCase { 22 private XMLReader xmlReader; 23 24 protected String [] testDocuments = {"/xml/test/test_schema.xml", 25 "/xml/test/encode.xml", "/xml/fibo.xml", 26 "/xml/test/schema/personal-prefix.xsd", "/xml/test/soap2.xml"}; 27 28 public static void main(String [] args) { 29 TestRunner.run(SAXContentHandlerTest.class); 30 } 31 32 protected void setUp() throws Exception { 33 super.setUp(); 34 35 SAXParserFactory spf = SAXParserFactory.newInstance(); 36 spf.setNamespaceAware(true); 37 38 SAXParser parser = spf.newSAXParser(); 39 xmlReader = parser.getXMLReader(); 40 } 41 42 public void testSAXContentHandler() throws Exception { 43 SAXContentHandler contentHandler = new SAXContentHandler(); 44 xmlReader.setContentHandler(contentHandler); 45 xmlReader.setDTDHandler(contentHandler); 46 xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", 47 contentHandler); 48 49 for (int i = 0, size = testDocuments.length; i < size; i++) { 50 Document docFromSAXReader = getDocument(testDocuments[i]); 51 52 xmlReader.parse(getFile(testDocuments[i]).toString()); 53 54 Document docFromSAXContentHandler = contentHandler.getDocument(); 55 56 docFromSAXContentHandler.setName(docFromSAXReader.getName()); 57 58 assertDocumentsEqual(docFromSAXReader, docFromSAXContentHandler); 59 assertEquals(docFromSAXReader.asXML(), docFromSAXContentHandler 60 .asXML()); 61 } 62 } 63 64 public void testBug926713() throws Exception { 65 Document doc = getDocument("/xml/test/cdata.xml"); 66 Element foo = doc.getRootElement(); 67 Element bar = foo.element("bar"); 68 List content = bar.content(); 69 assertEquals(3, content.size()); 70 assertEquals(Node.TEXT_NODE, ((Node) content.get(0)).getNodeType()); 71 assertEquals(Node.CDATA_SECTION_NODE, ((Node) content.get(1)) 72 .getNodeType()); 73 assertEquals(Node.TEXT_NODE, ((Node) content.get(2)).getNodeType()); 74 } 75 } 76 77 113
| Popular Tags
|