1 23 24 package org.enhydra.xml.xmlc.parsers.xerces; 25 26 import org.enhydra.apache.xerces.framework.XMLAttrList; 27 import org.enhydra.apache.xerces.framework.XMLContentSpec; 28 import org.enhydra.apache.xerces.readers.XMLEntityHandler; 29 import org.enhydra.apache.xerces.utils.QName; 30 import org.enhydra.apache.xerces.utils.StringPool; 31 import org.enhydra.apache.xerces.validators.common.XMLAttributeDecl; 32 import org.enhydra.xml.io.InputSourceOps; 33 import org.enhydra.xml.xmlc.XMLCError; 34 import org.enhydra.xml.xmlc.parsers.ParseTracer; 35 import org.xml.sax.EntityResolver ; 36 import org.xml.sax.InputSource ; 37 38 42 class XercesTracer { 43 46 private ParseTracer fTracer; 47 48 51 private StringPool fStringPool; 52 53 56 private boolean fIsEnabled; 57 58 61 protected XercesTracer(StringPool stringPool, 62 ParseTracer tracer) { 63 fStringPool = stringPool; 64 fTracer = tracer; 65 fIsEnabled = (fTracer != null) && fTracer.enabled(); 66 } 67 68 71 public boolean enabled() { 72 return fIsEnabled; 73 } 74 75 78 private String getEntityTypeString(int entityType) { 79 switch (entityType) { 80 case XMLEntityHandler.ENTITYTYPE_INTERNAL_PE: 81 return "INTERNAL_PE"; 82 case XMLEntityHandler.ENTITYTYPE_EXTERNAL_PE: 83 return "EXTERNAL_PE"; 84 case XMLEntityHandler.ENTITYTYPE_INTERNAL: 85 return "INTERNAL"; 86 case XMLEntityHandler.ENTITYTYPE_EXTERNAL: 87 return "EXTERNAL"; 88 case XMLEntityHandler.ENTITYTYPE_UNPARSED: 89 return "UNPARSED"; 90 case XMLEntityHandler.ENTITYTYPE_DOCUMENT: 91 return "DOCUMENT"; 92 case XMLEntityHandler.ENTITYTYPE_EXTERNAL_SUBSET: 93 return "EXTERNAL_SUBSET"; 94 default: 95 throw new XMLCError("Xerces parser bug: request for entity type"); 96 } 97 } 98 99 102 private String getEntityContextString(int entityContent) { 103 switch (entityContent) { 104 case XMLEntityHandler.ENTITYREF_IN_ATTVALUE: 105 return "IN_ATTVALUE"; 106 case XMLEntityHandler.ENTITYREF_IN_DEFAULTATTVALUE: 107 return "IN_DEFAULTATTVALUE"; 108 case XMLEntityHandler.ENTITYREF_IN_CONTENT: 109 return "IN_CONTENT"; 110 case XMLEntityHandler.ENTITYREF_IN_DTD_AS_MARKUP: 111 return "IN_DTD_AS_MARKUP"; 112 case XMLEntityHandler.ENTITYREF_IN_ENTITYVALUE: 113 return "IN_ENTITYVALUE"; 114 case XMLEntityHandler.ENTITYREF_IN_DTD_WITHIN_MARKUP: 115 return "IN_DTD_WITHIN_MARKUP"; 116 case XMLEntityHandler.ENTITYREF_DOCUMENT: 117 return "DOCUMENT"; 118 case XMLEntityHandler.ENTITYREF_EXTERNAL_SUBSET: 119 return "EXTERNAL_SUBSET"; 120 default: 121 throw new XMLCError("Xerces parser bug: request for unknown entity context"); 122 } 123 } 124 125 128 private String getContentSpecTypeString(int contentSpecType) { 129 switch (contentSpecType) { 130 case XMLContentSpec.CONTENTSPECNODE_LEAF: 131 return "LEAF"; 132 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE: 133 return "ZERO_OR_ONE"; 134 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE: 135 return "ZERO_OR_MORE"; 136 case XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE: 137 return "ONE_OR_MORE"; 138 case XMLContentSpec.CONTENTSPECNODE_CHOICE: 139 return "CHOICE"; 140 case XMLContentSpec.CONTENTSPECNODE_SEQ: 141 return "SEQ"; 142 case XMLContentSpec.CONTENTSPECNODE_ANY: 143 return "ANY"; 144 case XMLContentSpec.CONTENTSPECNODE_ANY_OTHER: 145 return "ANY_OTHER"; 146 case XMLContentSpec.CONTENTSPECNODE_ANY_NS: 147 return "ANY_NS"; 148 case XMLContentSpec.CONTENTSPECNODE_ANY_LAX: 149 return "ANY_LAX"; 150 case XMLContentSpec.CONTENTSPECNODE_ANY_OTHER_LAX: 151 return "ANY_OTHER_LAX"; 152 case XMLContentSpec.CONTENTSPECNODE_ANY_NS_LAX: 153 return "ANY_NS_LAX"; 154 case XMLContentSpec.CONTENTSPECNODE_ANY_SKIP: 155 return "ANY_SKIP"; 156 case XMLContentSpec.CONTENTSPECNODE_ANY_OTHER_SKIP: 157 return "ANY_OTHER_SKIP"; 158 case XMLContentSpec.CONTENTSPECNODE_ANY_NS_SKIP: 159 return "ANY_NS_SKIP"; 160 default: 161 throw new XMLCError("Xerces parser bug: request for unknown contentspec type"); 162 } 163 } 164 165 168 private String getString(int index) { 169 return fStringPool.toString(index); 170 } 171 172 175 private String formatQName(QName qname) { 176 return getString(qname.rawname); 177 } 178 179 182 public void resolveEntity(String publicId, 183 String systemId, 184 InputSource source) { 185 if (fIsEnabled) { 186 fTracer.trace("resolveEntity: \"" 187 + publicId + "\" \"" 188 + systemId + "\" => " 189 + InputSourceOps.getName(source)); 190 } 191 192 } 193 194 197 public void startDocument() { 198 if (fIsEnabled) { 199 fTracer.trace("startDocument"); 200 } 201 } 202 203 207 public void endDocument() { 208 if (fIsEnabled) { 209 fTracer.trace("endDocument"); 210 } 211 } 212 213 216 public void xmlDecl(int version, int encoding, int standalone) throws Exception { 217 if (fIsEnabled) { 218 fTracer.trace("xmlDecl: " + getString(version) 219 + " " + getString(encoding) 220 + " " + getString(standalone)); 221 } 222 } 223 224 227 public void textDecl(int version, int encoding) throws Exception { 228 if (fIsEnabled) { 229 fTracer.trace("textDecl: " + getString(version) 230 + " " + getString(encoding)); 231 } 232 } 233 234 238 public void startElement(QName element, 239 XMLAttrList attrList, 240 int attrListHandle) { 241 if (fIsEnabled) { 242 StringBuffer buf = new StringBuffer ("startElement: " + 243 formatQName(element)); 244 int attrIndex = attrListHandle; 245 while (attrIndex >= 0) { 246 if (attrList.isSpecified(attrIndex)) { 247 String uri = getString(attrList.getAttrURI(attrIndex)); 248 buf.append(" "); 249 if (uri != null) { 250 buf.append("<"); 251 buf.append(uri); 252 buf.append(">"); 253 } 254 buf.append(getString(attrList.getAttrName(attrIndex))); 255 buf.append("=\""); 256 buf.append(getString(attrList.getAttValue(attrIndex))); 257 buf.append("\""); 258 } 259 attrIndex = attrList.getNextAttr(attrIndex); 260 } 261 fTracer.trace(buf.toString()); 262 fTracer.enter(); 263 } 264 } 265 266 270 public void endElement(QName element) { 271 if (fIsEnabled) { 272 fTracer.leave(); 273 fTracer.trace("endElement: " + formatQName(element)); 274 } 275 } 276 277 282 public void startNamespaceDeclScope(int prefix, int uri) { 283 if (fIsEnabled) { 284 fTracer.trace("startNamespaceDeclScope: " + getString(prefix) 285 + " " + getString(uri)); 286 fTracer.enter(); 287 } 288 } 289 290 295 public void endNamespaceDeclScope(int prefix) { 296 if (fIsEnabled) { 297 fTracer.leave(); 298 fTracer.trace("endNamespaceDeclScope: " + getString(prefix)); 299 } 300 } 301 302 306 public void startEntityReference(int entityName, 307 int entityType, 308 int entityContext, 309 boolean processed) { 310 if (fIsEnabled) { 311 fTracer.trace("startEntityReference: " 312 + getString(entityName) 313 + " " + getEntityTypeString(entityType) 314 + " " + getEntityContextString(entityContext) 315 + ((processed) ? "" : " (ignored)")); 316 fTracer.enter(); 317 } 318 } 319 320 324 public void endEntityReference(int entityName, 325 int entityType, 326 int entityContext, 327 boolean processed) { 328 if (fIsEnabled) { 329 fTracer.leave(); 330 fTracer.trace("endEntityReference: " + getString(entityName) 331 + ((processed) ? "" : " (ignored)")); 332 333 } 334 } 335 336 340 public void startCDATA() { 341 if (fIsEnabled) { 342 fTracer.trace("startCDATA"); 343 fTracer.enter(); 344 } 345 } 346 347 351 public void endCDATA() { 352 if (fIsEnabled) { 353 fTracer.leave(); 354 fTracer.trace("endCDATA"); 355 } 356 } 357 358 362 public void processingInstruction(int target, 363 int data) { 364 if (fIsEnabled) { 365 fTracer.trace("processingInstruction: " + getString(target)); 366 } 367 } 368 369 373 public void comment(int comment) { 374 if (fIsEnabled) { 375 fTracer.trace("comment: " + getString(comment)); 376 } 377 } 378 379 383 public void characters(char ch[], 384 int start, 385 int length) { 386 if (fIsEnabled) { 387 fTracer.trace("characters: \"" + new String (ch, start, length) + "\""); 388 } 389 } 390 391 395 public void ignorableWhitespace(char ch[], 396 int start, 397 int length) { 398 if (fIsEnabled) { 399 fTracer.trace("ignorableWhitespace: " + length); 400 } 401 } 402 403 406 public void startDTD(QName rootElement, 407 int publicId, 408 int systemId) { 409 if (fIsEnabled) { 410 fTracer.trace("startDTD: " + formatQName(rootElement) 411 + " " + getString(publicId) 412 + " " + getString(systemId)); 413 fTracer.enter(); 414 } 415 } 416 417 420 public void internalSubset(int internalSubset) { 421 if (fIsEnabled) { 422 fTracer.trace("internalSubset: " + getString(internalSubset)); 423 } 424 } 425 426 429 public void endDTD() { 430 if (fIsEnabled) { 431 fTracer.leave(); 432 } 433 } 434 435 440 public void elementDecl(QName elementDecl, 441 int contentSpecType, 442 int contentSpecIndex, 443 XMLContentSpec.Provider contentSpecProvider) { 444 if (fIsEnabled) { 445 fTracer.trace("elementDecl: " + formatQName(elementDecl) 446 + " " + getContentSpecTypeString(contentSpecType) 447 + " " + XMLContentSpec.toString(contentSpecProvider, 448 fStringPool, 449 contentSpecIndex)); 450 } 451 } 452 453 456 private String getAttrTypeString(int attType, 457 boolean attList) { 458 switch (attType) { 459 case XMLAttributeDecl.TYPE_ENTITY: 460 return (attList ? "ENTITIES" : "ENTITY"); 461 462 case XMLAttributeDecl.TYPE_ID: 463 return "ID"; 464 465 case XMLAttributeDecl.TYPE_IDREF: 466 return (attList ? "IDREFS" : "IDREF"); 467 468 case XMLAttributeDecl.TYPE_NMTOKEN: 469 return (attList ? "NMTOKENS" : "NMTOKEN"); 470 471 case XMLAttributeDecl.TYPE_NOTATION: 472 return "NOTATION"; 473 474 case XMLAttributeDecl.TYPE_CDATA: 475 return "string"; 476 default: 477 return "invalid-attType"; 478 } 479 } 480 481 486 public void attlistDecl(QName elementDecl, 487 QName attributeDecl, 488 int attType, 489 boolean attList, 490 String enumString, 491 int attDefaultType, 492 int attDefaultValue) { 493 if (fIsEnabled) { 495 fTracer.trace("attlistDecl: " + formatQName(elementDecl) 496 + " " + formatQName(attributeDecl) 497 + " " + getAttrTypeString(attType, attList) 498 + " " + enumString 499 + " " + getString(attDefaultType) 500 + " " + getString(attDefaultValue)); 501 } 502 } 503 504 509 public void internalPEDecl(int entityName, 510 int entityValue) { 511 if (fIsEnabled) { 512 fTracer.trace("internalPEDecl: " + getString(entityName) 513 + " \"" + getString(entityValue) + "\""); 514 } 515 } 516 517 522 public void externalPEDecl(int entityName, 523 int publicId, 524 int systemId) { 525 if (fIsEnabled) { 526 fTracer.trace("externalPEDecl: " + getString(entityName) 527 + " " + getString(publicId) 528 + " " + getString(systemId)); 529 } 530 } 531 532 537 public void internalEntityDecl(int entityName, 538 int entityValue) { 539 if (fIsEnabled) { 540 fTracer.trace("internalEntityDecl: " + getString(entityName) 541 + " \"" + getString(entityValue) + "\""); 542 } 543 } 544 545 550 public void externalEntityDecl(int entityName, 551 int publicId, 552 int systemId) { 553 if (fIsEnabled) { 554 fTracer.trace("externalEntityDecl: " + getString(entityName) 555 + " " + getString(publicId) 556 + " " + getString(systemId)); 557 } 558 } 559 560 565 public void unparsedEntityDecl(int entityName, 566 int publicId, 567 int systemId, 568 int notationName) { 569 if (fIsEnabled) { 570 fTracer.trace("unparsedEntityDecl: " + getString(entityName) 571 + " " + getString(publicId) 572 + " " + getString(systemId) 573 + " " + getString(notationName)); 574 } 575 } 576 577 582 public void notationDecl(int notationName, 583 int publicId, 584 int systemId) { 585 if (fIsEnabled) { 586 fTracer.trace("notationDecl: " + getString(notationName) 587 + " " + getString(publicId) 588 + " " + getString(systemId)); 589 } 590 } 591 } 592 | Popular Tags |