1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
38   * <a href="BaseTranslator.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Jorge Ferrer
41   */
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 }