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