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.CharPool;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
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 DirectTagMatcher extends CallbackMatcher {
030    
031            public DirectTagMatcher(WikiPage page) {
032                    _page = page;
033    
034                    setRegex(_REGEX);
035            }
036    
037            public String replaceMatches(CharSequence charSequence) {
038                    return replaceMatches(charSequence, _callBack);
039            }
040    
041            private static final String _REGEX = "\\[\\[([^\\]]+)\\]\\]";
042    
043            private Callback _callBack = new Callback() {
044    
045                    @Override
046                    public String foundMatch(MatchResult matchResult) {
047                            String fileName = matchResult.group(1);
048    
049                            if (!fileName.contains(StringPool.UNDERLINE)) {
050                                    return null;
051                            }
052    
053                            if (fileName.indexOf(CharPool.PIPE) >= 0) {
054                                    fileName = StringUtil.extractFirst(fileName, CharPool.PIPE);
055                            }
056    
057                            try {
058                                    String[] attachments = _page.getAttachmentsFiles();
059    
060                                    String link =
061                                            StringPool.SLASH + _page.getAttachmentsDir() +
062                                                    StringPool.SLASH + fileName;
063    
064                                    if (!ArrayUtil.contains(attachments, link)) {
065                                            return null;
066                                    }
067                            }
068                            catch (Exception e) {
069                                    return null;
070                            }
071    
072                            fileName = StringUtil.replace(
073                                    fileName, StringPool.UNDERLINE, "%5F");
074    
075                            return StringUtil.replace(
076                                    matchResult.group(0), matchResult.group(1), fileName);
077                    }
078    
079            };
080    
081            private WikiPage _page;
082    
083    }