KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > jsptag > LayoutTag


1 /*
2  * LayoutTag.java
3  *
4  * Version: $Revision: 1.19 $
5  *
6  * Date: $Date: 2006/09/25 19:11:32 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.webui.jsptag;
41
42 import java.io.IOException JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.List JavaDoc;
45
46 import javax.servlet.RequestDispatcher JavaDoc;
47 import javax.servlet.ServletConfig JavaDoc;
48 import javax.servlet.ServletException JavaDoc;
49 import javax.servlet.ServletRequest JavaDoc;
50 import javax.servlet.ServletResponse JavaDoc;
51 import javax.servlet.http.HttpServletResponse JavaDoc;
52 import javax.servlet.jsp.JspException JavaDoc;
53 import javax.servlet.jsp.jstl.fmt.LocaleSupport;
54 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
55
56 import org.apache.log4j.Logger;
57 import org.dspace.content.Collection;
58 import org.dspace.content.Community;
59 import org.dspace.core.ConfigurationManager;
60 import org.dspace.app.webui.servlet.FeedServlet;
61
62 /**
63  * Tag for HTML page layout ("skin").
64  * <P>
65  * This tag <em>sets</em> request attributes that should be used by the header
66  * and footer to render the page appropriately:
67  * <P>
68  * <ul>
69  * <li><code>dspace.layout.title</code> - title of page</li>
70  * <li><code>dspace.layout.locbar</code> - value will Boolean true or false
71  * </li>
72  * <li><code>dspace.layout.parenttitles</code> - a <code>List</code> of
73  * <code>String</code>s corresponding with titles to put in the location bar.
74  * Only set if <code>dspace.layout.locbar</code> is true</li>
75  * <li><code>dspace.layout.parentlinks</code> - a <code>List</code> of
76  * <code>String</code>s corresponding with links to put in the location bar.
77  * Empty strings mean no link. Will only be set if
78  * <code>dspace.layout.locbar</code> is true.</li>
79  * <li><code>dspace.layout.navbar</code> - value will be "off", or the
80  * navigation bar to include, e.g. "/layout/navbar_default.jsp"</li>
81  * <li><code>dspace.layout.sidebar</code> - contents of the sidebar</li>
82  * <li><code>dspace.current.user</code> - the EPerson currently logged in, or
83  * <code>null</code> if anonymous access</li>
84  * <li><code>dspace.layout.feeddata</code> - <code>String</code>. "NONE"
85  * means no feed from this page; otherwise the Handle of object (community or
86  * collection) the feed is from</li>
87  * <li><code>dspace.layout.linkparts</code> - <code>String[]</code>. A
88  * cycling sequence of: 2nd part of MIME type (e.g. <code>rdf+rss</code>);
89  * title of feed; path component to go after /feed/ for the actual feed URL
90  * (e.g. <code>rss_1.0</code>). Hence, this array will have 3<i>n</i>
91  * elements where <i>n</i> is the number of feeds.</li>
92  * </ul>
93  * <p>
94  * Furthermore it sets the content type of the response to text/html using UTF-8
95  * to ensure this will be returned in the HTTP header.
96  * </p>
97  *
98  * @author Robert Tansley
99  * @version $Revision: 1.19 $
100  */

101 public class LayoutTag extends TagSupport JavaDoc
102 {
103     /** log4j logger */
104     private static Logger log = Logger.getLogger(LayoutTag.class);
105
106     /** layout style name */
107     private String JavaDoc style;
108
109     /** title */
110     private String JavaDoc title;
111
112     /** title key (from message dictionary) */
113     private String JavaDoc titleKey;
114
115     /** Navigation bar type, null means none */
116     private String JavaDoc navbar;
117
118     /** Location bar type */
119     private String JavaDoc locbar;
120
121     /** Name of "parent" page */
122     private String JavaDoc parentTitle;
123
124     /** Name of "parent" page key (from message dictionary) */
125     private String JavaDoc parentTitleKey;
126
127     /** Link to "parent" page */
128     private String JavaDoc parentLink;
129
130     /** Contents of side bar */
131     private String JavaDoc sidebar;
132
133     /** Whether to add headers to prevent browsers caching the page */
134     private String JavaDoc noCache;
135     
136     /** Syndication feed "autodiscovery" link data */
137     private String JavaDoc feedData;
138
139     public LayoutTag()
140     {
141         super();
142     }
143
144     public int doStartTag() throws JspException JavaDoc
145     {
146         ServletRequest JavaDoc request = pageContext.getRequest();
147
148         // header file
149
String JavaDoc header = "/layout/header-default.jsp";
150
151         // Choose default style unless one is specified
152
if (style != null)
153         {
154             header = "/layout/header-" + style.toLowerCase() + ".jsp";
155         }
156
157         // Sort out location bar
158
if (locbar == null)
159         {
160             locbar = "auto";
161         }
162
163         // These lists will contain titles and links to put in the location
164
// bar
165
List JavaDoc parents = new ArrayList JavaDoc();
166         List JavaDoc parentLinks = new ArrayList JavaDoc();
167
168         if (locbar.equalsIgnoreCase("off"))
169         {
170             // No location bar
171
request.setAttribute("dspace.layout.locbar", new Boolean JavaDoc(false));
172         }
173         else
174         {
175             // We'll always add "DSpace Home" to the a location bar
176
parents.add(ConfigurationManager.getProperty("dspace.name"));
177
178             if (locbar.equalsIgnoreCase("nolink"))
179             {
180                 parentLinks.add("");
181             }
182             else
183             {
184                 parentLinks.add("/");
185             }
186
187             // Add other relevant components to the location bar
188
if (locbar.equalsIgnoreCase("link"))
189             {
190                 // "link" mode - next thing in location bar is taken from
191
// parameters of tag, with a link
192
if (parentTitle != null)
193                 {
194                     parents.add(parentTitle);
195                     parentLinks.add(parentLink);
196                 }
197                 else if (parentTitleKey != null)
198                 {
199                     parents.add(LocaleSupport.getLocalizedMessage(pageContext,
200                             parentTitleKey));
201                     parentLinks.add(parentLink);
202                 }
203
204             }
205             else if (locbar.equalsIgnoreCase("commLink"))
206             {
207                 // "commLink" mode - show all parent communities
208
Community[] comms = (Community[]) request
209                         .getAttribute("dspace.communities");
210
211                 if (comms != null)
212                 {
213                     for (int i = 0; i < comms.length; i++)
214                     {
215                         parents.add(comms[i].getMetadata("name"));
216                         parentLinks.add("/handle/" + comms[i].getHandle());
217                     }
218                 }
219             }
220             else if (locbar.equalsIgnoreCase("nolink"))
221             {
222                 // "nolink" mode - next thing in location bar is taken from
223
// parameters of tag, with no link
224
if (parentTitle != null)
225                 {
226                     parents.add(parentTitle);
227                     parentLinks.add("");
228                 }
229             }
230             else
231             {
232                 // Grab parents from the URL - these should have been picked up
233
// by the HandleServlet
234
Collection col = (Collection) request
235                         .getAttribute("dspace.collection");
236                 Community[] comms = (Community[]) request
237                         .getAttribute("dspace.communities");
238
239                 if (comms != null)
240                 {
241                     for (int i = 0; i < comms.length; i++)
242                     {
243                         parents.add(comms[i].getMetadata("name"));
244                         parentLinks.add("/handle/" + comms[i].getHandle());
245                     }
246
247                     if (col != null)
248                     {
249                         parents.add(col.getMetadata("name"));
250                         parentLinks.add("/handle/" + col.getHandle());
251                     }
252                 }
253             }
254
255             request.setAttribute("dspace.layout.locbar", new Boolean JavaDoc(true));
256         }
257
258         request.setAttribute("dspace.layout.parenttitles", parents);
259         request.setAttribute("dspace.layout.parentlinks", parentLinks);
260
261         // Navigation bar: "default" is default :)
262
if (navbar == null)
263         {
264             navbar = "default";
265         }
266
267         if (navbar.equals("off"))
268         {
269             request.setAttribute("dspace.layout.navbar", "off");
270         }
271         else
272         {
273             request.setAttribute("dspace.layout.navbar", "/layout/navbar-"
274                     + navbar + ".jsp");
275         }
276
277         // Set title
278
if (title != null)
279         {
280             request.setAttribute("dspace.layout.title", title);
281         }
282         else if (titleKey != null)
283         {
284             request.setAttribute("dspace.layout.title", LocaleSupport
285                     .getLocalizedMessage(pageContext, titleKey));
286         }
287         else
288         {
289             request.setAttribute("dspace.layout.title", "NO TITLE");
290         }
291         
292         // Set feedData if present
293
if (feedData != null && ! "NONE".equals(feedData))
294         {
295             // set the links' reference - community or collection
296
boolean commLinks = feedData.startsWith("comm:");
297             boolean collLinks = feedData.startsWith("coll:");
298             if ( commLinks )
299             {
300                 Community com = (Community)request.getAttribute("dspace.community");
301                 request.setAttribute("dspace.layout.feedref", com.getHandle());
302             }
303             else if( collLinks )
304             {
305                 Collection col = (Collection)request.getAttribute("dspace.collection");
306                 request.setAttribute("dspace.layout.feedref", col.getHandle());
307             }
308             else //feed is across all of DSpace and not Community/Collection specific
309
{
310                 request.setAttribute("dspace.layout.feedref", FeedServlet.SITE_FEED_KEY);
311             }
312             
313             // build a list of link attributes for each link format
314
String JavaDoc[] formats = feedData.substring(feedData.indexOf(":")+1).split(",");
315             List JavaDoc linkParts = new ArrayList JavaDoc();
316             // each link has a mime-type, title, and format (used in href URL)
317
for (int i = 0; i < formats.length; i++)
318             {
319                 if("rss_1.0".equals(formats[i]))
320                 {
321                     linkParts.add("rdf+xml");
322                 }
323                 else
324                 {
325                     linkParts.add("rss+xml");
326                 }
327                 
328                 if (commLinks)
329                 {
330                     linkParts.add("Items in Community");
331                 }
332                 else if(collLinks)
333                 {
334                     linkParts.add("Items in Collection");
335                 }
336                 else
337                 {
338                     linkParts.add("Items in " + ConfigurationManager.getProperty("dspace.name"));
339                 }
340                 
341                 linkParts.add(formats[i]);
342             }
343             request.setAttribute("dspace.layout.linkparts", linkParts);
344         }
345         else
346         {
347             request.setAttribute("dspace.layout.feedref", "NONE" );
348         }
349
350         // Now include the header
351
try
352         {
353             HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext
354                     .getResponse();
355
356             // Set headers to prevent browser caching, if appropriate
357
if ((noCache != null) && noCache.equalsIgnoreCase("true"))
358             {
359                 response.addDateHeader("expires", 1);
360                 response.addHeader("Pragma", "no-cache");
361                 response.addHeader("Cache-control", "no-store");
362             }
363
364             // Ensure the HTTP header will declare that UTF-8 is used
365
// in the response.
366
response.setContentType("text/html; charset=UTF-8");
367
368             ServletConfig JavaDoc config = pageContext.getServletConfig();
369
370             RequestDispatcher JavaDoc rd = config.getServletContext()
371                     .getRequestDispatcher(header);
372
373             rd.include(request, response);
374         }
375         catch (IOException JavaDoc ioe)
376         {
377             throw new JspException JavaDoc("Got IOException: " + ioe);
378         }
379         catch (ServletException JavaDoc se)
380         {
381             log.warn("Exception", se.getRootCause());
382             throw new JspException JavaDoc("Got ServletException: " + se);
383         }
384
385         return EVAL_BODY_INCLUDE;
386     }
387
388     public int doEndTag() throws JspException JavaDoc
389     {
390         // Footer file to use
391
String JavaDoc footer = "/layout/footer-default.jsp";
392
393         // Choose default flavour unless one is specified
394
if (style != null)
395         {
396             footer = "/layout/footer-" + style.toLowerCase() + ".jsp";
397         }
398
399         try
400         {
401             // Ensure body is included before footer
402
pageContext.getOut().flush();
403
404             // Context objects
405
ServletRequest JavaDoc request = pageContext.getRequest();
406             ServletResponse JavaDoc response = pageContext.getResponse();
407             ServletConfig JavaDoc config = pageContext.getServletConfig();
408
409             if (sidebar != null)
410             {
411                 request.setAttribute("dspace.layout.sidebar", sidebar);
412             }
413
414             RequestDispatcher JavaDoc rd = config.getServletContext()
415                     .getRequestDispatcher(footer);
416
417             rd.include(request, response);
418         }
419         catch (ServletException JavaDoc se)
420         {
421             throw new JspException JavaDoc("Got ServletException: " + se);
422         }
423         catch (IOException JavaDoc ioe)
424         {
425             throw new JspException JavaDoc("Got IOException: " + ioe);
426         }
427
428         return EVAL_PAGE;
429     }
430
431     /**
432      * Get the value of title.
433      *
434      * @return Value of title.
435      */

436     public String JavaDoc getTitle()
437     {
438         return title;
439     }
440
441     /**
442      * Set the value of title.
443      *
444      * @param v
445      * Value to assign to title.
446      */

447     public void setTitle(String JavaDoc v)
448     {
449         this.title = v;
450     }
451
452     /**
453      * @return Returns the titleKey.
454      */

455     public String JavaDoc getTitlekey()
456     {
457         return titleKey;
458     }
459     
460     /**
461      * @param titleKey The titleKey to set.
462      */

463     public void setTitlekey(String JavaDoc titleKey)
464     {
465         this.titleKey = titleKey;
466     }
467     
468     /**
469      * Get the value of navbar.
470      *
471      * @return Value of navbar.
472      */

473     public String JavaDoc getNavbar()
474     {
475         return navbar;
476     }
477
478     /**
479      * Set the value of navbar.
480      *
481      * @param v
482      * Value to assign to navbar.
483      */

484     public void setNavbar(String JavaDoc v)
485     {
486         this.navbar = v;
487     }
488
489     /**
490      * Get the value of locbar.
491      *
492      * @return Value of locbar.
493      */

494     public String JavaDoc getLocbar()
495     {
496         return locbar;
497     }
498
499     /**
500      * Set the value of locbar.
501      *
502      * @param v
503      * Value to assign to locbar.
504      */

505     public void setLocbar(String JavaDoc v)
506     {
507         this.locbar = v;
508     }
509
510     /**
511      * Get the value of parentTitle.
512      *
513      * @return Value of parentTitle.
514      */

515     public String JavaDoc getParenttitle()
516     {
517         return parentTitle;
518     }
519
520     /**
521      * Set the value of parent.
522      *
523      * @param v
524      * Value to assign to parent.
525      */

526     public void setParenttitle(String JavaDoc v)
527     {
528         this.parentTitle = v;
529     }
530
531     /**
532      * get parent title key (from message dictionary)
533      *
534      * @return Returns the parentTitleKey.
535      */

536     public String JavaDoc getParenttitlekey()
537     {
538         return parentTitleKey;
539     }
540
541     /**
542      * set parent title key (from message dictionary)
543      *
544      * @param parentTitleKey The parentTitleKey to set.
545      */

546     public void setParenttitlekey(String JavaDoc parentTitleKey)
547     {
548         this.parentTitleKey = parentTitleKey;
549     }
550
551     /**
552      * Get the value of parentlink.
553      *
554      * @return Value of parentlink.
555      */

556     public String JavaDoc getParentlink()
557     {
558         return parentLink;
559     }
560
561     /**
562      * Set the value of parentlink.
563      *
564      * @param v
565      * Value to assign to parentlink.
566      */

567     public void setParentlink(String JavaDoc v)
568     {
569         this.parentLink = v;
570     }
571
572     /**
573      * Get the value of style.
574      *
575      * @return Value of style.
576      */

577     public String JavaDoc getStyle()
578     {
579         return style;
580     }
581
582     /**
583      * Set the value of style.
584      *
585      * @param v
586      * Value to assign to style.
587      */

588     public void setStyle(String JavaDoc v)
589     {
590         this.style = v;
591     }
592
593     /**
594      * Get the value of sidebar.
595      *
596      * @return Value of sidebar.
597      */

598     public String JavaDoc getSidebar()
599     {
600         return sidebar;
601     }
602
603     /**
604      * Set the value of sidebar.
605      *
606      * @param v
607      * Value to assign to sidebar.
608      */

609     public void setSidebar(String JavaDoc v)
610     {
611         this.sidebar = v;
612     }
613
614     /**
615      * Get the value of sidebar.
616      *
617      * @return Value of sidebar.
618      */

619     public String JavaDoc getNocache()
620     {
621         return noCache;
622     }
623
624     /**
625      * Set the value of sidebar.
626      *
627      * @param v
628      * Value to assign to sidebar.
629      */

630     public void setNocache(String JavaDoc v)
631     {
632         this.noCache = v;
633     }
634     
635     /**
636      * Get the value of feedData.
637      *
638      * @return Value of feedData.
639      */

640     public String JavaDoc getFeedData()
641     {
642         return feedData;
643     }
644
645     /**
646      * Set the value of feedData.
647      *
648      * @param v
649      * Value to assign to feedData.
650      */

651     public void setFeedData(String JavaDoc v)
652     {
653         this.feedData = v;
654     }
655
656     public void release()
657     {
658         style = null;
659         title = null;
660         sidebar = null;
661         navbar = null;
662         locbar = null;
663         parentTitle = null;
664         parentLink = null;
665         noCache = null;
666         feedData = null;
667     }
668 }
669
Popular Tags