1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.util;
16  
17  import com.liferay.portal.kernel.servlet.StringServletResponse;
18  import com.liferay.portal.kernel.util.HttpUtil;
19  import com.liferay.portal.kernel.util.WebKeys;
20  import com.liferay.portal.model.Layout;
21  import com.liferay.portal.model.LayoutConstants;
22  import com.liferay.portal.model.Portlet;
23  import com.liferay.portal.model.Theme;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.PropsValues;
26  import com.liferay.taglib.portlet.ActionURLTag;
27  import com.liferay.taglib.portletext.IconBackTag;
28  import com.liferay.taglib.portletext.IconCloseTag;
29  import com.liferay.taglib.portletext.IconConfigurationTag;
30  import com.liferay.taglib.portletext.IconEditDefaultsTag;
31  import com.liferay.taglib.portletext.IconEditGuestTag;
32  import com.liferay.taglib.portletext.IconEditTag;
33  import com.liferay.taglib.portletext.IconHelpTag;
34  import com.liferay.taglib.portletext.IconMaximizeTag;
35  import com.liferay.taglib.portletext.IconMinimizeTag;
36  import com.liferay.taglib.portletext.IconOptionsTag;
37  import com.liferay.taglib.portletext.IconPortletCssTag;
38  import com.liferay.taglib.portletext.IconPortletTag;
39  import com.liferay.taglib.portletext.IconPrintTag;
40  import com.liferay.taglib.portletext.IconRefreshTag;
41  import com.liferay.taglib.portletext.RuntimeTag;
42  import com.liferay.taglib.security.DoAsURLTag;
43  import com.liferay.taglib.security.PermissionsURLTag;
44  import com.liferay.taglib.theme.LayoutIconTag;
45  import com.liferay.taglib.theme.MetaTagsTag;
46  import com.liferay.taglib.theme.WrapPortletTag;
47  import com.liferay.taglib.ui.BreadcrumbTag;
48  import com.liferay.taglib.ui.JournalContentSearchTag;
49  import com.liferay.taglib.ui.LanguageTag;
50  import com.liferay.taglib.ui.MyPlacesTag;
51  import com.liferay.taglib.ui.PngImageTag;
52  import com.liferay.taglib.ui.SearchTag;
53  import com.liferay.taglib.ui.StagingTag;
54  import com.liferay.taglib.ui.ToggleTag;
55  
56  import java.util.Map;
57  
58  import javax.portlet.PortletMode;
59  import javax.portlet.PortletRequest;
60  import javax.portlet.PortletURL;
61  import javax.portlet.WindowState;
62  
63  import javax.servlet.RequestDispatcher;
64  import javax.servlet.ServletContext;
65  import javax.servlet.http.HttpServletRequest;
66  import javax.servlet.jsp.PageContext;
67  
68  /**
69   * <a href="VelocityTaglib.java.html"><b><i>View Source</i></b></a>
70   *
71   * @author Brian Wing Shun Chan
72   */
73  public class VelocityTaglib {
74  
75      public VelocityTaglib() {
76      }
77  
78      public VelocityTaglib(
79          ServletContext servletContext, HttpServletRequest request,
80          StringServletResponse stringResponse, PageContext pageContext) {
81  
82          init(servletContext, request, stringResponse, pageContext);
83      }
84  
85      public VelocityTaglib init(
86          ServletContext servletContext, HttpServletRequest request,
87          StringServletResponse stringResponse, PageContext pageContext) {
88  
89          _servletContext = servletContext;
90          _request = request;
91          _stringResponse = stringResponse;
92          _pageContext = pageContext;
93  
94          return this;
95      }
96  
97      public String actionURL(String portletName, String queryString)
98          throws Exception {
99  
100         return actionURL(
101             LayoutConstants.DEFAULT_PLID, portletName, queryString);
102     }
103 
104     public String actionURL(long plid, String portletName, String queryString)
105         throws Exception {
106 
107         String windowState = WindowState.NORMAL.toString();
108         String portletMode = PortletMode.VIEW.toString();
109 
110         return actionURL(
111             windowState, portletMode, plid, portletName, queryString);
112     }
113 
114     public String actionURL(
115             String windowState, String portletMode, String portletName,
116             String queryString)
117         throws Exception {
118 
119         return actionURL(
120             windowState, portletMode, LayoutConstants.DEFAULT_PLID, portletName,
121             queryString);
122     }
123 
124     public String actionURL(
125             String windowState, String portletMode, long plid,
126             String portletName, String queryString)
127         throws Exception {
128 
129         Boolean secure = null;
130         Boolean copyCurrentRenderParameters = null;
131         Boolean escapeXml = null;
132         String name = null;
133         Boolean anchor = null;
134         Boolean encrypt = null;
135         long doAsUserId = 0;
136         Boolean portletConfiguration = null;
137 
138         return actionURL(
139             windowState, portletMode, secure, copyCurrentRenderParameters,
140             escapeXml, name, plid, portletName, anchor, encrypt, doAsUserId,
141             portletConfiguration, queryString);
142     }
143 
144     public String actionURL(
145             String windowState, String portletMode, Boolean secure,
146             Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
147             long plid, String portletName, Boolean anchor, Boolean encrypt,
148             long doAsUserId, Boolean portletConfiguration, String queryString)
149         throws Exception {
150 
151         String var = null;
152         String varImpl = null;
153         String resourceID = null;
154         String cacheability = null;
155         Map<String, String[]> params = HttpUtil.parameterMapFromString(
156             queryString);
157         boolean writeOutput = false;
158 
159         return ActionURLTag.doTag(
160             PortletRequest.ACTION_PHASE, windowState, portletMode, var, varImpl,
161             secure, copyCurrentRenderParameters, escapeXml, name, resourceID,
162             cacheability, plid, portletName, anchor, encrypt, doAsUserId,
163             portletConfiguration, params, writeOutput, _pageContext);
164     }
165 
166     public String breadcrumb() throws Exception {
167         _stringResponse.recycle();
168 
169         BreadcrumbTag.doTag(_servletContext, _request, _stringResponse);
170 
171         return _stringResponse.getString();
172     }
173 
174     public String breadcrumb(
175             String page, Layout selLayout, String selLayoutParam,
176             PortletURL portletURL, int displayStyle)
177         throws Exception {
178 
179         return breadcrumb (
180             page, selLayout, selLayoutParam, portletURL, displayStyle,
181             PropsValues.BREADCRUMB_SHOW_GUEST_GROUP,
182             PropsValues.BREADCRUMB_SHOW_PARENT_GROUPS, true, true);
183     }
184 
185     public String breadcrumb(
186             String page, Layout selLayout, String selLayoutParam,
187             PortletURL portletURL, int displayStyle, boolean showGuestGroup,
188             boolean showParentGroups, boolean showLayout,
189             boolean showPortletBreadcrumb)
190         throws Exception {
191 
192         _stringResponse.recycle();
193 
194         BreadcrumbTag.doTag(
195             page, selLayout, selLayoutParam, portletURL, displayStyle,
196             showGuestGroup, showParentGroups, showLayout, showPortletBreadcrumb,
197             _servletContext, _request, _stringResponse);
198 
199         return _stringResponse.getString();
200     }
201 
202     public String breadcrumb(
203             int displayStyle, boolean showGuestGroup, boolean showParentGroups,
204             boolean showLayout, boolean showPortletBreadcrumb)
205         throws Exception {
206 
207         _stringResponse.recycle();
208 
209         BreadcrumbTag.doTag(
210             displayStyle, showGuestGroup, showParentGroups, showLayout,
211             showPortletBreadcrumb, _servletContext, _request, _stringResponse);
212 
213         return _stringResponse.getString();
214     }
215 
216     public String doAsURL(long doAsUserId) throws Exception {
217         return DoAsURLTag.doTag(doAsUserId, null, false, _pageContext);
218     }
219 
220     public String getSetting(String name) {
221         ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
222             WebKeys.THEME_DISPLAY);
223 
224         Theme theme = themeDisplay.getTheme();
225 
226         return theme.getSetting(name);
227     }
228 
229     public String iconBack() throws Exception {
230         _stringResponse.recycle();
231 
232         IconBackTag.doTag(_servletContext, _request, _stringResponse);
233 
234         return _stringResponse.getString();
235     }
236 
237     public String iconBack(String page) throws Exception {
238         _stringResponse.recycle();
239 
240         IconBackTag.doTag(page, _servletContext, _request, _stringResponse);
241 
242         return _stringResponse.getString();
243     }
244 
245     public String iconClose() throws Exception {
246         _stringResponse.recycle();
247 
248         IconCloseTag.doTag(_servletContext, _request, _stringResponse);
249 
250         return _stringResponse.getString();
251     }
252 
253     public String iconClose(String page) throws Exception {
254         _stringResponse.recycle();
255 
256         IconCloseTag.doTag(page, _servletContext, _request, _stringResponse);
257 
258         return _stringResponse.getString();
259     }
260 
261     public String iconConfiguration() throws Exception {
262         _stringResponse.recycle();
263 
264         IconConfigurationTag.doTag(_servletContext, _request, _stringResponse);
265 
266         return _stringResponse.getString();
267     }
268 
269     public String iconConfiguration(String page) throws Exception {
270         _stringResponse.recycle();
271 
272         IconConfigurationTag.doTag(
273             page, _servletContext, _request, _stringResponse);
274 
275         return _stringResponse.getString();
276     }
277 
278     public String iconEdit() throws Exception {
279         _stringResponse.recycle();
280 
281         IconEditTag.doTag(_servletContext, _request, _stringResponse);
282 
283         return _stringResponse.getString();
284     }
285 
286     public String iconEdit(String page) throws Exception {
287         _stringResponse.recycle();
288 
289         IconEditTag.doTag(page, _servletContext, _request, _stringResponse);
290 
291         return _stringResponse.getString();
292     }
293 
294     public String iconEditDefaults() throws Exception {
295         _stringResponse.recycle();
296 
297         IconEditDefaultsTag.doTag(_servletContext, _request, _stringResponse);
298 
299         return _stringResponse.getString();
300     }
301 
302     public String iconEditGuest() throws Exception {
303         _stringResponse.recycle();
304 
305         IconEditGuestTag.doTag(_servletContext, _request, _stringResponse);
306 
307         return _stringResponse.getString();
308     }
309 
310     public String iconEditGuest(String page) throws Exception {
311         _stringResponse.recycle();
312 
313         IconEditGuestTag.doTag(
314             page, _servletContext, _request, _stringResponse);
315 
316         return _stringResponse.getString();
317     }
318 
319     public String iconHelp() throws Exception {
320         _stringResponse.recycle();
321 
322         IconHelpTag.doTag(_servletContext, _request, _stringResponse);
323 
324         return _stringResponse.getString();
325     }
326 
327     public String iconHelp(String page) throws Exception {
328         _stringResponse.recycle();
329 
330         IconHelpTag.doTag(page, _servletContext, _request, _stringResponse);
331 
332         return _stringResponse.getString();
333     }
334 
335     public String iconMaximize() throws Exception {
336         _stringResponse.recycle();
337 
338         IconMaximizeTag.doTag(_servletContext, _request, _stringResponse);
339 
340         return _stringResponse.getString();
341     }
342 
343     public String iconMaximize(String page) throws Exception {
344         _stringResponse.recycle();
345 
346         IconMaximizeTag.doTag(page, _servletContext, _request, _stringResponse);
347 
348         return _stringResponse.getString();
349     }
350 
351     public String iconMinimize() throws Exception {
352         _stringResponse.recycle();
353 
354         IconMinimizeTag.doTag(_servletContext, _request, _stringResponse);
355 
356         return _stringResponse.getString();
357     }
358 
359     public String iconMinimize(String page) throws Exception {
360         _stringResponse.recycle();
361 
362         IconMinimizeTag.doTag(page, _servletContext, _request, _stringResponse);
363 
364         return _stringResponse.getString();
365     }
366 
367     public String iconOptions() throws Exception {
368         _stringResponse.recycle();
369 
370         IconOptionsTag.doTag(_servletContext, _request, _stringResponse);
371 
372         return _stringResponse.getString();
373     }
374 
375     public String iconOptions(String page) throws Exception {
376         _stringResponse.recycle();
377 
378         IconOptionsTag.doTag(page, _servletContext, _request, _stringResponse);
379 
380         return _stringResponse.getString();
381     }
382 
383     public String iconPortlet() throws Exception {
384         _stringResponse.recycle();
385 
386         IconPortletTag.doTag(_servletContext, _request, _stringResponse);
387 
388         return _stringResponse.getString();
389     }
390 
391     public String iconPortlet(String page, Portlet portlet) throws Exception {
392         _stringResponse.recycle();
393 
394         IconPortletTag.doTag(
395             page, portlet, _servletContext, _request, _stringResponse);
396 
397         return _stringResponse.getString();
398     }
399 
400     public String iconPortletCss() throws Exception {
401         _stringResponse.recycle();
402 
403         IconPortletCssTag.doTag(_servletContext, _request, _stringResponse);
404 
405         return _stringResponse.getString();
406     }
407 
408     public String iconPortletCss(String page) throws Exception {
409         _stringResponse.recycle();
410 
411         IconPortletCssTag.doTag(
412             page, _servletContext, _request, _stringResponse);
413 
414         return _stringResponse.getString();
415     }
416 
417     public String iconPrint() throws Exception {
418         _stringResponse.recycle();
419 
420         IconPrintTag.doTag(_servletContext, _request, _stringResponse);
421 
422         return _stringResponse.getString();
423     }
424 
425     public String iconPrint(String page) throws Exception {
426         _stringResponse.recycle();
427 
428         IconPrintTag.doTag(page, _servletContext, _request, _stringResponse);
429 
430         return _stringResponse.getString();
431     }
432 
433     public String iconRefresh() throws Exception {
434         _stringResponse.recycle();
435 
436         IconRefreshTag.doTag(_servletContext, _request, _stringResponse);
437 
438         return _stringResponse.getString();
439     }
440 
441     public String iconRefresh(String page) throws Exception {
442         _stringResponse.recycle();
443 
444         IconRefreshTag.doTag(page, _servletContext, _request, _stringResponse);
445 
446         return _stringResponse.getString();
447     }
448 
449     public String include(String page) throws Exception {
450         _stringResponse.recycle();
451 
452         RequestDispatcher requestDispatcher =
453             _servletContext.getRequestDispatcher(page);
454 
455         requestDispatcher.include(_request, _stringResponse);
456 
457         return _stringResponse.getString();
458     }
459 
460     public String include(ServletContext servletContext, String page)
461         throws Exception {
462 
463         _stringResponse.recycle();
464 
465         RequestDispatcher requestDispatcher =
466             servletContext.getRequestDispatcher(page);
467 
468         requestDispatcher.include(_request, _stringResponse);
469 
470         return _stringResponse.getString();
471     }
472 
473     public String journalContentSearch() throws Exception {
474         _stringResponse.recycle();
475 
476         JournalContentSearchTag.doTag(
477             _servletContext, _request, _stringResponse);
478 
479         return _stringResponse.getString();
480     }
481 
482     public String language() throws Exception {
483         _stringResponse.recycle();
484 
485         LanguageTag.doTag(_servletContext, _request, _stringResponse);
486 
487         return _stringResponse.getString();
488     }
489 
490     public String language(
491             String formName, String formAction, String name, int displayStyle)
492         throws Exception {
493 
494         _stringResponse.recycle();
495 
496         LanguageTag.doTag(
497             formName, formAction, name, null, displayStyle, _servletContext,
498             _request, _stringResponse);
499 
500         return _stringResponse.getString();
501     }
502 
503     public String language(
504             String formName, String formAction, String name,
505             String[] languageIds, int displayStyle)
506         throws Exception {
507 
508         _stringResponse.recycle();
509 
510         LanguageTag.doTag(
511             formName, formAction, name, languageIds, displayStyle,
512             _servletContext, _request, _stringResponse);
513 
514         return _stringResponse.getString();
515     }
516 
517     public String language(
518             String page, String formName, String formAction, String name,
519             int displayStyle)
520         throws Exception {
521 
522         _stringResponse.recycle();
523 
524         LanguageTag.doTag(
525             page, formName, formAction, name, null, displayStyle,
526             _servletContext, _request, _stringResponse);
527 
528         return _stringResponse.getString();
529     }
530 
531     public String language(
532             String page, String formName, String formAction, String name,
533             String[] languageIds, int displayStyle)
534         throws Exception {
535 
536         _stringResponse.recycle();
537 
538         LanguageTag.doTag(
539             page, formName, formAction, name, languageIds, displayStyle,
540             _servletContext, _request, _stringResponse);
541 
542         return _stringResponse.getString();
543     }
544 
545     public String layoutIcon(Layout layout) throws Exception {
546         _stringResponse.recycle();
547 
548         LayoutIconTag.doTag(layout, _servletContext, _request, _stringResponse);
549 
550         return _stringResponse.getString();
551     }
552 
553     public String metaTags() throws Exception {
554         _stringResponse.recycle();
555 
556         MetaTagsTag.doTag(_servletContext, _request, _stringResponse);
557 
558         return _stringResponse.getString();
559     }
560 
561     public String myPlaces() throws Exception {
562         _stringResponse.recycle();
563 
564         MyPlacesTag.doTag(_servletContext, _request, _stringResponse);
565 
566         return _stringResponse.getString();
567     }
568 
569     public String myPlaces(int max) throws Exception {
570         _stringResponse.recycle();
571 
572         MyPlacesTag.doTag(max, _servletContext, _request, _stringResponse);
573 
574         return _stringResponse.getString();
575     }
576 
577     public String permissionsURL(
578             String redirect, String modelResource,
579             String modelResourceDescription, String resourcePrimKey)
580         throws Exception {
581 
582         return PermissionsURLTag.doTag(
583             redirect, modelResource, modelResourceDescription, resourcePrimKey,
584             null, false, _pageContext);
585     }
586 
587     public String pngImage(String image, String height, String width)
588         throws Exception {
589 
590         _stringResponse.recycle();
591 
592         PngImageTag.doTag(image, height, width, _servletContext, _request,
593             _stringResponse);
594 
595         return _stringResponse.getString();
596     }
597 
598     public String renderURL(String portletName, String queryString)
599         throws Exception {
600 
601         return renderURL(
602             LayoutConstants.DEFAULT_PLID, portletName, queryString);
603     }
604 
605     public String renderURL(long plid, String portletName, String queryString)
606         throws Exception {
607 
608         String windowState = WindowState.NORMAL.toString();
609         String portletMode = PortletMode.VIEW.toString();
610 
611         return renderURL(
612             windowState, portletMode, plid, portletName, queryString);
613     }
614 
615     public String renderURL(
616             String windowState, String portletMode, String portletName,
617             String queryString)
618         throws Exception {
619 
620         return renderURL(
621             windowState, portletMode, LayoutConstants.DEFAULT_PLID, portletName,
622             queryString);
623     }
624 
625     public String renderURL(
626             String windowState, String portletMode, long plid,
627             String portletName, String queryString)
628         throws Exception {
629 
630         Boolean secure = null;
631         Boolean copyCurrentRenderParameters = null;
632         Boolean escapeXml = null;
633         Boolean anchor = null;
634         Boolean encrypt = null;
635         long doAsUserId = 0;
636         Boolean portletConfiguration = null;
637 
638         return renderURL(
639             windowState, portletMode, secure, copyCurrentRenderParameters,
640             escapeXml, plid, portletName, anchor, encrypt, doAsUserId,
641             portletConfiguration, queryString);
642     }
643 
644     public String renderURL(
645             String windowState, String portletMode, Boolean secure,
646             Boolean copyCurrentRenderParameters, Boolean escapeXml,
647             long plid, String portletName, Boolean anchor, Boolean encrypt,
648             long doAsUserId, Boolean portletConfiguration, String queryString)
649         throws Exception {
650 
651         String var = null;
652         String varImpl = null;
653         String name = null;
654         String resourceID = null;
655         String cacheability = null;
656         Map<String, String[]> params = HttpUtil.parameterMapFromString(
657             queryString);
658         boolean writeOutput = false;
659 
660         return ActionURLTag.doTag(
661             PortletRequest.RENDER_PHASE, windowState, portletMode, var, varImpl,
662             secure, copyCurrentRenderParameters, escapeXml, name, resourceID,
663             cacheability, plid, portletName, anchor, encrypt, doAsUserId,
664             portletConfiguration, params, writeOutput, _pageContext);
665     }
666 
667     public String runtime(String portletName)
668         throws Exception {
669 
670         return runtime(portletName, null);
671     }
672 
673     public String runtime(String portletName, String queryString)
674         throws Exception {
675 
676         _stringResponse.recycle();
677 
678         RuntimeTag.doTag(
679             portletName, queryString, null, _servletContext, _request,
680             _stringResponse);
681 
682         return _stringResponse.getString();
683     }
684 
685     public String runtime(
686             String portletName, String queryString, String defaultPreferences)
687         throws Exception {
688 
689         _stringResponse.recycle();
690 
691         RuntimeTag.doTag(
692             portletName, queryString, defaultPreferences, null, _servletContext,
693             _request, _stringResponse);
694 
695         return _stringResponse.getString();
696     }
697 
698     public String search() throws Exception {
699         _stringResponse.recycle();
700 
701         SearchTag.doTag(_servletContext, _request, _stringResponse);
702 
703         return _stringResponse.getString();
704     }
705 
706     public String staging() throws Exception {
707         _stringResponse.recycle();
708 
709         StagingTag.doTag(_servletContext, _request, _stringResponse);
710 
711         return _stringResponse.getString();
712     }
713 
714     public String toggle(
715             String id, String showImage, String hideImage, String showMessage,
716             String hideMessage, boolean defaultShowContent)
717         throws Exception {
718 
719         _stringResponse.recycle();
720 
721         ToggleTag.doTag(
722             id, showImage, hideImage, showMessage, hideMessage,
723             defaultShowContent, null, _servletContext, _request,
724             _stringResponse);
725 
726         return _stringResponse.getString();
727     }
728 
729     public String wrapPortlet(String wrapPage, String portletPage)
730         throws Exception {
731 
732         _stringResponse.recycle();
733 
734         return WrapPortletTag.doTag(
735             wrapPage, portletPage, _servletContext, _request, _stringResponse,
736             _pageContext);
737     }
738 
739     private ServletContext _servletContext;
740     private HttpServletRequest _request;
741     private StringServletResponse _stringResponse;
742     private PageContext _pageContext;
743 
744 }