1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.ui;
16  
17  import com.liferay.portal.kernel.servlet.StringServletResponse;
18  import com.liferay.portal.kernel.util.Validator;
19  import com.liferay.taglib.util.IncludeTag;
20  
21  import javax.servlet.RequestDispatcher;
22  import javax.servlet.ServletContext;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import javax.servlet.jsp.JspException;
26  
27  /**
28   * <a href="WriteTag.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class WriteTag extends IncludeTag {
33  
34      public static void doTag(
35              Object bean, String property, ServletContext servletContext,
36              HttpServletRequest request, HttpServletResponse response)
37          throws Exception {
38  
39          doTag(_PAGE, bean, property, servletContext, request, response);
40      }
41  
42      public static void doTag(
43              String page, Object bean, String property,
44              ServletContext servletContext, HttpServletRequest request,
45              HttpServletResponse response)
46          throws Exception {
47  
48          if ((bean == null) || Validator.isNull(property)) {
49              return;
50          }
51  
52          request.setAttribute("liferay-ui:write:bean", bean);
53          request.setAttribute("liferay-ui:write:property", property);
54  
55          RequestDispatcher requestDispatcher =
56              servletContext.getRequestDispatcher(page);
57  
58          requestDispatcher.include(request, response);
59      }
60  
61      public int doEndTag() throws JspException {
62          try {
63              ServletContext servletContext = getServletContext();
64              HttpServletRequest request = getServletRequest();
65              StringServletResponse stringResponse = getServletResponse();
66  
67              doTag(
68                  getPage(), _bean, _property, servletContext, request,
69                  stringResponse);
70  
71              pageContext.getOut().print(stringResponse.getString());
72  
73              return EVAL_PAGE;
74          }
75          catch (Exception e) {
76              throw new JspException(e);
77          }
78      }
79  
80      public void setBean(Object bean) {
81          _bean = bean;
82      }
83  
84      public void setProperty(String property) {
85          _property = property;
86      }
87  
88      protected String getDefaultPage() {
89          return _PAGE;
90      }
91  
92      private static final String _PAGE = "/html/taglib/ui/write/page.jsp";
93  
94      private Object _bean;
95      private String _property;
96  
97  }