001    /**
002     * Copyright (c) 2000-2012 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.Validator;
024    
025    import java.util.Properties;
026    
027    import javax.servlet.http.HttpServletRequest;
028    
029    /**
030     * @author Jorge Ferrer
031     */
032    public class LiferayURLConstructor implements URLConstructor {
033    
034            public String getForwardPage(HttpServletRequest request) {
035                    return "Wiki.jsp";
036            }
037    
038            public void initialize(
039                    com.ecyrd.jspwiki.WikiEngine engine, Properties props) {
040            }
041    
042            public String makeURL(
043                    String context, String name, boolean absolute, String parameters) {
044    
045                    if (Validator.isNotNull(parameters)) {
046                            if (context.equals(WikiContext.ATTACH)) {
047                                    parameters = StringPool.QUESTION + parameters;
048                            }
049                            else if (context.equals(WikiContext.NONE)) {
050                                    if (name.indexOf(CharPool.QUESTION) != -1) {
051                                            parameters = "&" + parameters;
052                                    }
053                                    else {
054                                            parameters = StringPool.QUESTION + parameters;
055                                    }
056                            }
057                            else {
058                                    parameters = "&" + parameters;
059                            }
060                    }
061                    else {
062                            parameters = StringPool.BLANK;
063                    }
064    
065                    String path;
066    
067                    if (context.equals(WikiContext.EDIT)) {
068                            path =
069                                    "[$BEGIN_PAGE_TITLE_EDIT$]" +
070                                            JSPWikiEngine.decodeJSPWikiName(name) +
071                                                    "[$END_PAGE_TITLE_EDIT$]";
072                    }
073                    else if (context.equals(WikiContext.VIEW)) {
074                            path =
075                                    "[$BEGIN_PAGE_TITLE$]" + JSPWikiEngine.decodeJSPWikiName(name) +
076                                            "[$END_PAGE_TITLE$]";
077                    }
078                    else if (context.equals(WikiContext.ATTACH)) {
079                            if (name.indexOf(CharPool.SLASH) == -1) {
080                                    path =
081                                            "[$ATTACHMENT_URL_PREFIX$][$WIKI_PAGE_NAME$]/" +
082                                                    HttpUtil.encodeURL(
083                                                            JSPWikiEngine.decodeJSPWikiName(name));
084                            }
085                            else {
086                                    path =
087                                            "[$ATTACHMENT_URL_PREFIX$]" +
088                                                    HttpUtil.encodeURL(
089                                                            JSPWikiEngine.decodeJSPWikiName(name));
090                            }
091                    }
092                    else {
093                            path = JSPWikiEngine.decodeJSPWikiName(name);
094                    }
095    
096                    return path + parameters;
097            }
098    
099            public String parsePage(
100                    String context, HttpServletRequest request, String encoding) {
101    
102                    return "Wiki.jsp";
103            }
104    
105    }