001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    public String foundMatch(MatchResult matchResult) {
048                            String portletURLString = _portletURL.toString();
049    
050                            String title = matchResult.group(1);
051    
052                            title = title.replace(CharPool.UNDERLINE, CharPool.PLUS);
053    
054                            String url = portletURLString.replace(_TITLE_PLACEHOLDER, title);
055    
056                            return "<a href=\"" + url + "\"";
057                    }
058    
059            };
060    
061            private PortletURL _portletURL;
062    
063    }