1
22
23 package com.liferay.portlet.wiki.translators;
24
25 import com.liferay.portal.kernel.util.DigesterUtil;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.kernel.util.StringUtil;
28
29 import java.util.ArrayList;
30 import java.util.LinkedHashMap;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36
37
43 public abstract class BaseTranslator {
44
45 public String translate(String content) {
46 _protectedMap.clear();
47
48 content = preProcess(content);
49 content = runRegexps(content);
50 content = postProcess(content);
51
52 return content;
53 }
54
55 protected String postProcess(String content) {
56 return unprotectNowikiText(content);
57 }
58
59 protected String preProcess(String content) {
60 content = _normalizeLineBreaks(content);
61
62 for (String regexp : nowikiRegexps) {
63 content = protectText(content, regexp);
64 }
65
66 return content;
67 }
68
69 protected String protectText(String content, String markupRegex) {
70 Matcher matcher = Pattern.compile(
71 markupRegex, Pattern.MULTILINE | Pattern.DOTALL).matcher(content);
72
73 StringBuffer sb = new StringBuffer();
74
75 while (matcher.find()) {
76 String protectedText = matcher.group();
77
78 String hash = DigesterUtil.digest(protectedText);
79
80 matcher.appendReplacement(sb, "$1" + hash + "$3");
81
82 _protectedMap.put(hash, matcher.group(2));
83 }
84
85 matcher.appendTail(sb);
86
87 return sb.toString();
88 }
89
90 protected String runRegexps(String content) {
91 for (String regexp : regexps.keySet()) {
92 String replacement = regexps.get(regexp);
93
94 content = runRegexp(content, regexp, replacement);
95 }
96
97 return content;
98 }
99
100 protected String runRegexp(
101 String content, String regexp, String replacement) {
102
103 Matcher matcher = Pattern.compile(
104 regexp, Pattern.MULTILINE).matcher(content);
105
106 StringBuffer sb = new StringBuffer();
107
108 while (matcher.find()) {
109 matcher.appendReplacement(sb, replacement);
110 }
111
112 matcher.appendTail(sb);
113
114 return sb.toString();
115 }
116
117 protected String unprotectNowikiText(String content) {
118 List<String> hashList = new ArrayList<String>(_protectedMap.keySet());
119
120 for (int i = hashList.size() - 1; i >= 0; i--) {
121 String hash = hashList.get(i);
122
123 String protectedMarkup = _protectedMap.get(hash);
124
125 content = content.replace(hash, protectedMarkup);
126 }
127
128 return content;
129 }
130
131 private String _normalizeLineBreaks(String content) {
132 content = StringUtil.replace(
133 content,
134 new String[] {StringPool.RETURN_NEW_LINE, StringPool.RETURN},
135 new String[] {StringPool.NEW_LINE, StringPool.NEW_LINE});
136
137 return content;
138 }
139
140 protected Map<String, String> regexps =
141 new LinkedHashMap<String, String>();
142 protected List<String> nowikiRegexps = new LinkedList<String>();
143
144 private Map<String, String> _protectedMap =
145 new LinkedHashMap<String, String>();
146
147 }