1
package com.thaiopensource.resolver.catalog;
3
import com.thaiopensource.resolver.ResolverException;
4
import com.thaiopensource.resolver.xml.XMLDocumentIdentifier;
5
import com.thaiopensource.resolver.xml.sax.SAXResolver;
6
import com.thaiopensource.xml.sax.DraconianErrorHandler;
7
import org.apache.xml.resolver.Catalog;
8
import org.apache.xml.resolver.CatalogManager;
9
import org.apache.xml.resolver.readers.OASISXMLCatalogReader;
10
import org.xml.sax.SAXException;
11
import org.xml.sax.XMLReader;
13
import javax.xml.transform.sax.SAXSource;
14
import java.io.IOException;
18
* A catalog with customized parsing of catalog files. In particular, it only supports
19
* OASIS XML Catalogs and it uses a SAXResolver for access to the catalog URIs.
21
class OasisCatalog extends Catalog {
22
private final SAXResolver saxResolver;
24
OasisCatalog(CatalogManager catalogManager, SAXResolver saxResolver) {
25
super(catalogManager);
26
this.saxResolver = saxResolver;
27
// don't call setupReaders; since we use our own parseCatalogFile
28
// we'll load the catalogs lazily
31
protected void parseCatalogFile(String uri) throws IOException {
32
OASISXMLCatalogReader catalogReader = new OASISXMLCatalogReader();
34
SAXSource source = saxResolver.resolve(new XMLDocumentIdentifier(uri, null, OASISXMLCatalogReader.namespaceName));
35
String systemId = source.getInputSource().getSystemId();
38
base = new URL(systemId);
39
catalogReader.setCatalog(this);
40
XMLReader xmlReader = source.getXMLReader();
41
xmlReader.setEntityResolver(new CatalogEntityResolver(xmlReader.getEntityResolver()));
42
xmlReader.setContentHandler(catalogReader);
43
xmlReader.setErrorHandler(new DraconianErrorHandler());
44
xmlReader.parse(source.getInputSource());
46
catch (SAXException e) {
47
Exception wrapped = e.getException();
48
// this will get unwrapped by CatalogResolver
49
throw new ResolverIOException(wrapped instanceof ResolverException
50
? (ResolverException)wrapped
51
: new ResolverException(e));