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.engines.jspwiki;
016    
017    import com.ecyrd.jspwiki.WikiContext;
018    import com.ecyrd.jspwiki.url.URLConstructor;
019    
020    import com.liferay.portal.kernel.util.CharPool;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    
026    import java.util.Properties;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * @author Jorge Ferrer
032     */
033    public class LiferayURLConstructor implements URLConstructor {
034    
035            @Override
036            public String getForwardPage(HttpServletRequest request) {
037                    return "Wiki.jsp";
038            }
039    
040            @Override
041            public void initialize(
042                    com.ecyrd.jspwiki.WikiEngine engine, Properties props) {
043            }
044    
045            @Override
046            public String makeURL(
047                    String context, String name, boolean absolute, String parameters) {
048    
049                    if (Validator.isNotNull(parameters)) {
050                            if (context.equals(WikiContext.ATTACH)) {
051                                    parameters = StringPool.QUESTION + parameters;
052                            }
053                            else if (context.equals(WikiContext.NONE)) {
054                                    if (name.indexOf(CharPool.QUESTION) != -1) {
055                                            parameters = "&" + parameters;
056                                    }
057                                    else {
058                                            parameters = StringPool.QUESTION + parameters;
059                                    }
060                            }
061                            else {
062                                    parameters = "&" + parameters;
063                            }
064                    }
065                    else {
066                            parameters = StringPool.BLANK;
067                    }
068    
069                    String path;
070    
071                    if (context.equals(WikiContext.EDIT)) {
072                            path =
073                                    "[$BEGIN_PAGE_TITLE_EDIT$]" +
074                                            JSPWikiEngine.decodeJSPWikiName(name) +
075                                                    "[$END_PAGE_TITLE_EDIT$]";
076                    }
077                    else if (context.equals(WikiContext.VIEW)) {
078                            path =
079                                    "[$BEGIN_PAGE_TITLE$]" +
080                                            _escapeName(JSPWikiEngine.decodeJSPWikiName(name)) +
081                                                    "[$END_PAGE_TITLE$]";
082                    }
083                    else if (context.equals(WikiContext.ATTACH)) {
084                            if (name.indexOf(CharPool.SLASH) == -1) {
085                                    path =
086                                            "[$ATTACHMENT_URL_PREFIX$][$WIKI_PAGE_NAME$]/" +
087                                                    HttpUtil.encodeURL(
088                                                            JSPWikiEngine.decodeJSPWikiName(name));
089                            }
090                            else {
091                                    path =
092                                            "[$ATTACHMENT_URL_PREFIX$]" +
093                                                    HttpUtil.encodeURL(
094                                                            JSPWikiEngine.decodeJSPWikiName(name));
095                            }
096                    }
097                    else {
098                            path = JSPWikiEngine.decodeJSPWikiName(name);
099                    }
100    
101                    return path + parameters;
102            }
103    
104            @Override
105            public String parsePage(
106                    String context, HttpServletRequest request, String encoding) {
107    
108                    return "Wiki.jsp";
109            }
110    
111            private static String _escapeName(String name) {
112                    return StringUtil.replace(name, _UNESCAPED_CHARS, _ESCAPED_CHARS);
113            }
114    
115            private static final String[] _ESCAPED_CHARS = new String[] {
116                    "<PLUS>", "<QUESTION>", "<SLASH>"
117            };
118    
119            private static final String[] _UNESCAPED_CHARS = new String[] {
120                    StringPool.PLUS, StringPool.QUESTION, StringPool.SLASH
121            };
122    
123    }