1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.wiki.translators;
16  
17  import com.liferay.portal.kernel.util.DigesterUtil;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.StringUtil;
20  
21  import java.util.ArrayList;
22  import java.util.LinkedHashMap;
23  import java.util.LinkedList;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  /**
30   * <a href="BaseTranslator.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Jorge Ferrer
33   */
34  public abstract class BaseTranslator {
35  
36      public String translate(String content) {
37          _protectedMap.clear();
38  
39          content = preProcess(content);
40          content = runRegexps(content);
41          content = postProcess(content);
42  
43          return content;
44      }
45  
46      protected String postProcess(String content) {
47          return unprotectNowikiText(content);
48      }
49  
50      protected String preProcess(String content) {
51          content = _normalizeLineBreaks(content);
52  
53          for (String regexp : nowikiRegexps) {
54              content = protectText(content, regexp);
55          }
56  
57          return content;
58      }
59  
60      protected String protectText(String content, String markupRegex) {
61          Matcher matcher = Pattern.compile(
62              markupRegex, Pattern.MULTILINE | Pattern.DOTALL).matcher(content);
63  
64          StringBuffer sb = new StringBuffer();
65  
66          while (matcher.find()) {
67              String protectedText = matcher.group();
68  
69              String hash = DigesterUtil.digest(protectedText);
70  
71              matcher.appendReplacement(sb, "$1" + hash + "$3");
72  
73              _protectedMap.put(hash, matcher.group(2));
74          }
75  
76          matcher.appendTail(sb);
77  
78          return sb.toString();
79      }
80  
81      protected String runRegexps(String content) {
82          for (String regexp : regexps.keySet()) {
83              String replacement = regexps.get(regexp);
84  
85              content = runRegexp(content, regexp, replacement);
86          }
87  
88          return content;
89      }
90  
91      protected String runRegexp(
92          String content, String regexp, String replacement) {
93  
94          Matcher matcher = Pattern.compile(
95              regexp, Pattern.MULTILINE).matcher(content);
96  
97          StringBuffer sb = new StringBuffer();
98  
99          while (matcher.find()) {
100             matcher.appendReplacement(sb, replacement);
101         }
102 
103         matcher.appendTail(sb);
104 
105         return sb.toString();
106     }
107 
108     protected String unprotectNowikiText(String content) {
109         List<String> hashList = new ArrayList<String>(_protectedMap.keySet());
110 
111         for (int i = hashList.size() - 1; i >= 0; i--) {
112             String hash = hashList.get(i);
113 
114             String protectedMarkup = _protectedMap.get(hash);
115 
116             content = content.replace(hash, protectedMarkup);
117         }
118 
119         return content;
120     }
121 
122     private String _normalizeLineBreaks(String content) {
123         content = StringUtil.replace(
124             content,
125             new String[] {StringPool.RETURN_NEW_LINE, StringPool.RETURN},
126             new String[] {StringPool.NEW_LINE, StringPool.NEW_LINE});
127 
128         return content;
129     }
130 
131     protected Map<String, String> regexps =
132         new LinkedHashMap<String, String>();
133     protected List<String> nowikiRegexps = new LinkedList<String>();
134 
135     private Map<String, String> _protectedMap =
136         new LinkedHashMap<String, String>();
137 
138 }