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