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.util.CallbackMatcher;
018    import com.liferay.portal.kernel.util.HttpUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    
021    import java.util.regex.MatchResult;
022    
023    /**
024     * @author Jonathan Potter
025     * @author Brian Wing Shun Chan
026     */
027    public class ImageURLMatcher extends CallbackMatcher {
028    
029            public ImageURLMatcher(String attachmentURLPrefix) {
030                    _attachmentURLPrefix = attachmentURLPrefix;
031    
032                    setRegex(_REGEX);
033            }
034    
035            public String replaceMatches(CharSequence charSequence) {
036                    return replaceMatches(charSequence, _callBack);
037            }
038    
039            private static final String _REGEX =
040                    "<a href=\"[^\"]*?Special:Upload[^\"]*?topic=Image:([^\"]*?)\".*?</a>";
041    
042            private Callback _callBack = new Callback() {
043    
044                    public String foundMatch(MatchResult matchResult) {
045                            String title = matchResult.group(1);
046    
047                            String url = _attachmentURLPrefix + HttpUtil.encodeURL(title);
048    
049                            StringBundler sb = new StringBundler(5);
050    
051                            sb.append("<img alt=\"");
052                            sb.append(title);
053                            sb.append("\" class=\"wikiimg\" src=\"");
054                            sb.append(url);
055                            sb.append("\" />");
056    
057                            return sb.toString();
058                    }
059    
060            };
061    
062            private String _attachmentURLPrefix;
063    
064    }