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.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                    public String foundMatch(MatchResult matchResult) {
046                            String fileName = matchResult.group(1);
047    
048                            if (!fileName.contains(StringPool.UNDERLINE)) {
049                                    return null;
050                            }
051    
052                            if (fileName.indexOf(CharPool.PIPE) >= 0) {
053                                    fileName = StringUtil.extractFirst(fileName, CharPool.PIPE);
054                            }
055    
056                            try {
057                                    for (FileEntry fileEntry : _page.getAttachmentsFileEntries()) {
058                                            if (!fileName.equals(fileEntry.getTitle())) {
059                                                    continue;
060                                            }
061    
062                                            fileName = StringUtil.replace(
063                                                    fileName, StringPool.UNDERLINE, "%5F");
064    
065                                            return StringUtil.replace(
066                                                    matchResult.group(0), matchResult.group(1), fileName);
067                                    }
068                            }
069                            catch (Exception e) {
070                            }
071    
072                            return null;
073                    }
074    
075            };
076    
077            private WikiPage _page;
078    
079    }