001    /**
002     * Copyright (c) 2000-2010 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 OPEN_TAG = "<runtime-action-url";
033    
034            public static final String CLOSE_1_TAG = "</runtime-action-url>";
035    
036            public static final String CLOSE_2_TAG = "/>";
037    
038            public ActionURLLogic(RenderResponse renderResponse) {
039                    _renderResponseImpl = (RenderResponseImpl)renderResponse;
040            }
041    
042            public String getOpenTag() {
043                    return OPEN_TAG;
044            }
045    
046            public String getClose1Tag() {
047                    return CLOSE_1_TAG;
048            }
049    
050            public String processXML(String xml) throws Exception {
051                    Document doc = SAXReaderUtil.read(xml);
052    
053                    Element root = doc.getRootElement();
054    
055                    LiferayPortletURL liferayPortletURL =
056                            _renderResponseImpl.createLiferayPortletURL(getLifecycle());
057    
058                    String portletId = root.attributeValue("portlet-name");
059    
060                    if (portletId != null) {
061                            portletId = PortalUtil.getJsSafePortletId(portletId);
062    
063                            liferayPortletURL.setPortletId(portletId);
064                    }
065    
066                    for (int i = 1;; i++) {
067                            String paramName = root.attributeValue("param-name-" + i);
068                            String paramValue = root.attributeValue("param-value-" + i);
069    
070                            if ((paramName == null) || (paramValue == null)) {
071                                    break;
072                            }
073    
074                            liferayPortletURL.setParameter(paramName, paramValue);
075                    }
076    
077                    return liferayPortletURL.toString();
078            }
079    
080            public String getLifecycle() {
081                    return _lifecycle;
082            }
083    
084            private RenderResponseImpl _renderResponseImpl;
085            private String _lifecycle = PortletRequest.ACTION_PHASE;
086    
087    }