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