1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.sun.org.apache.xml.internal.resolver.readers;
19
20import java.util.Vector;
21import com.sun.org.apache.xml.internal.resolver.Catalog;
22import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
23import com.sun.org.apache.xml.internal.resolver.CatalogException;
24import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
25
26import org.xml.sax.*;
27
28import javax.xml.parsers.*;
29
30/**
31 * Parse "XCatalog" XML Catalog files, this is the XML Catalog format
32 * developed by John Cowan and supported by Apache.
33 *
34 * @see Catalog
35 *
36 * @author Norman Walsh
37 * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
38 *
39 */
40public class XCatalogReader extends SAXCatalogReader implements SAXCatalogParser {
41  /** The catalog object needs to be stored by the object so that
42   * SAX callbacks can use it.
43   */
44  protected Catalog catalog = null;
45
46  /** Set the current catalog. */
47  public void setCatalog (Catalog catalog) {
48    this.catalog = catalog;
49    debug = catalog.getCatalogManager().debug;
50  }
51
52  /** Get the current catalog. */
53  public Catalog getCatalog () {
54    return catalog;
55  }
56
57  /** Default constructor */
58  public XCatalogReader() {
59    super();
60  }
61
62  /** Constructor allowing for providing custom SAX parser factory */
63  public XCatalogReader(SAXParserFactory parserFactory, Catalog catalog) {
64    super(parserFactory);
65    setCatalog(catalog);
66  }
67
68  // ----------------------------------------------------------------------
69  // Implement the SAX ContentHandler interface
70
71  /** The SAX <code>setDocumentLocator</code> method does nothing. */
72  public void setDocumentLocator (Locator locator) {
73    return;
74  }
75
76  /** The SAX <code>startDocument</code> method does nothing. */
77  public void startDocument ()
78    throws SAXException {
79    return;
80  }
81
82  /** The SAX <code>endDocument</code> method does nothing. */
83  public void endDocument ()
84    throws SAXException {
85    return;
86  }
87
88  /**
89   * The SAX <code>startElement</code> method recognizes elements
90   * from the plain catalog format and instantiates CatalogEntry
91   * objects for them.
92   *
93   * @param namespaceURI The namespace name of the element.
94   * @param localName The local name of the element.
95   * @param qName The QName of the element.
96   * @param atts The list of attributes on the element.
97   *
98   * @see CatalogEntry
99   */
100  public void startElement (String namespaceURI,
101                            String localName,
102                            String qName,
103                            Attributes atts)
104    throws SAXException {
105
106    int entryType = -1;
107    Vector entryArgs = new Vector();
108
109    if (localName.equals("Base")) {
110      entryType = Catalog.BASE;
111      entryArgs.add(atts.getValue("HRef"));
112
113      debug.message(4, "Base", atts.getValue("HRef"));
114    } else if (localName.equals("Delegate")) {
115      entryType = Catalog.DELEGATE_PUBLIC;
116      entryArgs.add(atts.getValue("PublicID"));
117      entryArgs.add(atts.getValue("HRef"));
118
119      debug.message(4, "Delegate",
120                    PublicId.normalize(atts.getValue("PublicID")),
121                    atts.getValue("HRef"));
122    } else if (localName.equals("Extend")) {
123      entryType = Catalog.CATALOG;
124      entryArgs.add(atts.getValue("HRef"));
125
126      debug.message(4, "Extend", atts.getValue("HRef"));
127    } else if (localName.equals("Map")) {
128      entryType = Catalog.PUBLIC;
129      entryArgs.add(atts.getValue("PublicID"));
130      entryArgs.add(atts.getValue("HRef"));
131
132      debug.message(4, "Map",
133                    PublicId.normalize(atts.getValue("PublicID")),
134                    atts.getValue("HRef"));
135    } else if (localName.equals("Remap")) {
136      entryType = Catalog.SYSTEM;
137      entryArgs.add(atts.getValue("SystemID"));
138      entryArgs.add(atts.getValue("HRef"));
139
140      debug.message(4, "Remap",
141                    atts.getValue("SystemID"),
142                    atts.getValue("HRef"));
143    } else if (localName.equals("XCatalog")) {
144      // nop, start of catalog
145    } else {
146      // This is equivalent to an invalid catalog entry type
147      debug.message(1, "Invalid catalog entry type", localName);
148    }
149
150    if (entryType >= 0) {
151      try {
152        CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
153        catalog.addEntry(ce);
154      } catch (CatalogException cex) {
155        if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
156          debug.message(1, "Invalid catalog entry type", localName);
157        } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
158          debug.message(1, "Invalid catalog entry", localName);
159        }
160      }
161    }
162  }
163
164  /** The SAX <code>endElement</code> method does nothing. */
165  public void endElement (String namespaceURI,
166                          String localName,
167                          String qName)
168    throws SAXException {
169    return;
170  }
171
172  /** The SAX <code>characters</code> method does nothing. */
173  public void characters (char ch[], int start, int length)
174    throws SAXException {
175    return;
176  }
177
178  /** The SAX <code>ignorableWhitespace</code> method does nothing. */
179  public void ignorableWhitespace (char ch[], int start, int length)
180    throws SAXException {
181    return;
182  }
183
184  /** The SAX <code>processingInstruction</code> method does nothing. */
185  public void processingInstruction (String target, String data)
186    throws SAXException {
187    return;
188  }
189
190  /** The SAX <code>skippedEntity</code> method does nothing. */
191  public void skippedEntity (String name)
192    throws SAXException {
193    return;
194  }
195
196  /** The SAX <code>startPrefixMapping</code> method does nothing. */
197  public void startPrefixMapping(String prefix, String uri)
198    throws SAXException {
199    return;
200  }
201
202  /** The SAX <code>endPrefixMapping</code> method does nothing. */
203  public void endPrefixMapping(String prefix)
204    throws SAXException {
205    return;
206  }
207
208}
209