01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.views.xslt;
06:
07: import org.apache.commons.logging.Log;
08: import org.apache.commons.logging.LogFactory;
09:
10: import javax.servlet.ServletContext;
11: import javax.xml.transform.Source;
12: import javax.xml.transform.TransformerException;
13: import javax.xml.transform.URIResolver;
14: import javax.xml.transform.stream.StreamSource;
15: import java.io.InputStream;
16:
17: /**
18: * ServletURIResolver is a URIResolver that can retrieve resources from the servlet context using the scheme "res".
19: * e.g.
20: *
21: * A URI resolver is called when a stylesheet uses an xsl:include, xsl:import, or document() function to find the
22: * resource (file).
23: *
24: * @author <a href="mailto:meier@meisterbohne.de">Philipp Meier</a>
25: */
26: public class ServletURIResolver implements URIResolver {
27: //~ Static fields/initializers /////////////////////////////////////////////
28:
29: private Log log = LogFactory.getLog(getClass());
30: static final String protocol = "res:";
31:
32: //~ Instance fields ////////////////////////////////////////////////////////
33:
34: private ServletContext sc;
35:
36: //~ Constructors ///////////////////////////////////////////////////////////
37:
38: public ServletURIResolver(ServletContext sc) {
39: log.trace("ServletURIResolver: " + sc);
40: this .sc = sc;
41: }
42:
43: //~ Methods ////////////////////////////////////////////////////////////////
44:
45: public Source resolve(String href, String base)
46: throws TransformerException {
47: log.debug("ServletURIResolver resolve(): href=" + href
48: + ", base=" + base);
49: if (href.startsWith(protocol)) {
50: String res = href.substring(protocol.length());
51: log.debug("Resolving resource <" + res + ">");
52:
53: InputStream is = sc.getResourceAsStream(res);
54:
55: if (is == null) {
56: throw new TransformerException("Resource " + res
57: + " not found in resources.");
58: }
59:
60: return new StreamSource(is);
61: }
62:
63: throw new TransformerException(
64: "Cannot handle procotol of resource " + href);
65: }
66: }
|