1 19 package org.netbeans.api.xml.parsers; 20 21 import java.io.*; 22 import java.net.URL ; 23 import junit.framework.*; 24 import org.netbeans.junit.*; 25 import org.xml.sax.*; 26 import org.xml.sax.helpers.*; 27 import org.xml.sax.ext.*; 28 29 35 public class SAXEntityParserTest extends NbTestCase { 36 37 public SAXEntityParserTest(java.lang.String testName) { 38 super(testName); 39 } 40 41 public static void main(java.lang.String [] args) { 42 junit.textui.TestRunner.run(suite()); 43 } 44 45 public static Test suite() { 46 TestSuite suite = new NbTestSuite(SAXEntityParserTest.class); 47 48 return suite; 49 } 50 51 52 public void testParse() throws Exception { 53 System.out.println("testParse"); 54 55 57 InputSource input = new InputSource(new StringReader("<!ELEMENT x ANY>")); 58 input.setSystemId("StringReader"); 59 60 XMLReader peer = XMLReaderFactory.createXMLReader("org.apache.crimson.parser.XMLReaderImpl"); 61 TestDeclHandler dtdHandler = new TestDeclHandler(); 62 peer.setProperty("http://xml.org/sax/properties/declaration-handler", dtdHandler); 63 SAXEntityParser parser = new SAXEntityParser(peer, false); 64 parser.parse(input); 65 66 assertTrue("DTD entity parser did not detected 'x' decl!", dtdHandler.pass); 68 69 71 boolean exceptionThrown = false; 72 try { 73 parser.parse(new InputSource(new StringReader(""))); 74 } 75 catch (IllegalStateException ex) { 76 exceptionThrown = true; 77 } 78 finally { 79 assertTrue("Parser may not be reused!", exceptionThrown); 80 } 81 82 relativeReferenceTest(); 83 } 84 85 88 private void relativeReferenceTest() throws Exception { 89 90 final boolean pass[] = {false}; 91 92 try { 93 URL url = getClass().getResource("data/RelativeTest.dtd"); 94 System.out.println("URL:" + url); 95 InputSource input = new InputSource(url.toExternalForm()); 96 XMLReader peer = XMLReaderFactory.createXMLReader("org.apache.crimson.parser.XMLReaderImpl"); 97 peer.setDTDHandler(new DefaultHandler() { 98 public void notationDecl(String name, String publicId, String systemId) { 99 if ("notation".equals(name)) pass[0] = true; 100 } 101 }); 102 SAXEntityParser parser = new SAXEntityParser(peer, false); 103 parser.parse(input); 104 } 105 finally { 106 assertTrue("External entity not reached!", pass[0]); 107 } 108 } 109 110 111 class TestDeclHandler implements DeclHandler { 112 113 boolean pass; 114 115 public void attributeDecl(String str, String str1, String str2, String str3, String str4) throws org.xml.sax.SAXException { 116 } 117 118 public void elementDecl(String str, String str1) throws org.xml.sax.SAXException { 119 if ("x".equals(str)) pass = true; 120 } 121 122 public void externalEntityDecl(String str, String str1, String str2) throws org.xml.sax.SAXException { 123 } 124 125 public void internalEntityDecl(String str, String str1) throws org.xml.sax.SAXException { 126 } 127 128 } 129 130 } 131 | Popular Tags |