001    /**
002     * Copyright (c) 2000-2012 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.portlet.wiki.model.WikiPage;
023    
024    import java.util.regex.MatchResult;
025    
026    /**
027     * @author Kenneth Chang
028     */
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    }