summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/Resolver.java
blob: 575764cb638e12ca06812b22cf8f3287b518b7ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package org.madore.damlengine;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.MalformedURLException;
import javax.xml.parsers.SAXParserFactory;
import org.w3c.dom.ls.LSResourceResolver;
import org.w3c.dom.ls.LSInput;
import org.xml.sax.SAXException;
import org.xml.sax.EntityResolver;
import org.xml.sax.ext.EntityResolver2;
import org.xml.sax.InputSource;
import org.apache.xml.resolver.CatalogManager;
import org.apache.xml.resolver.Catalog;
import org.apache.xml.resolver.readers.SAXCatalogReader;
import org.apache.xml.resolver.readers.OASISXMLCatalogReader;
import org.apache.xerces.dom.DOMInputImpl;
import org.apache.xerces.jaxp.SAXParserFactoryImpl;

public final class Resolver
    implements LSResourceResolver, EntityResolver, EntityResolver2 {

    private static final String catalogFiles[] = { "/etc/xml/catalog" };

    private CatalogManager catalogManager = null;
    private Catalog catalog = null;

    public static final class BootstrapResolver
	extends org.apache.xml.resolver.helpers.BootstrapResolver {
	@Override
	public InputSource resolveEntity(String publicId, String systemId) {
	    String resolved = null;
	    if ( publicId.equals("-//GlobalTransCorp//DTD XML Catalogs "
				 +"V1.0-Based Extension V1.0//EN") ) {
		resolved
		    = "file:///usr/share/xml/schema/xml-core/tr9401.dtd";
	    }
	    if ( resolved != null ) {
		InputSource source = new InputSource(resolved);
		source.setPublicId(publicId);
		return source;
	    }
	    return super.resolveEntity(publicId, systemId);
	}
    }

    public Resolver() {
	catalogManager = new CatalogManager();
	// catalogManager.setVerbosity(42);
	catalogManager.setPreferPublic(true);
	catalogManager.setRelativeCatalogs(false);
	catalogManager.setBootstrapResolver(new BootstrapResolver());
	catalog = new Catalog(catalogManager);
        SAXParserFactory spf = new SAXParserFactoryImpl();
        spf.setNamespaceAware(true);
        spf.setValidating(false);
        SAXCatalogReader saxReader = new SAXCatalogReader(spf);
        saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName,
				   "catalog", 
				   "org.apache.xml.resolver.readers."
				   +"OASISXMLCatalogReader");
        catalog.addReader("application/xml", saxReader);
	for ( String f : catalogFiles ) {
	    try {
		catalog.parseCatalog(f);
	    } catch (MalformedURLException e) {
		throw new RuntimeException(e);
	    } catch (IOException e) {
		throw new RuntimeException(e);
	    }
	}
    }

    public String resolvePublic(String publicId, String systemId) {
	try {
	    return catalog.resolvePublic(publicId, systemId);
	} catch (MalformedURLException e) {
	    throw new RuntimeException(e);
	} catch (IOException e) {
	    throw new RuntimeException(e);
	}
    }

    public String resolveSystem(String systemId) {
	try {
	    return catalog.resolveSystem(systemId);
	} catch (MalformedURLException e) {
	    throw new RuntimeException(e);
	} catch (IOException e) {
	    throw new RuntimeException(e);
	}
    }

    @SuppressWarnings("unused") // namespaceURI parameter is unused
    protected String resolve(String namespaceURI,
			     String publicId,
			     String systemId,
			     String baseURI) {
	String resolved;
	if ( publicId != null )
	    resolved = resolvePublic(publicId, systemId);
	else
	    resolved = resolveSystem(systemId);
	if ( resolved != null )
	    return resolved;
	if ( baseURI != null ) {
	    try {
		URI u = new URI(baseURI);
		URI v = u.resolve(systemId);
		return v.toString();
	    } catch ( URISyntaxException e ) {
		// Fail and continue to next return.
	    }
	}
	return systemId;
    }

    // Interface LSResourceResolver

    public LSInput resolveResource(String type,
				   String namespaceURI,
				   String publicId,
				   String systemId,
				   String baseURI) {
	String resolved = resolve(namespaceURI, publicId, systemId, baseURI);
	return new DOMInputImpl(publicId, resolved, baseURI);
    }

    // Interface EntityResolver

    public InputSource resolveEntity(String publicId, String systemId)
	throws SAXException {
	String resolved = resolve(null, publicId, systemId, null);
	InputSource source = new InputSource(resolved);
	source.setPublicId(publicId);
	return source;
    }

    // Interface EntityResolver2

    public InputSource resolveEntity(String name, String publicId,
				     String baseURI, String systemId)
	throws SAXException {
	String resolved = resolve(null, publicId, systemId, baseURI);
	InputSource source = new InputSource(resolved);
	source.setPublicId(publicId);
	return source;
    }

    public InputSource getExternalSubset(String name,
					 String baseURI) {
	return null;
    }

}