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