BrightSide Workbench Full Report + Source Code
CoreParser.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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 
19 package org.turro.parser;
20 
21 import freemarker.ext.dom.NodeModel;
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Date;
25 import java.util.Random;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.Cookie;
30 import javax.xml.parsers.ParserConfigurationException;
31 import org.amic.util.date.DateFormats;
32 import org.turro.collections.parser.ParserException;
33 import org.turro.string.ObjectString;
34 import org.turro.string.Strings;
35 import org.turro.action.ContentService;
36 import org.turro.action.IEntityCtrl;
37 import org.turro.action.Plugins;
38 import org.turro.action.UserSummaries;
39 import org.turro.annotation.ExternalParser;
40 import org.turro.collections.KeyObjectMap;
41 import org.turro.elephant.context.ElephantContext;
42 import org.turro.elephant.impl.feed.FeedMacro;
43 import org.turro.elephant.impl.util.CookieUtil;
44 import org.turro.elephant.impl.util.FileUtil;
45 import org.turro.elephant.impl.util.Files;
46 import org.turro.elephant.impl.util.Parser;
47 import org.turro.elephant.impl.util.StringParser;
48 import org.turro.elephant.security.IUser;
49 import org.turro.entities.Entities;
50 import org.turro.entities.IElephantEntity;
51 import org.turro.html.HTMLHelper;
52 import org.turro.i18n.I_;
53 import org.turro.indicator.IndicatorResult;
54 import org.turro.indicator.Indicators;
55 import org.turro.json.JsonSearch;
56 import org.turro.marker.ElephantMarker;
57 import org.turro.plugin.contacts.IContact;
58 import org.turro.reflection.Reflections;
59 import org.turro.user.content.UserContents;
60 import org.xml.sax.SAXException;
61 
66 @ExternalParser
67 public class CoreParser extends AbstractParser {
68 
69  @Override
70  public String getParserName() {
71  return "Core";
72  }
73 
74  @Override
75  protected boolean doExecute() {
76  boolean done = false;
77  if("webroot".equals(getMacroName())) {
79  done = true;
80  } else if("site".equals(getMacroName())) {
82  done = true;
83  } else if("rendering".equals(getMacroName())) {
85  done = true;
86  } else if("lang".equals(getMacroName())) {
87  out.write("_" + I_.api().used().getLanguage());
88  done = true;
89  } else if("user-summary".equals(getMacroName())) {
90  if("init".equals(getToken(1))) {
92  } else {
94  }
95  done = true;
96  } else if("user-contents".equals(getMacroName())) {
98  done = true;
99  } else if("include".equals(getMacroName())) {
100  try {
102  } catch (ServletException | IOException ex) {
103  Logger.getLogger(CoreParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
104  }
105  done = true;
106  } else if("param".equals(getMacroName())) {
107  String value = constructor.getParameter(getToken(1, getToken(2)));
108  if(!Strings.isBlank(value)) {
109  out.write(value);
110  }
111  done = true;
112  } else if("render".equals(getMacroName())) {
113  try {
115  } catch (ServletException | IOException ex) {
116  Logger.getLogger(CoreParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
117  }
118  done = true;
119  } else if("date".equals(getMacroName())) {
120  Date now = new Date();
121  if("date".equals(getToken(1))) {
122  out.write(DateFormats.format(now, getToken(2), I_.api().used()));
123  } else if("time".equals(getToken(1))) {
124  out.write(DateFormats.formatTime(now, getToken(2), I_.api().used()));
125  } else if("all".equals(getToken(1))) {
126  out.write(DateFormats.format(now, getToken(2), getToken(2), I_.api().used()));
127  }
128  done = true;
129  } else if("feed".equals(getMacroName())) {
131  } else if("marker-json".equals(getMacroName())) {
133  File file = Files.file(constructor, getToken(3));
134  if(file.exists()) {
135  marker.put("json", JsonSearch.of(file));
136  }
137  marker.process(getToken(1), getToken(2));
138  done = true;
139  } else if("marker-xml".equals(getMacroName())) {
141  try {
142  File file = Files.file(constructor, getToken(3));
143  if(file.exists()) {
144  marker.put("doc", NodeModel.parse(file));
145  }
146  } catch (SAXException | ParserConfigurationException | IOException ex) {
147  Logger.getLogger(CoreParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
148  }
149  marker.process(getToken(1), getToken(2));
150  done = true;
151  } else if("marker-props".equals(getMacroName())) {
153  File file = Files.file(constructor, getToken(3));
154  if(file.exists()) {
155  try {
156  marker.put("props", FileUtil.getOrderedProperties(file));
157  } catch (IOException ex) {
158  Logger.getLogger(CoreParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
159  }
160  }
161  marker.process(getToken(1), getToken(2));
162  done = true;
163  } else if("marker".equals(getMacroName())) {
165  try {
166  marker.putAll(new KeyObjectMap(getToken(3)));
167  } catch (ParserException ex) {
168  Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
169  }
170  marker.process(getToken(1), getToken(2));
171  done = true;
172  } else if("files".equals(getMacroName())) {
173  if("sort".equals(getToken(3))) {
175  } else if("shuffle".equals(tokens[3])) {
177  } else {
178  out.write(Files.getJsonFiles(getToken(1), getToken(2)));
179  }
180  done = true;
181  } else if("frame".equals(getMacroName())) {
182  HTMLHelper hh = new HTMLHelper(out);
183  String path = getToken(1);
184  if(path.startsWith("/")) {
185  path = ElephantContext.getRootWebPath() + path;
186  }
187  hh.startTag("iframe", "src=\"" + path +
188  "\" style=\"border:0;margin:0;padding:0;width:" +
189  getToken(2) + ";height:" + getToken(3) + "\"");
190  hh.endTag();
191  done = true;
192  } else if("res".equals(getMacroName())) {
193  String res = I_.compatibility().get(getToken(1));
194  if(res != null) {
195  out.write(Strings.transform(res, ((Integer)ObjectString.parseString(getToken(2), Integer.class, true)).intValue()));
196  }
197  done = true;
198  } else if("i18n".equals(getMacroName())) {
199  String res = I_.get(getToken(1));
200  if(res != null) {
201  out.write(Strings.transform(res, ((Integer)ObjectString.parseString(getToken(2), Integer.class, true)).intValue()));
202  }
203  done = true;
204  } else if("random".equals(getMacroName())) {
205  Random random = new Random();
206  int v = random.nextInt(Integer.valueOf(getToken(1)));
207  out.write(Integer.toString(v));
208  done = true;
209  } else if("context".equals(getMacroName())) {
210  String[] s = constructor.getCurrentContext().getPath().split("\\/");
211  int i = Integer.valueOf(getToken(1));
212  if(i < s.length) {
213  out.write(s[i]);
214  }
215  done = true;
216  } else if("user".equals(getMacroName())) {
217  IContact user = constructor.getUser();
218  if(user != null) {
219  if("name".equals(getToken(1))) {
220  out.write(user.getName());
221  }
222  else if("id".equals(getToken(1))) {
223  out.write(user.getId());
224  }
225  else if("email".equals(getToken(1))) {
226  out.write(user.getProperty(IUser.CONNECTOR_EMAIL));
227  }
228  }
229  done = true;
230  } else if("if".equals(getMacroName())) {
231  out.write(constructor.isInRole(getToken(1) + ":" + getToken(2)) ? getToken(3) : getToken(4));
232  done = true;
233  } else if("ifcookie".equals(getMacroName())) {
234  Cookie cookie = CookieUtil.getCookie(constructor.getRequest(), getToken(1));
235  if(cookie != null) {
236  out.write(getToken(2).equals(cookie.getValue()) ? getToken(3) : getToken(4));
237  }
238  done = true;
239  } else if("class".equals(getMacroName())) {
240  out.write(Reflections.exist(getToken(1)) ? getToken(2) : getToken(3));
241  done = true;
242  } else if("equals".equals(getMacroName())) {
243  out.write(getToken(1).equals(getToken(2)) ? getToken(3) : getToken(4));
244  done = true;
245  } else if("object".equals(getMacroName())) {
246  Object entity = constructor.findAttribute(getToken(1));
247  if(entity == null) {
248  entity = Entities.getController(getToken(1)).getEntity();
249  }
250  if(entity != null) {
251  Object obj = StringParser.getValueFrom(getToken(2), entity);
252  if(obj != null) {
253  out.write(obj.toString());
254  }
255  }
256  done = true;
257  } else if("attribute".equals(getMacroName())) {
258  Object obj = constructor.findAttribute(getToken(1));
259  if(obj != null) {
260  String format = getToken(2);
261  if(!Strings.isBlank(format)) {
262  out.format(I_.api().used(), format, obj);
263  } else {
264  out.write(obj.toString());
265  }
266  }
267  done = true;
268  } else if("entity".equals(getMacroName())) {
270  Object[] desc = new Object[] { ee.getName(), ee.getDescription() };
271  if(ee != null) {
272  out.format(getToken(2), desc);
273  }
274  done = true;
275  } else if("entity-ctrl".equals(getMacroName())) {
277  if(ec != null) {
278  ec.setEntityPath(getToken(2));
279  ec.setTemplate(getToken(3));
280  if(ec.hasContent()) {
281  out.write(ec.parse(constructor));
282  }
283  }
284  done = true;
285  } else if("cookie".equals(getMacroName())) {
286  String value = getToken(2);
287  if(Strings.isBlank(value)) {
288  Cookie cookie = CookieUtil.getCookie(constructor.getRequest(), getToken(1));
289  if(cookie != null) out.write(cookie.getValue());
290  } else {
291  CookieUtil.setCookie(constructor.getResponse(), getToken(1), value, "/", 30*24*60*60);
292  out.write(value);
293  }
294  done = true;
295  } else if("indicator".equals(getMacroName())) {
296  String indicator = null;
297  indicator = getToken(1) + ":" + getToken(2) + ":" + getToken(3) +
298  (getToken(5) != null ? "." + getToken(5) : "") + "." + getToken(4);
299  IndicatorResult ir = Indicators.readIndicator(indicator);
300  out.write(ObjectString.formatObject(Double.valueOf(ir.getValue()), "#,##0.00", true));
301  out.write(" [" + ir.getCount() + "]");
302  done = true;
303  } else if("entry-point".equals(getMacroName())) {
305  done = true;
306  }
307  return done;
308  }
309 
310  @Override
311  protected void explainMacros(ParserMacroSet macros) {
312  macros.addMacro("webroot");
313  macros.addMacro("site");
314  macros.addMacro("rendering");
315  macros.addMacro("lang");
316  macros.addMacro("user-summary")
317  .addParameter("mode", true)
318  .addPossibleValue("init");
319  macros.addMacro("user-contents");
320  macros.addMacro("include")
321  .addParameter("content-file", false);
322  macros.addMacro("param")
323  .addParameter("name", false)
324  .addParameter("default", true);
325  macros.addMacro("render")
326  .addParameter("context", false)
327  .addParameter("stopper", true);
328  macros.addMacro("date")
329  .addParameter("mode", true, "date")
330  .addPossibleValue("date")
331  .addPossibleValue("time")
332  .addPossibleValue("all")
333  .addParameter("format", true, "short")
334  .addPossibleValue("full")
335  .addPossibleValue("long")
336  .addPossibleValue("medium")
337  .addPossibleValue("short");
338  macros.addMacro("feed")
339  .addParameter("url", false);
340  macros.addMacro("marker")
341  .addParameter("tmpl-root", false)
342  .addParameter("template", false)
343  .addParameter("value-map", false);
344  macros.addMacro("marker-props")
345  .addParameter("tmpl-root", false)
346  .addParameter("template", false)
347  .addParameter("repository-file", false);
348  macros.addMacro("marker-xml")
349  .addParameter("tmpl-root", false)
350  .addParameter("template", false)
351  .addParameter("repository-file", false);
352  macros.addMacro("marker-json")
353  .addParameter("tmpl-root", false)
354  .addParameter("template", false)
355  .addParameter("repository-file", false);
356  macros.addMacro("files")
357  .addParameter("folder", false)
358  .addParameter("pattern", false)
359  .addParameter("mode", false)
360  .addPossibleValue("normal")
361  .addPossibleValue("sort")
362  .addPossibleValue("shuffle");
363  macros.addMacro("frame")
364  .addParameter("url", false)
365  .addParameter("width", false)
366  .addParameter("height", false);
367  macros.addMacro("res")
368  .addParameter("resource-key", false)
369  .addParameter("string-mode", true, "0");
370  macros.addMacro("i18n")
371  .addParameter("resource", false)
372  .addParameter("string-mode", true, "0");
373  macros.addMacro("random")
374  .addParameter("random-limit", false);
375  macros.addMacro("context")
376  .addParameter("level", false);
377  macros.addMacro("user")
378  .addParameter("mode", true, "name")
379  .addPossibleValue("name")
380  .addPossibleValue("id")
381  .addPossibleValue("email");
382  macros.addMacro("if")
383  .addParameter("role", false)
384  .addParameter("attribute", false)
385  .addParameter("true-expression", false)
386  .addParameter("false-expression", false);
387  macros.addMacro("ifcookie")
388  .addParameter("cookie", false)
389  .addParameter("value", false)
390  .addParameter("true-expression", false)
391  .addParameter("false-expression", false);
392  macros.addMacro("class")
393  .addParameter("class-name", false)
394  .addParameter("true-expression", false)
395  .addParameter("false-expression", false);
396  macros.addMacro("equals")
397  .addParameter("value", false)
398  .addParameter("value", false)
399  .addParameter("true-expression", false)
400  .addParameter("false-expression", false);
401  macros.addMacro("object")
402  .addParameter("ref-object", false)
403  .addParameter("attribute", false);
404  macros.addMacro("attribute")
405  .addParameter("attribute", false)
406  .addParameter("format", true);
407  macros.addMacro("entity")
408  .addParameter("entityPath", false)
409  .addParameter("fmt-name-desc", false);
410  macros.addMacro("entity-ctrl")
411  .addParameter("plugin", false)
412  .addParameter("entityPath", false)
413  .addParameter("template", true);
414  macros.addMacro("cookie")
415  .addParameter("name", false)
416  .addParameter("value", true);
417  macros.addMacro("indicator")
418  .addParameter("storeClass", false)
419  .addParameter("indicatorClass", false)
420  .addParameter("entityPath", false)
421  .addParameter("indicator", false)
422  .addParameter("relatedPath", true);
423  macros.addMacro("entry-point")
424  .addParameter("identifier", false)
425  .addParameter("values", true);
426  }
427 
428 }
static String createEntryPoint(IConstructor constructor, String entryPoint, String valueStr)
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static void initSummaries(IConstructor constructor)
static String parseSummaries(IConstructor constructor)
static void writeFeed(IConstructor constructor, String feeds)
Definition: FeedMacro.java:40
static Cookie getCookie(HttpServletRequest request, String name)
Definition: CookieUtil.java:36
static void setCookie(HttpServletResponse response, String name, String value, String path, int age)
Definition: CookieUtil.java:45
static Properties getOrderedProperties(File file)
Definition: FileUtil.java:180
static String getJsonFiles(final String path, final String pattern)
Definition: Files.java:37
static String getJsonSortedFiles(final String path, final String pattern)
Definition: Files.java:41
static String getJsonShuffledFiles(final String path, final String pattern)
Definition: Files.java:47
static File file(IConstructor constructor, String file)
Definition: Files.java:71
static Object getValueFrom(String pathToValue, Object object)
static IElephantEntity getController(String path)
Definition: Entities.java:78
HTMLGenerator startTag(String tag)
static String get(String msg)
Definition: I_.java:41
static I18nApiWrapper api()
Definition: I_.java:65
static I18nCompatibilityWrapper compatibility()
Definition: I_.java:109
static IndicatorResult readIndicator(String indicator)
Definition: Indicators.java:99
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
void explainMacros(ParserMacroSet macros)
ParserMacro addMacro(String macroName)
ParserMacro addPossibleValue(String value)
ParserMacro addParameter(String name, boolean optional)
static String parseContents(IConstructor constructor)
void setEntityPath(String entityPath)
String parse(IConstructor constructor)
void setTemplate(String template)
void renderContext(String path, String stopper)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27
String getProperty(String key)