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.portlet.LiferayPortletURL;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portlet.RenderResponseImpl;
29  
30  import java.io.StringReader;
31  
32  import javax.portlet.PortletRequest;
33  import javax.portlet.RenderResponse;
34  
35  import org.dom4j.Document;
36  import org.dom4j.Element;
37  import org.dom4j.io.SAXReader;
38  
39  /**
40   * <a href="ActionURLLogic.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class ActionURLLogic extends RuntimeLogic {
46  
47      public static final String OPEN_TAG = "<runtime-action-url";
48  
49      public static final String CLOSE_1_TAG = "</runtime-action-url>";
50  
51      public static final String CLOSE_2_TAG = "/>";
52  
53      public ActionURLLogic(RenderResponse res) {
54          _res = (RenderResponseImpl)res;
55      }
56  
57      public String getOpenTag() {
58          return OPEN_TAG;
59      }
60  
61      public String getClose1Tag() {
62          return CLOSE_1_TAG;
63      }
64  
65      public void processXML(StringMaker sm, String xml) throws Exception {
66          SAXReader reader = new SAXReader();
67  
68          Document doc = reader.read(new StringReader(xml));
69  
70          Element root = doc.getRootElement();
71  
72          LiferayPortletURL portletURL = _res.createPortletURLImpl(
73              getLifecycle());
74  
75          String portletId = root.attributeValue("portlet-name");
76  
77          if (portletId != null) {
78              portletId = PortalUtil.getJsSafePortletId(portletId);
79  
80              portletURL.setPortletId(portletId);
81          }
82  
83          for (int i = 1;; i++) {
84              String paramName = root.attributeValue("param-name-" + i);
85              String paramValue = root.attributeValue("param-value-" + i);
86  
87              if ((paramName == null) || (paramValue == null))  {
88                  break;
89              }
90  
91              portletURL.setParameter(paramName, paramValue);
92          }
93  
94          sm.append(portletURL.toString());
95      }
96  
97      public String getLifecycle() {
98          return _lifecycle;
99      }
100 
101     private RenderResponseImpl _res;
102     private String _lifecycle = PortletRequest.ACTION_PHASE;
103 
104 }