1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.layoutconfiguration.util.velocity;
21  
22  import com.liferay.portal.model.Portlet;
23  import com.liferay.portal.util.comparator.PortletRenderWeightComparator;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.TreeMap;
28  
29  import javax.servlet.ServletContext;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  /**
34   * <a href="TemplateProcessor.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Ivica Cardic
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class TemplateProcessor {
41  
42      public TemplateProcessor(
43          ServletContext servletContext, HttpServletRequest request,
44          HttpServletResponse response, String portletId) {
45  
46          _servletContext = servletContext;
47          _request = request;
48          _response = response;
49          _portletId = portletId;
50          _columnsMap = new HashMap<String, String>();
51          _portletsMap = new TreeMap<Portlet, Object[]>(
52              new PortletRenderWeightComparator());
53      }
54  
55      public String processColumn(String columnId) throws Exception {
56          Map<String, String> attributes = new HashMap<String, String>();
57  
58          attributes.put("id", columnId);
59  
60          PortletColumnLogic logic = new PortletColumnLogic(
61              _servletContext, _request, _response);
62  
63          StringBuilder sb = new StringBuilder();
64  
65          logic.processContent(sb, attributes);
66  
67          _portletsMap.putAll(logic.getPortletsMap());
68  
69          String columnIdPlaceHolder = "[$TEMPLATE_COLUMN_" + columnId + "$]";
70  
71          _columnsMap.put(columnIdPlaceHolder, sb.toString());
72  
73          return columnIdPlaceHolder;
74      }
75  
76      public String processMax() throws Exception {
77          RuntimeLogic logic = new PortletLogic(
78              _servletContext, _request, _response, _portletId);
79  
80          StringBuilder sb = new StringBuilder();
81  
82          logic.processContent(sb, new HashMap<String, String>());
83  
84          return sb.toString();
85      }
86  
87      public String processPortlet(String portletId) throws Exception {
88          RuntimeLogic logic = new PortletLogic(
89              _servletContext, _request, _response, portletId);
90  
91          StringBuilder sb = new StringBuilder();
92  
93          logic.processContent(sb, new HashMap<String, String>());
94  
95          return sb.toString();
96      }
97  
98      public Map<String, String> getColumnsMap() {
99          return _columnsMap;
100     }
101 
102     public Map<Portlet, Object[]> getPortletsMap() {
103         return _portletsMap;
104     }
105 
106     private ServletContext _servletContext;
107     private HttpServletRequest _request;
108     private HttpServletResponse _response;
109     private String _portletId;
110     private Map<String, String> _columnsMap;
111     private Map<Portlet, Object[]> _portletsMap;
112 
113 }