001: // XCatalogReader.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.CatalogEntry;
025: import com.sun.org.apache.xml.internal.resolver.CatalogException;
026: import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
027:
028: import org.xml.sax.*;
029:
030: import javax.xml.parsers.*;
031:
032: /**
033: * Parse "xcatalog" XML Catalog files, this is the XML Catalog format
034: * developed by John Cowan and supported by Apache.
035: *
036: * @see Catalog
037: *
038: * @author Norman Walsh
039: * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
040: *
041: * @version 1.0
042: */
043: public class XCatalogReader extends SAXCatalogReader implements
044: SAXCatalogParser {
045: /** The catalog object needs to be stored by the object so that
046: * SAX callbacks can use it.
047: */
048: protected Catalog catalog = null;
049:
050: /** Set the current catalog. */
051: public void setCatalog(Catalog catalog) {
052: this .catalog = catalog;
053: }
054:
055: /** Get the current catalog. */
056: public Catalog getCatalog() {
057: return catalog;
058: }
059:
060: /** The constructor */
061: public XCatalogReader(SAXParserFactory parserFactory) {
062: super (parserFactory);
063: }
064:
065: // ----------------------------------------------------------------------
066: // Implement the SAX DocumentHandler interface
067:
068: /** The SAX <code>setDocumentLocator</code> method does nothing. */
069: public void setDocumentLocator(Locator locator) {
070: return;
071: }
072:
073: /** The SAX <code>startDocument</code> method does nothing. */
074: public void startDocument() throws SAXException {
075: return;
076: }
077:
078: /** The SAX <code>endDocument</code> method does nothing. */
079: public void endDocument() throws SAXException {
080: return;
081: }
082:
083: /**
084: * The SAX <code>startElement</code> method recognizes elements
085: * from the plain catalog format and instantiates CatalogEntry
086: * objects for them.
087: *
088: * @param namespaceURI The namespace name of the element.
089: * @param localName The local name of the element.
090: * @param qName The QName of the element.
091: * @param atts The list of attributes on the element.
092: *
093: * @see CatalogEntry
094: */
095: public void startElement(String namespaceURI, String localName,
096: String qName, Attributes atts) throws SAXException {
097:
098: int entryType = -1;
099: Vector entryArgs = new Vector();
100:
101: if (localName.equals("Base")) {
102: entryType = catalog.BASE;
103: entryArgs.add(atts.getValue("HRef"));
104:
105: catalog.getCatalogManager().debug.message(4, "Base", atts
106: .getValue("HRef"));
107: } else if (localName.equals("Delegate")) {
108: entryType = catalog.DELEGATE_PUBLIC;
109: entryArgs.add(atts.getValue("PublicId"));
110: entryArgs.add(atts.getValue("HRef"));
111:
112: catalog.getCatalogManager().debug.message(4, "Delegate",
113: PublicId.normalize(atts.getValue("PublicId")), atts
114: .getValue("HRef"));
115: } else if (localName.equals("Extend")) {
116: entryType = catalog.CATALOG;
117: entryArgs.add(atts.getValue("HRef"));
118:
119: catalog.getCatalogManager().debug.message(4, "Extend", atts
120: .getValue("HRef"));
121: } else if (localName.equals("Map")) {
122: entryType = catalog.PUBLIC;
123: entryArgs.add(atts.getValue("PublicId"));
124: entryArgs.add(atts.getValue("HRef"));
125:
126: catalog.getCatalogManager().debug.message(4, "Map",
127: PublicId.normalize(atts.getValue("PublicId")), atts
128: .getValue("HRef"));
129: } else if (localName.equals("Remap")) {
130: entryType = catalog.SYSTEM;
131: entryArgs.add(atts.getValue("SystemId"));
132: entryArgs.add(atts.getValue("HRef"));
133:
134: catalog.getCatalogManager().debug.message(4, "Remap", atts
135: .getValue("SystemId"), atts.getValue("HRef"));
136: } else if (localName.equals("XMLCatalog")) {
137: // nop, start of catalog
138: } else {
139: // This is equivalent to an invalid catalog entry type
140: catalog.getCatalogManager().debug.message(1,
141: "Invalid catalog entry type", localName);
142: }
143:
144: if (entryType >= 0) {
145: try {
146: CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
147: catalog.addEntry(ce);
148: } catch (CatalogException cex) {
149: if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
150: catalog.getCatalogManager().debug.message(1,
151: "Invalid catalog entry type", localName);
152: } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
153: catalog.getCatalogManager().debug.message(1,
154: "Invalid catalog entry", localName);
155: }
156: }
157: }
158: }
159:
160: /** The SAX <code>endElement</code> method does nothing. */
161: public void endElement(String namespaceURI, String localName,
162: String qName) throws SAXException {
163: return;
164: }
165:
166: /** The SAX <code>characters</code> method does nothing. */
167: public void characters(char ch[], int start, int length)
168: throws SAXException {
169: return;
170: }
171:
172: /** The SAX <code>ignorableWhitespace</code> method does nothing. */
173: public void ignorableWhitespace(char ch[], int start, int length)
174: throws SAXException {
175: return;
176: }
177:
178: /** The SAX <code>processingInstruction</code> method does nothing. */
179: public void processingInstruction(String target, String data)
180: throws SAXException {
181: return;
182: }
183: }
|