1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.layoutconfiguration.util.xml;
21  
22  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
23  import com.liferay.portal.kernel.xml.Document;
24  import com.liferay.portal.kernel.xml.Element;
25  import com.liferay.portal.kernel.xml.SAXReaderUtil;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portlet.RenderResponseImpl;
28  
29  import javax.portlet.PortletRequest;
30  import javax.portlet.RenderResponse;
31  
32  /**
33   * <a href="ActionURLLogic.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class ActionURLLogic extends RuntimeLogic {
39  
40      public static final String OPEN_TAG = "<runtime-action-url";
41  
42      public static final String CLOSE_1_TAG = "</runtime-action-url>";
43  
44      public static final String CLOSE_2_TAG = "/>";
45  
46      public ActionURLLogic(RenderResponse renderResponse) {
47          _renderResponseImpl = (RenderResponseImpl)renderResponse;
48      }
49  
50      public String getOpenTag() {
51          return OPEN_TAG;
52      }
53  
54      public String getClose1Tag() {
55          return CLOSE_1_TAG;
56      }
57  
58      public void processXML(StringBuilder sb, String xml) throws Exception {
59          Document doc = SAXReaderUtil.read(xml);
60  
61          Element root = doc.getRootElement();
62  
63          LiferayPortletURL portletURL = _renderResponseImpl.createPortletURLImpl(
64              getLifecycle());
65  
66          String portletId = root.attributeValue("portlet-name");
67  
68          if (portletId != null) {
69              portletId = PortalUtil.getJsSafePortletId(portletId);
70  
71              portletURL.setPortletId(portletId);
72          }
73  
74          for (int i = 1;; i++) {
75              String paramName = root.attributeValue("param-name-" + i);
76              String paramValue = root.attributeValue("param-value-" + i);
77  
78              if ((paramName == null) || (paramValue == null)) {
79                  break;
80              }
81  
82              portletURL.setParameter(paramName, paramValue);
83          }
84  
85          sb.append(portletURL.toString());
86      }
87  
88      public String getLifecycle() {
89          return _lifecycle;
90      }
91  
92      private RenderResponseImpl _renderResponseImpl;
93      private String _lifecycle = PortletRequest.ACTION_PHASE;
94  
95  }