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