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.portlet.layoutconfiguration.util.velocity;
24  
25  import com.liferay.portal.model.Portlet;
26  import com.liferay.portal.util.comparator.PortletRenderWeightComparator;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  import java.util.TreeMap;
31  
32  import javax.servlet.ServletContext;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  /**
37   * <a href="TemplateProcessor.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Ivica Cardic
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class TemplateProcessor {
44  
45      public TemplateProcessor(
46          ServletContext servletContext, HttpServletRequest request,
47          HttpServletResponse response, String portletId) {
48  
49          _servletContext = servletContext;
50          _request = request;
51          _response = response;
52          _portletId = portletId;
53          _columnsMap = new HashMap<String, String>();
54          _portletsMap = new TreeMap<Portlet, Object[]>(
55              new PortletRenderWeightComparator());
56      }
57  
58      public String processColumn(String columnId) throws Exception {
59          Map<String, String> attributes = new HashMap<String, String>();
60  
61          attributes.put("id", columnId);
62  
63          PortletColumnLogic logic = new PortletColumnLogic(
64              _servletContext, _request, _response);
65  
66          StringBuilder sb = new StringBuilder();
67  
68          logic.processContent(sb, attributes);
69  
70          _portletsMap.putAll(logic.getPortletsMap());
71  
72          String columnIdPlaceHolder = "[$TEMPLATE_COLUMN_" + columnId + "$]";
73  
74          _columnsMap.put(columnIdPlaceHolder, sb.toString());
75  
76          return columnIdPlaceHolder;
77      }
78  
79      public String processMax() throws Exception {
80          RuntimeLogic logic = new PortletLogic(
81              _servletContext, _request, _response, _portletId);
82  
83          StringBuilder sb = new StringBuilder();
84  
85          logic.processContent(sb, new HashMap<String, String>());
86  
87          return sb.toString();
88      }
89  
90      public String processPortlet(String portletId) throws Exception {
91          RuntimeLogic logic = new PortletLogic(
92              _servletContext, _request, _response, portletId);
93  
94          StringBuilder sb = new StringBuilder();
95  
96          logic.processContent(sb, new HashMap<String, String>());
97  
98          return sb.toString();
99      }
100 
101     public Map<String, String> getColumnsMap() {
102         return _columnsMap;
103     }
104 
105     public Map<Portlet, Object[]> getPortletsMap() {
106         return _portletsMap;
107     }
108 
109     private ServletContext _servletContext;
110     private HttpServletRequest _request;
111     private HttpServletResponse _response;
112     private String _portletId;
113     private Map<String, String> _columnsMap;
114     private Map<Portlet, Object[]> _portletsMap;
115 
116 }