DOMCatalogReaderpublic class DOMCatalogReader extends Object implements com.sun.org.apache.xml.internal.resolver.readers.CatalogReaderA DOM-based CatalogReader.
This class is used to read XML Catalogs using the DOM. This reader
has an advantage over the SAX-based reader that it can analyze the
DOM tree rather than simply a series of SAX events. It has the disadvantage
that it requires all of the code necessary to build and walk a DOM
tree.
Since the choice of CatalogReaders (in the InputStream case) can only
be made on the basis of MIME type, the following problem occurs: only
one CatalogReader can exist for all XML mime types. In order to get
around this problem, the DOMCatalogReader relies on a set of external
CatalogParsers to actually build the catalog.
The selection of CatalogParsers is made on the basis of the QName
of the root element of the document.
This class requires the Java API for XML Parsing. |
Fields Summary |
---|
protected Hashtable | namespaceMapMapping table from QNames to CatalogParser classes.
Each key in this hash table has the form "elementname"
or "{namespaceuri}elementname". The former is used if the
namespace URI is null. |
Constructors Summary |
---|
public DOMCatalogReader()Null constructor; something for subclasses to call.
|
Methods Summary |
---|
public java.lang.String | getCatalogParser(java.lang.String namespaceURI, java.lang.String rootElement)Get the name of the parser class for a given catalog type.
This method returns the parserClass associated with the
namespaceURI/rootElement names specified.
if (namespaceURI == null) {
return (String) namespaceMap.get(rootElement);
} else {
return (String) namespaceMap.get("{"+namespaceURI+"}"+rootElement);
}
| public void | readCatalog(com.sun.org.apache.xml.internal.resolver.Catalog catalog, java.io.InputStream is)Read a catalog from an input stream.
This class reads a catalog from an input stream:
- Based on the QName of the root element, it determines which
parser to instantiate for this catalog.
- It constructs a DOM Document from the catalog and
- For each child of the root node, it calls the parser's
parseCatalogEntry method. This method is expected to make
appropriate calls back into the catalog to add entries for the
entries in the catalog. It is free to do this in whatever manner
is appropriate (perhaps using just the node passed in, perhaps
wandering arbitrarily throughout the tree).
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
throw new CatalogException(CatalogException.UNPARSEABLE);
}
Document doc = null;
try {
doc = builder.parse(is);
} catch (SAXException se) {
throw new CatalogException(CatalogException.UNKNOWN_FORMAT);
}
Element root = doc.getDocumentElement();
String namespaceURI = Namespaces.getNamespaceURI(root);
String localName = Namespaces.getLocalName(root);
String domParserClass = getCatalogParser(namespaceURI,
localName);
if (domParserClass == null) {
if (namespaceURI == null) {
catalog.getCatalogManager().debug.message(1, "No Catalog parser for "
+ localName);
} else {
catalog.getCatalogManager().debug.message(1, "No Catalog parser for "
+ "{" + namespaceURI + "}"
+ localName);
}
return;
}
DOMCatalogParser domParser = null;
try {
domParser = (DOMCatalogParser) Class.forName(domParserClass).newInstance();
} catch (ClassNotFoundException cnfe) {
catalog.getCatalogManager().debug.message(1, "Cannot load XML Catalog Parser class", domParserClass);
throw new CatalogException(CatalogException.UNPARSEABLE);
} catch (InstantiationException ie) {
catalog.getCatalogManager().debug.message(1, "Cannot instantiate XML Catalog Parser class", domParserClass);
throw new CatalogException(CatalogException.UNPARSEABLE);
} catch (IllegalAccessException iae) {
catalog.getCatalogManager().debug.message(1, "Cannot access XML Catalog Parser class", domParserClass);
throw new CatalogException(CatalogException.UNPARSEABLE);
} catch (ClassCastException cce ) {
catalog.getCatalogManager().debug.message(1, "Cannot cast XML Catalog Parser class", domParserClass);
throw new CatalogException(CatalogException.UNPARSEABLE);
}
Node node = root.getFirstChild();
while (node != null) {
domParser.parseCatalogEntry(catalog, node);
node = node.getNextSibling();
}
| public void | readCatalog(com.sun.org.apache.xml.internal.resolver.Catalog catalog, java.lang.String fileUrl)Read the catalog behind the specified URL.
URL url = new URL(fileUrl);
URLConnection urlCon = url.openConnection();
readCatalog(catalog, urlCon.getInputStream());
| public void | setCatalogParser(java.lang.String namespaceURI, java.lang.String rootElement, java.lang.String parserClass)Add a new parser to the reader.
This method associates the specified parserClass with the
namespaceURI/rootElement names specified.
if (namespaceURI == null) {
namespaceMap.put(rootElement, parserClass);
} else {
namespaceMap.put("{"+namespaceURI+"}"+rootElement, parserClass);
}
|
|