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