1 /*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5 // XCatalogReader.java - Read XML Catalog files
6
7 /*
8 * Copyright 2001-2004 The Apache Software Foundation or its licensors,
9 * as applicable.
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24 package com.sun.org.apache.xml.internal.resolver.readers;
25
26 import java.util.Vector;
27 import com.sun.org.apache.xml.internal.resolver.Catalog;
28 import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
29 import com.sun.org.apache.xml.internal.resolver.CatalogException;
30 import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
31
32 import org.xml.sax;
33
34 import javax.xml.parsers;
35
36 /**
37 * Parse "xcatalog" XML Catalog files, this is the XML Catalog format
38 * developed by John Cowan and supported by Apache.
39 *
40 * @see Catalog
41 *
42 * @author Norman Walsh
43 * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
44 *
45 */
46 public class XCatalogReader extends SAXCatalogReader implements SAXCatalogParser {
47 /** The catalog object needs to be stored by the object so that
48 * SAX callbacks can use it.
49 */
50 protected Catalog catalog = null;
51
52 /** Set the current catalog. */
53 public void setCatalog (Catalog catalog) {
54 this.catalog = catalog;
55 }
56
57 /** Get the current catalog. */
58 public Catalog getCatalog () {
59 return catalog;
60 }
61
62 /** The constructor */
63 public XCatalogReader(SAXParserFactory parserFactory) {
64 super(parserFactory);
65 }
66
67 // ----------------------------------------------------------------------
68 // Implement the SAX DocumentHandler interface
69
70 /** The SAX <code>setDocumentLocator</code> method does nothing. */
71 public void setDocumentLocator (Locator locator) {
72 return;
73 }
74
75 /** The SAX <code>startDocument</code> method does nothing. */
76 public void startDocument ()
77 throws SAXException {
78 return;
79 }
80
81 /** The SAX <code>endDocument</code> method does nothing. */
82 public void endDocument ()
83 throws SAXException {
84 return;
85 }
86
87 /**
88 * The SAX <code>startElement</code> method recognizes elements
89 * from the plain catalog format and instantiates CatalogEntry
90 * objects for them.
91 *
92 * @param namespaceURI The namespace name of the element.
93 * @param localName The local name of the element.
94 * @param qName The QName of the element.
95 * @param atts The list of attributes on the element.
96 *
97 * @see CatalogEntry
98 */
99 public void startElement (String namespaceURI,
100 String localName,
101 String qName,
102 Attributes atts)
103 throws SAXException {
104
105 int entryType = -1;
106 Vector entryArgs = new Vector();
107
108 if (localName.equals("Base")) {
109 entryType = catalog.BASE;
110 entryArgs.add(atts.getValue("HRef"));
111
112 catalog.getCatalogManager().debug.message(4, "Base", atts.getValue("HRef"));
113 } else if (localName.equals("Delegate")) {
114 entryType = catalog.DELEGATE_PUBLIC;
115 entryArgs.add(atts.getValue("PublicId"));
116 entryArgs.add(atts.getValue("HRef"));
117
118 catalog.getCatalogManager().debug.message(4, "Delegate",
119 PublicId.normalize(atts.getValue("PublicId")),
120 atts.getValue("HRef"));
121 } else if (localName.equals("Extend")) {
122 entryType = catalog.CATALOG;
123 entryArgs.add(atts.getValue("HRef"));
124
125 catalog.getCatalogManager().debug.message(4, "Extend", atts.getValue("HRef"));
126 } else if (localName.equals("Map")) {
127 entryType = catalog.PUBLIC;
128 entryArgs.add(atts.getValue("PublicId"));
129 entryArgs.add(atts.getValue("HRef"));
130
131 catalog.getCatalogManager().debug.message(4, "Map",
132 PublicId.normalize(atts.getValue("PublicId")),
133 atts.getValue("HRef"));
134 } else if (localName.equals("Remap")) {
135 entryType = catalog.SYSTEM;
136 entryArgs.add(atts.getValue("SystemId"));
137 entryArgs.add(atts.getValue("HRef"));
138
139 catalog.getCatalogManager().debug.message(4, "Remap",
140 atts.getValue("SystemId"),
141 atts.getValue("HRef"));
142 } else if (localName.equals("XMLCatalog")) {
143 // nop, start of catalog
144 } else {
145 // This is equivalent to an invalid catalog entry type
146 catalog.getCatalogManager().debug.message(1, "Invalid catalog entry type", localName);
147 }
148
149 if (entryType >= 0) {
150 try {
151 CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
152 catalog.addEntry(ce);
153 } catch (CatalogException cex) {
154 if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
155 catalog.getCatalogManager().debug.message(1, "Invalid catalog entry type", localName);
156 } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
157 catalog.getCatalogManager().debug.message(1, "Invalid catalog entry", localName);
158 }
159 }
160 }
161 }
162
163 /** The SAX <code>endElement</code> method does nothing. */
164 public void endElement (String namespaceURI,
165 String localName,
166 String qName)
167 throws SAXException {
168 return;
169 }
170
171 /** The SAX <code>characters</code> method does nothing. */
172 public void characters (char ch[], int start, int length)
173 throws SAXException {
174 return;
175 }
176
177 /** The SAX <code>ignorableWhitespace</code> method does nothing. */
178 public void ignorableWhitespace (char ch[], int start, int length)
179 throws SAXException {
180 return;
181 }
182
183 /** The SAX <code>processingInstruction</code> method does nothing. */
184 public void processingInstruction (String target, String data)
185 throws SAXException {
186 return;
187 }
188 }