~ubuntu-branches/debian/wheezy/jing-trang/wheezy

« back to all changes in this revision

Viewing changes to mod/catalog/src/main/com/thaiopensource/resolver/catalog/OasisCatalog.java

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Thibault
  • Date: 2009-09-01 15:53:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090901155303-2kweef05h5v9j3ni
Tags: upstream-20090818
ImportĀ upstreamĀ versionĀ 20090818

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.thaiopensource.resolver.catalog;
 
2
 
 
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;
 
12
 
 
13
import javax.xml.transform.sax.SAXSource;
 
14
import java.io.IOException;
 
15
import java.net.URL;
 
16
 
 
17
/**
 
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.
 
20
 */
 
21
class OasisCatalog extends Catalog {
 
22
  private final SAXResolver saxResolver;
 
23
  
 
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
 
29
  }
 
30
 
 
31
  protected void parseCatalogFile(String uri) throws IOException {
 
32
    OASISXMLCatalogReader catalogReader = new OASISXMLCatalogReader();
 
33
    try {
 
34
      SAXSource source = saxResolver.resolve(new XMLDocumentIdentifier(uri, null, OASISXMLCatalogReader.namespaceName));
 
35
      String systemId = source.getInputSource().getSystemId();
 
36
      if (systemId == null)
 
37
        systemId = uri;
 
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());
 
45
    }
 
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));
 
52
    }
 
53
  }
 
54
}