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