1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class TemplateProcessor {
43  
44      public TemplateProcessor(
45          ServletContext servletContext, HttpServletRequest request,
46          HttpServletResponse response, String portletId) {
47  
48          _servletContext = servletContext;
49          _request = request;
50          _response = response;
51          _portletId = portletId;
52          _columnsMap = new HashMap<String, String>();
53          _portletsMap = new TreeMap<Portlet, Object[]>(
54              new PortletRenderWeightComparator());
55      }
56  
57      public String processColumn(String columnId) throws Exception {
58          Map<String, String> attributes = new HashMap<String, String>();
59  
60          attributes.put("id", columnId);
61  
62          PortletColumnLogic logic = new PortletColumnLogic(
63              _servletContext, _request, _response);
64  
65          StringBuilder sb = new StringBuilder();
66  
67          logic.processContent(sb, attributes);
68  
69          _portletsMap.putAll(logic.getPortletsMap());
70  
71          String columnIdPlaceHolder = "[$TEMPLATE_COLUMN_" + columnId + "$]";
72  
73          _columnsMap.put(columnIdPlaceHolder, sb.toString());
74  
75          return columnIdPlaceHolder;
76      }
77  
78      public String processMax() throws Exception {
79          RuntimeLogic logic = new PortletLogic(
80              _servletContext, _request, _response, _portletId);
81  
82          StringBuilder sb = new StringBuilder();
83  
84          logic.processContent(sb, new HashMap<String, String>());
85  
86          return sb.toString();
87      }
88  
89      public String processPortlet(String portletId) throws Exception {
90          RuntimeLogic logic = new PortletLogic(
91              _servletContext, _request, _response, portletId);
92  
93          StringBuilder sb = new StringBuilder();
94  
95          logic.processContent(sb, new HashMap<String, String>());
96  
97          return sb.toString();
98      }
99  
100     public Map<String, String> getColumnsMap() {
101         return _columnsMap;
102     }
103 
104     public Map<Portlet, Object[]> getPortletsMap() {
105         return _portletsMap;
106     }
107 
108     private ServletContext _servletContext;
109     private HttpServletRequest _request;
110     private HttpServletResponse _response;
111     private String _portletId;
112     private Map<String, String> _columnsMap;
113     private Map<Portlet, Object[]> _portletsMap;
114 
115 }