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.aui;
16  
17  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
18  import com.liferay.portal.kernel.util.HtmlUtil;
19  import com.liferay.portal.kernel.util.ServerDetector;
20  import com.liferay.portal.kernel.util.Validator;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.portlet.PortletURL;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.jsp.JspException;
29  import javax.servlet.jsp.tagext.DynamicAttributes;
30  import javax.servlet.jsp.tagext.TagSupport;
31  
32  /**
33   * <a href="FormTag.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Julio Camarero
36   * @author Jorge Ferrer
37   * @author Brian Wing Shun Chan
38   */
39  public class FormTag extends TagSupport implements DynamicAttributes {
40  
41      public int doEndTag() throws JspException {
42          try{
43              PortalIncludeUtil.include(pageContext, getEndPage());
44  
45              return EVAL_PAGE;
46          }
47          catch (Exception e) {
48              throw new JspException(e);
49          }
50          finally {
51              if (!ServerDetector.isResin()) {
52                  _action = null;
53                  _cssClass = null;
54                  _dynamicAttributes.clear();
55                  _endPage = null;
56                  _escapeXml = true;
57                  _inlineLabel = false;
58                  _name = "fm";
59                  _onSubmit = null;
60                  _startPage = null;
61              }
62          }
63      }
64  
65      public int doStartTag() throws JspException{
66          try{
67              HttpServletRequest request =
68                  (HttpServletRequest)pageContext.getRequest();
69  
70              if (_escapeXml) {
71                  _action = HtmlUtil.escape(_action);
72              }
73  
74              request.setAttribute("aui:form:action", _action);
75              request.setAttribute("aui:form:cssClass", _cssClass);
76              request.setAttribute(
77                  "aui:form:dynamicAttributes", _dynamicAttributes);
78              request.setAttribute(
79                  "aui:form:inlineLabel", String.valueOf(_inlineLabel));
80              request.setAttribute("aui:form:name", _name);
81              request.setAttribute("aui:form:onSubmit", _onSubmit);
82  
83              PortalIncludeUtil.include(pageContext, getStartPage());
84  
85              return EVAL_BODY_INCLUDE;
86          }
87          catch (Exception e) {
88              throw new JspException(e);
89          }
90      }
91  
92      public String getEndPage() {
93          if (Validator.isNull(_endPage)) {
94              return _END_PAGE;
95          }
96          else {
97              return _endPage;
98          }
99      }
100 
101     public String getStartPage() {
102         if (Validator.isNull(_startPage)) {
103             return _START_PAGE;
104         }
105         else {
106             return _startPage;
107         }
108     }
109 
110     public void setAction(PortletURL portletURL) {
111         if (portletURL != null) {
112             _action = portletURL.toString();
113         }
114     }
115 
116     public void setAction(String action) {
117         _action = action;
118     }
119 
120     public void setCssClass(String cssClass) {
121         _cssClass = cssClass;
122     }
123 
124     public void setDynamicAttribute(
125         String uri, String localName, Object value) {
126 
127         _dynamicAttributes.put(localName, value);
128     }
129 
130     public void setEndPage(String endPage) {
131         _endPage = endPage;
132     }
133 
134     public void setEscapeXml(boolean escapeXml) {
135         _escapeXml = escapeXml;
136     }
137 
138     public void setInlineLabel(boolean inlineLabel) {
139         _inlineLabel = inlineLabel;
140     }
141 
142     public void setName(String name) {
143         _name = name;
144     }
145 
146     public void setOnSubmit(String onSubmit) {
147         _onSubmit = onSubmit;
148     }
149 
150     public void setStartPage(String startPage) {
151         _startPage = startPage;
152     }
153 
154     private static final String _END_PAGE = "/html/taglib/aui/form/end.jsp";
155 
156     private static final String _START_PAGE = "/html/taglib/aui/form/start.jsp";
157 
158     private String _action;
159     private String _cssClass;
160     private Map<String, Object> _dynamicAttributes =
161         new HashMap<String, Object>();
162     private String _endPage;
163     private boolean _escapeXml = true;
164     private boolean _inlineLabel;
165     private String _name = "fm";
166     private String _onSubmit;
167     private String _startPage;
168 
169 }