BrightSide Workbench Full Report + Source Code
Parser.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.util;
19 
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import javax.servlet.ServletException;
27 import org.turro.string.Strings;
28 import org.jsoup.Jsoup;
29 import org.turro.annotation.ExternalParser;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.context.IConstructor;
32 import org.turro.html.HtmlWhitelist;
33 import org.turro.i18n.I_;
34 import org.turro.parser.IParser;
35 import org.turro.parser.processor.MacroProcessors;
36 import org.turro.reflection.Instances;
37 import org.turro.syntax.parser.ExternalResolver;
38 import org.turro.syntax.parser.HtmlParser;
39 import org.turro.syntax.wiki.WikiDef;
40 
45 public class Parser implements ExternalResolver {
46 
47  private IConstructor constructor;
48  private StringBuffer buffer;
49  private ExternalResolver resolver;
50 
53  public Parser(String text) {
54  this(text, null);
55  }
56 
60  public Parser(String text, String end) {
61  int p = -1;
62  if(end != null) {
63  p = text.indexOf(end);
64  }
65  if(p > -1) {
66  buffer = new StringBuffer(text.substring(0, p));
67  }
68  else {
69  buffer = new StringBuffer(text);
70  }
71  // Correct lists
72  if(text.startsWith("$wiki\n * ") || text.startsWith("$wiki\n 1 ")) {
73  buffer.insert(6, "\n");
74  }
75  }
76 
77  public static String toWiki(String text) {
78  text = Jsoup.clean(text, HtmlWhitelist.wikiSafe());
79  return new HtmlParser(text).parse();
80  }
81 
82  public void setResolver(ExternalResolver resolver) {
83  this.resolver = resolver;
84  }
85 
86  public String parse() {
87  return parse(null);
88  }
89 
90  public String parse(IConstructor constructor) {
91  this.constructor = constructor;
92  WikiDef wikiDef = new WikiDef();
93  if(resolver != null) wikiDef.addResolver(resolver);
94  wikiDef.addResolver(this);
95  return wikiDef.parseBuffer(buffer);
96  }
97 
98  public String simpleParse() {
99  WikiDef wikiDef = new WikiDef();
100  return wikiDef.parseBuffer(buffer);
101  }
102 
103  public String parseThumb(String line) {
104  if(constructor == null || line.indexOf("[thumb") == -1) return line;
105  Pattern pat = Pattern.compile("\\[thumb (.+?)\\:([0-9]+?)(\\:[RL])?\\]");
106  Matcher mat = pat.matcher(line);
107  StringBuffer result = new StringBuffer();
108  int dim = 0;
109  while(mat.find()) {
110  try {
111  dim = Integer.parseInt(mat.group(2));
112  } catch(Exception ex) {}
113  mat.appendReplacement(
114  result,
115  "<a target=\"_blank\" href=\"" + mat.group(1) + "\">" +
116  "<img border=\"0\" src=\"" +
118  new Thumbs(constructor.getRequest()).getThumb(
119  mat.group(1),
120  dim,
121  mat.group(3)
122  )
123  ) +
124  "\"/>" +
125  "</a>"
126  );
127  }
128  mat.appendTail(result);
129  return result.toString();
130  }
131 
132  public String parseMacros(String line) {
133  if(constructor == null || line.indexOf("macro") == -1) return line;
134  Pattern pat = Pattern.compile("macro ?\\((.*?)\\)");
135  Matcher mat = pat.matcher(line);
136  StringBuffer result = new StringBuffer();
137  String tmp;
138  while(mat.find()) {
139  tmp = I_.get(mat.group(1));
140  if(tmp == null) tmp = mat.group(1);
141  mat.appendReplacement(result, tmp);
142  }
143  mat.appendTail(result);
144  return result.toString();
145  }
146 
147  @Override
148  public String resolve(String text) {
149  if(!constructor.isInRole("inc:new")) {
150  text = text.replaceAll("\\{\\@inc", "{@---");
151  }
152  text = text.replaceAll(
153  "grepository ?\\((.*?)\\)",
155  "/_internal/repository$1"
156  );
157  if(constructor.getParameter("context") != null) {
158  text = text.replaceAll(
159  "repository ?\\((.*?)\\)",
161  constructor.getParameter("context") +
162  "/_internal/repository$1"
163  );
164  }
165  text = text.replaceAll(
166  "pubimage ?\\((.*?)\\)",
168  "/_internal/UserFiles/Image$1"
169  );
170  text = text.replaceAll(
171  "pubflash ?\\((.*?)\\)",
173  "/_internal/UserFiles/Flash$1"
174  );
175  text = text.replaceAll(
176  "download ?\\((.*?)\\)",
177  ElephantContext.getRootWebPath() + "/Download?down_path=$1"
178  );
179  text = text.replaceAll("<verbatim>", "<pre>");
180  text = text.replaceAll("</verbatim>", "</pre>");
181 // text = text.replaceAll("\\[download \\[(.*?)\\]\\[(.*?)\\]\\]", "<a href='" + context + "/Download?path=$1'>$2</a>");
182 // text = text.replaceAll("\\[download \\[(.*?)\\](.*?)\\]", "<a href='" + context + "/Download?path=$1'>$2</a>");
183  text = text.replaceAll("\\[\\[(.*?)\\]\\[(.*?)\\]\\]", "<a href='$1'>$2</a>");
184  text = text.replaceAll("\\[new\\[(.*?)\\]\\[(.*?)\\]\\]", "<a class='newwindow' target='_blank' href='$1'>$2</a>");
185  text = text.replaceAll("\\[ext\\[(.*?)\\]\\[(.*?)\\]\\]", "<a class='externallink' target='_blank' href='$1'>$2</a>");
186  text = text.replaceAll("\\[\\[(.*?)\\]\\]", "<a href='$1'>$1</a>");
187  text = text.replaceAll("\\[\\[(.*?)\\](.*?)\\]", "<a href='$1'>$2</a>");
188  text = text.replaceAll("\\[new\\[(.*?)\\](.*?)\\]", "<a class='newwindow' target='_blank' href='$1'>$2</a>");
189  text = text.replaceAll("\\[new\\[(.*?)\\]\\]", "<a class='newwindow' target='_blank' href='$1'>$1</a>");
190  text = text.replaceAll("\\[ext\\[(.*?)\\](.*?)\\]", "<a class='externallink' target='_blank' href='$1'>$2</a>");
191  text = text.replaceAll("\\[ext\\[(.*?)\\]\\]", "<a class='externallink' target='_blank' href='$1'>$1</a>");
192  text = text.replaceAll("\\[color\\((.*?)\\) (.*?)\\]", "<span style='color:$1'>$2</span>");
193  text = parseThumb(text);
194  text = text.replaceAll("\\[frame ([^,]*?),([^,]*?),(.*?)\\]", "<iframe src='$1' style='width:$2;height:$3;border:0px solid gray'></iframe>");
195  text = text.replaceAll("\\%BR\\%", "<br/>\n");
196  text = Emoticons.replaceEmoticons(constructor, text);
197  return text;
198  }
199 
200  public static void processMacros(IConstructor constructor, PrintWriter out, String text) throws ServletException, IOException {
201  text = MacroProcessors.full().constructor(constructor).process(text);
202  if(text.indexOf("{@") > -1) {
203  text = processNestedMacros(constructor, text);
204  int p = 0, e = 0;
205  String tmp;
206  while((p = text.indexOf("{@")) > -1) {
207  e = text.indexOf('}', p + 2);
208  if(e > -1) {
209  tmp = text.substring(p + 2, e);
210  out.write(text.substring(0, p));
211  Parser.processMacro(constructor, out, tmp);
212  text = text.substring(e + 1);
213  }
214  }
215  }
216  out.write(text);
217  out.flush();
218  }
219 
220 
221  private static String processNestedMacros(IConstructor constructor, String text) throws ServletException, IOException {
222  List<String> l = Strings.extractAll(text, "\\{\\@[^\\{\\@]*(\\{\\@[^\\}]*\\})");
223  for(String s : l) {
224  text = text.replaceAll(Strings.literal(s), processMacroString(constructor, s.substring(2, s.length() - 1)));
225  }
226  return text;
227  }
228 
229  public static String processMacroString(IConstructor constructor, String macro) throws ServletException, IOException {
230  StringWriter sw = new StringWriter();
231  PrintWriter pw = new PrintWriter(sw);
232  processMacro(constructor, pw, macro);
233  return sw.toString();
234  }
235 
236  public static void processMacro(IConstructor constructor, PrintWriter out, String macro) throws ServletException, IOException {
237  String tokens[] = macro.split("(?<!\\/)(\\:)");
238  executeExternalParsers(constructor, out, tokens);
239  }
240 
241  public static String escapeGrouping(String sequence) {
242  if(sequence == null || !sequence.contains("$")) return sequence;
243  StringBuilder sb = new StringBuilder(sequence);
244  int start = 0, p;
245  while((p = sb.indexOf("$", start)) > -1) {
246  sb.insert(p, '\\');
247  start = p + 2;
248  }
249  return sb.toString();
250  }
251 
252  /* External Parsers */
253 
254  public static void executeExternalParsers(IConstructor constructor, PrintWriter out, String[] tokens) {
255  for(IParser iParser : Instances.cached().byAnnotation(ExternalParser.class, IParser.class)) {
256  if(iParser.execute(constructor, out, tokens)) {
257  break;
258  }
259  }
260  }
261 
262 }
static String replaceEmoticons(IConstructor constructor, String text)
Definition: Emoticons.java:36
static void processMacros(IConstructor constructor, PrintWriter out, String text)
Definition: Parser.java:200
static String toWiki(String text)
Definition: Parser.java:77
static String escapeGrouping(String sequence)
Definition: Parser.java:241
Parser(String text, String end)
Definition: Parser.java:60
String parseMacros(String line)
Definition: Parser.java:132
String resolve(String text)
Definition: Parser.java:148
String parse(IConstructor constructor)
Definition: Parser.java:90
static void processMacro(IConstructor constructor, PrintWriter out, String macro)
Definition: Parser.java:236
static String processMacroString(IConstructor constructor, String macro)
Definition: Parser.java:229
static void executeExternalParsers(IConstructor constructor, PrintWriter out, String[] tokens)
Definition: Parser.java:254
String parseThumb(String line)
Definition: Parser.java:103
void setResolver(ExternalResolver resolver)
Definition: Parser.java:82
String getThumb(String file, int maxDim, String rotate)
Definition: Thumbs.java:46
static String get(String msg)
Definition: I_.java:41