001
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.ArrayList;
022 import java.util.LinkedHashMap;
023 import java.util.LinkedList;
024 import java.util.List;
025 import java.util.Map;
026 import java.util.regex.Matcher;
027 import java.util.regex.Pattern;
028
029
032 public abstract class BaseTranslator {
033
034 public String translate(String content) {
035 _protectedMap.clear();
036
037 content = preProcess(content);
038 content = runRegexps(content);
039 content = postProcess(content);
040
041 return content;
042 }
043
044 protected String postProcess(String content) {
045 return unprotectNowikiText(content);
046 }
047
048 protected String preProcess(String content) {
049 content = _normalizeLineBreaks(content);
050
051 for (String regexp : nowikiRegexps) {
052 content = protectText(content, regexp);
053 }
054
055 return content;
056 }
057
058 protected String protectText(String content, String markupRegex) {
059 Matcher matcher = Pattern.compile(
060 markupRegex, Pattern.MULTILINE | Pattern.DOTALL).matcher(content);
061
062 StringBuffer sb = new StringBuffer();
063
064 while (matcher.find()) {
065 String protectedText = matcher.group();
066
067 String hash = DigesterUtil.digest(protectedText);
068
069 matcher.appendReplacement(sb, "$1" + hash + "$3");
070
071 _protectedMap.put(hash, matcher.group(2));
072 }
073
074 matcher.appendTail(sb);
075
076 return sb.toString();
077 }
078
079 protected String runRegexps(String content) {
080 for (String regexp : regexps.keySet()) {
081 String replacement = regexps.get(regexp);
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 List<String> hashList = new ArrayList<String>(_protectedMap.keySet());
108
109 for (int i = hashList.size() - 1; i >= 0; i--) {
110 String hash = hashList.get(i);
111
112 String protectedMarkup = _protectedMap.get(hash);
113
114 content = content.replace(hash, protectedMarkup);
115 }
116
117 return content;
118 }
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 protected Map<String, String> regexps =
130 new LinkedHashMap<String, String>();
131 protected List<String> nowikiRegexps = new LinkedList<String>();
132
133 private Map<String, String> _protectedMap =
134 new LinkedHashMap<String, String>();
135
136 }