KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > xsl > ImportTag


1 /*
2  * Copyright 1999,2004 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.taglibs.xsl;
18
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLConnection JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
28
29
30 /**
31  * Import the content of the specified page, and assign it (as a String)
32  * to the specified scripting variable in the specified scope.
33  *
34  * @author Craig R. McClanahan
35  * @version $Revision: 1.2 $ $Date: 2004/03/08 02:41:31 $
36  */

37
38 public class ImportTag extends TagSupport JavaDoc {
39
40
41     // ------------------------------------------------------------- Properties
42

43
44     /**
45      * The name of the scripting variable to be created.
46      */

47     private String JavaDoc id = null;
48
49     public String JavaDoc getId() {
50     return (this.id);
51     }
52
53     public void setId(String JavaDoc id) {
54     this.id = id;
55     }
56
57
58     /**
59      * The URI of the page or servlet to be included.
60      */

61     private String JavaDoc page = null;
62
63     public String JavaDoc getPage() {
64     return (this.page);
65     }
66
67     public void setPage(String JavaDoc page) {
68     this.page = page;
69     }
70
71
72     /**
73      * The scope in which the scripting variable should be created.
74      */

75     private String JavaDoc scope = "page";
76
77     public String JavaDoc getScope() {
78     return (this.scope);
79     }
80
81     public void setScope(String JavaDoc scope) {
82     this.scope = scope;
83     }
84
85
86     // --------------------------------------------------------- Public Methods
87

88
89     /**
90      * Import the specified page and assign it to the specified bean
91      * in the specified scope.
92      *
93      * @exception JspException if a JSP error occurs
94      */

95     public int doStartTag() throws JspException JavaDoc {
96
97     // Validate the format of the "page" attribute
98
// FIXME - deal with relative URIs like <jsp:include> does
99
if (!page.startsWith("/"))
100         throw new JspException JavaDoc("Page value must start with '/'");
101
102     // Validate the value of the "scope" attribute
103
int scopeId = 0;
104     if ("page".equalsIgnoreCase(scope))
105         scopeId = PageContext.PAGE_SCOPE;
106     else if ("request".equalsIgnoreCase(scope))
107         scopeId = PageContext.REQUEST_SCOPE;
108     else if ("session".equalsIgnoreCase(scope))
109         scopeId = PageContext.SESSION_SCOPE;
110     else if ("application".equalsIgnoreCase(scope))
111         scopeId = PageContext.APPLICATION_SCOPE;
112     else
113         throw new JspException JavaDoc("Invalid scope value '" + scope + "'");
114
115     // Set up the buffer to which we will write (if id is set)
116
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
117
118     // Set up a URLConnection to read the requested page. We cannot use
119
// PageContext.include() because it writes directly to the output
120
// stream that goes back to the client. :-(
121
HttpServletRequest JavaDoc request =
122         (HttpServletRequest JavaDoc) pageContext.getRequest();
123     StringBuffer JavaDoc url = new StringBuffer JavaDoc();
124     url.append(request.getScheme());
125     url.append("://");
126     url.append(request.getServerName());
127     if (request.getServerPort() != 80) {
128         url.append(':');
129         url.append(request.getServerPort());
130     }
131     if (request.getContextPath() != null)
132         url.append(request.getContextPath());
133     url.append(page); // FIXME - deal with relative page references
134
URLConnection JavaDoc conn = null;
135     try {
136         conn = (new URL JavaDoc(url.toString())).openConnection();
137         conn.setAllowUserInteraction(false);
138         conn.setDoInput(true);
139         conn.setDoOutput(false);
140         conn.connect();
141     } catch (Exception JavaDoc e) {
142         throw new JspException JavaDoc("Error opening connection: " +
143                    e.toString());
144     }
145
146     // Copy the contents of this URL
147
try {
148         BufferedInputStream JavaDoc is =
149         new BufferedInputStream JavaDoc(conn.getInputStream());
150         InputStreamReader JavaDoc in = new InputStreamReader JavaDoc(is);
151         while (true) {
152         int ch = in.read();
153         if (ch < 0)
154             break;
155         sb.append((char) ch);
156         }
157     } catch (Exception JavaDoc e) {
158         throw new JspException JavaDoc("Error reading connection: " +
159                    e.toString());
160     }
161
162     // Store the accumulated buffer as a bean
163
pageContext.setAttribute(id, sb.toString(), scopeId);
164
165     // Skip the body of this tag (which should be empty anyway)
166
return (SKIP_BODY);
167
168     }
169
170
171 }
172
Popular Tags