1   /**
2    * Copyright (c) 2000-2009 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.ui;
24  
25  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.HtmlUtil;
28  import com.liferay.portal.kernel.util.ServerDetector;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  
32  import java.util.List;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.jsp.JspException;
36  import javax.servlet.jsp.tagext.TagSupport;
37  
38  /**
39   * <a href="TableIteratorTag.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class TableIteratorTag extends TagSupport {
45  
46      public int doStartTag() throws JspException {
47          try {
48              if (_list.size() > 0) {
49                  HttpServletRequest request =
50                      (HttpServletRequest)pageContext.getRequest();
51  
52                  request.setAttribute("liferay-ui:table-iterator:list", _list);
53                  request.setAttribute(
54                      "liferay-ui:table-iterator:rowLength",
55                      String.valueOf(_rowLength));
56                  request.setAttribute(
57                      "liferay-ui:table-iterator:rowPadding", _rowPadding);
58                  request.setAttribute(
59                      "liferay-ui:table-iterator:rowValign", _rowValign);
60                  request.setAttribute(
61                      "liferay-ui:table-iterator:rowBreak", _rowBreak);
62                  request.setAttribute("liferay-ui:table-iterator:width", _width);
63  
64                  PortalIncludeUtil.include(pageContext, getStartPage());
65  
66                  pageContext.setAttribute(
67                      "tableIteratorObj", _list.get(_listPos));
68                  pageContext.setAttribute(
69                      "tableIteratorPos", new Integer(_listPos));
70  
71                  return EVAL_BODY_INCLUDE;
72              }
73              else {
74                  return SKIP_BODY;
75              }
76          }
77          catch (Exception e) {
78              throw new JspException(e);
79          }
80      }
81  
82      public int doAfterBody() throws JspException {
83          try {
84              HttpServletRequest request =
85                  (HttpServletRequest)pageContext.getRequest();
86  
87              request.setAttribute(
88                  "liferay-ui:table-iterator:listPos", String.valueOf(_listPos));
89  
90              PortalIncludeUtil.include(pageContext, getBodyPage());
91  
92              _listPos++;
93  
94              if (_listPos < _list.size()) {
95                  pageContext.setAttribute(
96                      "tableIteratorObj", _list.get(_listPos));
97                  pageContext.setAttribute(
98                      "tableIteratorPos", new Integer(_listPos));
99  
100                 return EVAL_BODY_AGAIN;
101             }
102             else {
103                 return SKIP_BODY;
104             }
105         }
106         catch (Exception e) {
107             throw new JspException(e);
108         }
109     }
110 
111     public int doEndTag() throws JspException {
112         try {
113             if (_list.size() > 0) {
114                 PortalIncludeUtil.include(pageContext, getEndPage());
115             }
116 
117             return EVAL_PAGE;
118         }
119         catch (Exception e) {
120             throw new JspException(e);
121         }
122         finally {
123             if (!ServerDetector.isResin()) {
124                 _startPage = null;
125                 _bodyPage = null;
126                 _endPage = null;
127                 _list = null;
128                 _listPos = 0;
129                 _rowLength = 0;
130                 _rowPadding = "0";
131                 _rowValign = "middle";
132                 _rowBreak = null;
133             }
134         }
135     }
136 
137     public String getStartPage() {
138         if (Validator.isNull(_startPage)) {
139             return _START_PAGE;
140         }
141         else {
142             return _startPage;
143         }
144     }
145 
146     public void setStartPage(String startPage) {
147         _startPage = startPage;
148     }
149 
150     public String getBodyPage() {
151         if (Validator.isNull(_bodyPage)) {
152             return _BODY_PAGE;
153         }
154         else {
155             return _bodyPage;
156         }
157     }
158 
159     public void setBodyPage(String bodyPage) {
160         _bodyPage = bodyPage;
161     }
162 
163     public String getEndPage() {
164         if (Validator.isNull(_endPage)) {
165             return _END_PAGE;
166         }
167         else {
168             return _endPage;
169         }
170     }
171 
172     public void setEndPage(String endPage) {
173         _endPage = endPage;
174     }
175 
176     public void setList(List<?> list) {
177         _list = list;
178     }
179 
180     public void setListType(String listType) {
181     }
182 
183     public void setRowLength(String rowLength) {
184         _rowLength = GetterUtil.getInteger(rowLength);
185     }
186 
187     public void setRowPadding(String rowPadding) {
188         _rowPadding = rowPadding;
189     }
190 
191     public void setRowValign(String rowValign) {
192         _rowValign = rowValign;
193     }
194 
195     public void setRowBreak(String rowBreak) {
196         _rowBreak = HtmlUtil.unescape(rowBreak);
197     }
198 
199     public void setWidth(String width) {
200         _width = width;
201     }
202 
203     private static final String _START_PAGE =
204         "/html/taglib/ui/table_iterator/start.jsp";
205 
206     private static final String _BODY_PAGE =
207         "/html/taglib/ui/table_iterator/body.jsp";
208 
209     private static final String _END_PAGE =
210         "/html/taglib/ui/table_iterator/end.jsp";
211 
212     private String _startPage;
213     private String _bodyPage;
214     private String _endPage;
215     private List<?> _list;
216     private int _listPos;
217     private int _rowLength;
218     private String _rowPadding = "0";
219     private String _rowValign = "middle";
220     private String _rowBreak = "<br />";
221     private String _width = StringPool.BLANK;
222 
223 }