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                    Matcher matcher = Pattern.compile(
059                            markupRegex, Pattern.MULTILINE | Pattern.DOTALL).matcher(content);
060    
061                    StringBuffer sb = new StringBuffer();
062    
063                    while (matcher.find()) {
064                            String protectedText = matcher.group();
065    
066                            String hash = DigesterUtil.digest(protectedText);
067    
068                            matcher.appendReplacement(sb, "$1" + hash + "$3");
069    
070                            _protectedMap.put(hash, matcher.group(2));
071                    }
072    
073                    matcher.appendTail(sb);
074    
075                    return sb.toString();
076            }
077    
078            protected String runRegexp(
079                    String content, String regexp, String replacement) {
080    
081                    Matcher matcher = Pattern.compile(
082                            regexp, Pattern.MULTILINE).matcher(content);
083    
084                    StringBuffer sb = new StringBuffer();
085    
086                    while (matcher.find()) {
087                            matcher.appendReplacement(sb, replacement);
088                    }
089    
090                    matcher.appendTail(sb);
091    
092                    return sb.toString();
093            }
094    
095            protected String runRegexps(String content) {
096                    for (Map.Entry<String, String> entry : regexps.entrySet()) {
097                            String regexp = entry.getKey();
098                            String replacement = entry.getValue();
099    
100                            content = runRegexp(content, regexp, replacement);
101                    }
102    
103                    return content;
104            }
105    
106            protected String unprotectNowikiText(String content) {
107                    for (Map.Entry<String, String> entry : _protectedMap.entrySet()) {
108                            String hash = entry.getKey();
109                            String protectedMarkup = entry.getValue();
110    
111                            content = content.replace(hash, protectedMarkup);
112                    }
113    
114                    return content;
115            }
116    
117            protected List<String> nowikiRegexps = new LinkedList<String>();
118            protected Map<String, String> regexps = new LinkedHashMap<String, String>();
119    
120            private String _normalizeLineBreaks(String content) {
121                    content = StringUtil.replace(
122                            content,
123                            new String[] {StringPool.RETURN_NEW_LINE, StringPool.RETURN},
124                            new String[] {StringPool.NEW_LINE, StringPool.NEW_LINE});
125    
126                    return content;
127            }
128    
129            private Map<String, String> _protectedMap =
130                    new LinkedHashMap<String, String>();
131    
132    }