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.xml;
24  
25  import com.liferay.portal.kernel.util.StringMaker;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.impl.PortletImpl;
28  import com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil;
29  
30  import java.io.StringReader;
31  
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  
35  import javax.servlet.ServletContext;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  
39  import org.dom4j.Document;
40  import org.dom4j.Element;
41  import org.dom4j.io.SAXReader;
42  
43  /**
44   * <a href="PortletLogic.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class PortletLogic extends RuntimeLogic {
50  
51      public static final String OPEN_TAG = "<runtime-portlet";
52  
53      public static final String CLOSE_1_TAG = "</runtime-portlet>";
54  
55      public static final String CLOSE_2_TAG = "/>";
56  
57      public PortletLogic(ServletContext ctx, HttpServletRequest req,
58                          HttpServletResponse res, RenderRequest renderRequest,
59                          RenderResponse renderResponse) {
60  
61          _ctx = ctx;
62          _req = req;
63          _res = res;
64          _renderRequest = renderRequest;
65          _renderResponse = renderResponse;
66      }
67  
68      public String getOpenTag() {
69          return OPEN_TAG;
70      }
71  
72      public String getClose1Tag() {
73          return CLOSE_1_TAG;
74      }
75  
76      public void processXML(StringMaker sm, String xml) throws Exception {
77          SAXReader reader = new SAXReader();
78  
79          Document doc = reader.read(new StringReader(xml));
80  
81          Element root = doc.getRootElement();
82  
83          String rootPortletId = root.attributeValue("name");
84          String instanceId = root.attributeValue("instance");
85          String queryString = root.attributeValue("queryString");
86  
87          String portletId = rootPortletId;
88  
89          if (Validator.isNotNull(instanceId)) {
90              portletId += PortletImpl.INSTANCE_SEPARATOR + instanceId;
91          }
92  
93          RuntimePortletUtil.processPortlet(
94              sm, _ctx, _req, _res, _renderRequest, _renderResponse, portletId,
95              queryString);
96      }
97  
98      private ServletContext _ctx;
99      private HttpServletRequest _req;
100     private HttpServletResponse _res;
101     private RenderRequest _renderRequest;
102     private RenderResponse _renderResponse;
103 
104 }