001: // ExtendedXMLCatalogReader.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.Vector;
023: import com.sun.org.apache.xml.internal.resolver.Catalog;
024: import com.sun.org.apache.xml.internal.resolver.Resolver;
025: import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
026: import com.sun.org.apache.xml.internal.resolver.CatalogException;
027:
028: import org.xml.sax.*;
029: import org.w3c.dom.*;
030:
031: /**
032: * Parse Extended OASIS Entity Resolution Technical Committee
033: * XML Catalog files.
034: *
035: * @see Catalog
036: *
037: * @author Norman Walsh
038: * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
039: *
040: * @version 1.0
041: */
042: public class ExtendedXMLCatalogReader extends OASISXMLCatalogReader {
043: /** The namespace name of extended catalog elements */
044: public static final String extendedNamespaceName = "http://nwalsh.com/xcatalog/1.0";
045:
046: /**
047: * The SAX <code>startElement</code> method recognizes elements
048: * from the plain catalog format and instantiates CatalogEntry
049: * objects for them.
050: *
051: * @param namespaceURI The namespace name of the element.
052: * @param localName The local name of the element.
053: * @param qName The QName of the element.
054: * @param atts The list of attributes on the element.
055: *
056: * @see CatalogEntry
057: */
058: public void startElement(String namespaceURI, String localName,
059: String qName, Attributes atts) throws SAXException {
060:
061: // Check before calling the super because super will report our
062: // namespace as an extension namespace, but that doesn't count
063: // for this element.
064: boolean inExtension = inExtensionNamespace();
065:
066: super .startElement(namespaceURI, localName, qName, atts);
067:
068: int entryType = -1;
069: Vector entryArgs = new Vector();
070:
071: if (namespaceURI != null
072: && extendedNamespaceName.equals(namespaceURI)
073: && !inExtension) {
074: // This is an Extended XML Catalog entry
075:
076: if (atts.getValue("xml:base") != null) {
077: String baseURI = atts.getValue("xml:base");
078: entryType = Catalog.BASE;
079: entryArgs.add(baseURI);
080: baseURIStack.push(baseURI);
081:
082: debug.message(4, "xml:base", baseURI);
083:
084: try {
085: CatalogEntry ce = new CatalogEntry(entryType,
086: entryArgs);
087: catalog.addEntry(ce);
088: } catch (CatalogException cex) {
089: if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
090: debug.message(1, "Invalid catalog entry type",
091: localName);
092: } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
093: debug.message(1,
094: "Invalid catalog entry (base)",
095: localName);
096: }
097: }
098:
099: entryType = -1;
100: entryArgs = new Vector();
101: } else {
102: baseURIStack.push(baseURIStack.peek());
103: }
104:
105: if (localName.equals("uriSuffix")) {
106: if (checkAttributes(atts, "suffix", "uri")) {
107: entryType = Resolver.URISUFFIX;
108: entryArgs.add(atts.getValue("suffix"));
109: entryArgs.add(atts.getValue("uri"));
110:
111: debug.message(4, "uriSuffix", atts
112: .getValue("suffix"), atts.getValue("uri"));
113: }
114: } else if (localName.equals("systemSuffix")) {
115: if (checkAttributes(atts, "suffix", "uri")) {
116: entryType = Resolver.SYSTEMSUFFIX;
117: entryArgs.add(atts.getValue("suffix"));
118: entryArgs.add(atts.getValue("uri"));
119:
120: debug.message(4, "systemSuffix", atts
121: .getValue("suffix"), atts.getValue("uri"));
122: }
123: } else {
124: // This is equivalent to an invalid catalog entry type
125: debug.message(1, "Invalid catalog entry type",
126: localName);
127: }
128:
129: if (entryType >= 0) {
130: try {
131: CatalogEntry ce = new CatalogEntry(entryType,
132: entryArgs);
133: catalog.addEntry(ce);
134: } catch (CatalogException cex) {
135: if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
136: debug.message(1, "Invalid catalog entry type",
137: localName);
138: } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
139: debug.message(1, "Invalid catalog entry",
140: localName);
141: }
142: }
143: }
144: }
145: }
146:
147: /** The SAX <code>endElement</code> method does nothing. */
148: public void endElement(String namespaceURI, String localName,
149: String qName) throws SAXException {
150:
151: super .endElement(namespaceURI, localName, qName);
152:
153: // Check after popping the stack so we don't erroneously think we
154: // are our own extension namespace...
155: boolean inExtension = inExtensionNamespace();
156:
157: int entryType = -1;
158: Vector entryArgs = new Vector();
159:
160: if (namespaceURI != null
161: && (extendedNamespaceName.equals(namespaceURI))
162: && !inExtension) {
163:
164: String popURI = (String) baseURIStack.pop();
165: String baseURI = (String) baseURIStack.peek();
166:
167: if (!baseURI.equals(popURI)) {
168: entryType = catalog.BASE;
169: entryArgs.add(baseURI);
170:
171: debug.message(4, "(reset) xml:base", baseURI);
172:
173: try {
174: CatalogEntry ce = new CatalogEntry(entryType,
175: entryArgs);
176: catalog.addEntry(ce);
177: } catch (CatalogException cex) {
178: if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
179: debug.message(1, "Invalid catalog entry type",
180: localName);
181: } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
182: debug.message(1,
183: "Invalid catalog entry (rbase)",
184: localName);
185: }
186: }
187: }
188: }
189: }
190: }
|