KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > servlet > ServletContextURIResolver


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id$ */
19
20 package org.apache.fop.servlet;
21
22 import java.io.InputStream JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import javax.servlet.ServletContext JavaDoc;
27 import javax.xml.transform.Source JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import javax.xml.transform.URIResolver JavaDoc;
30 import javax.xml.transform.stream.StreamSource JavaDoc;
31
32 /**
33  * This class is a URIResolver implementation that provides access to resources in the WEB-INF
34  * directory of a web application using "servlet-content:" URIs.
35  */

36 public class ServletContextURIResolver implements URIResolver JavaDoc {
37
38     /** The protocol name for the servlet context URIs. */
39     public static final String JavaDoc SERVLET_CONTEXT_PROTOCOL = "servlet-context:";
40     
41     private ServletContext JavaDoc servletContext;
42     
43     /**
44      * Main constructor
45      * @param servletContext the servlet context to access the resources through
46      */

47     public ServletContextURIResolver(ServletContext JavaDoc servletContext) {
48         this.servletContext = servletContext;
49     }
50     
51     /** @see javax.xml.transform.URIResolver#resolve(java.lang.String, java.lang.String) */
52     public Source JavaDoc resolve(String JavaDoc href, String JavaDoc base) throws TransformerException JavaDoc {
53         if (href.startsWith(SERVLET_CONTEXT_PROTOCOL)) {
54             return resolveServletContextURI(href.substring(SERVLET_CONTEXT_PROTOCOL.length()));
55         } else {
56             if (base != null
57                     && base.startsWith(SERVLET_CONTEXT_PROTOCOL)
58                     && (href.indexOf(':') < 0)) {
59                 String JavaDoc abs = base + href;
60                 return resolveServletContextURI(
61                         abs.substring(SERVLET_CONTEXT_PROTOCOL.length()));
62             } else {
63                 return null;
64             }
65         }
66     }
67
68     /**
69      * Resolves the "servlet-context:" URI.
70      * @param path the path part after the protocol (should start with a "/")
71      * @return the resolved Source or null if the resource was not found
72      * @throws TransformerException if no URL can be constructed from the path
73      */

74     protected Source JavaDoc resolveServletContextURI(String JavaDoc path) throws TransformerException JavaDoc {
75         while (path.startsWith("//")) {
76             path = path.substring(1);
77         }
78         try {
79             URL JavaDoc url = this.servletContext.getResource(path);
80             InputStream JavaDoc in = this.servletContext.getResourceAsStream(path);
81             if (in != null) {
82                 if (url != null) {
83                     return new StreamSource JavaDoc(in, url.toExternalForm());
84                 } else {
85                     return new StreamSource JavaDoc(in);
86                 }
87             } else {
88                 throw new TransformerException JavaDoc("Resource does not exist. \"" + path
89                         + "\" is not accessible through the servlet context.");
90             }
91         } catch (MalformedURLException JavaDoc mfue) {
92             throw new TransformerException JavaDoc(
93                     "Error accessing resource using servlet context: " + path, mfue);
94         }
95     }
96 }
97
Popular Tags