BrightSide Workbench Full Report + Source Code
Scripting.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.script;
20 
21 import java.io.IOException;
22 import org.turro.action.Contacts;
23 import org.turro.elephant.context.Application;
24 import org.turro.elephant.context.ElephantContext;
25 import org.turro.entities.Entities;
26 import org.turro.entities.IElephantEntity;
27 import org.turro.file.Document;
28 import org.turro.plugin.contacts.IContact;
29 import org.turro.reflection.Instances;
30 
35 public class Scripting {
36 
37  public static boolean eval(String value) {
38  return evalFor((IContact) Application.getUser(), null, value);
39  }
40 
41  public static boolean eval(IContact contact, String value) {
42  return evalFor(contact, null, value);
43  }
44 
45  public static boolean evalFor(Object entity, String value) {
46  return evalFor((IContact) Application.getUser(), entity, value);
47  }
48 
49  public static boolean evalFor(IContact contact, Object entity, String value) {
50  return evalFor(contact, Entities.getController(entity), value);
51  }
52 
53  public static boolean evalFor(IElephantEntity iee, String value) {
54  return evalFor((IContact) Application.getUser(), iee, value);
55  }
56 
57  public static boolean evalFor(IContact contact, IElephantEntity iee, String value) {
58  if(Contacts.isValid(contact)) {
59  Script script = iee == null ? null : iee.getActorScript();
60  if(script == null) {
61  script = instance();
62  }
63  if(script != null) {
64  script.addVariable("user", contact);
65  return script.evalToBoolean(Scripting.realScript(value));
66  }
67  }
68  return false;
69  }
70 
71  /* Factory */
72 
73  public static final String SCRIPT_FOLDER = "/WEB-INF/elephant/scripts";
74 
75  public static String realScript(String script) {
76  if(script.startsWith("file:")) {
77  return Scripting.scriptContent(script.substring(5));
78  } else {
79  return script;
80  }
81  }
82 
83  public static String scriptContent(String scriptName) {
84  try {
85  String file = ElephantContext.getRealPath(SCRIPT_FOLDER + "/" + scriptName);
86  return Document.from(file).content();
87  } catch (IOException ex) {
88  return null;
89  }
90  }
91 
92  public static Script instance() {
93  Script script = new Script();
94  script.addVariable("pfunc", new ProfileFunctions());
95  Instances.cached().byAnnotation(ScriptingFunction.class, IScriptingFunction.class).forEach((iScript) -> {
96  iScript.addFunctions(script);
97  });
98  return script;
99  }
100 
101 }
static boolean isValid(IContact contact)
Definition: Contacts.java:52
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String realScript(String script)
Definition: Scripting.java:75
static final String SCRIPT_FOLDER
Definition: Scripting.java:73
static boolean evalFor(IContact contact, Object entity, String value)
Definition: Scripting.java:49
static boolean eval(IContact contact, String value)
Definition: Scripting.java:41
static boolean evalFor(Object entity, String value)
Definition: Scripting.java:45
static Script instance()
Definition: Scripting.java:92
static boolean evalFor(IContact contact, IElephantEntity iee, String value)
Definition: Scripting.java:57
static String scriptContent(String scriptName)
Definition: Scripting.java:83
static boolean eval(String value)
Definition: Scripting.java:37
static boolean evalFor(IElephantEntity iee, String value)
Definition: Scripting.java:53