01: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.car;
07:
08: import java.io.IOException;
09: import java.io.InputStream;
10: import java.net.MalformedURLException;
11: import java.net.URL;
12:
13: import javax.xml.transform.Source;
14: import javax.xml.transform.TransformerException;
15: import javax.xml.transform.URIResolver;
16: import javax.xml.transform.stream.StreamSource;
17:
18: /**
19: * Enables resolving of external resources specified in xsl:import or xsl:include
20: * elements or the document() allowing such resources to also be found
21: * within CARs.
22: *
23: * @author Mark Boyd {@link <a href="mailto:mark.boyd@engineer.com">mark.boyd@engineer.com</a>}
24: * @version $Revision: 36690 $
25: */
26: public class ResourceResolver implements URIResolver {
27: /**
28: * Allows external resources specified in xsl:import or xsl:include
29: * elements or the document() method to be resolved in custom manner.
30: * This allows resources in CARs to be accessed readily via these xsl
31: * constructs. If the specified resource is not found within a CAR then
32: * a null value is returned allowing the processor to try and resolve it
33: * in some other way.
34: *
35: * @see javax.xml.transform.URIResolver#resolve(java.lang.String, java.lang.String)
36: */
37: public Source resolve(String href, String base)
38: throws TransformerException {
39: // first try loading a resource relative to the base URI
40: if (base != null) {
41: try {
42: URL ctx = new URL(base);
43: URL res = new URL(ctx, href);
44: InputStream is = res.openStream();
45: return new StreamSource(is, res.toExternalForm());
46: } catch (MalformedURLException e) {
47: } catch (IOException e1) {
48: }
49: }
50: // if can't load relative to base see if this is a resource sitting in
51: // a channel archive.
52: URL res = CarResources.getInstance().getClassLoader()
53: .getResource(href);
54:
55: if (res != null) {
56: InputStream is;
57: try {
58: is = res.openStream();
59: return new StreamSource(is, res.toExternalForm());
60: } catch (IOException e) {
61: }
62: }
63: // oh well. Did the best we could. Let the default handling try.
64: return null;
65: }
66: }
|