001
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
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 }