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.mediawiki.matchers;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
018    import com.liferay.portal.kernel.util.CallbackMatcher;
019    import com.liferay.portal.kernel.util.CharPool;
020    
021    import java.util.regex.MatchResult;
022    
023    import javax.portlet.PortletURL;
024    
025    /**
026     * @author Jonathan Potter
027     * @author Brian Wing Shun Chan
028     */
029    public class PortletURLMatcher extends CallbackMatcher {
030    
031            public PortletURLMatcher(PortletURL portletURL) {
032                    _portletURL = portletURL;
033    
034                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
035    
036                    liferayPortletURL.setParameter("title", _TITLE_PLACEHOLDER, false);
037            }
038    
039            public String replaceMatches(CharSequence charSequence) {
040                    return replaceMatches(charSequence, _callBack);
041            }
042    
043            private static final String _TITLE_PLACEHOLDER = "__TITLE_PLACEHOLDER__";
044    
045            private Callback _callBack = new Callback() {
046    
047                    @Override
048                    public String foundMatch(MatchResult matchResult) {
049                            String portletURLString = _portletURL.toString();
050    
051                            String title = matchResult.group(1);
052    
053                            title = title.replace(CharPool.UNDERLINE, CharPool.PLUS);
054    
055                            String url = portletURLString.replace(_TITLE_PLACEHOLDER, title);
056    
057                            return "<a href=\"" + url + "\"";
058                    }
059    
060            };
061    
062            private PortletURL _portletURL;
063    
064    }