1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.layoutconfiguration.util.xml;
16  
17  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
18  import com.liferay.portal.kernel.xml.Document;
19  import com.liferay.portal.kernel.xml.Element;
20  import com.liferay.portal.kernel.xml.SAXReaderUtil;
21  import com.liferay.portal.util.PortalUtil;
22  import com.liferay.portlet.RenderResponseImpl;
23  
24  import javax.portlet.PortletRequest;
25  import javax.portlet.RenderResponse;
26  
27  /**
28   * <a href="ActionURLLogic.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class ActionURLLogic extends RuntimeLogic {
33  
34      public static final String OPEN_TAG = "<runtime-action-url";
35  
36      public static final String CLOSE_1_TAG = "</runtime-action-url>";
37  
38      public static final String CLOSE_2_TAG = "/>";
39  
40      public ActionURLLogic(RenderResponse renderResponse) {
41          _renderResponseImpl = (RenderResponseImpl)renderResponse;
42      }
43  
44      public String getOpenTag() {
45          return OPEN_TAG;
46      }
47  
48      public String getClose1Tag() {
49          return CLOSE_1_TAG;
50      }
51  
52      public void processXML(StringBuilder sb, String xml) throws Exception {
53          Document doc = SAXReaderUtil.read(xml);
54  
55          Element root = doc.getRootElement();
56  
57          LiferayPortletURL portletURL =
58              _renderResponseImpl.createLiferayPortletURL(getLifecycle());
59  
60          String portletId = root.attributeValue("portlet-name");
61  
62          if (portletId != null) {
63              portletId = PortalUtil.getJsSafePortletId(portletId);
64  
65              portletURL.setPortletId(portletId);
66          }
67  
68          for (int i = 1;; i++) {
69              String paramName = root.attributeValue("param-name-" + i);
70              String paramValue = root.attributeValue("param-value-" + i);
71  
72              if ((paramName == null) || (paramValue == null)) {
73                  break;
74              }
75  
76              portletURL.setParameter(paramName, paramValue);
77          }
78  
79          sb.append(portletURL.toString());
80      }
81  
82      public String getLifecycle() {
83          return _lifecycle;
84      }
85  
86      private RenderResponseImpl _renderResponseImpl;
87      private String _lifecycle = PortletRequest.ACTION_PHASE;
88  
89  }