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.translators;
016    
017    import com.liferay.portal.kernel.util.DigesterUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import java.util.LinkedHashMap;
022    import java.util.LinkedList;
023    import java.util.List;
024    import java.util.Map;
025    import java.util.regex.Matcher;
026    import java.util.regex.Pattern;
027    
028    /**
029     * @author Jorge Ferrer
030     */
031    public abstract class BaseTranslator {
032    
033            public String translate(String content) {
034                    _protectedMap.clear();
035    
036                    content = preProcess(content);
037                    content = runRegexps(content);
038                    content = postProcess(content);
039    
040                    return content;
041            }
042    
043            protected String postProcess(String content) {
044                    return unprotectNowikiText(content);
045            }
046    
047            protected String preProcess(String content) {
048                    content = _normalizeLineBreaks(content);
049    
050                    for (String regexp : nowikiRegexps) {
051                            content = protectText(content, regexp);
052                    }
053    
054                    return content;
055            }
056    
057            protected String protectText(String content, String markupRegex) {
058                    Pattern pattern = Pattern.compile(
059                            markupRegex, Pattern.MULTILINE | Pattern.DOTALL);
060    
061                    Matcher matcher = pattern.matcher(content);
062    
063                    StringBuffer sb = new StringBuffer();
064    
065                    while (matcher.find()) {
066                            String protectedText = matcher.group();
067    
068                            String hash = DigesterUtil.digest(protectedText);
069    
070                            matcher.appendReplacement(sb, "$1" + hash + "$3");
071    
072                            _protectedMap.put(hash, matcher.group(2));
073                    }
074    
075                    matcher.appendTail(sb);
076    
077                    return sb.toString();
078            }
079    
080            protected String runRegexp(
081                    String content, String regexp, String replacement) {
082    
083                    Pattern pattern = Pattern.compile(regexp, Pattern.MULTILINE);
084    
085                    Matcher matcher = pattern.matcher(content);
086    
087                    StringBuffer sb = new StringBuffer();
088    
089                    while (matcher.find()) {
090                            matcher.appendReplacement(sb, replacement);
091                    }
092    
093                    matcher.appendTail(sb);
094    
095                    return sb.toString();
096            }
097    
098            protected String runRegexps(String content) {
099                    for (Map.Entry<String, String> entry : regexps.entrySet()) {
100                            String regexp = entry.getKey();
101                            String replacement = entry.getValue();
102    
103                            content = runRegexp(content, regexp, replacement);
104                    }
105    
106                    return content;
107            }
108    
109            protected String unprotectNowikiText(String content) {
110                    for (Map.Entry<String, String> entry : _protectedMap.entrySet()) {
111                            String hash = entry.getKey();
112                            String protectedMarkup = entry.getValue();
113    
114                            content = content.replace(hash, protectedMarkup);
115                    }
116    
117                    return content;
118            }
119    
120            protected List<String> nowikiRegexps = new LinkedList<String>();
121            protected Map<String, String> regexps = new LinkedHashMap<String, String>();
122    
123            private String _normalizeLineBreaks(String content) {
124                    content = StringUtil.replace(
125                            content,
126                            new String[] {StringPool.RETURN_NEW_LINE, StringPool.RETURN},
127                            new String[] {StringPool.NEW_LINE, StringPool.NEW_LINE});
128    
129                    return content;
130            }
131    
132            private Map<String, String> _protectedMap =
133                    new LinkedHashMap<String, String>();
134    
135    }