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