001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.layoutconfiguration.util.xml;
016    
017    import com.liferay.portal.kernel.layoutconfiguration.util.xml.RuntimeLogic;
018    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
019    import com.liferay.portal.kernel.util.PortalUtil;
020    import com.liferay.portal.kernel.xml.Document;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.kernel.xml.SAXReaderUtil;
023    import com.liferay.portlet.RenderResponseImpl;
024    
025    import javax.portlet.PortletRequest;
026    import javax.portlet.RenderResponse;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class ActionURLLogic extends RuntimeLogic {
032    
033            public static final String CLOSE_1_TAG = "</runtime-action-url>";
034    
035            public static final String CLOSE_2_TAG = "/>";
036    
037            public static final String OPEN_TAG = "<runtime-action-url";
038    
039            public ActionURLLogic(RenderResponse renderResponse) {
040                    _renderResponseImpl = (RenderResponseImpl)renderResponse;
041            }
042    
043            @Override
044            public String getClose1Tag() {
045                    return CLOSE_1_TAG;
046            }
047    
048            public String getLifecycle() {
049                    return _LIFECYCLE;
050            }
051    
052            @Override
053            public String getOpenTag() {
054                    return OPEN_TAG;
055            }
056    
057            @Override
058            public String processXML(String xml) throws Exception {
059                    Document doc = SAXReaderUtil.read(xml);
060    
061                    Element root = doc.getRootElement();
062    
063                    LiferayPortletURL liferayPortletURL =
064                            _renderResponseImpl.createLiferayPortletURL(getLifecycle());
065    
066                    String portletId = root.attributeValue("portlet-name");
067    
068                    if (portletId != null) {
069                            portletId = PortalUtil.getJsSafePortletId(portletId);
070    
071                            liferayPortletURL.setPortletId(portletId);
072                    }
073    
074                    for (int i = 1;; i++) {
075                            String paramName = root.attributeValue("param-name-" + i);
076                            String paramValue = root.attributeValue("param-value-" + i);
077    
078                            if ((paramName == null) || (paramValue == null)) {
079                                    break;
080                            }
081    
082                            liferayPortletURL.setParameter(paramName, paramValue);
083                    }
084    
085                    return liferayPortletURL.toString();
086            }
087    
088            private static final String _LIFECYCLE = PortletRequest.ACTION_PHASE;
089    
090            private final RenderResponseImpl _renderResponseImpl;
091    
092    }