01: package net.sf.saxon.dom;
02:
03: import org.w3c.dom.ls.LSResourceResolver;
04: import org.w3c.dom.ls.LSInput;
05:
06: import javax.xml.transform.URIResolver;
07: import javax.xml.transform.Source;
08: import javax.xml.transform.TransformerException;
09: import javax.xml.transform.stream.StreamSource;
10: import java.io.Reader;
11: import java.io.InputStream;
12: import java.io.StringReader;
13:
14: /**
15: * This class implements the JAXP URIResourceResolver as a wrapper around
16: * a DOM Level 3 LSResourceResolver. This serves two purposes: it allows the
17: * same underlying object to be used in both roles, and it allows an LSResourceResolver
18: * to be passed around the system in places where a URIResolver is expected, for
19: * example in the PipelineConfiguration
20: */
21:
22: public class URIResourceResolver implements URIResolver {
23:
24: private LSResourceResolver resolver;
25:
26: public URIResourceResolver(LSResourceResolver resolver) {
27: this .resolver = resolver;
28: }
29:
30: public LSResourceResolver getLSResourceResolver() {
31: return resolver;
32: }
33:
34: /**
35: * Called by an XSLT processor when it encounters
36: * an xsl:include, xsl:import, or document() function.
37: *
38: * @param href An href attribute, which may be relative or absolute.
39: * @param base The base URI against which the first argument will be made
40: * absolute if the absolute URI is required.
41: * @return A Source object, or null if the href cannot be resolved,
42: * and the processor should try to resolve the URI itself.
43: * @throws javax.xml.transform.TransformerException
44: * if an error occurs when trying to
45: * resolve the URI.
46: */
47: public Source resolve(String href, String base)
48: throws TransformerException {
49: LSInput lsin = resolver.resolveResource(
50: "http://www.w3.org/TR/REC-xml", null, null, href, base);
51: if (lsin == null) {
52: return null;
53: }
54:
55: Reader reader = lsin.getCharacterStream();
56: InputStream stream = lsin.getByteStream();
57: String content = lsin.getStringData();
58: String systemId = lsin.getSystemId();
59: String publicId = lsin.getPublicId();
60:
61: if (content != null) {
62: reader = new StringReader(content);
63: }
64:
65: StreamSource source = new StreamSource();
66: source.setSystemId(systemId);
67: source.setPublicId(publicId);
68: source.setReader(reader);
69: source.setInputStream(stream);
70:
71: return source;
72:
73: }
74: }
75:
76: //
77: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
78: // you may not use this file except in compliance with the License. You may obtain a copy of the
79: // License at http://www.mozilla.org/MPL/
80: //
81: // Software distributed under the License is distributed on an "AS IS" basis,
82: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
83: // See the License for the specific language governing rights and limitations under the License.
84: //
85: // The Original Code is: all this file.
86: //
87: // The Initial Developer of the Original Code is Michael H. Kay
88: //
89: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
90: //
91: // Contributor(s): none.
92: //
|