1 23 24 package tests.org.enhydra.xml.xhtml.dominfo; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.io.StringReader ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import java.util.TreeMap ; 34 35 import org.enhydra.apache.xerces.framework.XMLAttrList; 36 import org.enhydra.apache.xerces.framework.XMLContentSpec; 37 import org.enhydra.apache.xerces.framework.XMLDocumentHandler; 38 import org.enhydra.apache.xerces.framework.XMLParser; 39 import org.enhydra.apache.xerces.utils.QName; 40 import org.enhydra.error.FatalExceptionError; 41 import org.enhydra.xml.io.ErrorReporter; 42 import org.xml.sax.EntityResolver ; 43 import org.xml.sax.InputSource ; 44 import org.xml.sax.SAXException ; 45 46 50 class DTDInfo { 51 54 public class ElementInfo { 55 56 private final String fRawName; 57 private Map fAttrs = new TreeMap (); 58 59 60 61 ElementInfo(String rawName) { 62 fRawName = rawName; 63 } 64 65 66 public void addAttr(AttrInfo attr) { 67 fAttrs.put(attr.getRawName(), attr); 68 } 69 70 71 public String getRawName() { 72 return fRawName; 73 } 74 75 76 public Set getAttrNames() { 77 return fAttrs.keySet(); 78 } 79 80 81 public AttrInfo getAttr(String name) { 82 AttrInfo attr = (AttrInfo)fAttrs.get(name); 83 if (attr == null) { 84 throw new Error ("Can't find attr \"" + attr + "\" of element \"" + fRawName + "\""); 85 } 86 return attr; 87 } 88 89 90 public void dump(PrintWriter out, 91 int level) { 92 PrintUtils.printIndent(out, level); 93 out.println(fRawName); 94 Iterator attrNames = getAttrNames().iterator(); 95 while (attrNames.hasNext()) { 96 getAttr((String )attrNames.next()).dump(out, level+1); 97 } 98 } 99 100 101 public String toString() { 102 return fRawName; 103 } 104 } 105 106 109 public class AttrInfo { 110 111 private ElementInfo fElement; 112 113 114 private String fRawName; 115 116 117 AttrInfo(ElementInfo element, 118 String rawName) { 119 fElement = element; 120 fRawName = rawName; 121 } 122 123 124 public ElementInfo getElement() { 125 return fElement; 126 } 127 128 129 public String getRawName() { 130 return fRawName; 131 } 132 133 134 public void dump(PrintWriter out, 135 int level) { 136 PrintUtils.printIndent(out, level); 137 out.println(fRawName); 138 } 139 140 141 public String toString() { 142 return fRawName; 143 } 144 } 145 146 149 private String fDTDPath; 150 151 154 private Map fElements = new TreeMap (); 155 156 161 private class DTDParser extends XMLParser 162 implements EntityResolver , XMLDocumentHandler, XMLDocumentHandler.DTDHandler { 163 164 167 public DTDParser() throws IOException , SAXException { 168 ErrorReporter errorReporter = new ErrorReporter(); 169 170 initHandlers(true, this, this); 172 setEntityResolver(this); 173 setErrorHandler(errorReporter); 174 setAllowJavaEncodings(true); 175 setNamespaces(true); 176 setValidation(true); 177 } 178 179 182 public void parse(String dtdPath) throws IOException , SAXException { 183 String xmlDoc = "<?xml version=\"1.0\"?>" 184 + "<!DOCTYPE html SYSTEM \"" +dtdPath + "\">" 185 + "<html></html>"; 186 StringReader reader = new StringReader (xmlDoc); 187 188 InputSource input = new InputSource (); 189 input.setCharacterStream(reader); 190 input.setSystemId(dtdPath); 191 super.parse(input); 192 } 193 194 197 private String getString(int index) { 198 return fStringPool.toString(index); 199 } 200 201 205 public InputSource resolveEntity(String publicId, 206 String systemId) throws SAXException , IOException { 207 return null; 208 } 209 210 214 public void startDocument() throws Exception { 215 } 216 217 221 public void endDocument() throws Exception { 222 } 223 224 227 public void xmlDecl(int version, int encoding, int standalone) throws Exception { 228 } 229 230 234 public void textDecl(int version, int encoding) throws Exception { 235 } 236 237 242 public void startNamespaceDeclScope(int prefix, 243 int uri) throws Exception { 244 } 245 246 251 public void endNamespaceDeclScope(int prefix) throws Exception { 252 } 253 254 258 public void startElement(QName element, 259 XMLAttrList attrList, 260 int attrListHandle) throws Exception { 261 } 262 263 267 public void endElement(QName element) throws Exception { 268 } 269 270 277 public void startEntityReference(int entityName, 278 int entityType, 279 int entityContext) throws Exception { 280 } 281 282 286 public void endEntityReference(int entityName, 287 int entityType, 288 int entityContext) throws Exception { 289 } 290 291 295 public void characters(int data) throws Exception { 296 throw new Error ("fatal error: method that should not be invoked called"); 297 } 298 299 303 public void ignorableWhitespace(int data) throws Exception { 304 throw new Error ("fatal error: method that should not be invoked called"); 305 } 306 307 311 public void startCDATA() { 312 } 313 314 318 public void endCDATA() { 319 } 320 321 326 public void processingInstruction(int target, 327 int data) throws Exception { 328 } 329 330 334 public void comment(int comment) throws Exception { 335 } 336 337 341 public void characters(char ch[], 342 int start, 343 int length) throws Exception { 344 } 345 346 350 public void ignorableWhitespace(char ch[], 351 int start, 352 int length) throws Exception { 353 } 354 355 358 public void startDTD(QName rootElement, 359 int publicId, 360 int systemId) { 361 } 362 363 366 public void internalSubset(int internalSubset) { 367 } 368 369 372 public void endDTD() { 373 } 374 375 380 public void elementDecl(QName elementDecl, 381 int contentSpecType, 382 int contentSpecIndex, 383 XMLContentSpec.Provider contentSpecProvider) throws Exception { 384 if (fElements.containsKey(getString(elementDecl.rawname))) { 385 throw new Error ("Duplicate element definintion: " + elementDecl.rawname); 386 } 387 ElementInfo element = new ElementInfo(getString(elementDecl.rawname)); 388 fElements.put(element.getRawName(), element); 389 } 390 391 396 public void attlistDecl(QName elementDecl, 397 QName attributeDecl, 398 int attType, 399 boolean attList, 400 String enumString, 401 int attDefaultType, 402 int attDefaultValue) throws Exception { 403 ElementInfo element = (ElementInfo)fElements.get(getString(elementDecl.rawname)); 404 if (!fElements.containsKey(getString(elementDecl.rawname))) { 405 throw new Error ("Element definintion not found when adding attribute: " + elementDecl.rawname); 406 } 407 element.addAttr(new AttrInfo(element, getString(attributeDecl.rawname))); 408 } 409 410 415 public void internalPEDecl(int entityName, 416 int entityValue) { 417 } 418 419 424 public void externalPEDecl(int entityName, 425 int publicId, 426 int systemId) { 427 } 428 429 434 public void internalEntityDecl(int entityName, 435 int entityValue) { 436 } 437 438 443 public void externalEntityDecl(int entityName, 444 int publicId, 445 int systemId) { 446 } 447 448 453 public void unparsedEntityDecl(int entityName, 454 int publicId, 455 int systemId, 456 int notationName) { 457 } 458 459 464 public void notationDecl(int notationName, 465 int publicId, 466 int systemId) { 467 } 468 } 469 470 473 public DTDInfo(String dtdPath) { 474 try { 475 fDTDPath = dtdPath; 476 DTDParser parser = new DTDParser(); 477 parser.parse(dtdPath); 478 } catch (IOException except) { 479 throw new FatalExceptionError(except); 480 } catch (SAXException except) { 481 throw new FatalExceptionError(except); 482 } 483 } 484 485 488 public String getDTDPath() { 489 return fDTDPath; 490 } 491 492 493 public Set getElementNames() { 494 return fElements.keySet(); 495 } 496 497 498 public ElementInfo getElement(String name) { 499 ElementInfo element = (ElementInfo)fElements.get(name); 500 if (element == null) { 501 throw new Error ("Can't find element \"" + name + "\""); 502 } 503 return element; 504 } 505 506 507 public Set getElementInfoSet() { 508 HashSet set = new HashSet (); 509 510 Iterator elementNames = getElementNames().iterator(); 511 while (elementNames.hasNext()) { 512 set.add(getElement((String )elementNames.next())); 513 } 514 return set; 515 } 516 517 520 public void dump(PrintWriter out) { 521 Iterator elementNames = getElementNames().iterator(); 522 while (elementNames.hasNext()) { 523 getElement((String )elementNames.next()).dump(out, 0); 524 } 525 } 526 527 530 public static void main(String [] args) throws IOException , SAXException { 531 if (args.length != 1) { 532 System.err.println("Wrong # args: DTDInfo dtdfile"); 533 System.exit(1); 534 } 535 DTDInfo dtdInfo = new DTDInfo(args[0]); 536 dtdInfo.dump(new PrintWriter (System.out, true)); 537 } 538 } 539 | Popular Tags |