001
014
015 package com.liferay.portlet.wiki.engines.mediawiki.matchers;
016
017 import com.liferay.portal.kernel.util.ArrayUtil;
018 import com.liferay.portal.kernel.util.CallbackMatcher;
019 import com.liferay.portal.kernel.util.HttpUtil;
020 import com.liferay.portal.kernel.util.StringBundler;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portlet.wiki.model.WikiPage;
023
024 import java.util.regex.MatchResult;
025
026
029 public class DirectURLMatcher extends CallbackMatcher {
030
031 public DirectURLMatcher(WikiPage page, String attachmentURLPrefix) {
032 _page = page;
033 _attachmentURLPrefix = attachmentURLPrefix;
034
035 setRegex(_REGEX);
036 }
037
038 public String replaceMatches(CharSequence charSequence) {
039 return replaceMatches(charSequence, _callBack);
040 }
041
042 private static final String _REGEX =
043 "<a href=\"[^\"]*?Special:Edit[^\"]*?topic=[^\"]*?\".*?title=\"" +
044 "([^\"]*?)\".*?>(.*?)</a>";
045
046 private String _attachmentURLPrefix;
047
048 private Callback _callBack = new Callback() {
049
050 public String foundMatch(MatchResult matchResult) {
051 String fileName = matchResult.group(1);
052 String title = matchResult.group(2);
053
054 String url = _attachmentURLPrefix + HttpUtil.encodeURL(fileName);
055
056 try {
057 String[] attachments = _page.getAttachmentsFiles();
058
059 String link =
060 StringPool.SLASH + _page.getAttachmentsDir() +
061 StringPool.SLASH + fileName;
062
063 if (!ArrayUtil.contains(attachments, link)) {
064 return null;
065 }
066 }
067 catch (Exception e) {
068 return null;
069 }
070
071 StringBundler sb = new StringBundler(5);
072
073 sb.append("<a href=\"");
074 sb.append(url);
075 sb.append("\">");
076 sb.append(title);
077 sb.append("</a>");
078
079 return sb.toString();
080 }
081
082 };
083
084 private WikiPage _page;
085
086 }