1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package io.github.prolobjectlink.prolog;
27
28 import java.util.Arrays;
29 import java.util.Iterator;
30 import java.util.List;
31
32 import javax.script.ScriptEngine;
33 import javax.script.ScriptEngineFactory;
34
35
36
37
38
39
40
41 public abstract class PrologScriptEngineFactory implements ScriptEngineFactory {
42
43 private final PrologEngine engine;
44
45 public PrologScriptEngineFactory(PrologEngine engine) {
46 this.engine = engine;
47 }
48
49 public final String getEngineName() {
50 return engine.getName();
51 }
52
53 public final String getEngineVersion() {
54 return engine.getVersion();
55 }
56
57 public final List<String> getExtensions() {
58 return Arrays.asList("pro", "pl");
59 }
60
61 public final List<String> getMimeTypes() {
62 return Arrays.asList("text/plain");
63 }
64
65 public final List<String> getNames() {
66 return Arrays.asList(getEngineName(), "Prolog", "prolog");
67 }
68
69 public final String getLanguageName() {
70 return "Prolog";
71 }
72
73 public final String getLanguageVersion() {
74 return engine.getVersion();
75 }
76
77 public final Object getParameter(String key) {
78 if (key.equals(ScriptEngine.ENGINE)) {
79 return getEngineName();
80 } else if (key.equals(ScriptEngine.ENGINE_VERSION)) {
81 return getEngineVersion();
82 } else if (key.equals(ScriptEngine.LANGUAGE)) {
83 return getLanguageName();
84 } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) {
85 return getLanguageVersion();
86 } else if (key.equals(ScriptEngine.NAME)) {
87 return getEngineName();
88 }
89 return null;
90 }
91
92 public final String getOutputStatement(String toDisplay) {
93 return "write('" + toDisplay + "')";
94 }
95
96 public final String getProgram(String... statements) {
97 StringBuilder b = new StringBuilder();
98 Iterator<String> i = new ArrayIterator<String>(statements);
99 if (i.hasNext()) {
100 while (i.hasNext()) {
101 b.append(i.next());
102 if (i.hasNext()) {
103 b.append(".\n");
104 }
105 }
106 b.append('.');
107 }
108 return "" + b + "";
109 }
110
111 public final ScriptEngine getScriptEngine() {
112 return new PrologScriptEngine(this, engine);
113 }
114
115 @Override
116 public String toString() {
117 return "PrologScriptEngineFactory [engine=" + engine + "]";
118 }
119
120 @Override
121 public int hashCode() {
122 final int prime = 31;
123 int result = 1;
124 result = prime * result + ((engine == null) ? 0 : engine.getName().hashCode());
125 result = prime * result + ((engine == null) ? 0 : engine.getVersion().hashCode());
126 return result;
127 }
128
129 @Override
130 public boolean equals(Object object) {
131 if (this == object)
132 return true;
133 if (object == null)
134 return false;
135 if (getClass() != object.getClass())
136 return false;
137 PrologScriptEngineFactory other = (PrologScriptEngineFactory) object;
138 if (engine == null) {
139 if (other.engine != null)
140 return false;
141 } else if (!engine.getName().equals(other.engine.getName())) {
142 return false;
143 } else if (!engine.getVersion().equals(other.engine.getVersion())) {
144 return false;
145 }
146 return true;
147 }
148
149 }