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     */
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    }