1 2 4 package org.xmlpull.v1.tests; 5 6 import junit.framework.TestCase; 8 import junit.framework.TestSuite; 9 10 import java.io.ByteArrayInputStream ; 11 import java.io.StringReader ; 12 13 import org.xmlpull.v1.XmlPullParser; 14 import org.xmlpull.v1.XmlPullParserFactory; 15 import org.xmlpull.v1.XmlPullParserException; 16 17 22 public class TestProcessDocdecl extends UtilTestCase { 23 private XmlPullParserFactory factory; 24 25 public static void main (String [] args) { 26 junit.textui.TestRunner.run (new TestSuite(TestProcessDocdecl.class)); 27 } 28 29 30 public TestProcessDocdecl(String name) { 31 super(name); 32 } 33 34 protected void setUp() throws XmlPullParserException { 35 factory = factoryNewInstance(); 36 assertEquals(false, factory.isNamespaceAware()); 39 assertEquals(false, factory.isValidating()); 40 } 42 43 private XmlPullParser newParser(boolean useNamespaces, boolean useValidation) 44 throws XmlPullParserException 45 { 46 XmlPullParser xpp = factory.newPullParser(); 47 xpp.setFeature(xpp.FEATURE_PROCESS_NAMESPACES, useNamespaces); 48 assertEquals(useNamespaces, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); 49 try { 50 xpp.setFeature(xpp.FEATURE_PROCESS_DOCDECL, true); 51 } catch(XmlPullParserException ex) { 52 return null; 53 } 54 assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL)); 55 try { 56 xpp.setFeature(xpp.FEATURE_VALIDATION, useValidation); 57 } catch(XmlPullParserException ex) { 58 return null; 59 } 60 assertEquals(useValidation, xpp.getFeature(XmlPullParser.FEATURE_VALIDATION)); 61 return xpp; 62 } 63 64 protected void tearDown() { 65 } 66 67 public void testSimpleEntity() throws Exception { 68 testSimpleEntity(false, false); 69 testSimpleEntity(true, false); 70 testSimpleEntity(false, true); 71 testSimpleEntity(true, true); 72 } 73 74 public void testAttributeNormalization() throws Exception { 75 78 } 79 80 public void testSimpleEntity(boolean useNamespaces, boolean useValidation) throws Exception { 81 XmlPullParser xpp = newParser(useNamespaces, useValidation); 82 if(xpp == null) return; 83 84 final String XML_SIMPLE_ENT_PROLOG = 86 "<?xml version='1.0'?>\n"+ 87 "<!DOCTYPE test [\n"+ 88 "<!ENTITY % YN '\"Yes\"' >\n"+ 89 "<!ELEMENT test (#PCDATA) >\n"+ 91 "<!ENTITY pub \"Éditions Gallimard\" >\n"+ 95 "<!ENTITY rights \"All rights reserved\" >\n"+ 96 "<!ENTITY book \"La Peste: Albert Camus,\n"+ 97 "© 1947 &pub;. &rights;\" >\n"+ 98 "]>\n"; 99 final String XML_SIMPLE_ENT = XML_SIMPLE_ENT_PROLOG+ 100 "<test>Publication: &book; </test>\n"; 101 102 final String PUB_ENTITY_INTERNAL_REPLACEMENT = 106 "La Peste: Albert Camus,\n"+ 107 "&pub;. &rights;"; 108 109 110 final String PUB_ENTITY_REPLACEMENT = 111 "La Peste: Albert Camus,\n"+ 112 "© 1947 Éditions Gallimard. All rights reserved"; 113 114 xpp.setInput(new StringReader ( XML_SIMPLE_ENT )); 116 checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1); 117 xpp.next(); 118 checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "test", null, false, 0); 119 xpp.next(); 120 String expectedContent = "Publication: "+PUB_ENTITY_REPLACEMENT+" "; 121 checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, expectedContent, false, -1); 122 xpp.next(); 123 checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "test", null, false, -1); 124 xpp.next(); 125 checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1); 126 127 } 129 130 public void testEntityWithMarkup() throws Exception { 131 testEntityWithMarkup(false, false); 132 testEntityWithMarkup(true, false); 133 testEntityWithMarkup(true, true); 134 testEntityWithMarkup(false, true); 135 } 136 137 public void testEntityWithMarkup(boolean useNamespaces, boolean useValidation) 138 throws Exception 139 { 140 XmlPullParser xpp = newParser(useNamespaces, useValidation); 141 if(xpp == null) return; 142 143 final String XML_REPLACE_ENT = 146 "<?xml version='1.0'?>\n"+ 147 "<!DOCTYPE test [\n"+ 148 "<!ELEMENT test (#PCDATA|p)* >\n"+ 149 "<!ELEMENT p (#PCDATA) >\n"+ 150 "<!ENTITY example \"<p>An ampersand (&#38;) may be escaped\n"+ 151 "numerically (&#38;#38;) or with a general entity\n"+ 152 "(&amp;).</p>\" >\n"+ 153 "]>\n"+ 154 "<test>&example; </test> "; 155 156 final String EXAMPLE_ENTITY_INTERNAL_REPLACEMENT = 157 "<p>An ampersand (&) may be escaped\n"+ 158 "numerically (&#38;) or with a general entity\n"+ 159 "(&amp;).</p>\n"; 160 161 final String EXAMPLE_ENTITY_TEXT_EVENT = 162 "An ampersand (&) may be escaped\n"+ 163 "numerically (&) or with a general entity\n"+ 164 "(&)."; 165 166 xpp.setInput(new StringReader ( XML_REPLACE_ENT )); 168 checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1); 169 xpp.next(); 170 checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "test", null, false, 0); 171 xpp.next(); 172 checkParserStateNs(xpp, 2, xpp.START_TAG, null, 0, "", "p", null, false, 0); 173 xpp.next(); 174 String expectedContent = EXAMPLE_ENTITY_TEXT_EVENT; 175 checkParserStateNs(xpp, 2, xpp.TEXT, null, 0, null, null, expectedContent, false, -1); 176 xpp.next(); 177 checkParserStateNs(xpp, 2, xpp.END_TAG, null, 0, "", "p", null, false, -1); 178 xpp.next(); 179 checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, " ", false, -1); 180 xpp.next(); 181 checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "test", null, false, -1); 182 xpp.next(); 183 checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1); 184 } 185 186 public void testTricky() throws Exception { 187 testTricky(false, false); 188 testTricky(true, false); 189 testTricky(true, true); 190 testTricky(false, true); 191 } 192 193 public void testTricky(boolean useNamespaces, boolean useValidation) 194 throws Exception 195 { 196 XmlPullParser xpp = newParser(useNamespaces, useValidation); 197 if(xpp == null) return; 198 199 final String XML_TRICKY = 201 "<?xml version='1.0'?>\n"+ 202 "<!DOCTYPE test [\n"+ 203 "<!ELEMENT test (#PCDATA) >\n"+ 204 "<!ENTITY % xx '%zz;'>\n"+ 205 "<!ENTITY % zz '<!ENTITY tricky \"error-prone\" >' >\n"+ 206 "%xx;\n"+ 207 "]>\n"+ 208 "<test>This sample shows a &tricky; method.</test>\n"; 209 210 211 final String EXPECTED_XML_TRICKY = 212 "This sample shows a error-prone method."; 213 214 xpp.setInput(new StringReader ( XML_TRICKY )); 216 checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1); 217 xpp.next(); 218 checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "test", null, false, 0); 219 xpp.next(); 220 String expectedContent = EXPECTED_XML_TRICKY; 221 checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, expectedContent, false, -1); 222 xpp.next(); 223 checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "test", null, false, -1); 224 xpp.next(); 225 checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1); 226 227 229 } 230 231 } 232 233 | Popular Tags |