001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 runRegexps(String content) {
079                    for (Map.Entry<String, String> entry : regexps.entrySet()) {
080                            String regexp = entry.getKey();
081                            String replacement = entry.getValue();
082    
083                            content = runRegexp(content, regexp, replacement);
084                    }
085    
086                    return content;
087            }
088    
089            protected String runRegexp(
090                    String content, String regexp, String replacement) {
091    
092                    Matcher matcher = Pattern.compile(
093                            regexp, Pattern.MULTILINE).matcher(content);
094    
095                    StringBuffer sb = new StringBuffer();
096    
097                    while (matcher.find()) {
098                            matcher.appendReplacement(sb, replacement);
099                    }
100    
101                    matcher.appendTail(sb);
102    
103                    return sb.toString();
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            private String _normalizeLineBreaks(String content) {
118                    content = StringUtil.replace(
119                            content,
120                            new String[] {StringPool.RETURN_NEW_LINE, StringPool.RETURN},
121                            new String[] {StringPool.NEW_LINE, StringPool.NEW_LINE});
122    
123                    return content;
124            }
125    
126            protected Map<String, String> regexps =
127                    new LinkedHashMap<String, String>();
128            protected List<String> nowikiRegexps = new LinkedList<String>();
129    
130            private Map<String, String> _protectedMap =
131                    new LinkedHashMap<String, String>();
132    
133    }