286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A// DOMCatalogReader.java - Read XML Catalog files
286N/A
286N/A/*
286N/A * Copyright 2001-2004 The Apache Software Foundation or its licensors,
286N/A * as applicable.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xml.internal.resolver.readers;
286N/A
286N/Aimport java.util.Hashtable;
286N/Aimport java.io.IOException;
286N/Aimport java.io.InputStream;
286N/Aimport java.net.URL;
286N/Aimport java.net.URLConnection;
286N/Aimport java.net.MalformedURLException;
286N/A
286N/Aimport javax.xml.parsers.DocumentBuilderFactory;
286N/Aimport javax.xml.parsers.DocumentBuilder;
286N/Aimport javax.xml.parsers.ParserConfigurationException;
286N/A
286N/Aimport com.sun.org.apache.xml.internal.resolver.Catalog;
286N/Aimport com.sun.org.apache.xml.internal.resolver.CatalogException;
286N/Aimport com.sun.org.apache.xml.internal.resolver.readers.CatalogReader;
286N/Aimport com.sun.org.apache.xml.internal.resolver.helpers.Namespaces;
286N/A
286N/Aimport org.xml.sax.SAXException;
286N/Aimport org.w3c.dom.*;
286N/A
286N/A/**
286N/A * A DOM-based CatalogReader.
286N/A *
286N/A * <p>This class is used to read XML Catalogs using the DOM. This reader
286N/A * has an advantage over the SAX-based reader that it can analyze the
286N/A * DOM tree rather than simply a series of SAX events. It has the disadvantage
286N/A * that it requires all of the code necessary to build and walk a DOM
286N/A * tree.</p>
286N/A *
286N/A * <p>Since the choice of CatalogReaders (in the InputStream case) can only
286N/A * be made on the basis of MIME type, the following problem occurs: only
286N/A * one CatalogReader can exist for all XML mime types. In order to get
286N/A * around this problem, the DOMCatalogReader relies on a set of external
286N/A * CatalogParsers to actually build the catalog.</p>
286N/A *
286N/A * <p>The selection of CatalogParsers is made on the basis of the QName
286N/A * of the root element of the document.</p>
286N/A *
286N/A * <p>This class requires the <a href="http://java.sun.com/aboutJava/communityprocess/final/jsr005/index.html">Java API for XML Parsing</a>.</p>
286N/A *
286N/A * @see Catalog
286N/A * @see CatalogReader
286N/A * @see SAXCatalogReader
286N/A * @see TextCatalogReader
286N/A * @see DOMCatalogParser
286N/A *
286N/A * @author Norman Walsh
286N/A * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
286N/A *
286N/A */
286N/Apublic class DOMCatalogReader implements CatalogReader {
286N/A /**
286N/A * Mapping table from QNames to CatalogParser classes.
286N/A *
286N/A * <p>Each key in this hash table has the form "elementname"
286N/A * or "{namespaceuri}elementname". The former is used if the
286N/A * namespace URI is null.</p>
286N/A */
286N/A protected Hashtable namespaceMap = new Hashtable();
286N/A
286N/A /**
286N/A * Add a new parser to the reader.
286N/A *
286N/A * <p>This method associates the specified parserClass with the
286N/A * namespaceURI/rootElement names specified.</p>
286N/A *
286N/A * @param namespaceURI The namespace URI. <em>Not</em> the prefix.
286N/A * @param rootElement The name of the root element.
286N/A * @param parserClass The name of the parserClass to instantiate
286N/A * for this kind of catalog.
286N/A */
286N/A public void setCatalogParser(String namespaceURI,
286N/A String rootElement,
286N/A String parserClass) {
286N/A if (namespaceURI == null) {
286N/A namespaceMap.put(rootElement, parserClass);
286N/A } else {
286N/A namespaceMap.put("{"+namespaceURI+"}"+rootElement, parserClass);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Get the name of the parser class for a given catalog type.
286N/A *
286N/A * <p>This method returns the parserClass associated with the
286N/A * namespaceURI/rootElement names specified.</p>
286N/A *
286N/A * @param namespaceURI The namespace URI. <em>Not</em> the prefix.
286N/A * @param rootElement The name of the root element.
286N/A * @return The parser class.
286N/A */
286N/A public String getCatalogParser(String namespaceURI,
286N/A String rootElement) {
286N/A if (namespaceURI == null) {
286N/A return (String) namespaceMap.get(rootElement);
286N/A } else {
286N/A return (String) namespaceMap.get("{"+namespaceURI+"}"+rootElement);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Null constructor; something for subclasses to call.
286N/A */
286N/A public DOMCatalogReader() { }
286N/A
286N/A /**
286N/A * Read a catalog from an input stream.
286N/A *
286N/A * <p>This class reads a catalog from an input stream:</p>
286N/A *
286N/A * <ul>
286N/A * <li>Based on the QName of the root element, it determines which
286N/A * parser to instantiate for this catalog.</li>
286N/A * <li>It constructs a DOM Document from the catalog and</li>
286N/A * <li>For each child of the root node, it calls the parser's
286N/A * parseCatalogEntry method. This method is expected to make
286N/A * appropriate calls back into the catalog to add entries for the
286N/A * entries in the catalog. It is free to do this in whatever manner
286N/A * is appropriate (perhaps using just the node passed in, perhaps
286N/A * wandering arbitrarily throughout the tree).</li>
286N/A * </ul>
286N/A *
286N/A * @param catalog The catalog for which this reader is called.
286N/A * @param is The input stream that is to be read.
286N/A * @throws IOException if the URL cannot be read.
286N/A * @throws UnknownCatalogFormatException if the catalog format is
286N/A * not recognized.
286N/A * @throws UnparseableCatalogException if the catalog cannot be parsed.
286N/A * (For example, if it is supposed to be XML and isn't well-formed or
286N/A * if the parser class cannot be instantiated.)
286N/A */
286N/A public void readCatalog(Catalog catalog, InputStream is)
286N/A throws IOException, CatalogException {
286N/A
286N/A DocumentBuilderFactory factory = null;
286N/A DocumentBuilder builder = null;
286N/A
286N/A factory = DocumentBuilderFactory.newInstance();
286N/A factory.setNamespaceAware(false);
286N/A factory.setValidating(false);
286N/A try {
286N/A builder = factory.newDocumentBuilder();
286N/A } catch (ParserConfigurationException pce) {
286N/A throw new CatalogException(CatalogException.UNPARSEABLE);
286N/A }
286N/A
286N/A Document doc = null;
286N/A
286N/A try {
286N/A doc = builder.parse(is);
286N/A } catch (SAXException se) {
286N/A throw new CatalogException(CatalogException.UNKNOWN_FORMAT);
286N/A }
286N/A
286N/A Element root = doc.getDocumentElement();
286N/A
286N/A String namespaceURI = Namespaces.getNamespaceURI(root);
286N/A String localName = Namespaces.getLocalName(root);
286N/A
286N/A String domParserClass = getCatalogParser(namespaceURI,
286N/A localName);
286N/A
286N/A if (domParserClass == null) {
286N/A if (namespaceURI == null) {
286N/A catalog.getCatalogManager().debug.message(1, "No Catalog parser for "
286N/A + localName);
286N/A } else {
286N/A catalog.getCatalogManager().debug.message(1, "No Catalog parser for "
286N/A + "{" + namespaceURI + "}"
286N/A + localName);
286N/A }
286N/A return;
286N/A }
286N/A
286N/A DOMCatalogParser domParser = null;
286N/A
286N/A try {
286N/A domParser = (DOMCatalogParser) Class.forName(domParserClass).newInstance();
286N/A } catch (ClassNotFoundException cnfe) {
286N/A catalog.getCatalogManager().debug.message(1, "Cannot load XML Catalog Parser class", domParserClass);
286N/A throw new CatalogException(CatalogException.UNPARSEABLE);
286N/A } catch (InstantiationException ie) {
286N/A catalog.getCatalogManager().debug.message(1, "Cannot instantiate XML Catalog Parser class", domParserClass);
286N/A throw new CatalogException(CatalogException.UNPARSEABLE);
286N/A } catch (IllegalAccessException iae) {
286N/A catalog.getCatalogManager().debug.message(1, "Cannot access XML Catalog Parser class", domParserClass);
286N/A throw new CatalogException(CatalogException.UNPARSEABLE);
286N/A } catch (ClassCastException cce ) {
286N/A catalog.getCatalogManager().debug.message(1, "Cannot cast XML Catalog Parser class", domParserClass);
286N/A throw new CatalogException(CatalogException.UNPARSEABLE);
286N/A }
286N/A
286N/A Node node = root.getFirstChild();
286N/A while (node != null) {
286N/A domParser.parseCatalogEntry(catalog, node);
286N/A node = node.getNextSibling();
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Read the catalog behind the specified URL.
286N/A *
286N/A * @see #readCatalog(Catalog, InputStream)
286N/A *
286N/A * @param catalog The catalog for which we are reading.
286N/A * @param fileUrl The URL of the document that should be read.
286N/A *
286N/A * @throws MalformedURLException if the specified URL cannot be
286N/A * turned into a URL object.
286N/A * @throws IOException if the URL cannot be read.
286N/A * @throws UnknownCatalogFormatException if the catalog format is
286N/A * not recognized.
286N/A * @throws UnparseableCatalogException if the catalog cannot be parsed.
286N/A * (For example, if it is supposed to be XML and isn't well-formed.)
286N/A */
286N/A public void readCatalog(Catalog catalog, String fileUrl)
286N/A throws MalformedURLException, IOException, CatalogException {
286N/A URL url = new URL(fileUrl);
286N/A URLConnection urlCon = url.openConnection();
286N/A readCatalog(catalog, urlCon.getInputStream());
286N/A }
286N/A}