1 3 19 20 package com.sun.org.apache.xml.internal.resolver.readers; 21 22 import java.util.Stack ; 23 import java.util.Vector ; 24 import java.util.Enumeration ; 25 import com.sun.org.apache.xml.internal.resolver.Catalog; 26 import com.sun.org.apache.xml.internal.resolver.CatalogEntry; 27 import com.sun.org.apache.xml.internal.resolver.CatalogException; 28 import com.sun.org.apache.xml.internal.resolver.helpers.PublicId; 29 30 import org.xml.sax.*; 31 import org.w3c.dom.*; 32 33 44 public class OASISXMLCatalogReader extends SAXCatalogReader implements SAXCatalogParser { 45 48 protected Catalog catalog = null; 49 50 51 public static final String namespaceName = "urn:oasis:names:tc:entity:xmlns:xml:catalog"; 52 53 54 public static final String tr9401NamespaceName = "urn:oasis:names:tc:entity:xmlns:tr9401:catalog"; 55 56 protected Stack baseURIStack = new Stack (); 57 protected Stack overrideStack = new Stack (); 58 protected Stack namespaceStack = new Stack (); 59 60 61 public void setCatalog (Catalog catalog) { 62 this.catalog = catalog; 63 debug = catalog.getCatalogManager().debug; 64 } 65 66 67 public Catalog getCatalog () { 68 return catalog; 69 } 70 71 77 protected boolean inExtensionNamespace() { 78 boolean inExtension = false; 79 80 Enumeration elements = namespaceStack.elements(); 81 while (!inExtension && elements.hasMoreElements()) { 82 String ns = (String ) elements.nextElement(); 83 if (ns == null) { 84 inExtension = true; 85 } else { 86 inExtension = (!ns.equals(tr9401NamespaceName) 87 && !ns.equals(namespaceName)); 88 } 89 } 90 91 return inExtension; 92 } 93 94 97 98 public void setDocumentLocator (Locator locator) { 99 return; 100 } 101 102 103 public void startDocument () 104 throws SAXException { 105 baseURIStack.push(catalog.getCurrentBase()); 106 overrideStack.push(catalog.getDefaultOverride()); 107 return; 108 } 109 110 111 public void endDocument () 112 throws SAXException { 113 return; 114 } 115 116 128 public void startElement (String namespaceURI, 129 String localName, 130 String qName, 131 Attributes atts) 132 throws SAXException { 133 134 int entryType = -1; 135 Vector entryArgs = new Vector (); 136 137 namespaceStack.push(namespaceURI); 138 139 boolean inExtension = inExtensionNamespace(); 140 141 if (namespaceURI != null && namespaceName.equals(namespaceURI) 142 && !inExtension) { 143 145 if (atts.getValue("xml:base") != null) { 146 String baseURI = atts.getValue("xml:base"); 147 entryType = Catalog.BASE; 148 entryArgs.add(baseURI); 149 baseURIStack.push(baseURI); 150 151 debug.message(4, "xml:base", baseURI); 152 153 try { 154 CatalogEntry ce = new CatalogEntry(entryType, entryArgs); 155 catalog.addEntry(ce); 156 } catch (CatalogException cex) { 157 if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) { 158 debug.message(1, "Invalid catalog entry type", localName); 159 } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) { 160 debug.message(1, "Invalid catalog entry (base)", localName); 161 } 162 } 163 164 entryType = -1; 165 entryArgs = new Vector (); 166 167 } else { 168 baseURIStack.push(baseURIStack.peek()); 169 } 170 171 if ((localName.equals("catalog") || localName.equals("group")) 172 && atts.getValue("prefer") != null) { 173 String override = atts.getValue("prefer"); 174 175 if (override.equals("public")) { 176 override = "yes"; 177 } else if (override.equals("system")) { 178 override = "no"; 179 } else { 180 debug.message(1, 181 "Invalid prefer: must be 'system' or 'public'", 182 localName); 183 override = catalog.getDefaultOverride(); 184 } 185 186 entryType = Catalog.OVERRIDE; 187 entryArgs.add(override); 188 overrideStack.push(override); 189 190 debug.message(4, "override", override); 191 192 try { 193 CatalogEntry ce = new CatalogEntry(entryType, entryArgs); 194 catalog.addEntry(ce); 195 } catch (CatalogException cex) { 196 if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) { 197 debug.message(1, "Invalid catalog entry type", localName); 198 } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) { 199 debug.message(1, "Invalid catalog entry (override)", localName); 200 } 201 } 202 203 entryType = -1; 204 entryArgs = new Vector (); 205 206 } else { 207 overrideStack.push(overrideStack.peek()); 208 } 209 210 if (localName.equals("delegatePublic")) { 211 if (checkAttributes(atts, "publicIdStartString", "catalog")) { 212 entryType = Catalog.DELEGATE_PUBLIC; 213 entryArgs.add(atts.getValue("publicIdStartString")); 214 entryArgs.add(atts.getValue("catalog")); 215 216 debug.message(4, "delegatePublic", 217 PublicId.normalize(atts.getValue("publicIdStartString")), 218 atts.getValue("catalog")); 219 } 220 } else if (localName.equals("delegateSystem")) { 221 if (checkAttributes(atts, "systemIdStartString", "catalog")) { 222 entryType = Catalog.DELEGATE_SYSTEM; 223 entryArgs.add(atts.getValue("systemIdStartString")); 224 entryArgs.add(atts.getValue("catalog")); 225 226 debug.message(4, "delegateSystem", 227 atts.getValue("systemIdStartString"), 228 atts.getValue("catalog")); 229 } 230 } else if (localName.equals("delegateURI")) { 231 if (checkAttributes(atts, "uriStartString", "catalog")) { 232 entryType = Catalog.DELEGATE_URI; 233 entryArgs.add(atts.getValue("uriStartString")); 234 entryArgs.add(atts.getValue("catalog")); 235 236 debug.message(4, "delegateURI", 237 atts.getValue("uriStartString"), 238 atts.getValue("catalog")); 239 } 240 } else if (localName.equals("rewriteSystem")) { 241 if (checkAttributes(atts, "systemIdStartString", "rewritePrefix")) { 242 entryType = Catalog.REWRITE_SYSTEM; 243 entryArgs.add(atts.getValue("systemIdStartString")); 244 entryArgs.add(atts.getValue("rewritePrefix")); 245 246 debug.message(4, "rewriteSystem", 247 atts.getValue("systemIdStartString"), 248 atts.getValue("rewritePrefix")); 249 } 250 } else if (localName.equals("systemSuffix")) { 251 if (checkAttributes(atts, "systemIdSuffix", "uri")) { 252 entryType = Catalog.SYSTEM_SUFFIX; 253 entryArgs.add(atts.getValue("systemIdSuffix")); 254 entryArgs.add(atts.getValue("uri")); 255 256 debug.message(4, "systemSuffix", 257 atts.getValue("systemIdSuffix"), 258 atts.getValue("uri")); 259 } 260 } else if (localName.equals("rewriteURI")) { 261 if (checkAttributes(atts, "uriStartString", "rewritePrefix")) { 262 entryType = Catalog.REWRITE_URI; 263 entryArgs.add(atts.getValue("uriStartString")); 264 entryArgs.add(atts.getValue("rewritePrefix")); 265 266 debug.message(4, "rewriteURI", 267 atts.getValue("uriStartString"), 268 atts.getValue("rewritePrefix")); 269 } 270 } else if (localName.equals("uriSuffix")) { 271 if (checkAttributes(atts, "uriSuffix", "uri")) { 272 entryType = Catalog.URI_SUFFIX; 273 entryArgs.add(atts.getValue("uriSuffix")); 274 entryArgs.add(atts.getValue("uri")); 275 276 debug.message(4, "uriSuffix", 277 atts.getValue("uriSuffix"), 278 atts.getValue("uri")); 279 } 280 } else if (localName.equals("nextCatalog")) { 281 if (checkAttributes(atts, "catalog")) { 282 entryType = Catalog.CATALOG; 283 entryArgs.add(atts.getValue("catalog")); 284 285 debug.message(4, "nextCatalog", atts.getValue("catalog")); 286 } 287 } else if (localName.equals("public")) { 288 if (checkAttributes(atts, "publicId", "uri")) { 289 entryType = Catalog.PUBLIC; 290 entryArgs.add(atts.getValue("publicId")); 291 entryArgs.add(atts.getValue("uri")); 292 293 debug.message(4, "public", 294 PublicId.normalize(atts.getValue("publicId")), 295 atts.getValue("uri")); 296 } 297 } else if (localName.equals("system")) { 298 if (checkAttributes(atts, "systemId", "uri")) { 299 entryType = Catalog.SYSTEM; 300 entryArgs.add(atts.getValue("systemId")); 301 entryArgs.add(atts.getValue("uri")); 302 303 debug.message(4, "system", 304 atts.getValue("systemId"), 305 atts.getValue("uri")); 306 } 307 } else if (localName.equals("uri")) { 308 if (checkAttributes(atts, "name", "uri")) { 309 entryType = Catalog.URI; 310 entryArgs.add(atts.getValue("name")); 311 entryArgs.add(atts.getValue("uri")); 312 313 debug.message(4, "uri", 314 atts.getValue("name"), 315 atts.getValue("uri")); 316 } 317 } else if (localName.equals("catalog")) { 318 } else if (localName.equals("group")) { 320 } else { 322 debug.message(1, "Invalid catalog entry type", localName); 324 } 325 326 if (entryType >= 0) { 327 try { 328 CatalogEntry ce = new CatalogEntry(entryType, entryArgs); 329 catalog.addEntry(ce); 330 } catch (CatalogException cex) { 331 if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) { 332 debug.message(1, "Invalid catalog entry type", localName); 333 } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) { 334 debug.message(1, "Invalid catalog entry", localName); 335 } 336 } 337 } 338 } 339 340 if (namespaceURI != null && tr9401NamespaceName.equals(namespaceURI) 341 && !inExtension) { 342 344 if (atts.getValue("xml:base") != null) { 345 String baseURI = atts.getValue("xml:base"); 346 entryType = Catalog.BASE; 347 entryArgs.add(baseURI); 348 baseURIStack.push(baseURI); 349 350 debug.message(4, "xml:base", baseURI); 351 352 try { 353 CatalogEntry ce = new CatalogEntry(entryType, entryArgs); 354 catalog.addEntry(ce); 355 } catch (CatalogException cex) { 356 if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) { 357 debug.message(1, "Invalid catalog entry type", localName); 358 } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) { 359 debug.message(1, "Invalid catalog entry (base)", localName); 360 } 361 } 362 363 entryType = -1; 364 entryArgs = new Vector (); 365 366 } else { 367 baseURIStack.push(baseURIStack.peek()); 368 } 369 370 if (localName.equals("doctype")) { 371 entryType = catalog.DOCTYPE; 372 entryArgs.add(atts.getValue("name")); 373 entryArgs.add(atts.getValue("uri")); 374 } else if (localName.equals("document")) { 375 entryType = catalog.DOCUMENT; 376 entryArgs.add(atts.getValue("uri")); 377 } else if (localName.equals("dtddecl")) { 378 entryType = catalog.DTDDECL; 379 entryArgs.add(atts.getValue("publicId")); 380 entryArgs.add(atts.getValue("uri")); 381 } else if (localName.equals("entity")) { 382 entryType = Catalog.ENTITY; 383 entryArgs.add(atts.getValue("name")); 384 entryArgs.add(atts.getValue("uri")); 385 } else if (localName.equals("linktype")) { 386 entryType = Catalog.LINKTYPE; 387 entryArgs.add(atts.getValue("name")); 388 entryArgs.add(atts.getValue("uri")); 389 } else if (localName.equals("notation")) { 390 entryType = Catalog.NOTATION; 391 entryArgs.add(atts.getValue("name")); 392 entryArgs.add(atts.getValue("uri")); 393 } else if (localName.equals("sgmldecl")) { 394 entryType = Catalog.SGMLDECL; 395 entryArgs.add(atts.getValue("uri")); 396 } else { 397 debug.message(1, "Invalid catalog entry type", localName); 399 } 400 401 if (entryType >= 0) { 402 try { 403 CatalogEntry ce = new CatalogEntry(entryType, entryArgs); 404 catalog.addEntry(ce); 405 } catch (CatalogException cex) { 406 if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) { 407 debug.message(1, "Invalid catalog entry type", localName); 408 } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) { 409 debug.message(1, "Invalid catalog entry", localName); 410 } 411 } 412 } 413 } 414 } 415 416 public boolean checkAttributes (Attributes atts, String attName) { 417 if (atts.getValue(attName) == null) { 418 debug.message(1, "Error: required attribute " + attName + " missing."); 419 return false; 420 } else { 421 return true; 422 } 423 } 424 425 public boolean checkAttributes (Attributes atts, 426 String attName1, 427 String attName2) { 428 return checkAttributes(atts, attName1) 429 && checkAttributes(atts, attName2); 430 } 431 432 433 public void endElement (String namespaceURI, 434 String localName, 435 String qName) 436 throws SAXException { 437 438 int entryType = -1; 439 Vector entryArgs = new Vector (); 440 441 boolean inExtension = inExtensionNamespace(); 442 443 if (namespaceURI != null 444 && !inExtension 445 && (namespaceName.equals(namespaceURI) 446 || tr9401NamespaceName.equals(namespaceURI))) { 447 448 String popURI = (String ) baseURIStack.pop(); 449 String baseURI = (String ) baseURIStack.peek(); 450 451 if (!baseURI.equals(popURI)) { 452 entryType = catalog.BASE; 453 entryArgs.add(baseURI); 454 455 debug.message(4, "(reset) xml:base", baseURI); 456 457 try { 458 CatalogEntry ce = new CatalogEntry(entryType, entryArgs); 459 catalog.addEntry(ce); 460 } catch (CatalogException cex) { 461 if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) { 462 debug.message(1, "Invalid catalog entry type", localName); 463 } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) { 464 debug.message(1, "Invalid catalog entry (rbase)", localName); 465 } 466 } 467 } 468 } 469 470 if (namespaceURI != null && namespaceName.equals(namespaceURI) 471 && !inExtension) { 472 if (localName.equals("catalog") || localName.equals("group")) { 473 String popOverride = (String ) overrideStack.pop(); 474 String override = (String ) overrideStack.peek(); 475 476 if (!override.equals(popOverride)) { 477 entryType = catalog.OVERRIDE; 478 entryArgs.add(override); 479 overrideStack.push(override); 480 481 debug.message(4, "(reset) override", override); 482 483 try { 484 CatalogEntry ce = new CatalogEntry(entryType, entryArgs); 485 catalog.addEntry(ce); 486 } catch (CatalogException cex) { 487 if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) { 488 debug.message(1, "Invalid catalog entry type", localName); 489 } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) { 490 debug.message(1, "Invalid catalog entry (roverride)", localName); 491 } 492 } 493 } 494 } 495 } 496 497 namespaceStack.pop(); 498 499 return; 500 } 501 502 503 public void characters (char ch[], int start, int length) 504 throws SAXException { 505 return; 506 } 507 508 509 public void ignorableWhitespace (char ch[], int start, int length) 510 throws SAXException { 511 return; 512 } 513 514 515 public void processingInstruction (String target, String data) 516 throws SAXException { 517 return; 518 } 519 520 521 public void skippedEntity (String name) 522 throws SAXException { 523 return; 524 } 525 526 527 public void startPrefixMapping(String prefix, String uri) 528 throws SAXException { 529 return; 530 } 531 532 533 public void endPrefixMapping(String prefix) 534 throws SAXException { 535 return; 536 } 537 538 } 539 | Popular Tags |