1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.views.xslt;
23
24 import java.io.InputStream;
25
26 import javax.servlet.ServletContext;
27 import javax.xml.transform.Source;
28 import javax.xml.transform.TransformerException;
29 import javax.xml.transform.URIResolver;
30 import javax.xml.transform.stream.StreamSource;
31
32 import com.opensymphony.xwork2.util.logging.Logger;
33 import com.opensymphony.xwork2.util.logging.LoggerFactory;
34
35
36 /***
37 * ServletURIResolver is a URIResolver that can retrieve resources from the servlet context using the scheme "response".
38 * e.g.
39 *
40 * A URI resolver is called when a stylesheet uses an xsl:include, xsl:import, or document() function to find the
41 * resource (file).
42 */
43 public class ServletURIResolver implements URIResolver {
44
45 private Logger log = LoggerFactory.getLogger(getClass());
46 static final String PROTOCOL = "response:";
47
48 private ServletContext sc;
49
50 public ServletURIResolver(ServletContext sc) {
51 log.trace("ServletURIResolver: " + sc);
52 this.sc = sc;
53 }
54
55 public Source resolve(String href, String base) throws TransformerException {
56 log.debug("ServletURIResolver resolve(): href=" + href + ", base=" + base);
57 if (href.startsWith(PROTOCOL)) {
58 String res = href.substring(PROTOCOL.length());
59 log.debug("Resolving resource <" + res + ">");
60
61 InputStream is = sc.getResourceAsStream(res);
62
63 if (is == null) {
64 throw new TransformerException(
65 "Resource " + res + " not found in resources.");
66 }
67
68 return new StreamSource(is);
69 }
70
71 throw new TransformerException(
72 "Cannot handle procotol of resource " + href);
73 }
74 }