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   */
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 }