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.JournalContentSearchTag;
52  import com.liferay.taglib.ui.LanguageTag;
53  import com.liferay.taglib.ui.MyPlacesTag;
54  import com.liferay.taglib.ui.PngImageTag;
55  import com.liferay.taglib.ui.SearchTag;
56  import com.liferay.taglib.ui.StagingTag;
57  import com.liferay.taglib.ui.ToggleTag;
58  
59  import java.util.Map;
60  
61  import javax.portlet.PortletMode;
62  import javax.portlet.PortletRequest;
63  import javax.portlet.WindowState;
64  
65  import javax.servlet.RequestDispatcher;
66  import javax.servlet.ServletContext;
67  import javax.servlet.http.HttpServletRequest;
68  import javax.servlet.jsp.PageContext;
69  
70  /**
71   * <a href="VelocityTaglib.java.html"><b><i>View Source</i></b></a>
72   *
73   * @author Brian Wing Shun Chan
74   *
75   */
76  public class VelocityTaglib {
77  
78      public VelocityTaglib() {
79      }
80  
81      public VelocityTaglib(
82          ServletContext servletContext, HttpServletRequest request,
83          StringServletResponse stringResponse, PageContext pageContext) {
84  
85          init(servletContext, request, stringResponse, pageContext);
86      }
87  
88      public VelocityTaglib init(
89          ServletContext servletContext, HttpServletRequest request,
90          StringServletResponse stringResponse, PageContext pageContext) {
91  
92          _servletContext = servletContext;
93          _request = request;
94          _stringResponse = stringResponse;
95          _pageContext = pageContext;
96  
97          return this;
98      }
99  
100     public String actionURL(String portletName, String queryString)
101         throws Exception {
102 
103         return actionURL(
104             LayoutConstants.DEFAULT_PLID, portletName, queryString);
105     }
106 
107     public String actionURL(long plid, String portletName, String queryString)
108         throws Exception {
109 
110         String windowState = WindowState.NORMAL.toString();
111         String portletMode = PortletMode.VIEW.toString();
112 
113         return actionURL(
114             windowState, portletMode, plid, portletName, queryString);
115     }
116 
117     public String actionURL(
118             String windowState, String portletMode, String portletName,
119             String queryString)
120         throws Exception {
121 
122         return actionURL(
123             windowState, portletMode, LayoutConstants.DEFAULT_PLID, portletName,
124             queryString);
125     }
126 
127     public String actionURL(
128             String windowState, String portletMode, long plid,
129             String portletName, String queryString)
130         throws Exception {
131 
132         Boolean secure = null;
133         Boolean copyCurrentRenderParameters = null;
134         Boolean escapeXml = null;
135         String name = null;
136         Boolean anchor = null;
137         Boolean encrypt = null;
138         long doAsUserId = 0;
139         Boolean portletConfiguration = null;
140 
141         return actionURL(
142             windowState, portletMode, secure, copyCurrentRenderParameters,
143             escapeXml, name, plid, portletName, anchor, encrypt, doAsUserId,
144             portletConfiguration, queryString);
145     }
146 
147     public String actionURL(
148             String windowState, String portletMode, Boolean secure,
149             Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
150             long plid, String portletName, Boolean anchor, Boolean encrypt,
151             long doAsUserId, Boolean portletConfiguration, String queryString)
152         throws Exception {
153 
154         String var = null;
155         String varImpl = null;
156         String resourceID = null;
157         String cacheability = null;
158         Map<String, String[]> params = HttpUtil.parameterMapFromString(
159             queryString);
160         boolean writeOutput = false;
161 
162         return ActionURLTag.doTag(
163             PortletRequest.ACTION_PHASE, windowState, portletMode, var, varImpl,
164             secure, copyCurrentRenderParameters, escapeXml, name, resourceID,
165             cacheability, plid, portletName, anchor, encrypt, doAsUserId,
166             portletConfiguration, params, writeOutput, _pageContext);
167     }
168 
169     public String doAsURL(long doAsUserId) throws Exception {
170         return DoAsURLTag.doTag(doAsUserId, null, false, _pageContext);
171     }
172 
173     public String iconBack() throws Exception {
174         _stringResponse.recycle();
175 
176         IconBackTag.doTag(_servletContext, _request, _stringResponse);
177 
178         return _stringResponse.getString();
179     }
180 
181     public String iconBack(String page) throws Exception {
182         _stringResponse.recycle();
183 
184         IconBackTag.doTag(page, _servletContext, _request, _stringResponse);
185 
186         return _stringResponse.getString();
187     }
188 
189     public String iconClose() throws Exception {
190         _stringResponse.recycle();
191 
192         IconCloseTag.doTag(_servletContext, _request, _stringResponse);
193 
194         return _stringResponse.getString();
195     }
196 
197     public String iconClose(String page) throws Exception {
198         _stringResponse.recycle();
199 
200         IconCloseTag.doTag(page, _servletContext, _request, _stringResponse);
201 
202         return _stringResponse.getString();
203     }
204 
205     public String iconConfiguration() throws Exception {
206         _stringResponse.recycle();
207 
208         IconConfigurationTag.doTag(_servletContext, _request, _stringResponse);
209 
210         return _stringResponse.getString();
211     }
212 
213     public String iconConfiguration(String page) throws Exception {
214         _stringResponse.recycle();
215 
216         IconConfigurationTag.doTag(
217             page, _servletContext, _request, _stringResponse);
218 
219         return _stringResponse.getString();
220     }
221 
222     public String iconEdit() throws Exception {
223         _stringResponse.recycle();
224 
225         IconEditTag.doTag(_servletContext, _request, _stringResponse);
226 
227         return _stringResponse.getString();
228     }
229 
230     public String iconEdit(String page) throws Exception {
231         _stringResponse.recycle();
232 
233         IconEditTag.doTag(page, _servletContext, _request, _stringResponse);
234 
235         return _stringResponse.getString();
236     }
237 
238     public String iconEditDefaults() throws Exception {
239         _stringResponse.recycle();
240 
241         IconEditDefaultsTag.doTag(_servletContext, _request, _stringResponse);
242 
243         return _stringResponse.getString();
244     }
245 
246     public String iconEditGuest() throws Exception {
247         _stringResponse.recycle();
248 
249         IconEditGuestTag.doTag(_servletContext, _request, _stringResponse);
250 
251         return _stringResponse.getString();
252     }
253 
254     public String iconEditGuest(String page) throws Exception {
255         _stringResponse.recycle();
256 
257         IconEditGuestTag.doTag(
258             page, _servletContext, _request, _stringResponse);
259 
260         return _stringResponse.getString();
261     }
262 
263     public String iconHelp() throws Exception {
264         _stringResponse.recycle();
265 
266         IconHelpTag.doTag(_servletContext, _request, _stringResponse);
267 
268         return _stringResponse.getString();
269     }
270 
271     public String iconHelp(String page) throws Exception {
272         _stringResponse.recycle();
273 
274         IconHelpTag.doTag(page, _servletContext, _request, _stringResponse);
275 
276         return _stringResponse.getString();
277     }
278 
279     public String iconMaximize() throws Exception {
280         _stringResponse.recycle();
281 
282         IconMaximizeTag.doTag(_servletContext, _request, _stringResponse);
283 
284         return _stringResponse.getString();
285     }
286 
287     public String iconMaximize(String page) throws Exception {
288         _stringResponse.recycle();
289 
290         IconMaximizeTag.doTag(page, _servletContext, _request, _stringResponse);
291 
292         return _stringResponse.getString();
293     }
294 
295     public String iconMinimize() throws Exception {
296         _stringResponse.recycle();
297 
298         IconMinimizeTag.doTag(_servletContext, _request, _stringResponse);
299 
300         return _stringResponse.getString();
301     }
302 
303     public String iconMinimize(String page) throws Exception {
304         _stringResponse.recycle();
305 
306         IconMinimizeTag.doTag(page, _servletContext, _request, _stringResponse);
307 
308         return _stringResponse.getString();
309     }
310 
311     public String iconOptions() throws Exception {
312         _stringResponse.recycle();
313 
314         IconOptionsTag.doTag(_servletContext, _request, _stringResponse);
315 
316         return _stringResponse.getString();
317     }
318 
319     public String iconOptions(String page) throws Exception {
320         _stringResponse.recycle();
321 
322         IconOptionsTag.doTag(page, _servletContext, _request, _stringResponse);
323 
324         return _stringResponse.getString();
325     }
326 
327     public String iconPortlet() throws Exception {
328         _stringResponse.recycle();
329 
330         IconPortletTag.doTag(_servletContext, _request, _stringResponse);
331 
332         return _stringResponse.getString();
333     }
334 
335     public String iconPortlet(String page, Portlet portlet) throws Exception {
336         _stringResponse.recycle();
337 
338         IconPortletTag.doTag(
339             page, portlet, _servletContext, _request, _stringResponse);
340 
341         return _stringResponse.getString();
342     }
343 
344     public String iconPortletCss() throws Exception {
345         _stringResponse.recycle();
346 
347         IconPortletCssTag.doTag(_servletContext, _request, _stringResponse);
348 
349         return _stringResponse.getString();
350     }
351 
352     public String iconPortletCss(String page) throws Exception {
353         _stringResponse.recycle();
354 
355         IconPortletCssTag.doTag(
356             page, _servletContext, _request, _stringResponse);
357 
358         return _stringResponse.getString();
359     }
360 
361     public String iconPrint() throws Exception {
362         _stringResponse.recycle();
363 
364         IconPrintTag.doTag(_servletContext, _request, _stringResponse);
365 
366         return _stringResponse.getString();
367     }
368 
369     public String iconPrint(String page) throws Exception {
370         _stringResponse.recycle();
371 
372         IconPrintTag.doTag(page, _servletContext, _request, _stringResponse);
373 
374         return _stringResponse.getString();
375     }
376 
377     public String iconRefresh() throws Exception {
378         _stringResponse.recycle();
379 
380         IconRefreshTag.doTag(_servletContext, _request, _stringResponse);
381 
382         return _stringResponse.getString();
383     }
384 
385     public String iconRefresh(String page) throws Exception {
386         _stringResponse.recycle();
387 
388         IconRefreshTag.doTag(page, _servletContext, _request, _stringResponse);
389 
390         return _stringResponse.getString();
391     }
392 
393     public String include(String page) throws Exception {
394         _stringResponse.recycle();
395 
396         RequestDispatcher requestDispatcher =
397             _servletContext.getRequestDispatcher(page);
398 
399         requestDispatcher.include(_request, _stringResponse);
400 
401         return _stringResponse.getString();
402     }
403 
404     public String include(ServletContext servletContext, String page)
405         throws Exception {
406 
407         _stringResponse.recycle();
408 
409         RequestDispatcher requestDispatcher =
410             servletContext.getRequestDispatcher(page);
411 
412         requestDispatcher.include(_request, _stringResponse);
413 
414         return _stringResponse.getString();
415     }
416 
417     public String journalContentSearch() throws Exception {
418         _stringResponse.recycle();
419 
420         JournalContentSearchTag.doTag(
421             _servletContext, _request, _stringResponse);
422 
423         return _stringResponse.getString();
424     }
425 
426     public String language() throws Exception {
427         _stringResponse.recycle();
428 
429         LanguageTag.doTag(_servletContext, _request, _stringResponse);
430 
431         return _stringResponse.getString();
432     }
433 
434     public String language(
435             String formName, String formAction, String name, int displayStyle)
436         throws Exception {
437 
438         _stringResponse.recycle();
439 
440         LanguageTag.doTag(
441             formName, formAction, name, null, displayStyle, _servletContext,
442             _request, _stringResponse);
443 
444         return _stringResponse.getString();
445     }
446 
447     public String language(
448             String formName, String formAction, String name,
449             String[] languageIds, int displayStyle)
450         throws Exception {
451 
452         _stringResponse.recycle();
453 
454         LanguageTag.doTag(
455             formName, formAction, name, languageIds, displayStyle,
456             _servletContext, _request, _stringResponse);
457 
458         return _stringResponse.getString();
459     }
460 
461     public String language(
462             String page, String formName, String formAction, String name,
463             int displayStyle)
464         throws Exception {
465 
466         _stringResponse.recycle();
467 
468         LanguageTag.doTag(
469             page, formName, formAction, name, null, displayStyle,
470             _servletContext, _request, _stringResponse);
471 
472         return _stringResponse.getString();
473     }
474 
475     public String language(
476             String page, String formName, String formAction, String name,
477             String[] languageIds, int displayStyle)
478         throws Exception {
479 
480         _stringResponse.recycle();
481 
482         LanguageTag.doTag(
483             page, formName, formAction, name, languageIds, displayStyle,
484             _servletContext, _request, _stringResponse);
485 
486         return _stringResponse.getString();
487     }
488 
489     public String layoutIcon(Layout layout) throws Exception {
490         _stringResponse.recycle();
491 
492         LayoutIconTag.doTag(layout, _servletContext, _request, _stringResponse);
493 
494         return _stringResponse.getString();
495     }
496 
497     public String metaTags() throws Exception {
498         _stringResponse.recycle();
499 
500         MetaTagsTag.doTag(_servletContext, _request, _stringResponse);
501 
502         return _stringResponse.getString();
503     }
504 
505     public String myPlaces() throws Exception {
506         _stringResponse.recycle();
507 
508         MyPlacesTag.doTag(_servletContext, _request, _stringResponse);
509 
510         return _stringResponse.getString();
511     }
512 
513     public String myPlaces(int max) throws Exception {
514         _stringResponse.recycle();
515 
516         MyPlacesTag.doTag(max, _servletContext, _request, _stringResponse);
517 
518         return _stringResponse.getString();
519     }
520 
521     public String permissionsURL(
522             String redirect, String modelResource,
523             String modelResourceDescription, String resourcePrimKey)
524         throws Exception {
525 
526         return PermissionsURLTag.doTag(
527             redirect, modelResource, modelResourceDescription, resourcePrimKey,
528             null, false, _pageContext);
529     }
530 
531     public String pngImage(String image, String height, String width)
532         throws Exception {
533 
534         _stringResponse.recycle();
535 
536         PngImageTag.doTag(image, height, width, _servletContext, _request,
537             _stringResponse);
538 
539         return _stringResponse.getString();
540     }
541 
542     public String renderURL(String portletName, String queryString)
543         throws Exception {
544 
545         return renderURL(
546             LayoutConstants.DEFAULT_PLID, portletName, queryString);
547     }
548 
549     public String renderURL(long plid, String portletName, String queryString)
550         throws Exception {
551 
552         String windowState = WindowState.NORMAL.toString();
553         String portletMode = PortletMode.VIEW.toString();
554 
555         return renderURL(
556             windowState, portletMode, plid, portletName, queryString);
557     }
558 
559     public String renderURL(
560             String windowState, String portletMode, String portletName,
561             String queryString)
562         throws Exception {
563 
564         return renderURL(
565             windowState, portletMode, LayoutConstants.DEFAULT_PLID, portletName,
566             queryString);
567     }
568 
569     public String renderURL(
570             String windowState, String portletMode, long plid,
571             String portletName, String queryString)
572         throws Exception {
573 
574         Boolean secure = null;
575         Boolean copyCurrentRenderParameters = null;
576         Boolean escapeXml = null;
577         Boolean anchor = null;
578         Boolean encrypt = null;
579         long doAsUserId = 0;
580         Boolean portletConfiguration = null;
581 
582         return renderURL(
583             windowState, portletMode, secure, copyCurrentRenderParameters,
584             escapeXml, plid, portletName, anchor, encrypt, doAsUserId,
585             portletConfiguration, queryString);
586     }
587 
588     public String renderURL(
589             String windowState, String portletMode, Boolean secure,
590             Boolean copyCurrentRenderParameters, Boolean escapeXml,
591             long plid, String portletName, Boolean anchor, Boolean encrypt,
592             long doAsUserId, Boolean portletConfiguration, String queryString)
593         throws Exception {
594 
595         String var = null;
596         String varImpl = null;
597         String name = null;
598         String resourceID = null;
599         String cacheability = null;
600         Map<String, String[]> params = HttpUtil.parameterMapFromString(
601             queryString);
602         boolean writeOutput = false;
603 
604         return ActionURLTag.doTag(
605             PortletRequest.RENDER_PHASE, windowState, portletMode, var, varImpl,
606             secure, copyCurrentRenderParameters, escapeXml, name, resourceID,
607             cacheability, plid, portletName, anchor, encrypt, doAsUserId,
608             portletConfiguration, params, writeOutput, _pageContext);
609     }
610 
611     public String runtime(String portletName)
612         throws Exception {
613 
614         return runtime(portletName, null);
615     }
616 
617     public String runtime(String portletName, String queryString)
618         throws Exception {
619 
620         _stringResponse.recycle();
621 
622         RuntimeTag.doTag(
623             portletName, queryString, null, _servletContext, _request,
624             _stringResponse);
625 
626         return _stringResponse.getString();
627     }
628 
629     public String runtime(
630             String portletName, String queryString, String defaultPreferences)
631         throws Exception {
632 
633         _stringResponse.recycle();
634 
635         RuntimeTag.doTag(
636             portletName, queryString, defaultPreferences, null, _servletContext,
637             _request, _stringResponse);
638 
639         return _stringResponse.getString();
640     }
641 
642     public String search() throws Exception {
643         _stringResponse.recycle();
644 
645         SearchTag.doTag(_servletContext, _request, _stringResponse);
646 
647         return _stringResponse.getString();
648     }
649 
650     public String staging() throws Exception {
651         _stringResponse.recycle();
652 
653         StagingTag.doTag(_servletContext, _request, _stringResponse);
654 
655         return _stringResponse.getString();
656     }
657 
658     public String toggle(
659             String id, String showImage, String hideImage, String showMessage,
660             String hideMessage, boolean defaultShowContent)
661         throws Exception {
662 
663         _stringResponse.recycle();
664 
665         ToggleTag.doTag(
666             id, showImage, hideImage, showMessage, hideMessage,
667             defaultShowContent, null, _servletContext, _request,
668             _stringResponse);
669 
670         return _stringResponse.getString();
671     }
672 
673     public String wrapPortlet(String wrapPage, String portletPage)
674         throws Exception {
675 
676         _stringResponse.recycle();
677 
678         return WrapPortletTag.doTag(
679             wrapPage, portletPage, _servletContext, _request, _stringResponse,
680             _pageContext);
681     }
682 
683     private ServletContext _servletContext;
684     private HttpServletRequest _request;
685     private StringServletResponse _stringResponse;
686     private PageContext _pageContext;
687 
688 }