KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > Template


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.FilenameFilter JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.text.Collator JavaDoc;
32
33 import com.methodhead.sitecontext.SiteContextCapable;
34 import com.methodhead.sitecontext.SiteContext;
35 import javax.servlet.ServletContext JavaDoc;
36 import javax.servlet.RequestDispatcher JavaDoc;
37 import javax.servlet.ServletException JavaDoc;
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import org.apache.commons.lang.exception.ExceptionUtils;
41
42 /**
43  * A class to access and profile Shim templates. Template configuration
44  * information is embedded in templates themselves, and this class helps access
45  * that information.
46  */

47 public class Template
48 implements
49   SiteContextCapable {
50
51   // constructors /////////////////////////////////////////////////////////////
52

53   // constants ////////////////////////////////////////////////////////////////
54

55   // classes //////////////////////////////////////////////////////////////////
56

57   /**
58    * Returns <tt>true</tt> if file name ends in ".jsp".
59    */

60   private static class TemplateFilenameFilter
61   implements
62     FilenameFilter JavaDoc {
63
64     public boolean accept(
65       File JavaDoc dir,
66       String JavaDoc name ) {
67
68       return name.toLowerCase().endsWith( ".jsp" );
69     }
70   }
71
72   // methods //////////////////////////////////////////////////////////////////
73

74   /**
75    * Returns the current site context for this object, getting the default
76    * context if it has not been set.
77    */

78   public SiteContext getSiteContext() {
79     if ( siteContext_ == null )
80       throw new ShimException( "Site context has not been set." );
81
82     return siteContext_;
83   }
84
85   /**
86    * Loads template information from <tt>template</tt>. <tt>template</tt>
87    * should be a simple file name. The template's actual location is
88    * calculated using the resource root and site context. An exception is
89    * thrown if the resource root hasn't been set.
90    */

91   public void load(
92     HttpServletRequest JavaDoc request,
93     String JavaDoc template ) {
94
95     //
96
// make sure we got a valid template name.
97
//
98
if ( ( template == null ) || ( template.equals( "" ) ) )
99       throw new ShimException( "Invalid template \"" + template + "\"." );
100
101     //
102
// get a dispatcher for the template
103
//
104
fileName_ = template;
105
106     String JavaDoc resource =
107       "/WEB-INF/resources/" + getSiteContext().getInt( "id" ) + "/templates/" +
108       fileName_;
109
110     RequestDispatcher JavaDoc dispatcher =
111       request.getSession().getServletContext().getRequestDispatcher( resource );
112
113     if ( dispatcher == null )
114       throw new ShimException(
115         "Couldn't get request dispatcher for \"" + resource + "\"." );
116
117     //
118
// put an empty panel map in the request
119
//
120
request.setAttribute( ShimGlobals.PANELMAP_KEY, new HashMap JavaDoc() );
121
122     //
123
// include the template
124
//
125
try {
126       dispatcher.include( request, new NullHttpServletResponse() );
127     }
128     catch ( ServletException JavaDoc e ) {
129       throw new ShimException(
130         "Unexpected ServletException: " + e.getMessage() );
131     }
132     catch ( IOException JavaDoc e ) {
133       throw new ShimException(
134         "Unexpected IOException: " + e.getMessage() );
135     }
136
137     panelConfigs_ = ( Map JavaDoc )request.getAttribute( ShimGlobals.PANELMAP_KEY );
138
139     //
140
// take the panel map out of the request (this affects PanelTag)
141
//
142
request.removeAttribute( ShimGlobals.PANELMAP_KEY );
143   }
144
145   /**
146    * Returns a list of available templates sorted in alphabetic,
147    * case-insensitive order. Templates are JSP files located in
148    * <tt>WEB-INF/resources/<i>sitecontext_id</i>/templates</tt>.
149    */

150   public List JavaDoc getTemplateList(
151     HttpServletRequest JavaDoc request ) {
152
153     //
154
// verify the resources directory
155
//
156
File JavaDoc templatesDir =
157       new File JavaDoc(
158         request.getSession().getServletContext().getRealPath(
159           "/WEB-INF/resources/" + getSiteContext().getInt( "id" ) +
160           "/templates" ) );
161
162     if ( !templatesDir.exists() || !templatesDir.isDirectory() )
163       throw new ShimException(
164         "Invalid templates directory \"" + templatesDir + "\"" );
165
166     //
167
// get file list
168
//
169
String JavaDoc[] fileNames = templatesDir.list( new TemplateFilenameFilter() );
170
171     //
172
// sort the file list
173
//
174
Arrays.sort( fileNames, Collator.getInstance() );
175
176     //
177
// return the list
178
//
179
return Arrays.asList( fileNames );
180   }
181
182   /**
183    * Includes <tt>template</tt> in <tt>response</tt>.
184    */

185   public void include(
186     HttpServletRequest JavaDoc request,
187     HttpServletResponse JavaDoc response,
188     String JavaDoc template ) {
189
190     try {
191       RequestDispatcher JavaDoc dispatcher =
192         request.getSession().getServletContext().getRequestDispatcher(
193           "/WEB-INF/resources/" + getSiteContext().getInt( "id" ) +
194           "/templates/" + template );
195
196       dispatcher.include( request, response );
197     }
198     catch ( Exception JavaDoc e ) {
199       throw new ShimException(
200         "Unexpected exception trying to include \"" +
201         "/WEB-INF/resources/" + getSiteContext().getInt( "id" ) +
202         "/templates/" + template + "\": " +
203         ExceptionUtils.getStackTrace( e ) );
204     }
205   }
206
207   // properties ///////////////////////////////////////////////////////////////
208

209   /**
210    * Implements <tt>SiteContextCapable.setSiteContext()</tt>.
211    */

212   public void setSiteContext(
213     SiteContext siteContext ) {
214     siteContext_ = siteContext;
215   }
216
217   /**
218    * Returns the name of the template. This is only set when {#load} is called.
219    */

220   public String JavaDoc getFileName() {
221     return fileName_;
222   }
223
224   /**
225    * Returns the panel configs of the template. This is only set when {#load}
226    * is called.
227    */

228   public Map JavaDoc getPanelConfigs() {
229     return panelConfigs_;
230   }
231
232   // attributes ///////////////////////////////////////////////////////////////
233

234   protected SiteContext siteContext_ = null;
235
236   protected String JavaDoc fileName_ = null;
237
238   protected Map JavaDoc panelConfigs_ = null;
239 }
240
Popular Tags