KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > taglibs > IncludeTag


1 /*
2  * IncludeTag is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/taglibs/IncludeTag.java,v 1.6.2.1 2006/12/11 16:27:38 fxrobin Exp $
21  */

22
23 package org.cofax.taglibs;
24
25 import javax.servlet.*;
26 import javax.servlet.jsp.*;
27 import javax.servlet.jsp.tagext.*;
28 import java.io.*;
29 import org.cofax.*;
30
31 /**
32  * Includes a resource via relative URL (most likely a JSP page)s
33  *
34  * @param file
35  * file to be include
36  */

37 public class IncludeTag extends TagSupport {
38
39     private String JavaDoc includeFile;
40
41     private boolean hasInclude = false;
42
43     /**
44      * Set's includeFile name
45      */

46     public void setFile(String JavaDoc include) {
47         this.includeFile = include;
48         hasInclude = true;
49     }
50
51     /**
52      * Includes a resource base on real path or recursive method to find include
53      * url
54      */

55     public int doStartTag() throws JspException {
56         ServletRequest req = pageContext.getRequest();
57         ServletResponse res = pageContext.getResponse();
58
59         CofaxPage page = (CofaxPage) req.getAttribute("cofaxPage");
60
61         try {
62             String JavaDoc includeId = null;
63
64             // real path or recursive method to find include url
65
if (hasInclude) {
66                 if (includeFile.startsWith("/")) {
67                     includeId = page.getGlossaryValue("request:templatePathInit") + includeFile;
68                 } else {
69                     TemplateLoader tmpLoader = page.getTemplateLoader();
70                     tmpLoader.setTemplateSearch(tmpLoader.getTemplateRoot() + "/" + page.getGlossaryValue("request:templateSearch"));
71                     includeId = tmpLoader.choose(page.getGlossaryValue("request:fileNameWithExtension"), page.getGlossaryValue("request:template"), page
72                             .getGlossaryValue("request:fileNameExtension"));
73                     includeId = tmpLoader.getResource(includeId, (String JavaDoc) req.getAttribute("applicationPath"));
74                 }
75                 includeResource(includeId, req, res);
76             }
77         } catch (IOException ioe) {
78             System.out.println(" Error in set Include Tag ioe : " + ioe);
79         } catch (ServletException se) {
80             System.out.println(" Error in set Include Tag se : " + se);
81         }
82
83         return (SKIP_BODY);
84     }
85
86     /**
87      * Includes a resource via relative URL (most likely a JSP page)s
88      */

89     private void includeResource(String JavaDoc url, ServletRequest req, ServletResponse res) throws ServletException, IOException {
90
91         if ((url != null) && (!url.equals(""))) {
92             RequestDispatcher dispatcher = req.getRequestDispatcher(url);
93             dispatcher.include(req, res);
94         }
95
96         return;
97
98     }
99
100 } // end IncludeTag
101

102
Popular Tags