1 7 8 package org.dom4j.io; 9 10 import junit.framework.AssertionFailedError; 11 12 import junit.textui.TestRunner; 13 14 import java.io.FileInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import org.dom4j.AbstractTestCase; 22 import org.dom4j.Document; 23 import org.dom4j.DocumentType; 24 import org.dom4j.dtd.AttributeDecl; 25 import org.dom4j.dtd.ElementDecl; 26 import org.dom4j.dtd.ExternalEntityDecl; 27 import org.dom4j.dtd.InternalEntityDecl; 28 import org.dom4j.tree.DefaultDocumentType; 29 30 import org.xml.sax.EntityResolver ; 31 import org.xml.sax.InputSource ; 32 import org.xml.sax.SAXException ; 33 34 46 public class DTDTest extends AbstractTestCase { 47 51 private static final String XML_INTERNAL_FILE = "xml/dtd/internal.xml"; 52 53 60 private static final String XML_EXTERNAL_FILE = "xml/dtd/external.xml"; 61 62 69 private static final String XML_MIXED = "xml/dtd/mixed.xml"; 70 71 77 private static final String DTD_FILE = "xml/dtd/sample.dtd"; 78 79 83 protected static final String DTD_PUBLICID = "-//dom4j//DTD sample"; 84 85 89 protected static final String DTD_SYSTEM_ID = "sample.dtd"; 90 91 public static void main(String [] args) { 92 TestRunner.run(DTDTest.class); 93 } 94 95 98 105 public void testInternalDTDSubset() throws Exception { 106 115 DocumentType expected = new DefaultDocumentType(); 116 117 expected.setElementName("greeting"); 118 119 expected.setInternalDeclarations(getInternalDeclarations()); 120 121 125 try { 126 assertSameDocumentType(expected, readDocument( 127 XML_INTERNAL_FILE, true, false).getDocType()); 128 } catch (AssertionFailedError ex) { 129 throw ex; 130 } catch (Throwable t) { 131 fail("Not expecting: " + t); 132 } 133 } 134 135 139 public void testExternalDTDSubset() { 140 143 DocumentType expected = new DefaultDocumentType("another-greeting", 144 null, DTD_SYSTEM_ID); 145 146 expected.setExternalDeclarations(getExternalDeclarations()); 147 148 152 try { 153 assertSameDocumentType(expected, readDocument( 154 XML_EXTERNAL_FILE, false, true).getDocType()); 155 } catch (AssertionFailedError ex) { 156 throw ex; 157 } catch (Throwable t) { 158 fail("Not expecting: " + t); 159 } 160 } 161 162 166 public void testMixedDTDSubset() { 167 170 DocumentType expected = new DefaultDocumentType("another-greeting", 171 null, DTD_SYSTEM_ID); 172 173 expected.setInternalDeclarations(getInternalDeclarations()); 174 175 expected.setExternalDeclarations(getExternalDeclarations()); 176 177 181 try { 182 assertSameDocumentType(expected, readDocument(XML_MIXED, 183 true, true).getDocType()); 184 } catch (AssertionFailedError ex) { 185 throw ex; 186 } catch (Throwable t) { 187 fail("Not expecting: " + t); 188 } 189 } 190 191 194 210 protected List getInternalDeclarations() { 211 List decls = new ArrayList (); 212 213 decls.add(new ElementDecl("greeting", "(#PCDATA)")); 214 215 decls.add(new AttributeDecl("greeting", "foo", "ID", "#IMPLIED", null)); 216 217 decls.add(new InternalEntityDecl("%boolean", "( true | false )")); 218 219 return decls; 220 } 221 222 229 protected List getExternalDeclarations() { 230 List decls = new ArrayList (); 231 232 decls.add(new ElementDecl("another-greeting", "(#PCDATA)")); 233 234 return decls; 235 } 236 237 249 protected void assertSameDocumentType(DocumentType expected, 250 DocumentType actual) { 251 254 if (expected == null) { 255 if (actual == null) { 256 return; } else { 258 fail("Not expecting DOCTYPE."); 259 } 260 } else { 261 264 if (actual == null) { 265 fail("Expecting DOCTYPE"); 266 } 267 268 log("Expected DocumentType:\n" + expected.toString()); 269 270 log("Actual DocumentType:\n" + actual.toString()); 271 272 assertSameDTDSubset("Internal", expected.getInternalDeclarations(), 274 actual.getInternalDeclarations()); 275 276 assertSameDTDSubset("External", expected.getExternalDeclarations(), 278 actual.getExternalDeclarations()); 279 } 280 } 281 282 300 protected void assertSameDTDSubset(String txt, List expected, List actual) { 301 304 if (expected == null) { 305 if (actual == null) { 306 return; } else { 308 fail("Not expecting " + txt + " DTD subset."); 309 } 310 } else { 311 314 if (actual == null) { 315 fail("Expecting " + txt + " DTD subset."); 316 } 317 318 321 assertEquals(txt + " DTD subset has correct #of declarations" 322 + ": expected=[" + expected.toString() + "]" + ", actual=[" 323 + actual.toString() + "]", expected.size(), actual.size()); 324 325 329 Iterator itr1 = expected.iterator(); 330 331 Iterator itr2 = actual.iterator(); 332 333 while (itr1.hasNext()) { 334 Object obj1 = itr1.next(); 335 336 Object obj2 = itr2.next(); 337 338 assertEquals(txt + " DTD subset: Same type of declaration", 339 obj1.getClass().getName(), obj2.getClass().getName()); 340 341 if (obj1 instanceof AttributeDecl) { 342 assertSameDecl((AttributeDecl) obj1, (AttributeDecl) obj2); 343 } else if (obj1 instanceof ElementDecl) { 344 assertSameDecl((ElementDecl) obj1, (ElementDecl) obj2); 345 } else if (obj1 instanceof InternalEntityDecl) { 346 assertSameDecl((InternalEntityDecl) obj1, 347 (InternalEntityDecl) obj2); 348 } else if (obj1 instanceof ExternalEntityDecl) { 349 assertSameDecl((ExternalEntityDecl) obj1, 350 (ExternalEntityDecl) obj2); 351 } else { 352 throw new AssertionError ("Unexpected declaration type: " 353 + obj1.getClass()); 354 } 355 } 356 } 357 } 358 359 368 public void assertSameDecl(AttributeDecl expected, AttributeDecl actual) { 369 assertEquals("attributeName is correct", expected.getAttributeName(), 370 actual.getAttributeName()); 371 372 assertEquals("elementName is correct", expected.getElementName(), 373 actual.getElementName()); 374 375 assertEquals("type is correct", expected.getType(), actual.getType()); 376 377 assertEquals("value is not correct", expected.getValue(), actual 378 .getValue()); 379 380 assertEquals("valueDefault is correct", expected.getValueDefault(), 381 actual.getValueDefault()); 382 383 assertEquals("toString() is correct", expected.toString(), actual 384 .toString()); 385 } 386 387 396 protected void assertSameDecl(ElementDecl expected, ElementDecl actual) { 397 assertEquals("name is correct", expected.getName(), actual.getName()); 398 399 assertEquals("model is not correct", expected.getModel(), actual 400 .getModel()); 401 402 assertEquals("toString() is correct", expected.toString(), actual 403 .toString()); 404 } 405 406 415 protected void assertSameDecl(InternalEntityDecl expected, 416 InternalEntityDecl actual) { 417 assertEquals("name is correct", expected.getName(), actual.getName()); 418 419 assertEquals("value is not correct", expected.getValue(), actual 420 .getValue()); 421 422 assertEquals("toString() is correct", expected.toString(), actual 423 .toString()); 424 } 425 426 435 protected void assertSameDecl(ExternalEntityDecl expected, 436 ExternalEntityDecl actual) { 437 assertEquals("name is correct", expected.getName(), actual.getName()); 438 439 assertEquals("publicID is correct", expected.getPublicID(), actual 440 .getPublicID()); 441 442 assertEquals("systemID is correct", expected.getSystemID(), actual 443 .getSystemID()); 444 445 assertEquals("toString() is correct", expected.toString(), actual 446 .toString()); 447 } 448 449 470 protected Document readDocument(String resourceName, 471 boolean includeInternal, boolean includeExternal) throws Exception { 472 SAXReader reader = new SAXReader(); 473 474 reader.setIncludeInternalDTDDeclarations(includeInternal); 475 476 reader.setIncludeExternalDTDDeclarations(includeExternal); 477 478 reader.setEntityResolver(new MyEntityResolver(DTD_FILE, 479 DTD_PUBLICID, DTD_SYSTEM_ID)); 480 481 return getDocument(resourceName, reader); 482 } 483 484 487 protected static class MyEntityResolver implements EntityResolver { 488 private String resourceName; 489 490 private String pubId; 491 492 private String sysId; 493 494 public MyEntityResolver(String localResourceName, String publicId, 495 String systemId) { 496 resourceName = localResourceName; 497 498 sysId = systemId; 499 } 500 501 public InputSource resolveEntity(String publicId, String systemId) 502 throws SAXException , IOException { 503 if (pubId != null) { 504 if (pubId.equals(publicId)) { 505 return new InputSource (getInputStream(resourceName)); 506 } 507 } 508 509 if (sysId.equals(systemId)) { 510 return new InputSource (getInputStream(resourceName)); 511 } else { 512 return null; 513 } 514 } 515 516 528 protected InputStream getInputStream(String localResourceName) 529 throws IOException { 530 InputStream is = new FileInputStream (localResourceName); 531 532 return is; 533 } 534 } 535 } 536 537 573 | Popular Tags |