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            public String getForwardPage(HttpServletRequest request) {
036                    return "Wiki.jsp";
037            }
038    
039            public void initialize(
040                    com.ecyrd.jspwiki.WikiEngine engine, Properties props) {
041            }
042    
043            public String makeURL(
044                    String context, String name, boolean absolute, String parameters) {
045    
046                    if (Validator.isNotNull(parameters)) {
047                            if (context.equals(WikiContext.ATTACH)) {
048                                    parameters = StringPool.QUESTION + parameters;
049                            }
050                            else if (context.equals(WikiContext.NONE)) {
051                                    if (name.indexOf(CharPool.QUESTION) != -1) {
052                                            parameters = "&" + parameters;
053                                    }
054                                    else {
055                                            parameters = StringPool.QUESTION + parameters;
056                                    }
057                            }
058                            else {
059                                    parameters = "&" + parameters;
060                            }
061                    }
062                    else {
063                            parameters = StringPool.BLANK;
064                    }
065    
066                    String path;
067    
068                    if (context.equals(WikiContext.EDIT)) {
069                            path =
070                                    "[$BEGIN_PAGE_TITLE_EDIT$]" +
071                                            JSPWikiEngine.decodeJSPWikiName(name) +
072                                                    "[$END_PAGE_TITLE_EDIT$]";
073                    }
074                    else if (context.equals(WikiContext.VIEW)) {
075                            path =
076                                    "[$BEGIN_PAGE_TITLE$]" +
077                                            _escapeName(JSPWikiEngine.decodeJSPWikiName(name)) +
078                                                    "[$END_PAGE_TITLE$]";
079                    }
080                    else if (context.equals(WikiContext.ATTACH)) {
081                            if (name.indexOf(CharPool.SLASH) == -1) {
082                                    path =
083                                            "[$ATTACHMENT_URL_PREFIX$][$WIKI_PAGE_NAME$]/" +
084                                                    HttpUtil.encodeURL(
085                                                            JSPWikiEngine.decodeJSPWikiName(name));
086                            }
087                            else {
088                                    path =
089                                            "[$ATTACHMENT_URL_PREFIX$]" +
090                                                    HttpUtil.encodeURL(
091                                                            JSPWikiEngine.decodeJSPWikiName(name));
092                            }
093                    }
094                    else {
095                            path = JSPWikiEngine.decodeJSPWikiName(name);
096                    }
097    
098                    return path + parameters;
099            }
100    
101            public String parsePage(
102                    String context, HttpServletRequest request, String encoding) {
103    
104                    return "Wiki.jsp";
105            }
106    
107            private static String _escapeName(String name) {
108                    return StringUtil.replace(name, _UNESCAPED_CHARS, _ESCAPED_CHARS);
109            }
110    
111            private static final String[] _ESCAPED_CHARS = new String[] {
112                    "<PLUS>", "<QUESTION>", "<SLASH>"
113            };
114    
115            private static final String[] _UNESCAPED_CHARS = new String[] {
116                    StringPool.PLUS, StringPool.QUESTION, StringPool.SLASH
117            };
118    
119    }