01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: */
19: package de.schlund.pfixcore.util;
20:
21: import java.io.IOException;
22: import java.net.MalformedURLException;
23:
24: import javax.xml.parsers.SAXParserFactory;
25:
26: import org.apache.xml.resolver.Catalog;
27: import org.apache.xml.resolver.CatalogManager;
28: import org.apache.xml.resolver.readers.OASISXMLCatalogReader;
29: import org.apache.xml.resolver.readers.SAXCatalogReader;
30: import org.xml.sax.EntityResolver;
31: import org.xml.sax.InputSource;
32: import org.xml.sax.SAXException;
33:
34: import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
35:
36: /**
37: * @author adam
38: */
39: public class PfixXmlCatalogEntityResolver implements EntityResolver {
40:
41: Catalog catalog = new Catalog();
42:
43: public PfixXmlCatalogEntityResolver(String catalogfile)
44: throws MalformedURLException, IOException {
45: // get rid of ugly message during build (" [pfx-iwrp] Cannot find CatalogManager.properties")
46: CatalogManager.getStaticManager().setIgnoreMissingProperties(
47: true);
48: // see org.apache.xerces.util.XMLCatalogResolver.attachReaderToCatalog(Catalog)
49: SAXParserFactory spf = new SAXParserFactoryImpl();
50: spf.setNamespaceAware(true);
51: spf.setValidating(false);
52: SAXCatalogReader saxReader = new SAXCatalogReader(spf);
53: saxReader
54: .setCatalogParser(OASISXMLCatalogReader.namespaceName,
55: "catalog",
56: "org.apache.xml.resolver.readers.OASISXMLCatalogReader");
57: catalog.addReader("application/xml", saxReader);
58: catalog.parseCatalog(catalogfile);
59: }
60:
61: public InputSource resolveEntity(String publicId, String systemId)
62: throws SAXException, IOException {
63: String resolvedId = null;
64: if (publicId != null && systemId != null) {
65: resolvedId = catalog.resolvePublic(publicId, systemId);
66: } else if (systemId != null) {
67: resolvedId = catalog.resolveSystem(systemId);
68: }
69:
70: if (resolvedId != null) {
71: InputSource source = new InputSource(resolvedId);
72: source.setPublicId(publicId);
73: return source;
74: }
75: // DEBUG
76: //System.out.println("PfixXmlCatalogEntityResolver.resolveEntity(publicId="+publicId+", systemId="+systemId+")="+resolvedId);
77: return null;
78: }
79: }
|