KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > xslt > ServletURIResolver


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.xslt;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import javax.servlet.ServletContext JavaDoc;
11 import javax.xml.transform.Source JavaDoc;
12 import javax.xml.transform.TransformerException JavaDoc;
13 import javax.xml.transform.URIResolver JavaDoc;
14 import javax.xml.transform.stream.StreamSource JavaDoc;
15 import java.io.InputStream JavaDoc;
16
17
18 /**
19  * @author <a HREF="mailto:meier@meisterbohne.de">Philipp Meier</a>
20  * Date: 14.10.2003
21  * Time: 16:50:06
22  */

23 public class ServletURIResolver implements URIResolver JavaDoc {
24     //~ Static fields/initializers /////////////////////////////////////////////
25

26     protected static Log log = LogFactory.getLog(URIResolver JavaDoc.class);
27     static final String JavaDoc protocol = "res:";
28
29     //~ Instance fields ////////////////////////////////////////////////////////
30

31     private ServletContext JavaDoc sc;
32
33     //~ Constructors ///////////////////////////////////////////////////////////
34

35     public ServletURIResolver(ServletContext JavaDoc sc) {
36         this.sc = sc;
37     }
38
39     //~ Methods ////////////////////////////////////////////////////////////////
40

41     public Source JavaDoc resolve(String JavaDoc href, String JavaDoc base) throws TransformerException JavaDoc {
42         if (href.startsWith(protocol)) {
43             String JavaDoc res = href.substring(protocol.length());
44             log.debug("Resolving resource <" + res + ">");
45
46             InputStream JavaDoc is = sc.getResourceAsStream(res);
47
48             if (is == null) {
49                 throw new TransformerException JavaDoc("Resource " + res + " not found in resources.");
50             }
51
52             return new StreamSource JavaDoc(is);
53         }
54
55         throw new TransformerException JavaDoc("Cannot handle procotol of resource " + href);
56     }
57 }
58
Popular Tags