001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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     */
029    public class WikiFriendlyURLMapper extends DefaultFriendlyURLMapper {
030    
031            @Override
032            public String buildPath(LiferayPortletURL liferayPortletURL) {
033                    Map<String, String> routeParameters = new HashMap<String, String>();
034    
035                    buildRouteParameters(liferayPortletURL, routeParameters);
036    
037                    if (routeParameters.containsKey("title")) {
038                            String title = routeParameters.get("title");
039    
040                            title = StringUtil.replace(title, _UNESCAPED_CHARS, _ESCAPED_CHARS);
041    
042                            routeParameters.put("title", title);
043                    }
044    
045                    String friendlyURLPath = router.parametersToUrl(routeParameters);
046    
047                    if (Validator.isNull(friendlyURLPath)) {
048                            return null;
049                    }
050    
051                    addParametersIncludedInPath(liferayPortletURL, routeParameters);
052    
053                    friendlyURLPath = StringPool.SLASH.concat(getMapping()).concat(
054                            friendlyURLPath);
055    
056                    return friendlyURLPath;
057            }
058    
059            @Override
060            protected void populateParams(
061                    Map<String, String[]> parameterMap, String namespace,
062                    Map<String, String> routeParameters) {
063    
064                    if (routeParameters.containsKey("title")) {
065                            String title = routeParameters.get("title");
066    
067                            title = StringUtil.replace(title, _ESCAPED_CHARS, _UNESCAPED_CHARS);
068    
069                            routeParameters.put("title", title);
070                    }
071    
072                    super.populateParams(parameterMap, namespace, routeParameters);
073            }
074    
075            private static final String[] _ESCAPED_CHARS = new String[] {
076                    "<PLUS>", "<QUESTION>", "<SLASH>"
077            };
078    
079            private static final String[] _UNESCAPED_CHARS = new String[] {
080                    StringPool.PLUS, StringPool.QUESTION, StringPool.SLASH
081            };
082    
083    }