001: // OASISXMLCatalogReader.java - Read XML Catalog files
002:
003: /*
004: * Copyright 2001-2004 The Apache Software Foundation or its licensors,
005: * as applicable.
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: package com.sun.org.apache.xml.internal.resolver.readers;
021:
022: import java.util.Stack;
023: import java.util.Vector;
024: import java.util.Enumeration;
025: import com.sun.org.apache.xml.internal.resolver.Catalog;
026: import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
027: import com.sun.org.apache.xml.internal.resolver.CatalogException;
028: import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
029:
030: import org.xml.sax.*;
031: import org.w3c.dom.*;
032:
033: /**
034: * Parse OASIS Entity Resolution Technical Committee
035: * XML Catalog files.
036: *
037: * @see Catalog
038: *
039: * @author Norman Walsh
040: * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
041: *
042: * @version 1.0
043: */
044: public class OASISXMLCatalogReader extends SAXCatalogReader implements
045: SAXCatalogParser {
046: /** The catalog object needs to be stored by the object so that
047: * SAX callbacks can use it.
048: */
049: protected Catalog catalog = null;
050:
051: /** The namespace name of OASIS ERTC catalogs */
052: public static final String namespaceName = "urn:oasis:names:tc:entity:xmlns:xml:catalog";
053:
054: /** The namespace name of OASIS ERTC TR9401 catalog extension */
055: public static final String tr9401NamespaceName = "urn:oasis:names:tc:entity:xmlns:tr9401:catalog";
056:
057: protected Stack baseURIStack = new Stack();
058: protected Stack overrideStack = new Stack();
059: protected Stack namespaceStack = new Stack();
060:
061: /** Set the current catalog. */
062: public void setCatalog(Catalog catalog) {
063: this .catalog = catalog;
064: debug = catalog.getCatalogManager().debug;
065: }
066:
067: /** Get the current catalog. */
068: public Catalog getCatalog() {
069: return catalog;
070: }
071:
072: /**
073: * Are we in an extension namespace?
074: *
075: * @return true if the current stack of open namespaces includes
076: * an extension namespace.
077: */
078: protected boolean inExtensionNamespace() {
079: boolean inExtension = false;
080:
081: Enumeration elements = namespaceStack.elements();
082: while (!inExtension && elements.hasMoreElements()) {
083: String ns = (String) elements.nextElement();
084: if (ns == null) {
085: inExtension = true;
086: } else {
087: inExtension = (!ns.equals(tr9401NamespaceName) && !ns
088: .equals(namespaceName));
089: }
090: }
091:
092: return inExtension;
093: }
094:
095: // ----------------------------------------------------------------------
096: // Implement the SAX ContentHandler interface
097:
098: /** The SAX <code>setDocumentLocator</code> method does nothing. */
099: public void setDocumentLocator(Locator locator) {
100: return;
101: }
102:
103: /** The SAX <code>startDocument</code> method does nothing. */
104: public void startDocument() throws SAXException {
105: baseURIStack.push(catalog.getCurrentBase());
106: overrideStack.push(catalog.getDefaultOverride());
107: return;
108: }
109:
110: /** The SAX <code>endDocument</code> method does nothing. */
111: public void endDocument() throws SAXException {
112: return;
113: }
114:
115: /**
116: * The SAX <code>startElement</code> method recognizes elements
117: * from the plain catalog format and instantiates CatalogEntry
118: * objects for them.
119: *
120: * @param namespaceURI The namespace name of the element.
121: * @param localName The local name of the element.
122: * @param qName The QName of the element.
123: * @param atts The list of attributes on the element.
124: *
125: * @see CatalogEntry
126: */
127: public void startElement(String namespaceURI, String localName,
128: String qName, Attributes atts) throws SAXException {
129:
130: int entryType = -1;
131: Vector entryArgs = new Vector();
132:
133: namespaceStack.push(namespaceURI);
134:
135: boolean inExtension = inExtensionNamespace();
136:
137: if (namespaceURI != null && namespaceName.equals(namespaceURI)
138: && !inExtension) {
139: // This is an XML Catalog entry
140:
141: if (atts.getValue("xml:base") != null) {
142: String baseURI = atts.getValue("xml:base");
143: entryType = Catalog.BASE;
144: entryArgs.add(baseURI);
145: baseURIStack.push(baseURI);
146:
147: debug.message(4, "xml:base", baseURI);
148:
149: try {
150: CatalogEntry ce = new CatalogEntry(entryType,
151: entryArgs);
152: catalog.addEntry(ce);
153: } catch (CatalogException cex) {
154: if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
155: debug.message(1, "Invalid catalog entry type",
156: localName);
157: } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
158: debug.message(1,
159: "Invalid catalog entry (base)",
160: 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
172: .equals("group"))
173: && atts.getValue("prefer") != null) {
174: String override = atts.getValue("prefer");
175:
176: if (override.equals("public")) {
177: override = "yes";
178: } else if (override.equals("system")) {
179: override = "no";
180: } else {
181: debug
182: .message(
183: 1,
184: "Invalid prefer: must be 'system' or 'public'",
185: localName);
186: override = catalog.getDefaultOverride();
187: }
188:
189: entryType = Catalog.OVERRIDE;
190: entryArgs.add(override);
191: overrideStack.push(override);
192:
193: debug.message(4, "override", override);
194:
195: try {
196: CatalogEntry ce = new CatalogEntry(entryType,
197: entryArgs);
198: catalog.addEntry(ce);
199: } catch (CatalogException cex) {
200: if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
201: debug.message(1, "Invalid catalog entry type",
202: localName);
203: } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
204: debug.message(1,
205: "Invalid catalog entry (override)",
206: localName);
207: }
208: }
209:
210: entryType = -1;
211: entryArgs = new Vector();
212:
213: } else {
214: overrideStack.push(overrideStack.peek());
215: }
216:
217: if (localName.equals("delegatePublic")) {
218: if (checkAttributes(atts, "publicIdStartString",
219: "catalog")) {
220: entryType = Catalog.DELEGATE_PUBLIC;
221: entryArgs.add(atts.getValue("publicIdStartString"));
222: entryArgs.add(atts.getValue("catalog"));
223:
224: debug.message(4, "delegatePublic", PublicId
225: .normalize(atts
226: .getValue("publicIdStartString")),
227: atts.getValue("catalog"));
228: }
229: } else if (localName.equals("delegateSystem")) {
230: if (checkAttributes(atts, "systemIdStartString",
231: "catalog")) {
232: entryType = Catalog.DELEGATE_SYSTEM;
233: entryArgs.add(atts.getValue("systemIdStartString"));
234: entryArgs.add(atts.getValue("catalog"));
235:
236: debug.message(4, "delegateSystem", atts
237: .getValue("systemIdStartString"), atts
238: .getValue("catalog"));
239: }
240: } else if (localName.equals("delegateURI")) {
241: if (checkAttributes(atts, "uriStartString", "catalog")) {
242: entryType = Catalog.DELEGATE_URI;
243: entryArgs.add(atts.getValue("uriStartString"));
244: entryArgs.add(atts.getValue("catalog"));
245:
246: debug.message(4, "delegateURI", atts
247: .getValue("uriStartString"), atts
248: .getValue("catalog"));
249: }
250: } else if (localName.equals("rewriteSystem")) {
251: if (checkAttributes(atts, "systemIdStartString",
252: "rewritePrefix")) {
253: entryType = Catalog.REWRITE_SYSTEM;
254: entryArgs.add(atts.getValue("systemIdStartString"));
255: entryArgs.add(atts.getValue("rewritePrefix"));
256:
257: debug.message(4, "rewriteSystem", atts
258: .getValue("systemIdStartString"), atts
259: .getValue("rewritePrefix"));
260: }
261: } else if (localName.equals("systemSuffix")) {
262: if (checkAttributes(atts, "systemIdSuffix", "uri")) {
263: entryType = Catalog.SYSTEM_SUFFIX;
264: entryArgs.add(atts.getValue("systemIdSuffix"));
265: entryArgs.add(atts.getValue("uri"));
266:
267: debug.message(4, "systemSuffix", atts
268: .getValue("systemIdSuffix"), atts
269: .getValue("uri"));
270: }
271: } else if (localName.equals("rewriteURI")) {
272: if (checkAttributes(atts, "uriStartString",
273: "rewritePrefix")) {
274: entryType = Catalog.REWRITE_URI;
275: entryArgs.add(atts.getValue("uriStartString"));
276: entryArgs.add(atts.getValue("rewritePrefix"));
277:
278: debug.message(4, "rewriteURI", atts
279: .getValue("uriStartString"), atts
280: .getValue("rewritePrefix"));
281: }
282: } else if (localName.equals("uriSuffix")) {
283: if (checkAttributes(atts, "uriSuffix", "uri")) {
284: entryType = Catalog.URI_SUFFIX;
285: entryArgs.add(atts.getValue("uriSuffix"));
286: entryArgs.add(atts.getValue("uri"));
287:
288: debug.message(4, "uriSuffix", atts
289: .getValue("uriSuffix"), atts
290: .getValue("uri"));
291: }
292: } else if (localName.equals("nextCatalog")) {
293: if (checkAttributes(atts, "catalog")) {
294: entryType = Catalog.CATALOG;
295: entryArgs.add(atts.getValue("catalog"));
296:
297: debug.message(4, "nextCatalog", atts
298: .getValue("catalog"));
299: }
300: } else if (localName.equals("public")) {
301: if (checkAttributes(atts, "publicId", "uri")) {
302: entryType = Catalog.PUBLIC;
303: entryArgs.add(atts.getValue("publicId"));
304: entryArgs.add(atts.getValue("uri"));
305:
306: debug.message(4, "public", PublicId.normalize(atts
307: .getValue("publicId")), atts
308: .getValue("uri"));
309: }
310: } else if (localName.equals("system")) {
311: if (checkAttributes(atts, "systemId", "uri")) {
312: entryType = Catalog.SYSTEM;
313: entryArgs.add(atts.getValue("systemId"));
314: entryArgs.add(atts.getValue("uri"));
315:
316: debug
317: .message(4, "system", atts
318: .getValue("systemId"), atts
319: .getValue("uri"));
320: }
321: } else if (localName.equals("uri")) {
322: if (checkAttributes(atts, "name", "uri")) {
323: entryType = Catalog.URI;
324: entryArgs.add(atts.getValue("name"));
325: entryArgs.add(atts.getValue("uri"));
326:
327: debug.message(4, "uri", atts.getValue("name"), atts
328: .getValue("uri"));
329: }
330: } else if (localName.equals("catalog")) {
331: // nop, start of catalog
332: } else if (localName.equals("group")) {
333: // nop, a group
334: } else {
335: // This is equivalent to an invalid catalog entry type
336: debug.message(1, "Invalid catalog entry type",
337: localName);
338: }
339:
340: if (entryType >= 0) {
341: try {
342: CatalogEntry ce = new CatalogEntry(entryType,
343: entryArgs);
344: catalog.addEntry(ce);
345: } catch (CatalogException cex) {
346: if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
347: debug.message(1, "Invalid catalog entry type",
348: localName);
349: } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
350: debug.message(1, "Invalid catalog entry",
351: localName);
352: }
353: }
354: }
355: }
356:
357: if (namespaceURI != null
358: && tr9401NamespaceName.equals(namespaceURI)
359: && !inExtension) {
360: // This is a TR9401 Catalog entry
361:
362: if (atts.getValue("xml:base") != null) {
363: String baseURI = atts.getValue("xml:base");
364: entryType = Catalog.BASE;
365: entryArgs.add(baseURI);
366: baseURIStack.push(baseURI);
367:
368: debug.message(4, "xml:base", baseURI);
369:
370: try {
371: CatalogEntry ce = new CatalogEntry(entryType,
372: entryArgs);
373: catalog.addEntry(ce);
374: } catch (CatalogException cex) {
375: if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
376: debug.message(1, "Invalid catalog entry type",
377: localName);
378: } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
379: debug.message(1,
380: "Invalid catalog entry (base)",
381: localName);
382: }
383: }
384:
385: entryType = -1;
386: entryArgs = new Vector();
387:
388: } else {
389: baseURIStack.push(baseURIStack.peek());
390: }
391:
392: if (localName.equals("doctype")) {
393: entryType = catalog.DOCTYPE;
394: entryArgs.add(atts.getValue("name"));
395: entryArgs.add(atts.getValue("uri"));
396: } else if (localName.equals("document")) {
397: entryType = catalog.DOCUMENT;
398: entryArgs.add(atts.getValue("uri"));
399: } else if (localName.equals("dtddecl")) {
400: entryType = catalog.DTDDECL;
401: entryArgs.add(atts.getValue("publicId"));
402: entryArgs.add(atts.getValue("uri"));
403: } else if (localName.equals("entity")) {
404: entryType = Catalog.ENTITY;
405: entryArgs.add(atts.getValue("name"));
406: entryArgs.add(atts.getValue("uri"));
407: } else if (localName.equals("linktype")) {
408: entryType = Catalog.LINKTYPE;
409: entryArgs.add(atts.getValue("name"));
410: entryArgs.add(atts.getValue("uri"));
411: } else if (localName.equals("notation")) {
412: entryType = Catalog.NOTATION;
413: entryArgs.add(atts.getValue("name"));
414: entryArgs.add(atts.getValue("uri"));
415: } else if (localName.equals("sgmldecl")) {
416: entryType = Catalog.SGMLDECL;
417: entryArgs.add(atts.getValue("uri"));
418: } else {
419: // This is equivalent to an invalid catalog entry type
420: debug.message(1, "Invalid catalog entry type",
421: localName);
422: }
423:
424: if (entryType >= 0) {
425: try {
426: CatalogEntry ce = new CatalogEntry(entryType,
427: entryArgs);
428: catalog.addEntry(ce);
429: } catch (CatalogException cex) {
430: if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
431: debug.message(1, "Invalid catalog entry type",
432: localName);
433: } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
434: debug.message(1, "Invalid catalog entry",
435: localName);
436: }
437: }
438: }
439: }
440: }
441:
442: public boolean checkAttributes(Attributes atts, String attName) {
443: if (atts.getValue(attName) == null) {
444: debug.message(1, "Error: required attribute " + attName
445: + " missing.");
446: return false;
447: } else {
448: return true;
449: }
450: }
451:
452: public boolean checkAttributes(Attributes atts, String attName1,
453: String attName2) {
454: return checkAttributes(atts, attName1)
455: && checkAttributes(atts, attName2);
456: }
457:
458: /** The SAX <code>endElement</code> method does nothing. */
459: public void endElement(String namespaceURI, String localName,
460: String qName) throws SAXException {
461:
462: int entryType = -1;
463: Vector entryArgs = new Vector();
464:
465: boolean inExtension = inExtensionNamespace();
466:
467: if (namespaceURI != null
468: && !inExtension
469: && (namespaceName.equals(namespaceURI) || tr9401NamespaceName
470: .equals(namespaceURI))) {
471:
472: String popURI = (String) baseURIStack.pop();
473: String baseURI = (String) baseURIStack.peek();
474:
475: if (!baseURI.equals(popURI)) {
476: entryType = catalog.BASE;
477: entryArgs.add(baseURI);
478:
479: debug.message(4, "(reset) xml:base", baseURI);
480:
481: try {
482: CatalogEntry ce = new CatalogEntry(entryType,
483: entryArgs);
484: catalog.addEntry(ce);
485: } catch (CatalogException cex) {
486: if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
487: debug.message(1, "Invalid catalog entry type",
488: localName);
489: } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
490: debug.message(1,
491: "Invalid catalog entry (rbase)",
492: localName);
493: }
494: }
495: }
496: }
497:
498: if (namespaceURI != null && namespaceName.equals(namespaceURI)
499: && !inExtension) {
500: if (localName.equals("catalog")
501: || localName.equals("group")) {
502: String popOverride = (String) overrideStack.pop();
503: String override = (String) overrideStack.peek();
504:
505: if (!override.equals(popOverride)) {
506: entryType = catalog.OVERRIDE;
507: entryArgs.add(override);
508: overrideStack.push(override);
509:
510: debug.message(4, "(reset) override", override);
511:
512: try {
513: CatalogEntry ce = new CatalogEntry(entryType,
514: entryArgs);
515: catalog.addEntry(ce);
516: } catch (CatalogException cex) {
517: if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
518: debug.message(1,
519: "Invalid catalog entry type",
520: localName);
521: } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
522: debug
523: .message(
524: 1,
525: "Invalid catalog entry (roverride)",
526: localName);
527: }
528: }
529: }
530: }
531: }
532:
533: namespaceStack.pop();
534:
535: return;
536: }
537:
538: /** The SAX <code>characters</code> method does nothing. */
539: public void characters(char ch[], int start, int length)
540: throws SAXException {
541: return;
542: }
543:
544: /** The SAX <code>ignorableWhitespace</code> method does nothing. */
545: public void ignorableWhitespace(char ch[], int start, int length)
546: throws SAXException {
547: return;
548: }
549:
550: /** The SAX <code>processingInstruction</code> method does nothing. */
551: public void processingInstruction(String target, String data)
552: throws SAXException {
553: return;
554: }
555:
556: /** The SAX <code>skippedEntity</code> method does nothing. */
557: public void skippedEntity(String name) throws SAXException {
558: return;
559: }
560:
561: /** The SAX <code>startPrefixMapping</code> method does nothing. */
562: public void startPrefixMapping(String prefix, String uri)
563: throws SAXException {
564: return;
565: }
566:
567: /** The SAX <code>endPrefixMapping</code> method does nothing. */
568: public void endPrefixMapping(String prefix) throws SAXException {
569: return;
570: }
571:
572: }
|