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.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                    @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                                    for (FileEntry fileEntry : _page.getAttachmentsFileEntries()) {
067                                            if (!fileName.equals(fileEntry.getTitle())) {
068                                                    continue;
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                            catch (Exception e) {
083                            }
084    
085                            return null;
086                    }
087    
088            };
089    
090            private WikiPage _page;
091    
092    }