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.Validator;
021    import com.liferay.portlet.wiki.util.WikiUtil;
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", true);
039                    addParameter(routeParameters, "title", true);
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, boolean escape) {
057    
058                    if (!routeParameters.containsKey(name)) {
059                            return;
060                    }
061    
062                    String value = routeParameters.get(name);
063    
064                    if (escape) {
065                            value = WikiUtil.escapeName(value);
066                    }
067                    else {
068                            value = WikiUtil.unescapeName(value);
069                    }
070    
071                    routeParameters.put(name, value);
072            }
073    
074            @Override
075            protected void populateParams(
076                    Map<String, String[]> parameterMap, String namespace,
077                    Map<String, String> routeParameters) {
078    
079                    addParameter(routeParameters, "nodeName", false);
080                    addParameter(routeParameters, "title", false);
081    
082                    super.populateParams(parameterMap, namespace, routeParameters);
083            }
084    
085    }