001    /**
002     * Copyright (c) 2000-present 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 final 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                                    for (FileEntry fileEntry : _page.getAttachmentsFileEntries()) {
059                                            if (!fileName.equals(fileEntry.getTitle())) {
060                                                    continue;
061                                            }
062    
063                                            fileName = StringUtil.replace(
064                                                    fileName, StringPool.UNDERLINE, "%5F");
065    
066                                            return StringUtil.replace(
067                                                    matchResult.group(0), matchResult.group(1), fileName);
068                                    }
069                            }
070                            catch (Exception e) {
071                            }
072    
073                            return null;
074                    }
075    
076            };
077    
078            private final WikiPage _page;
079    
080    }