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.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.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portlet.wiki.model.WikiPage;
025    
026    import java.util.regex.MatchResult;
027    
028    /**
029     * @author Kenneth Chang
030     */
031    public class DirectURLMatcher extends CallbackMatcher {
032    
033            public DirectURLMatcher(WikiPage page, String attachmentURLPrefix) {
034                    _page = page;
035                    _attachmentURLPrefix = attachmentURLPrefix;
036    
037                    setRegex(_URL_REGEX);
038            }
039    
040            public String replaceMatches(CharSequence charSequence) {
041                    return replaceMatches(charSequence, _callBack);
042            }
043    
044            private static final String _URL_REGEX =
045                    "<a href=\"[^\"]*?Special:Edit[^\"]*?topic=[^\"]*?\".*?title=\"" +
046                            "([^\"]*?)\".*?>(.*?)</a>";
047    
048            private String _attachmentURLPrefix;
049    
050            private Callback _callBack = new Callback() {
051    
052                    @Override
053                    public String foundMatch(MatchResult matchResult) {
054                            String fileName = StringUtil.replace(
055                                    matchResult.group(1), "%5F", StringPool.UNDERLINE);
056                            String title = StringUtil.replace(
057                                    matchResult.group(2), "%5F", StringPool.UNDERLINE);
058    
059                            if (Validator.isNull(title)) {
060                                    title = fileName;
061                            }
062    
063                            String url = _attachmentURLPrefix + HttpUtil.encodeURL(fileName);
064    
065                            try {
066                                    String[] attachments = _page.getAttachmentsFiles();
067    
068                                    String link =
069                                            StringPool.SLASH + _page.getAttachmentsDir() +
070                                                    StringPool.SLASH + fileName;
071    
072                                    if (!ArrayUtil.contains(attachments, link)) {
073                                            return null;
074                                    }
075                            }
076                            catch (Exception e) {
077                                    return null;
078                            }
079    
080                            StringBundler sb = new StringBundler(5);
081    
082                            sb.append("<a href=\"");
083                            sb.append(url);
084                            sb.append("\">");
085                            sb.append(title);
086                            sb.append("</a>");
087    
088                            return sb.toString();
089                    }
090    
091            };
092    
093            private WikiPage _page;
094    
095    }