KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > xml > catalog > readers > ExtendedXMLCatalogReader


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

3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" must
29  * not be used to endorse or promote products derived from this
30  * software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * nor may "Apache" appear in their name, without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 package org.jboss.util.xml.catalog.readers;
58
59 import java.util.Vector JavaDoc;
60
61 import org.jboss.util.xml.catalog.Catalog;
62 import org.jboss.util.xml.catalog.CatalogEntry;
63 import org.jboss.util.xml.catalog.CatalogException;
64 import org.jboss.util.xml.catalog.Resolver;
65
66 import org.xml.sax.*;
67 import org.w3c.dom.*;
68
69 /**
70  * Parse Extended OASIS Entity Resolution Technical Committee
71  * XML Catalog files.
72  *
73  * @see Catalog
74  *
75  * @author Norman Walsh
76  * <a HREF="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
77  *
78  * @version 1.0
79  */

80 public class ExtendedXMLCatalogReader extends OASISXMLCatalogReader {
81   /** The namespace name of extended catalog elements */
82   public static final String JavaDoc extendedNamespaceName = "http://nwalsh.com/xcatalog/1.0";
83
84   /**
85    * The SAX <code>startElement</code> method recognizes elements
86    * from the plain catalog format and instantiates CatalogEntry
87    * objects for them.
88    *
89    * @param namespaceURI The namespace name of the element.
90    * @param localName The local name of the element.
91    * @param qName The QName of the element.
92    * @param atts The list of attributes on the element.
93    *
94    * @see CatalogEntry
95    */

96   public void startElement (String JavaDoc namespaceURI,
97                 String JavaDoc localName,
98                 String JavaDoc qName,
99                 Attributes atts)
100     throws SAXException {
101
102     // Check before calling the super because super will report our
103
// namespace as an extension namespace, but that doesn't count
104
// for this element.
105
boolean inExtension = inExtensionNamespace();
106
107     super.startElement(namespaceURI, localName, qName, atts);
108
109     int entryType = -1;
110     Vector JavaDoc entryArgs = new Vector JavaDoc();
111
112     if (namespaceURI != null && extendedNamespaceName.equals(namespaceURI)
113     && !inExtension) {
114       // This is an Extended XML Catalog entry
115

116       if (atts.getValue("xml:base") != null) {
117     String JavaDoc baseURI = atts.getValue("xml:base");
118     entryType = Catalog.BASE;
119     entryArgs.add(baseURI);
120     baseURIStack.push(baseURI);
121
122     debug.message(4, "xml:base", baseURI);
123
124     try {
125       CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
126       catalog.addEntry(ce);
127     } catch (CatalogException cex) {
128       if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
129         debug.message(1, "Invalid catalog entry type", localName);
130       } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
131         debug.message(1, "Invalid catalog entry (base)", localName);
132       }
133     }
134
135     entryType = -1;
136     entryArgs = new Vector JavaDoc();
137       } else {
138     baseURIStack.push(baseURIStack.peek());
139       }
140
141       if (localName.equals("uriSuffix")) {
142     if (checkAttributes(atts, "suffix", "uri")) {
143       entryType = Resolver.URISUFFIX;
144       entryArgs.add(atts.getValue("suffix"));
145       entryArgs.add(atts.getValue("uri"));
146
147       debug.message(4, "uriSuffix",
148             atts.getValue("suffix"),
149             atts.getValue("uri"));
150     }
151       } else if (localName.equals("systemSuffix")) {
152     if (checkAttributes(atts, "suffix", "uri")) {
153       entryType = Resolver.SYSTEMSUFFIX;
154       entryArgs.add(atts.getValue("suffix"));
155       entryArgs.add(atts.getValue("uri"));
156
157       debug.message(4, "systemSuffix",
158             atts.getValue("suffix"),
159             atts.getValue("uri"));
160     }
161       } else {
162     // This is equivalent to an invalid catalog entry type
163
debug.message(1, "Invalid catalog entry type", localName);
164       }
165
166       if (entryType >= 0) {
167     try {
168       CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
169       catalog.addEntry(ce);
170     } catch (CatalogException cex) {
171       if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
172         debug.message(1, "Invalid catalog entry type", localName);
173       } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
174         debug.message(1, "Invalid catalog entry", localName);
175       }
176     }
177       }
178     }
179   }
180
181   /** The SAX <code>endElement</code> method does nothing. */
182   public void endElement (String JavaDoc namespaceURI,
183               String JavaDoc localName,
184               String JavaDoc qName)
185     throws SAXException {
186
187     super.endElement(namespaceURI, localName, qName);
188
189     // Check after popping the stack so we don't erroneously think we
190
// are our own extension namespace...
191
boolean inExtension = inExtensionNamespace();
192
193     int entryType = -1;
194     Vector JavaDoc entryArgs = new Vector JavaDoc();
195
196     if (namespaceURI != null
197     && (extendedNamespaceName.equals(namespaceURI))
198     && !inExtension) {
199
200       String JavaDoc popURI = (String JavaDoc) baseURIStack.pop();
201       String JavaDoc baseURI = (String JavaDoc) baseURIStack.peek();
202
203       if (!baseURI.equals(popURI)) {
204     entryType = catalog.BASE;
205     entryArgs.add(baseURI);
206
207     debug.message(4, "(reset) xml:base", baseURI);
208
209     try {
210       CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
211       catalog.addEntry(ce);
212     } catch (CatalogException cex) {
213       if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
214         debug.message(1, "Invalid catalog entry type", localName);
215       } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
216         debug.message(1, "Invalid catalog entry (rbase)", localName);
217       }
218     }
219       }
220     }
221   }
222 }
223
Popular Tags