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.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 ctx, HttpServletRequest req, HttpServletResponse res,
47          String portletId) {
48  
49          _ctx = ctx;
50          _req = req;
51          _res = res;
52          _portletId = portletId;
53          _columnsMap = new HashMap();
54          _portletsMap = new TreeMap(new PortletRenderWeightComparator());
55      }
56  
57      public String processColumn(String columnId) throws Exception {
58          Map attributes = new HashMap();
59  
60          attributes.put("id", columnId);
61  
62          PortletColumnLogic logic = new PortletColumnLogic(_ctx, _req, _res);
63  
64          StringMaker sm = new StringMaker();
65  
66          logic.processContent(sm, attributes);
67  
68          _portletsMap.putAll(logic.getPortletsMap());
69  
70          String columnIdPlaceHolder = "[$TEMPLATE_COLUMN_" + columnId + "$]";
71  
72          _columnsMap.put(columnIdPlaceHolder, sm.toString());
73  
74          return columnIdPlaceHolder;
75      }
76  
77      public String processMax() throws Exception {
78          RuntimeLogic logic = new PortletLogic(_ctx, _req, _res, _portletId);
79  
80          StringMaker sm = new StringMaker();
81  
82          logic.processContent(sm, new HashMap());
83  
84          return sm.toString();
85      }
86  
87      public String processPortlet(String portletId) throws Exception {
88          RuntimeLogic logic = new PortletLogic(_ctx, _req, _res, portletId);
89  
90          StringMaker sm = new StringMaker();
91  
92          logic.processContent(sm, new HashMap());
93  
94          return sm.toString();
95      }
96  
97      public Map getColumnsMap() {
98          return _columnsMap;
99      }
100 
101     public Map getPortletsMap() {
102         return _portletsMap;
103     }
104 
105     private ServletContext _ctx;
106     private HttpServletRequest _req;
107     private HttpServletResponse _res;
108     private String _portletId;
109     private Map _columnsMap;
110     private Map _portletsMap;
111 
112 }