1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jexl3.scripting;
19
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23
24 import javax.script.ScriptEngine;
25 import javax.script.ScriptEngineFactory;
26
27 import org.apache.commons.jexl3.parser.StringParser;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class JexlScriptEngineFactory implements ScriptEngineFactory {
45
46 @Override
47 public String getEngineName() {
48 return "JEXL Engine";
49 }
50
51 @Override
52 public String getEngineVersion() {
53 return "3.4";
54 }
55
56 @Override
57 public List<String> getExtensions() {
58 return Collections.unmodifiableList(Arrays.asList("jexl", "jexl2", "jexl3"));
59 }
60
61 @Override
62 public String getLanguageName() {
63 return "JEXL";
64 }
65
66 @Override
67 public String getLanguageVersion() {
68 return "3.4";
69 }
70
71 @Override
72 public String getMethodCallSyntax(final String obj, final String m, final String... args) {
73 final StringBuilder sb = new StringBuilder();
74 sb.append(obj);
75 sb.append('.');
76 sb.append(m);
77 sb.append('(');
78 boolean needComma = false;
79 for(final String arg : args){
80 if (needComma) {
81 sb.append(',');
82 }
83 sb.append(arg);
84 needComma = true;
85 }
86 sb.append(')');
87 return sb.toString();
88 }
89
90 @Override
91 public List<String> getMimeTypes() {
92 return Collections.unmodifiableList(Arrays.asList("application/x-jexl",
93 "application/x-jexl2",
94 "application/x-jexl3"));
95 }
96
97 @Override
98 public List<String> getNames() {
99 return Collections.unmodifiableList(Arrays.asList("JEXL", "Jexl", "jexl",
100 "JEXL2", "Jexl2", "jexl2",
101 "JEXL3", "Jexl3", "jexl3"));
102 }
103
104 @Override
105 public String getOutputStatement(final String toDisplay) {
106 if (toDisplay == null) {
107 return "JEXL.out.print(null)";
108 }
109 return "JEXL.out.print("+StringParser.escapeString(toDisplay, '\'')+")";
110 }
111
112 @Override
113 public Object getParameter(final String key) {
114 switch (key) {
115 case ScriptEngine.ENGINE:
116 return getEngineName();
117 case ScriptEngine.ENGINE_VERSION:
118 return getEngineVersion();
119 case ScriptEngine.NAME:
120 return getNames();
121 case ScriptEngine.LANGUAGE:
122 return getLanguageName();
123 case ScriptEngine.LANGUAGE_VERSION:
124 return getLanguageVersion();
125 case "THREADING":
126
127
128
129
130
131
132 default:
133 return null;
134 }
135 }
136
137 @Override
138 public String getProgram(final String... statements) {
139 final StringBuilder sb = new StringBuilder();
140 for(final String statement : statements){
141 sb.append(statement.trim());
142 if (!statement.endsWith(";")){
143 sb.append(';');
144 }
145 }
146 return sb.toString();
147 }
148
149 @Override
150 public ScriptEngine getScriptEngine() {
151 return new JexlScriptEngine(this);
152 }
153
154 }