001    /**
002     * Copyright (c) 2000-2013 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.portlet.layoutconfiguration.util.xml;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
018    import com.liferay.portal.kernel.xml.Document;
019    import com.liferay.portal.kernel.xml.Element;
020    import com.liferay.portal.kernel.xml.SAXReaderUtil;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portlet.RenderResponseImpl;
023    
024    import javax.portlet.PortletRequest;
025    import javax.portlet.RenderResponse;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class ActionURLLogic extends RuntimeLogic {
031    
032            public static final String CLOSE_1_TAG = "</runtime-action-url>";
033    
034            public static final String CLOSE_2_TAG = "/>";
035    
036            public static final String OPEN_TAG = "<runtime-action-url";
037    
038            public ActionURLLogic(RenderResponse renderResponse) {
039                    _renderResponseImpl = (RenderResponseImpl)renderResponse;
040            }
041    
042            @Override
043            public String getClose1Tag() {
044                    return CLOSE_1_TAG;
045            }
046    
047            public String getLifecycle() {
048                    return _lifecycle;
049            }
050    
051            @Override
052            public String getOpenTag() {
053                    return OPEN_TAG;
054            }
055    
056            @Override
057            public String processXML(String xml) throws Exception {
058                    Document doc = SAXReaderUtil.read(xml);
059    
060                    Element root = doc.getRootElement();
061    
062                    LiferayPortletURL liferayPortletURL =
063                            _renderResponseImpl.createLiferayPortletURL(getLifecycle());
064    
065                    String portletId = root.attributeValue("portlet-name");
066    
067                    if (portletId != null) {
068                            portletId = PortalUtil.getJsSafePortletId(portletId);
069    
070                            liferayPortletURL.setPortletId(portletId);
071                    }
072    
073                    for (int i = 1;; i++) {
074                            String paramName = root.attributeValue("param-name-" + i);
075                            String paramValue = root.attributeValue("param-value-" + i);
076    
077                            if ((paramName == null) || (paramValue == null)) {
078                                    break;
079                            }
080    
081                            liferayPortletURL.setParameter(paramName, paramValue);
082                    }
083    
084                    return liferayPortletURL.toString();
085            }
086    
087            private String _lifecycle = PortletRequest.ACTION_PHASE;
088            private RenderResponseImpl _renderResponseImpl;
089    
090    }