KickJava   Java API By Example, From Geeks To Geeks.

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


1 // XCatalogReader.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 import org.jboss.util.xml.catalog.Catalog;
61 import org.jboss.util.xml.catalog.CatalogEntry;
62 import org.jboss.util.xml.catalog.CatalogException;
63 import org.jboss.util.xml.catalog.helpers.PublicId;
64
65 import org.xml.sax.*;
66
67 import javax.xml.parsers.*;
68
69 /**
70  * Parse "xcatalog" XML Catalog files, this is the XML Catalog format
71  * developed by John Cowan and supported by Apache.
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 XCatalogReader extends SAXCatalogReader implements SAXCatalogParser {
81   /** The catalog object needs to be stored by the object so that
82    * SAX callbacks can use it.
83    */

84   protected Catalog catalog = null;
85
86   /** Set the current catalog. */
87   public void setCatalog (Catalog catalog) {
88     this.catalog = catalog;
89   }
90
91   /** Get the current catalog. */
92   public Catalog getCatalog () {
93     return catalog;
94   }
95
96   /** The constructor */
97   public XCatalogReader(SAXParserFactory parserFactory) {
98     super(parserFactory);
99   }
100
101   // ----------------------------------------------------------------------
102
// Implement the SAX DocumentHandler interface
103

104   /** The SAX <code>setDocumentLocator</code> method does nothing. */
105   public void setDocumentLocator (Locator locator) {
106     return;
107   }
108
109   /** The SAX <code>startDocument</code> method does nothing. */
110   public void startDocument ()
111     throws SAXException {
112     return;
113   }
114
115   /** The SAX <code>endDocument</code> method does nothing. */
116   public void endDocument ()
117     throws SAXException {
118     return;
119   }
120
121   /**
122    * The SAX <code>startElement</code> method recognizes elements
123    * from the plain catalog format and instantiates CatalogEntry
124    * objects for them.
125    *
126    * @param namespaceURI The namespace name of the element.
127    * @param localName The local name of the element.
128    * @param qName The QName of the element.
129    * @param atts The list of attributes on the element.
130    *
131    * @see CatalogEntry
132    */

133   public void startElement (String JavaDoc namespaceURI,
134                 String JavaDoc localName,
135                 String JavaDoc qName,
136                 Attributes atts)
137     throws SAXException {
138
139     int entryType = -1;
140     Vector JavaDoc entryArgs = new Vector JavaDoc();
141
142     if (localName.equals("Base")) {
143       entryType = catalog.BASE;
144       entryArgs.add(atts.getValue("HRef"));
145
146       catalog.getCatalogManager().debug.message(4, "Base", atts.getValue("HRef"));
147     } else if (localName.equals("Delegate")) {
148       entryType = catalog.DELEGATE_PUBLIC;
149       entryArgs.add(atts.getValue("PublicId"));
150       entryArgs.add(atts.getValue("HRef"));
151
152       catalog.getCatalogManager().debug.message(4, "Delegate",
153             PublicId.normalize(atts.getValue("PublicId")),
154             atts.getValue("HRef"));
155     } else if (localName.equals("Extend")) {
156       entryType = catalog.CATALOG;
157       entryArgs.add(atts.getValue("HRef"));
158
159       catalog.getCatalogManager().debug.message(4, "Extend", atts.getValue("HRef"));
160     } else if (localName.equals("Map")) {
161       entryType = catalog.PUBLIC;
162       entryArgs.add(atts.getValue("PublicId"));
163       entryArgs.add(atts.getValue("HRef"));
164
165       catalog.getCatalogManager().debug.message(4, "Map",
166             PublicId.normalize(atts.getValue("PublicId")),
167             atts.getValue("HRef"));
168     } else if (localName.equals("Remap")) {
169       entryType = catalog.SYSTEM;
170       entryArgs.add(atts.getValue("SystemId"));
171       entryArgs.add(atts.getValue("HRef"));
172
173       catalog.getCatalogManager().debug.message(4, "Remap",
174             atts.getValue("SystemId"),
175             atts.getValue("HRef"));
176     } else if (localName.equals("XMLCatalog")) {
177       // nop, start of catalog
178
} else {
179       // This is equivalent to an invalid catalog entry type
180
catalog.getCatalogManager().debug.message(1, "Invalid catalog entry type", localName);
181     }
182
183     if (entryType >= 0) {
184       try {
185     CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
186     catalog.addEntry(ce);
187       } catch (CatalogException cex) {
188     if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
189       catalog.getCatalogManager().debug.message(1, "Invalid catalog entry type", localName);
190     } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
191       catalog.getCatalogManager().debug.message(1, "Invalid catalog entry", localName);
192     }
193       }
194     }
195     }
196
197     /** The SAX <code>endElement</code> method does nothing. */
198     public void endElement (String JavaDoc namespaceURI,
199                 String JavaDoc localName,
200                 String JavaDoc qName)
201       throws SAXException {
202       return;
203     }
204
205   /** The SAX <code>characters</code> method does nothing. */
206   public void characters (char ch[], int start, int length)
207     throws SAXException {
208     return;
209   }
210
211   /** The SAX <code>ignorableWhitespace</code> method does nothing. */
212   public void ignorableWhitespace (char ch[], int start, int length)
213     throws SAXException {
214     return;
215   }
216
217   /** The SAX <code>processingInstruction</code> method does nothing. */
218   public void processingInstruction (String JavaDoc target, String JavaDoc data)
219     throws SAXException {
220     return;
221   }
222 }
223
Popular Tags