KickJava   Java API By Example, From Geeks To Geeks.

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


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.JspWriter JavaDoc;
27 import javax.servlet.jsp.PageContext JavaDoc;
28 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
29
30
31 /**
32  * Include the contents of the specified page at this point in our output.
33  * This tag is similar to <code>&lt;jsp:include&gt;</code>, but does not
34  * cause the output to be sent directly to the servlet response. Therefore,
35  * it can be used to capture the content of the page as body content of
36  * a surrounding tag in which we are nested.
37  *
38  * @author Craig R. McClanahan
39  * @version $Revision: 1.2 $ $Date: 2004/03/08 02:41:31 $
40  */

41
42 public class IncludeTag extends TagSupport JavaDoc {
43
44
45     // ------------------------------------------------------------- Properties
46

47
48     /**
49      * The URI of the page or servlet to be included.
50      */

51     private String JavaDoc page = null;
52
53     public String JavaDoc getPage() {
54     return (this.page);
55     }
56
57     public void setPage(String JavaDoc page) {
58     this.page = page;
59     }
60
61
62     // --------------------------------------------------------- Public Methods
63

64
65     /**
66      * Include the specified page or servlet's output at this point.
67      *
68      * @exception JspException if a JSP error occurs
69      */

70     public int doStartTag() throws JspException JavaDoc {
71
72     // Validate the format of the "page" attribute
73
// FIXME - deal with relative URIs like <jsp:include> does
74
if (!page.startsWith("/"))
75         throw new JspException JavaDoc("Page value must start with '/'");
76
77     // Set up the output stream to which we will write
78
JspWriter JavaDoc out = pageContext.getOut();
79
80     // Set up a URLConnection to read the requested page. We cannot use
81
// PageContext.include() because it writes directly to the output
82
// stream that goes back to the client. :-(
83
HttpServletRequest JavaDoc request =
84         (HttpServletRequest JavaDoc) pageContext.getRequest();
85     StringBuffer JavaDoc url = new StringBuffer JavaDoc();
86     url.append(request.getScheme());
87     url.append("://");
88     url.append(request.getServerName());
89     if (request.getServerPort() != 80) {
90         url.append(':');
91         url.append(request.getServerPort());
92     }
93     if (request.getContextPath() != null)
94         url.append(request.getContextPath());
95     url.append(page); // FIXME - deal with relative page references
96
URLConnection JavaDoc conn = null;
97     try {
98         conn = (new URL JavaDoc(url.toString())).openConnection();
99         conn.setAllowUserInteraction(false);
100         conn.setDoInput(true);
101         conn.setDoOutput(false);
102         conn.connect();
103     } catch (Exception JavaDoc e) {
104         throw new JspException JavaDoc("Error opening connection: " +
105                    e.toString());
106     }
107
108     // Copy the contents of this URL
109
try {
110         BufferedInputStream JavaDoc is =
111         new BufferedInputStream JavaDoc(conn.getInputStream());
112         InputStreamReader JavaDoc in = new InputStreamReader JavaDoc(is);
113         while (true) {
114         int ch = in.read();
115         if (ch < 0)
116             break;
117         out.write(ch);
118         }
119     } catch (Exception JavaDoc e) {
120         throw new JspException JavaDoc("Error reading connection: " +
121                    e.toString());
122     }
123
124     // Skip the body of this tag (which should be empty anyway)
125
return (SKIP_BODY);
126
127     }
128
129
130 }
131
Popular Tags