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.wiki;
016    
017    import com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper;
018    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    /**
027     * @author Shinn Lok
028     * @author Levente Hud??k
029     */
030    public class WikiFriendlyURLMapper extends DefaultFriendlyURLMapper {
031    
032            @Override
033            public String buildPath(LiferayPortletURL liferayPortletURL) {
034                    Map<String, String> routeParameters = new HashMap<String, String>();
035    
036                    buildRouteParameters(liferayPortletURL, routeParameters);
037    
038                    addParameter(routeParameters, "nodeName");
039                    addParameter(routeParameters, "title");
040    
041                    String friendlyURLPath = router.parametersToUrl(routeParameters);
042    
043                    if (Validator.isNull(friendlyURLPath)) {
044                            return null;
045                    }
046    
047                    addParametersIncludedInPath(liferayPortletURL, routeParameters);
048    
049                    friendlyURLPath = StringPool.SLASH.concat(getMapping()).concat(
050                            friendlyURLPath);
051    
052                    return friendlyURLPath;
053            }
054    
055            protected void addParameter(
056                    Map<String, String> routeParameters, String name) {
057    
058                    if (!routeParameters.containsKey(name)) {
059                            return;
060                    }
061    
062                    String nodeName = routeParameters.get(name);
063    
064                    nodeName = StringUtil.replace(
065                            nodeName, _ESCAPED_CHARS, _UNESCAPED_CHARS);
066    
067                    routeParameters.put(name, nodeName);
068            }
069    
070            @Override
071            protected void populateParams(
072                    Map<String, String[]> parameterMap, String namespace,
073                    Map<String, String> routeParameters) {
074    
075                    addParameter(routeParameters, "nodeName");
076                    addParameter(routeParameters, "title");
077    
078                    super.populateParams(parameterMap, namespace, routeParameters);
079            }
080    
081            private static final String[] _ESCAPED_CHARS = new String[] {
082                    "<PLUS>", "<QUESTION>", "<SLASH>"
083            };
084    
085            private static final String[] _UNESCAPED_CHARS = new String[] {
086                    StringPool.PLUS, StringPool.QUESTION, StringPool.SLASH
087            };
088    
089    }