View Javadoc

1   /*-
2    * #%L
3    * prolobjectlink-jpi
4    * %%
5    * Copyright (C) 2012 - 2019 Prolobjectlink Project
6    * %%
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   * #L%
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   * Partial implementation of {@link ScriptEngineFactory}
37   * 
38   * @author Jose Zalacain
39   * @since 1.0
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 }