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.repository.model.FileEntry;
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                    public String foundMatch(MatchResult matchResult) {
053                            String fileName = StringUtil.replace(
054                                    matchResult.group(1), "%5F", StringPool.UNDERLINE);
055                            String title = StringUtil.replace(
056                                    matchResult.group(2), "%5F", StringPool.UNDERLINE);
057    
058                            if (Validator.isNull(title)) {
059                                    title = fileName;
060                            }
061    
062                            String url = _attachmentURLPrefix + HttpUtil.encodeURL(fileName);
063    
064                            try {
065                                    for (FileEntry fileEntry : _page.getAttachmentsFileEntries()) {
066                                            if (!fileName.equals(fileEntry.getTitle())) {
067                                                    continue;
068                                            }
069    
070                                            StringBundler sb = new StringBundler(5);
071    
072                                            sb.append("<a href=\"");
073                                            sb.append(url);
074                                            sb.append("\">");
075                                            sb.append(title);
076                                            sb.append("</a>");
077    
078                                            return sb.toString();
079                                    }
080                            }
081                            catch (Exception e) {
082                            }
083    
084                            return null;
085                    }
086    
087            };
088    
089            private WikiPage _page;
090    
091    }