1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 // ExtendedXMLCatalogReader.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.Resolver;
29 import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
30 import com.sun.org.apache.xml.internal.resolver.CatalogException;
31 
32 import org.xml.sax.*;
33 import org.w3c.dom.*;
34 
35 /**
36  * Parse Extended OASIS Entity Resolution Technical Committee
37  * XML Catalog files.
38  *
39  * @see Catalog
40  *
41  * @author Norman Walsh
42  * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
43  *
44  */
45 public class ExtendedXMLCatalogReader extends OASISXMLCatalogReader {
46   /** The namespace name of extended catalog elements */
47   public static final String extendedNamespaceName = "http://nwalsh.com/xcatalog/1.0";
48 
49   /**
50    * The SAX <code>startElement</code> method recognizes elements
51    * from the plain catalog format and instantiates CatalogEntry
52    * objects for them.
53    *
54    * @param namespaceURI The namespace name of the element.
55    * @param localName The local name of the element.
56    * @param qName The QName of the element.
57    * @param atts The list of attributes on the element.
58    *
59    * @see CatalogEntry
60    */
startElement(String namespaceURI, String localName, String qName, Attributes atts)61   public void startElement (String namespaceURI,
62                             String localName,
63                             String qName,
64                             Attributes atts)
65     throws SAXException {
66 
67     // Check before calling the super because super will report our
68     // namespace as an extension namespace, but that doesn't count
69     // for this element.
70     boolean inExtension = inExtensionNamespace();
71 
72     super.startElement(namespaceURI, localName, qName, atts);
73 
74     int entryType = -1;
75     Vector entryArgs = new Vector();
76 
77     if (namespaceURI != null && extendedNamespaceName.equals(namespaceURI)
78         && !inExtension) {
79       // This is an Extended XML Catalog entry
80 
81       if (atts.getValue("xml:base") != null) {
82         String baseURI = atts.getValue("xml:base");
83         entryType = Catalog.BASE;
84         entryArgs.add(baseURI);
85         baseURIStack.push(baseURI);
86 
87         debug.message(4, "xml:base", baseURI);
88 
89         try {
90           CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
91           catalog.addEntry(ce);
92         } catch (CatalogException cex) {
93           if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
94             debug.message(1, "Invalid catalog entry type", localName);
95           } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
96             debug.message(1, "Invalid catalog entry (base)", localName);
97           }
98         }
99 
100         entryType = -1;
101         entryArgs = new Vector();
102       } else {
103         baseURIStack.push(baseURIStack.peek());
104       }
105 
106       if (localName.equals("uriSuffix")) {
107         if (checkAttributes(atts, "suffix", "uri")) {
108           entryType = Resolver.URISUFFIX;
109           entryArgs.add(atts.getValue("suffix"));
110           entryArgs.add(atts.getValue("uri"));
111 
112           debug.message(4, "uriSuffix",
113                         atts.getValue("suffix"),
114                         atts.getValue("uri"));
115         }
116       } else if (localName.equals("systemSuffix")) {
117         if (checkAttributes(atts, "suffix", "uri")) {
118           entryType = Resolver.SYSTEMSUFFIX;
119           entryArgs.add(atts.getValue("suffix"));
120           entryArgs.add(atts.getValue("uri"));
121 
122           debug.message(4, "systemSuffix",
123                         atts.getValue("suffix"),
124                         atts.getValue("uri"));
125         }
126       } else {
127         // This is equivalent to an invalid catalog entry type
128         debug.message(1, "Invalid catalog entry type", localName);
129       }
130 
131       if (entryType >= 0) {
132         try {
133           CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
134           catalog.addEntry(ce);
135         } catch (CatalogException cex) {
136           if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
137             debug.message(1, "Invalid catalog entry type", localName);
138           } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
139             debug.message(1, "Invalid catalog entry", localName);
140           }
141         }
142       }
143     }
144   }
145 
146   /** The SAX <code>endElement</code> method does nothing. */
endElement(String namespaceURI, String localName, String qName)147   public void endElement (String namespaceURI,
148                           String localName,
149                           String qName)
150     throws SAXException {
151 
152     super.endElement(namespaceURI, localName, qName);
153 
154     // Check after popping the stack so we don't erroneously think we
155     // are our own extension namespace...
156     boolean inExtension = inExtensionNamespace();
157 
158     int entryType = -1;
159     Vector entryArgs = new Vector();
160 
161     if (namespaceURI != null
162         && (extendedNamespaceName.equals(namespaceURI))
163         && !inExtension) {
164 
165       String popURI = (String) baseURIStack.pop();
166       String baseURI = (String) baseURIStack.peek();
167 
168       if (!baseURI.equals(popURI)) {
169         entryType = catalog.BASE;
170         entryArgs.add(baseURI);
171 
172         debug.message(4, "(reset) xml:base", baseURI);
173 
174         try {
175           CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
176           catalog.addEntry(ce);
177         } catch (CatalogException cex) {
178           if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
179             debug.message(1, "Invalid catalog entry type", localName);
180           } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
181             debug.message(1, "Invalid catalog entry (rbase)", localName);
182           }
183         }
184       }
185     }
186   }
187 }
188