KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > dom > URIResourceResolver


1 package net.sf.saxon.dom;
2
3 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
4 import org.w3c.dom.ls.LSInput JavaDoc;
5
6 import javax.xml.transform.URIResolver JavaDoc;
7 import javax.xml.transform.Source JavaDoc;
8 import javax.xml.transform.TransformerException JavaDoc;
9 import javax.xml.transform.stream.StreamSource JavaDoc;
10 import java.io.Reader JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.StringReader JavaDoc;
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 JavaDoc {
23
24     private LSResourceResolver JavaDoc resolver;
25
26     public URIResourceResolver(LSResourceResolver JavaDoc resolver) {
27         this.resolver = resolver;
28     }
29
30     public LSResourceResolver JavaDoc 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 JavaDoc resolve(String JavaDoc href, String JavaDoc base) throws TransformerException JavaDoc {
48         LSInput JavaDoc lsin = resolver.resolveResource(
49                 "http://www.w3.org/TR/REC-xml", null, null, href, base);
50         if (lsin == null) {
51             return null;
52         }
53
54         Reader JavaDoc reader = lsin.getCharacterStream();
55         InputStream JavaDoc stream = lsin.getByteStream();
56         String JavaDoc content = lsin.getStringData();
57         String JavaDoc systemId = lsin.getSystemId();
58         String JavaDoc publicId = lsin.getPublicId();
59
60         if (content != null) {
61             reader = new StringReader JavaDoc(content);
62         }
63
64         StreamSource JavaDoc source = new StreamSource JavaDoc();
65         source.setSystemId(systemId);
66         source.setPublicId(publicId);
67         source.setReader(reader);
68         source.setInputStream(stream);
69
70         return source;
71
72     }
73 }
74
75 //
76
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
77
// you may not use this file except in compliance with the License. You may obtain a copy of the
78
// License at http://www.mozilla.org/MPL/
79
//
80
// Software distributed under the License is distributed on an "AS IS" basis,
81
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
82
// See the License for the specific language governing rights and limitations under the License.
83
//
84
// The Original Code is: all this file.
85
//
86
// The Initial Developer of the Original Code is Michael H. Kay
87
//
88
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
89
//
90
// Contributor(s): none.
91
//
Popular Tags