KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > view > tools > ImportTool


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

16
17 package org.apache.velocity.tools.view.tools;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import javax.servlet.http.HttpSession JavaDoc;
22 import javax.servlet.ServletContext JavaDoc;
23
24 import org.apache.velocity.app.Velocity;
25 import org.apache.velocity.tools.view.ImportSupport;
26 import org.apache.velocity.tools.view.context.ViewContext;
27 import org.apache.velocity.tools.view.tools.ViewTool;
28
29 /**
30  * General-purpose text-importing view tool for templates.
31  * <p>Usage:<br />
32  * Just call $import.read("http://www.foo.com/bleh.jsp?sneh=bar") to insert the contents of the named
33  * resource into the template.
34  * </p>
35  * <p><pre>
36  * Toolbox configuration:
37  * &lt;tool&gt;
38  * &lt;key&gt;import&lt;/key&gt;
39  * &lt;scope&gt;request&lt;/scope&gt;
40  * &lt;class&gt;org.apache.velocity.tools.view.tools.ImportTool&lt;/class&gt;
41  * &lt;/tool&gt;
42  * </pre></p>
43  *
44  * @author <a HREF="mailto:marinoj@centrum.is">Marino A. Jonsson</a>
45  * @since VelocityTools 1.1
46  * @version $Revision: 1.5.2.1 $ $Date: 2004/03/12 20:16:28 $
47  */

48 public class ImportTool extends ImportSupport
49     implements ViewTool {
50
51     /**
52      * Default constructor. Tool must be initialized before use.
53      */

54     public ImportTool() {}
55
56     /**
57      * Initializes this tool.
58      *
59      * @param obj the current ViewContext
60      * @throws IllegalArgumentException if the param is not a ViewContext
61      */

62     public void init(Object JavaDoc obj) {
63         if (! (obj instanceof ViewContext)) {
64             throw new IllegalArgumentException JavaDoc("Tool can only be initialized with a ViewContext");
65         }
66
67         ViewContext context = (ViewContext) obj;
68         this.request = context.getRequest();
69         this.response = context.getResponse();
70         this.application = context.getServletContext();
71     }
72
73     /**
74      * Returns the supplied URL rendered as a String.
75      *
76      * @param url the URL to import
77      * @return the URL as a string
78      */

79     public String JavaDoc read(String JavaDoc url) {
80         try {
81             // check the URL
82
if (url == null || url.equals("")) {
83                 Velocity.warn("ImportTool: import URL is null or empty");
84                 return null;
85             }
86
87             return acquireString(url);
88         }
89         catch (Exception JavaDoc ex) {
90             Velocity.error("Exception while importing URL: " + ex.getMessage());
91             return null;
92         }
93     }
94
95 }
96
Popular Tags