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