BrightSide Workbench Full Report + Source Code
ParserHelper.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 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 org.turro.actor.Actors;
22 import org.turro.annotation.ElephantHelper;
23 import org.turro.config.Configurator;
24 import org.turro.elephant.context.ElephantContext;
25 import org.turro.elephant.impl.util.Parser;
26 import org.turro.entities.IElephantEntity;
27 import org.turro.formatter.NumberReducer;
28 import org.turro.html.HtmlContent;
29 import org.turro.i18n.I_;
30 import org.turro.marker.IElephantHelper;
31 import org.turro.parser.wiki.WikiCompiler;
32 import org.turro.plugin.contacts.IContact;
33 import org.turro.script.Scripting;
34 import org.turro.string.Strings;
35 
40 @ElephantHelper
41 public class ParserHelper implements IElephantHelper {
42 
43  // Wiki
44 
45  public String compile(String wiki) {
46  return new Parser("$wiki\n" + wiki).simpleParse();
47  }
48 
49  public String wiki(String wiki) {
50  return WikiCompiler.preview(wiki).html();
51  }
52 
53  // Strings
54 
55  public String trunc(String text, int max) {
56  return Strings.truncate(text, max);
57  }
58 
59  public String html(String html, int max) {
60  return HtmlContent.truncate(html, max);
61  }
62 
63  public String htmlPlain(String html, int max) {
64  return HtmlContent.plain(html, max);
65  }
66 
67  // Bytes
68 
69  public String reduce(long value) {
70  return NumberReducer.reduce(value);
71  }
72 
73  public long expand(String value) {
74  return NumberReducer.expand(value);
75  }
76 
77  // i18n
78 
79  public String i(String value) {
80  return I_.get(value);
81  }
82 
83  public String i(String value, int count) {
84  return I_.get(value, count);
85  }
86 
87  public String f(String value, Object... arguments) {
88  return I_.format(value, arguments);
89  }
90 
91  // Context
92 
93  public String root() {
95  }
96 
97  // Configurator
98 
99  public Configurator configurator() {
100  return Configurator.instance();
101  }
102 
103  public String cfgs(String key) {
104  return Configurator.instance().asString(key);
105  }
106 
107  public Integer cfgi(String key) {
108  return Configurator.instance().asInteger(key);
109  }
110 
111  public Long cfgl(String key) {
112  return Configurator.instance().asLong(key);
113  }
114 
115  public Double cfgd(String key) {
116  return Configurator.instance().asDouble(key);
117  }
118 
119  public Boolean cfgb(String key) {
120  return Configurator.instance().asBoolean(key);
121  }
122 
123  // Actors
124 
125  public static boolean isActor(String actor) {
126  return Actors.isActor(actor);
127  }
128 
129  public static boolean isActor(IContact contact, String actor) {
130  return Actors.isActor(contact, actor);
131  }
132 
133  public static boolean isActorFor(Object entity, String actor) {
134  return Actors.isActorFor(entity, actor);
135  }
136 
137  public static boolean isActorFor(IContact contact, Object entity, String actor) {
138  return Actors.isActorFor(contact, entity, actor);
139  }
140 
141  public static boolean isActorFor(IElephantEntity iee, String actor) {
142  return Actors.isActorFor(iee, actor);
143  }
144 
145  public static boolean isActorFor(IContact contact, IElephantEntity iee, String actor) {
146  return Actors.isActorFor(contact, iee, actor);
147  }
148 
149  // Scripts
150 
151  public static boolean eval(String value) {
152  return Scripting.eval(value);
153  }
154 
155  public static boolean eval(IContact contact, String value) {
156  return Scripting.eval(contact, value);
157  }
158 
159  public static boolean evalFor(Object entity, String value) {
160  return Scripting.evalFor(entity, value);
161  }
162 
163  public static boolean evalFor(IContact contact, Object entity, String value) {
164  return Scripting.evalFor(contact, entity, value);
165  }
166 
167  public static boolean evalFor(IElephantEntity iee, String value) {
168  return Scripting.evalFor(iee, value);
169  }
170 
171  public static boolean evalFor(IContact contact, IElephantEntity iee, String value) {
172  return Scripting.evalFor(contact, iee, value);
173  }
174 
175  // Helper
176 
177  @Override
178  public String getName() {
179  return "parser";
180  }
181 
182 }
static boolean isActorFor(Object entity, String actor)
Definition: Actors.java:53
static boolean isActor(String actor)
Definition: Actors.java:45
static String format(String msg, Object... arguments)
Definition: I_.java:49
static String get(String msg)
Definition: I_.java:41
String f(String value, Object... arguments)
static boolean eval(IContact contact, String value)
static boolean isActor(IContact contact, String actor)
String html(String html, int max)
static boolean eval(String value)
static boolean evalFor(IElephantEntity iee, String value)
static boolean isActorFor(IContact contact, IElephantEntity iee, String actor)
String wiki(String wiki)
static boolean evalFor(IContact contact, IElephantEntity iee, String value)
static boolean isActorFor(IElephantEntity iee, String actor)
String trunc(String text, int max)
static boolean isActor(String actor)
String i(String value)
String htmlPlain(String html, int max)
String compile(String wiki)
static boolean evalFor(Object entity, String value)
static boolean isActorFor(Object entity, String actor)
static boolean evalFor(IContact contact, Object entity, String value)
String i(String value, int count)
static boolean isActorFor(IContact contact, Object entity, String actor)
static boolean evalFor(Object entity, String value)
Definition: Scripting.java:45
static boolean eval(String value)
Definition: Scripting.java:37