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                    if (routeParameters.containsKey("title")) {
039                            String title = routeParameters.get("title");
040    
041                            title = StringUtil.replace(title, _UNESCAPED_CHARS, _ESCAPED_CHARS);
042    
043                            routeParameters.put("title", title);
044                    }
045    
046                    String friendlyURLPath = router.parametersToUrl(routeParameters);
047    
048                    if (Validator.isNull(friendlyURLPath)) {
049                            return null;
050                    }
051    
052                    addParametersIncludedInPath(liferayPortletURL, routeParameters);
053    
054                    friendlyURLPath = StringPool.SLASH.concat(getMapping()).concat(
055                            friendlyURLPath);
056    
057                    return friendlyURLPath;
058            }
059    
060            @Override
061            protected void populateParams(
062                    Map<String, String[]> parameterMap, String namespace,
063                    Map<String, String> routeParameters) {
064    
065                    if (routeParameters.containsKey("title")) {
066                            String title = routeParameters.get("title");
067    
068                            title = StringUtil.replace(title, _ESCAPED_CHARS, _UNESCAPED_CHARS);
069    
070                            routeParameters.put("title", title);
071                    }
072    
073                    super.populateParams(parameterMap, namespace, routeParameters);
074            }
075    
076            private static final String[] _ESCAPED_CHARS = new String[] {
077                    "<PLUS>", "<QUESTION>", "<SLASH>"
078            };
079    
080            private static final String[] _UNESCAPED_CHARS = new String[] {
081                    StringPool.PLUS, StringPool.QUESTION, StringPool.SLASH
082            };
083    
084    }