KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > resolver > readers > ExtendedXMLCatalogReader


1 // ExtendedXMLCatalogReader.java - Read XML Catalog files
2

3 /*
4  * Copyright 2001-2004 The Apache Software Foundation or its licensors,
5  * as applicable.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package com.sun.org.apache.xml.internal.resolver.readers;
21
22 import java.util.Vector JavaDoc;
23 import com.sun.org.apache.xml.internal.resolver.Catalog;
24 import com.sun.org.apache.xml.internal.resolver.Resolver;
25 import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
26 import com.sun.org.apache.xml.internal.resolver.CatalogException;
27
28 import org.xml.sax.*;
29 import org.w3c.dom.*;
30
31 /**
32  * Parse Extended OASIS Entity Resolution Technical Committee
33  * XML Catalog files.
34  *
35  * @see Catalog
36  *
37  * @author Norman Walsh
38  * <a HREF="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
39  *
40  * @version 1.0
41  */

42 public class ExtendedXMLCatalogReader extends OASISXMLCatalogReader {
43   /** The namespace name of extended catalog elements */
44   public static final String JavaDoc extendedNamespaceName = "http://nwalsh.com/xcatalog/1.0";
45
46   /**
47    * The SAX <code>startElement</code> method recognizes elements
48    * from the plain catalog format and instantiates CatalogEntry
49    * objects for them.
50    *
51    * @param namespaceURI The namespace name of the element.
52    * @param localName The local name of the element.
53    * @param qName The QName of the element.
54    * @param atts The list of attributes on the element.
55    *
56    * @see CatalogEntry
57    */

58   public void startElement (String JavaDoc namespaceURI,
59                 String JavaDoc localName,
60                 String JavaDoc qName,
61                 Attributes atts)
62     throws SAXException {
63
64     // Check before calling the super because super will report our
65
// namespace as an extension namespace, but that doesn't count
66
// for this element.
67
boolean inExtension = inExtensionNamespace();
68
69     super.startElement(namespaceURI, localName, qName, atts);
70
71     int entryType = -1;
72     Vector JavaDoc entryArgs = new Vector JavaDoc();
73
74     if (namespaceURI != null && extendedNamespaceName.equals(namespaceURI)
75     && !inExtension) {
76       // This is an Extended XML Catalog entry
77

78       if (atts.getValue("xml:base") != null) {
79     String JavaDoc baseURI = atts.getValue("xml:base");
80     entryType = Catalog.BASE;
81     entryArgs.add(baseURI);
82     baseURIStack.push(baseURI);
83
84     debug.message(4, "xml:base", baseURI);
85
86     try {
87       CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
88       catalog.addEntry(ce);
89     } catch (CatalogException cex) {
90       if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
91         debug.message(1, "Invalid catalog entry type", localName);
92       } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
93         debug.message(1, "Invalid catalog entry (base)", localName);
94       }
95     }
96
97     entryType = -1;
98     entryArgs = new Vector JavaDoc();
99       } else {
100     baseURIStack.push(baseURIStack.peek());
101       }
102
103       if (localName.equals("uriSuffix")) {
104     if (checkAttributes(atts, "suffix", "uri")) {
105       entryType = Resolver.URISUFFIX;
106       entryArgs.add(atts.getValue("suffix"));
107       entryArgs.add(atts.getValue("uri"));
108
109       debug.message(4, "uriSuffix",
110             atts.getValue("suffix"),
111             atts.getValue("uri"));
112     }
113       } else if (localName.equals("systemSuffix")) {
114     if (checkAttributes(atts, "suffix", "uri")) {
115       entryType = Resolver.SYSTEMSUFFIX;
116       entryArgs.add(atts.getValue("suffix"));
117       entryArgs.add(atts.getValue("uri"));
118
119       debug.message(4, "systemSuffix",
120             atts.getValue("suffix"),
121             atts.getValue("uri"));
122     }
123       } else {
124     // This is equivalent to an invalid catalog entry type
125
debug.message(1, "Invalid catalog entry type", localName);
126       }
127
128       if (entryType >= 0) {
129     try {
130       CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
131       catalog.addEntry(ce);
132     } catch (CatalogException cex) {
133       if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
134         debug.message(1, "Invalid catalog entry type", localName);
135       } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
136         debug.message(1, "Invalid catalog entry", localName);
137       }
138     }
139       }
140     }
141   }
142
143   /** The SAX <code>endElement</code> method does nothing. */
144   public void endElement (String JavaDoc namespaceURI,
145               String JavaDoc localName,
146               String JavaDoc qName)
147     throws SAXException {
148
149     super.endElement(namespaceURI, localName, qName);
150
151     // Check after popping the stack so we don't erroneously think we
152
// are our own extension namespace...
153
boolean inExtension = inExtensionNamespace();
154
155     int entryType = -1;
156     Vector JavaDoc entryArgs = new Vector JavaDoc();
157
158     if (namespaceURI != null
159     && (extendedNamespaceName.equals(namespaceURI))
160     && !inExtension) {
161
162       String JavaDoc popURI = (String JavaDoc) baseURIStack.pop();
163       String JavaDoc baseURI = (String JavaDoc) baseURIStack.peek();
164
165       if (!baseURI.equals(popURI)) {
166     entryType = catalog.BASE;
167     entryArgs.add(baseURI);
168
169     debug.message(4, "(reset) xml:base", baseURI);
170
171     try {
172       CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
173       catalog.addEntry(ce);
174     } catch (CatalogException cex) {
175       if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
176         debug.message(1, "Invalid catalog entry type", localName);
177       } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
178         debug.message(1, "Invalid catalog entry (rbase)", localName);
179       }
180     }
181       }
182     }
183   }
184 }
185
Popular Tags