KickJava   Java API By Example, From Geeks To Geeks.

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


1 // OASISXMLCatalogReader.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.Stack JavaDoc;
23 import java.util.Vector JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import com.sun.org.apache.xml.internal.resolver.Catalog;
26 import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
27 import com.sun.org.apache.xml.internal.resolver.CatalogException;
28 import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
29
30 import org.xml.sax.*;
31 import org.w3c.dom.*;
32
33 /**
34  * Parse OASIS Entity Resolution Technical Committee
35  * XML Catalog files.
36  *
37  * @see Catalog
38  *
39  * @author Norman Walsh
40  * <a HREF="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
41  *
42  * @version 1.0
43  */

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

48   protected Catalog catalog = null;
49
50   /** The namespace name of OASIS ERTC catalogs */
51   public static final String JavaDoc namespaceName = "urn:oasis:names:tc:entity:xmlns:xml:catalog";
52
53   /** The namespace name of OASIS ERTC TR9401 catalog extension */
54   public static final String JavaDoc tr9401NamespaceName = "urn:oasis:names:tc:entity:xmlns:tr9401:catalog";
55
56   protected Stack JavaDoc baseURIStack = new Stack JavaDoc();
57   protected Stack JavaDoc overrideStack = new Stack JavaDoc();
58   protected Stack JavaDoc namespaceStack = new Stack JavaDoc();
59
60   /** Set the current catalog. */
61   public void setCatalog (Catalog catalog) {
62     this.catalog = catalog;
63     debug = catalog.getCatalogManager().debug;
64   }
65
66   /** Get the current catalog. */
67   public Catalog getCatalog () {
68     return catalog;
69   }
70
71   /**
72    * Are we in an extension namespace?
73    *
74    * @return true if the current stack of open namespaces includes
75    * an extension namespace.
76    */

77   protected boolean inExtensionNamespace() {
78     boolean inExtension = false;
79
80     Enumeration JavaDoc elements = namespaceStack.elements();
81     while (!inExtension && elements.hasMoreElements()) {
82       String JavaDoc ns = (String JavaDoc) elements.nextElement();
83       if (ns == null) {
84     inExtension = true;
85       } else {
86     inExtension = (!ns.equals(tr9401NamespaceName)
87                && !ns.equals(namespaceName));
88       }
89     }
90
91     return inExtension;
92   }
93
94   // ----------------------------------------------------------------------
95
// Implement the SAX ContentHandler interface
96

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

128   public void startElement (String JavaDoc namespaceURI,
129                 String JavaDoc localName,
130                 String JavaDoc qName,
131                 Attributes atts)
132     throws SAXException {
133
134     int entryType = -1;
135     Vector JavaDoc entryArgs = new Vector JavaDoc();
136
137     namespaceStack.push(namespaceURI);
138
139     boolean inExtension = inExtensionNamespace();
140
141     if (namespaceURI != null && namespaceName.equals(namespaceURI)
142     && !inExtension) {
143       // This is an XML Catalog entry
144

145       if (atts.getValue("xml:base") != null) {
146     String JavaDoc baseURI = atts.getValue("xml:base");
147     entryType = Catalog.BASE;
148     entryArgs.add(baseURI);
149     baseURIStack.push(baseURI);
150
151     debug.message(4, "xml:base", baseURI);
152
153     try {
154       CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
155       catalog.addEntry(ce);
156     } catch (CatalogException cex) {
157       if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
158         debug.message(1, "Invalid catalog entry type", localName);
159       } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
160         debug.message(1, "Invalid catalog entry (base)", localName);
161       }
162     }
163
164     entryType = -1;
165     entryArgs = new Vector JavaDoc();
166
167       } else {
168     baseURIStack.push(baseURIStack.peek());
169       }
170
171       if ((localName.equals("catalog") || localName.equals("group"))
172       && atts.getValue("prefer") != null) {
173     String JavaDoc override = atts.getValue("prefer");
174
175     if (override.equals("public")) {
176       override = "yes";
177     } else if (override.equals("system")) {
178       override = "no";
179     } else {
180       debug.message(1,
181             "Invalid prefer: must be 'system' or 'public'",
182             localName);
183       override = catalog.getDefaultOverride();
184     }
185
186     entryType = Catalog.OVERRIDE;
187     entryArgs.add(override);
188     overrideStack.push(override);
189
190     debug.message(4, "override", override);
191
192     try {
193       CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
194       catalog.addEntry(ce);
195     } catch (CatalogException cex) {
196       if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
197         debug.message(1, "Invalid catalog entry type", localName);
198       } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
199         debug.message(1, "Invalid catalog entry (override)", localName);
200       }
201     }
202
203     entryType = -1;
204     entryArgs = new Vector JavaDoc();
205
206       } else {
207     overrideStack.push(overrideStack.peek());
208       }
209
210       if (localName.equals("delegatePublic")) {
211     if (checkAttributes(atts, "publicIdStartString", "catalog")) {
212       entryType = Catalog.DELEGATE_PUBLIC;
213       entryArgs.add(atts.getValue("publicIdStartString"));
214       entryArgs.add(atts.getValue("catalog"));
215
216       debug.message(4, "delegatePublic",
217             PublicId.normalize(atts.getValue("publicIdStartString")),
218             atts.getValue("catalog"));
219     }
220       } else if (localName.equals("delegateSystem")) {
221     if (checkAttributes(atts, "systemIdStartString", "catalog")) {
222       entryType = Catalog.DELEGATE_SYSTEM;
223       entryArgs.add(atts.getValue("systemIdStartString"));
224       entryArgs.add(atts.getValue("catalog"));
225
226       debug.message(4, "delegateSystem",
227             atts.getValue("systemIdStartString"),
228             atts.getValue("catalog"));
229     }
230       } else if (localName.equals("delegateURI")) {
231     if (checkAttributes(atts, "uriStartString", "catalog")) {
232       entryType = Catalog.DELEGATE_URI;
233       entryArgs.add(atts.getValue("uriStartString"));
234       entryArgs.add(atts.getValue("catalog"));
235
236       debug.message(4, "delegateURI",
237             atts.getValue("uriStartString"),
238             atts.getValue("catalog"));
239     }
240       } else if (localName.equals("rewriteSystem")) {
241     if (checkAttributes(atts, "systemIdStartString", "rewritePrefix")) {
242       entryType = Catalog.REWRITE_SYSTEM;
243       entryArgs.add(atts.getValue("systemIdStartString"));
244       entryArgs.add(atts.getValue("rewritePrefix"));
245
246       debug.message(4, "rewriteSystem",
247             atts.getValue("systemIdStartString"),
248             atts.getValue("rewritePrefix"));
249     }
250       } else if (localName.equals("systemSuffix")) {
251     if (checkAttributes(atts, "systemIdSuffix", "uri")) {
252       entryType = Catalog.SYSTEM_SUFFIX;
253       entryArgs.add(atts.getValue("systemIdSuffix"));
254       entryArgs.add(atts.getValue("uri"));
255
256       debug.message(4, "systemSuffix",
257             atts.getValue("systemIdSuffix"),
258             atts.getValue("uri"));
259     }
260       } else if (localName.equals("rewriteURI")) {
261     if (checkAttributes(atts, "uriStartString", "rewritePrefix")) {
262       entryType = Catalog.REWRITE_URI;
263       entryArgs.add(atts.getValue("uriStartString"));
264       entryArgs.add(atts.getValue("rewritePrefix"));
265
266       debug.message(4, "rewriteURI",
267             atts.getValue("uriStartString"),
268             atts.getValue("rewritePrefix"));
269     }
270       } else if (localName.equals("uriSuffix")) {
271     if (checkAttributes(atts, "uriSuffix", "uri")) {
272       entryType = Catalog.URI_SUFFIX;
273       entryArgs.add(atts.getValue("uriSuffix"));
274       entryArgs.add(atts.getValue("uri"));
275
276       debug.message(4, "uriSuffix",
277             atts.getValue("uriSuffix"),
278             atts.getValue("uri"));
279     }
280       } else if (localName.equals("nextCatalog")) {
281     if (checkAttributes(atts, "catalog")) {
282       entryType = Catalog.CATALOG;
283       entryArgs.add(atts.getValue("catalog"));
284
285       debug.message(4, "nextCatalog", atts.getValue("catalog"));
286     }
287       } else if (localName.equals("public")) {
288     if (checkAttributes(atts, "publicId", "uri")) {
289       entryType = Catalog.PUBLIC;
290       entryArgs.add(atts.getValue("publicId"));
291       entryArgs.add(atts.getValue("uri"));
292
293       debug.message(4, "public",
294             PublicId.normalize(atts.getValue("publicId")),
295             atts.getValue("uri"));
296     }
297       } else if (localName.equals("system")) {
298     if (checkAttributes(atts, "systemId", "uri")) {
299       entryType = Catalog.SYSTEM;
300       entryArgs.add(atts.getValue("systemId"));
301       entryArgs.add(atts.getValue("uri"));
302
303       debug.message(4, "system",
304             atts.getValue("systemId"),
305             atts.getValue("uri"));
306     }
307       } else if (localName.equals("uri")) {
308     if (checkAttributes(atts, "name", "uri")) {
309       entryType = Catalog.URI;
310       entryArgs.add(atts.getValue("name"));
311       entryArgs.add(atts.getValue("uri"));
312
313       debug.message(4, "uri",
314             atts.getValue("name"),
315             atts.getValue("uri"));
316     }
317       } else if (localName.equals("catalog")) {
318     // nop, start of catalog
319
} else if (localName.equals("group")) {
320     // nop, a group
321
} else {
322     // This is equivalent to an invalid catalog entry type
323
debug.message(1, "Invalid catalog entry type", localName);
324       }
325
326       if (entryType >= 0) {
327     try {
328       CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
329       catalog.addEntry(ce);
330     } catch (CatalogException cex) {
331       if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
332         debug.message(1, "Invalid catalog entry type", localName);
333       } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
334         debug.message(1, "Invalid catalog entry", localName);
335       }
336     }
337       }
338     }
339
340     if (namespaceURI != null && tr9401NamespaceName.equals(namespaceURI)
341     && !inExtension) {
342       // This is a TR9401 Catalog entry
343

344       if (atts.getValue("xml:base") != null) {
345     String JavaDoc baseURI = atts.getValue("xml:base");
346     entryType = Catalog.BASE;
347     entryArgs.add(baseURI);
348     baseURIStack.push(baseURI);
349
350     debug.message(4, "xml:base", baseURI);
351
352     try {
353       CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
354       catalog.addEntry(ce);
355     } catch (CatalogException cex) {
356       if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
357         debug.message(1, "Invalid catalog entry type", localName);
358       } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
359         debug.message(1, "Invalid catalog entry (base)", localName);
360       }
361     }
362
363     entryType = -1;
364     entryArgs = new Vector JavaDoc();
365
366       } else {
367     baseURIStack.push(baseURIStack.peek());
368       }
369
370       if (localName.equals("doctype")) {
371     entryType = catalog.DOCTYPE;
372     entryArgs.add(atts.getValue("name"));
373     entryArgs.add(atts.getValue("uri"));
374       } else if (localName.equals("document")) {
375     entryType = catalog.DOCUMENT;
376     entryArgs.add(atts.getValue("uri"));
377       } else if (localName.equals("dtddecl")) {
378     entryType = catalog.DTDDECL;
379     entryArgs.add(atts.getValue("publicId"));
380     entryArgs.add(atts.getValue("uri"));
381       } else if (localName.equals("entity")) {
382     entryType = Catalog.ENTITY;
383     entryArgs.add(atts.getValue("name"));
384     entryArgs.add(atts.getValue("uri"));
385       } else if (localName.equals("linktype")) {
386     entryType = Catalog.LINKTYPE;
387     entryArgs.add(atts.getValue("name"));
388     entryArgs.add(atts.getValue("uri"));
389       } else if (localName.equals("notation")) {
390     entryType = Catalog.NOTATION;
391     entryArgs.add(atts.getValue("name"));
392     entryArgs.add(atts.getValue("uri"));
393       } else if (localName.equals("sgmldecl")) {
394     entryType = Catalog.SGMLDECL;
395     entryArgs.add(atts.getValue("uri"));
396       } else {
397     // This is equivalent to an invalid catalog entry type
398
debug.message(1, "Invalid catalog entry type", localName);
399       }
400
401       if (entryType >= 0) {
402     try {
403       CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
404       catalog.addEntry(ce);
405     } catch (CatalogException cex) {
406       if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
407         debug.message(1, "Invalid catalog entry type", localName);
408       } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
409         debug.message(1, "Invalid catalog entry", localName);
410       }
411     }
412       }
413     }
414   }
415
416   public boolean checkAttributes (Attributes atts, String JavaDoc attName) {
417     if (atts.getValue(attName) == null) {
418       debug.message(1, "Error: required attribute " + attName + " missing.");
419       return false;
420     } else {
421       return true;
422     }
423   }
424
425   public boolean checkAttributes (Attributes atts,
426                   String JavaDoc attName1,
427                   String JavaDoc attName2) {
428     return checkAttributes(atts, attName1)
429       && checkAttributes(atts, attName2);
430   }
431
432   /** The SAX <code>endElement</code> method does nothing. */
433   public void endElement (String JavaDoc namespaceURI,
434               String JavaDoc localName,
435               String JavaDoc qName)
436     throws SAXException {
437
438     int entryType = -1;
439     Vector JavaDoc entryArgs = new Vector JavaDoc();
440
441     boolean inExtension = inExtensionNamespace();
442
443     if (namespaceURI != null
444     && !inExtension
445     && (namespaceName.equals(namespaceURI)
446         || tr9401NamespaceName.equals(namespaceURI))) {
447
448       String JavaDoc popURI = (String JavaDoc) baseURIStack.pop();
449       String JavaDoc baseURI = (String JavaDoc) baseURIStack.peek();
450
451       if (!baseURI.equals(popURI)) {
452     entryType = catalog.BASE;
453     entryArgs.add(baseURI);
454
455     debug.message(4, "(reset) xml:base", baseURI);
456
457     try {
458       CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
459       catalog.addEntry(ce);
460     } catch (CatalogException cex) {
461       if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
462         debug.message(1, "Invalid catalog entry type", localName);
463       } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
464         debug.message(1, "Invalid catalog entry (rbase)", localName);
465       }
466     }
467       }
468     }
469
470     if (namespaceURI != null && namespaceName.equals(namespaceURI)
471     && !inExtension) {
472       if (localName.equals("catalog") || localName.equals("group")) {
473     String JavaDoc popOverride = (String JavaDoc) overrideStack.pop();
474     String JavaDoc override = (String JavaDoc) overrideStack.peek();
475
476     if (!override.equals(popOverride)) {
477       entryType = catalog.OVERRIDE;
478       entryArgs.add(override);
479       overrideStack.push(override);
480
481       debug.message(4, "(reset) override", override);
482
483       try {
484         CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
485         catalog.addEntry(ce);
486       } catch (CatalogException cex) {
487         if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
488           debug.message(1, "Invalid catalog entry type", localName);
489         } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
490           debug.message(1, "Invalid catalog entry (roverride)", localName);
491         }
492       }
493     }
494       }
495     }
496
497     namespaceStack.pop();
498
499     return;
500   }
501
502   /** The SAX <code>characters</code> method does nothing. */
503   public void characters (char ch[], int start, int length)
504     throws SAXException {
505     return;
506   }
507
508   /** The SAX <code>ignorableWhitespace</code> method does nothing. */
509   public void ignorableWhitespace (char ch[], int start, int length)
510     throws SAXException {
511     return;
512   }
513
514   /** The SAX <code>processingInstruction</code> method does nothing. */
515   public void processingInstruction (String JavaDoc target, String JavaDoc data)
516     throws SAXException {
517     return;
518   }
519
520   /** The SAX <code>skippedEntity</code> method does nothing. */
521   public void skippedEntity (String JavaDoc name)
522     throws SAXException {
523     return;
524   }
525
526   /** The SAX <code>startPrefixMapping</code> method does nothing. */
527   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
528     throws SAXException {
529     return;
530   }
531
532   /** The SAX <code>endPrefixMapping</code> method does nothing. */
533   public void endPrefixMapping(String JavaDoc prefix)
534     throws SAXException {
535     return;
536   }
537
538 }
539
Popular Tags