KickJava   Java API By Example, From Geeks To Geeks.

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


1 // XCatalogReader.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.CatalogEntry;
25 import com.sun.org.apache.xml.internal.resolver.CatalogException;
26 import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
27
28 import org.xml.sax.*;
29
30 import javax.xml.parsers.*;
31
32 /**
33  * Parse "xcatalog" XML Catalog files, this is the XML Catalog format
34  * developed by John Cowan and supported by Apache.
35  *
36  * @see Catalog
37  *
38  * @author Norman Walsh
39  * <a HREF="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
40  *
41  * @version 1.0
42  */

43 public class XCatalogReader extends SAXCatalogReader implements SAXCatalogParser {
44   /** The catalog object needs to be stored by the object so that
45    * SAX callbacks can use it.
46    */

47   protected Catalog catalog = null;
48
49   /** Set the current catalog. */
50   public void setCatalog (Catalog catalog) {
51     this.catalog = catalog;
52   }
53
54   /** Get the current catalog. */
55   public Catalog getCatalog () {
56     return catalog;
57   }
58
59   /** The constructor */
60   public XCatalogReader(SAXParserFactory parserFactory) {
61     super(parserFactory);
62   }
63
64   // ----------------------------------------------------------------------
65
// Implement the SAX DocumentHandler interface
66

67   /** The SAX <code>setDocumentLocator</code> method does nothing. */
68   public void setDocumentLocator (Locator locator) {
69     return;
70   }
71
72   /** The SAX <code>startDocument</code> method does nothing. */
73   public void startDocument ()
74     throws SAXException {
75     return;
76   }
77
78   /** The SAX <code>endDocument</code> method does nothing. */
79   public void endDocument ()
80     throws SAXException {
81     return;
82   }
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     int entryType = -1;
103     Vector JavaDoc entryArgs = new Vector JavaDoc();
104
105     if (localName.equals("Base")) {
106       entryType = catalog.BASE;
107       entryArgs.add(atts.getValue("HRef"));
108
109       catalog.getCatalogManager().debug.message(4, "Base", atts.getValue("HRef"));
110     } else if (localName.equals("Delegate")) {
111       entryType = catalog.DELEGATE_PUBLIC;
112       entryArgs.add(atts.getValue("PublicId"));
113       entryArgs.add(atts.getValue("HRef"));
114
115       catalog.getCatalogManager().debug.message(4, "Delegate",
116             PublicId.normalize(atts.getValue("PublicId")),
117             atts.getValue("HRef"));
118     } else if (localName.equals("Extend")) {
119       entryType = catalog.CATALOG;
120       entryArgs.add(atts.getValue("HRef"));
121
122       catalog.getCatalogManager().debug.message(4, "Extend", atts.getValue("HRef"));
123     } else if (localName.equals("Map")) {
124       entryType = catalog.PUBLIC;
125       entryArgs.add(atts.getValue("PublicId"));
126       entryArgs.add(atts.getValue("HRef"));
127
128       catalog.getCatalogManager().debug.message(4, "Map",
129             PublicId.normalize(atts.getValue("PublicId")),
130             atts.getValue("HRef"));
131     } else if (localName.equals("Remap")) {
132       entryType = catalog.SYSTEM;
133       entryArgs.add(atts.getValue("SystemId"));
134       entryArgs.add(atts.getValue("HRef"));
135
136       catalog.getCatalogManager().debug.message(4, "Remap",
137             atts.getValue("SystemId"),
138             atts.getValue("HRef"));
139     } else if (localName.equals("XMLCatalog")) {
140       // nop, start of catalog
141
} else {
142       // This is equivalent to an invalid catalog entry type
143
catalog.getCatalogManager().debug.message(1, "Invalid catalog entry type", localName);
144     }
145
146     if (entryType >= 0) {
147       try {
148     CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
149     catalog.addEntry(ce);
150       } catch (CatalogException cex) {
151     if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
152       catalog.getCatalogManager().debug.message(1, "Invalid catalog entry type", localName);
153     } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
154       catalog.getCatalogManager().debug.message(1, "Invalid catalog entry", localName);
155     }
156       }
157     }
158     }
159
160     /** The SAX <code>endElement</code> method does nothing. */
161     public void endElement (String JavaDoc namespaceURI,
162                 String JavaDoc localName,
163                 String JavaDoc qName)
164       throws SAXException {
165       return;
166     }
167
168   /** The SAX <code>characters</code> method does nothing. */
169   public void characters (char ch[], int start, int length)
170     throws SAXException {
171     return;
172   }
173
174   /** The SAX <code>ignorableWhitespace</code> method does nothing. */
175   public void ignorableWhitespace (char ch[], int start, int length)
176     throws SAXException {
177     return;
178   }
179
180   /** The SAX <code>processingInstruction</code> method does nothing. */
181   public void processingInstruction (String JavaDoc target, String JavaDoc data)
182     throws SAXException {
183     return;
184   }
185 }
186
Popular Tags