01: /*
02: * $Id: ServletURIResolver.java 471756 2006-11-06 15:01:43Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21: package org.apache.struts2.views.xslt;
22:
23: import java.io.InputStream;
24:
25: import javax.servlet.ServletContext;
26: import javax.xml.transform.Source;
27: import javax.xml.transform.TransformerException;
28: import javax.xml.transform.URIResolver;
29: import javax.xml.transform.stream.StreamSource;
30:
31: import org.apache.commons.logging.Log;
32: import org.apache.commons.logging.LogFactory;
33:
34: /**
35: * ServletURIResolver is a URIResolver that can retrieve resources from the servlet context using the scheme "response".
36: * e.g.
37: *
38: * A URI resolver is called when a stylesheet uses an xsl:include, xsl:import, or document() function to find the
39: * resource (file).
40: */
41: public class ServletURIResolver implements URIResolver {
42:
43: private Log log = LogFactory.getLog(getClass());
44: static final String PROTOCOL = "response:";
45:
46: private ServletContext sc;
47:
48: public ServletURIResolver(ServletContext sc) {
49: log.trace("ServletURIResolver: " + sc);
50: this .sc = sc;
51: }
52:
53: public Source resolve(String href, String base)
54: throws TransformerException {
55: log.debug("ServletURIResolver resolve(): href=" + href
56: + ", 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("Resource " + res
65: + " 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: }
|