KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import com.methodhead.sitecontext.SiteContext;
31 import org.apache.struts.util.MessageResources;
32 import com.methodhead.tree.Tree;
33 import org.apache.struts.Globals;
34 import org.apache.struts.action.ActionMapping;
35 import org.apache.struts.action.DynaActionForm;
36 import org.apache.struts.util.LabelValueBean;
37 import com.methodhead.tree.FoldingTreeNode;
38 import com.methodhead.auth.AuthUser;
39 import com.methodhead.auth.AuthUtil;
40 import com.methodhead.util.OperationContext;
41 import com.methodhead.util.StrutsUtil;
42 import org.apache.commons.lang.StringUtils;
43 import java.util.regex.Pattern JavaDoc;
44 import java.util.regex.Matcher JavaDoc;
45
46 /**
47  * A collection of utility methods used in the shim package.
48  */

49 public class ShimUtils {
50
51   // constructors /////////////////////////////////////////////////////////////
52

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

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

57   // methods //////////////////////////////////////////////////////////////////
58

59   /**
60    * Sets the <tt>siteMap</tt> in an application attribute. Site maps are
61    * expensive to load, and therefore stored as application attributes. Since
62    * a single application may run several sites, each with their own site map,
63    * a map of site maps is stored, keyed by site context id.
64    */

65   public static void setSiteMap(
66     HttpServletRequest JavaDoc request,
67     SiteMap siteMap ) {
68
69     SiteContext siteContext = siteMap.getSiteContext();
70
71     Map JavaDoc siteMapMap =
72       ( Map JavaDoc )request.getSession().getServletContext().getAttribute(
73         ShimGlobals.SITEMAPMAP_KEY );
74
75     //
76
// set the map if it doesn't already exist
77
//
78
if ( siteMapMap == null ) {
79       siteMapMap = new HashMap JavaDoc();
80       request.getSession().getServletContext().setAttribute(
81         ShimGlobals.SITEMAPMAP_KEY, siteMapMap );
82     }
83
84     siteMapMap.put( siteContext.get( "id" ), siteMap );
85   }
86
87   /**
88    * Gets the site map for the current site context (as returned by
89    * <tt>SiteContext.getContext()</tt>), loading it if necessary.
90    */

91   public static SiteMap getSiteMap(
92     HttpServletRequest JavaDoc request ) {
93
94     SiteContext siteContext = SiteContext.getContext( request );
95
96     if ( siteContext == null )
97       throw new ShimException( "Couldn't get site context." );
98
99     //
100
// set the map if it doesn't already exist
101
//
102
Map JavaDoc siteMapMap =
103       ( Map JavaDoc )request.getSession().getServletContext().getAttribute(
104         ShimGlobals.SITEMAPMAP_KEY );
105
106     if ( siteMapMap == null ) {
107       siteMapMap = new HashMap JavaDoc();
108       request.getSession().getServletContext().setAttribute(
109         ShimGlobals.SITEMAPMAP_KEY, siteMapMap );
110     }
111
112     //
113
// load and set the siteMap if it doesn't already exist
114
//
115
SiteMap siteMap = ( SiteMap )siteMapMap.get( siteContext.get( "id" ) );
116
117     if ( siteMap == null ) {
118       siteMap = new SiteMap();
119       siteMap.setSiteContext( siteContext );
120       siteMap.load();
121       siteMapMap.put( siteContext.get( "id" ), siteMap );
122     }
123
124     return siteMap;
125   }
126
127   /**
128    * Gets the site map tree from the session.
129    */

130   public static SiteMapTree getSiteMapTree(
131     HttpServletRequest JavaDoc request ) {
132
133     return ( SiteMapTree )request.getSession().getAttribute(
134       ShimGlobals.SITEMAPTREE_KEY );
135   }
136
137   /**
138    * Returns a menu customized for admin home.
139    */

140 /*
141   public static FoldingTreeNode getHomeMenu(
142     MessageResources resources ) {
143
144     FoldingTreeNode item = null;
145     FoldingTreeNode subitem = null;
146
147     //
148     // get the base menu
149     //
150     FoldingTreeNode menu = buildBaseMenu( resources );
151     FoldingTreeNode pageMenu = ( FoldingTreeNode )menu.getChildAt( 2 );
152
153     //
154     // new
155     //
156     item = new FoldingTreeNode();
157     item.setLabel( resources.getMessage( "shim.menu.fragments" ) );
158     item.setUrl( "htmlFragment.do?action=list" );
159     pageMenu.add( item );
160
161     item = new FoldingTreeNode();
162     item.setLabel( resources.getMessage( "shim.menu.new" ) );
163     item.setUrl( "newPageForm.do" );
164     pageMenu.add( item );
165
166     return menu;
167   }
168 */

169
170   /**
171    * Returns a menu customized for the page being edited in {@link
172    * PageAction#doEditPage doEditPage()}.
173    */

174 /*
175   public static FoldingTreeNode getEditPageMenu(
176     SiteMap siteMap,
177     Page page,
178     MessageResources resources ) {
179
180     Link link = siteMap.find( page.getInt( "id" ) );
181
182     FoldingTreeNode item = null;
183     FoldingTreeNode subitem = null;
184
185     //
186     // get the base menu
187     //
188     FoldingTreeNode menu = buildBaseMenu( resources );
189     FoldingTreeNode pageMenu = ( FoldingTreeNode )menu.getChildAt( 2 );
190
191     //
192     // new
193     //
194     item = new FoldingTreeNode();
195     item.setLabel( resources.getMessage( "shim.menu.fragments" ) );
196     item.setUrl( "htmlFragment.do?action=list" );
197     pageMenu.add( item );
198
199     item = new FoldingTreeNode();
200     item.setLabel( resources.getMessage( "shim.menu.new" ) );
201     item.setUrl( "" );
202     pageMenu.add( item );
203
204     if ( link != siteMap.getRoot() ) {
205       subitem = new FoldingTreeNode();
206       subitem.setLabel( resources.getMessage( "shim.menu.before" ) );
207       subitem.setUrl(
208         "newPageForm.do?destid=" + page.getInt( "id" ) + "&position=before" );
209       item.add( subitem );
210
211       subitem = new FoldingTreeNode();
212       subitem.setLabel( resources.getMessage( "shim.menu.after" ) );
213       subitem.setUrl(
214         "newPageForm.do?destid=" + page.getInt( "id" ) + "&position=after" );
215       item.add( subitem );
216     }
217
218     subitem = new FoldingTreeNode();
219     subitem.setLabel( resources.getMessage( "shim.menu.under" ) );
220     subitem.setUrl(
221       "newPageForm.do?destid=" + page.getInt( "id" ) + "&position=under" );
222     item.add( subitem );
223
224     item = new FoldingTreeNode();
225     item.setLabel( resources.getMessage( "shim.menu.properties" ) );
226     item.setUrl( "configurePageForm.do?id=" + page.get( "id" ) );
227     pageMenu.add( item );
228
229     if ( link != siteMap.getRoot() ) {
230       item = new FoldingTreeNode();
231       item.setLabel( resources.getMessage( "shim.menu.move" ) );
232       item.setUrl( "movePageForm.do?id=" + page.get( "id" ) );
233       pageMenu.add( item );
234     }
235
236     item = new FoldingTreeNode();
237     item.setLabel( resources.getMessage( "shim.menu.delete" ) );
238     item.setUrl( "deletePage.do?id=" + page.get( "id" ) );
239     pageMenu.add( item );
240
241     return menu;
242   }
243 */

244
245   /**
246    * Returns a list of {@link Module Module}s available to
247    * <tt>siteContext</tt>.
248    */

249   public static List JavaDoc getModules(
250     SiteContext siteContext ) {
251
252     List JavaDoc modules = new ArrayList JavaDoc();
253     modules.add( new TextModule() );
254     modules.add( new NavModule() );
255     modules.add( new IncludeModule() );
256
257     return modules;
258   }
259
260   /**
261    * Returns a relative URL for the page associated with
262    * <tt>link</tt>.
263    */

264   public static String JavaDoc getLinkUrl(
265     Link link ) {
266
267     return link.getAlias() + ".shtml";
268   }
269
270   /**
271    * Returns an relative URL for the page associated with <tt>link</tt>,
272    * specifying a parent directory to get rid of the site context id in page
273    * urls.
274    *
275    * @deprecated Path is no longer necessary with new ShimFilter.
276    */

277   public static String JavaDoc getLinkUrl(
278     Link link,
279     String JavaDoc path ) {
280
281 /*
282     if ( ( path == null ) || "".equals( path ) )
283       return link.getAlias() + ".shtml";
284     else
285       return path + "/" + link.getAlias() + ".shtml";
286 */

287     return link.getAlias() + ".shtml";
288   }
289
290   /**
291    * NOT UNIT TESTED Instantiates a <tt>ShimPolicy</tt> using <tt>mapping</tt>'s parameter.
292    */

293   public static ShimPolicy getPolicy(
294     ActionMapping mapping ) {
295
296     try {
297       return
298         ( ShimPolicy )Class.forName( mapping.getParameter() ).newInstance();
299     }
300     catch ( Exception JavaDoc e ) {
301       throw new ShimException(
302         "Unexpected exception while instantiating \"" +
303         mapping.getParameter() +"\":" + e.toString() );
304     }
305   }
306
307   /**
308    * NOT UNIT TESTED Returns a list of <tt>LabelValueBeans</tt> for the existing fragments in
309    * the current site context.
310    */

311   public static List JavaDoc getHtmlFragmentOptions(
312     HttpServletRequest JavaDoc request ) {
313
314     HtmlFragment fragment = new HtmlFragment();
315     fragment.setSiteContext( SiteContext.getContext( request ) );
316     List JavaDoc list = fragment.loadAll();
317
318     //
319
// create a list of options
320
//
321
List JavaDoc options = new ArrayList JavaDoc();
322     for ( Iterator JavaDoc iter = list.iterator(); iter.hasNext(); ) {
323       HtmlFragment f = ( HtmlFragment )iter.next();
324       options.add(
325         new LabelValueBean( f.getString( "name" ), "" + f.getInt( "id" ) ) );
326     }
327
328     return options;
329   }
330
331   /**
332    * NOT UNIT TESTED Sets up a <tt>SiteContext</tt> and <tt>SiteMapTree</tt>
333    * appropriately for shim session.
334    */

335   public static void setUpShimSession(
336     HttpServletRequest JavaDoc request,
337     SiteContext context ) {
338
339     SiteContext.setContext( request, context );
340
341     //
342
// override any site context that ShimFilter might have set in the request
343
//
344
request.setAttribute( SiteContext.SITECONTEXT_KEY, context );
345
346     SiteMapTree tree = new SiteMapTree();
347     tree.build( ShimUtils.getSiteMap( request ) );
348     request.getSession().setAttribute(
349       ShimGlobals.SITEMAPTREE_KEY, tree );
350   }
351
352   /**
353    * NOT UNIT TESTED Cleans up all shim-related session attributes
354    */

355   public static void tearDownShimSession(
356     HttpServletRequest JavaDoc request ) {
357
358     request.getSession().removeAttribute( SiteContext.SITECONTEXT_KEY );
359     request.getSession().removeAttribute( ShimGlobals.MODE_KEY );
360     request.getSession().removeAttribute( ShimGlobals.SITEMAPTREE_KEY );
361   }
362
363   /**
364    * NOT UNIT TESTED Sets up the menu for the current request by assembling an
365    * <tt>OperationContext</tt> and <tt>ShimPolicy</tt> and invoking the
366    * policy's <tt>setUpMenu()</tt> method.
367    */

368   public static void setUpMenu(
369     HttpServletRequest JavaDoc request,
370     HttpServletResponse JavaDoc response ) {
371
372     ActionMapping mapping =
373       ( ActionMapping )request.getAttribute( Globals.MAPPING_KEY );
374
375     DynaActionForm form =
376       ( DynaActionForm )request.getAttribute( mapping.getName() );
377
378     AuthUser user = AuthUtil.getUser( request );
379
380     OperationContext op =
381       new OperationContext( mapping, form, request, response, user );
382     
383     ShimPolicy policy = ( ShimPolicy )StrutsUtil.getPolicy( mapping );
384
385     policy.setUpMenu( op );
386   }
387
388   /**
389    * Returns <code>true</code> if there is a SiteContext in the session.
390    */

391   public static boolean displayEditElements(
392     HttpServletRequest JavaDoc request ) {
393     return request.getSession().getAttribute( SiteContext.SITECONTEXT_KEY ) != null;
394   }
395
396   /**
397    * Converts a file name to a label by splitting words in the file name on
398    * capital letters and removing any file extension. The first letter of the
399    * label is always capitalized. The extension is assumed to be anything
400    * after the first period in the name. Returns "&lt;null&gt;" if
401    * <code>fileName</code> is null.
402    */

403   public static String JavaDoc fileNameToLabel(
404     String JavaDoc fileName ) {
405
406     if ( null == fileName ) {
407       return "<null>";
408     }
409
410     if ( StringUtils.isBlank( fileName ) ) {
411       return "";
412     }
413
414     //
415
// remove extension
416
//
417
fileName = StringUtils.substringBefore( fileName, "." );
418
419     //
420
// capitalize first letter
421
//
422
fileName = StringUtils.capitalize( fileName );
423
424     //
425
// split on caps
426
//
427
Pattern JavaDoc pattern = Pattern.compile( "([A-Z][^A-Z]*)" );
428     Matcher JavaDoc matcher = pattern.matcher( fileName );
429
430     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
431
432     while ( matcher.find() ) {
433       buf.append( StringUtils.strip( matcher.group( 1 ) ) );
434       buf.append( " " );
435     }
436
437     return StringUtils.strip( buf.toString() );
438   }
439
440   // properties ///////////////////////////////////////////////////////////////
441

442   // attributes ///////////////////////////////////////////////////////////////
443
}
444
Popular Tags