use of org.eclipse.smarthome.model.script.engine.ScriptParsingException in project smarthome by eclipse.
the class ScriptEngineImpl method parseScriptIntoXTextEObject.
private XExpression parseScriptIntoXTextEObject(String scriptAsString) throws ScriptParsingException {
XtextResourceSet resourceSet = getResourceSet();
// IS-A XtextResource
Resource resource = resourceSet.createResource(computeUnusedUri(resourceSet));
try {
resource.load(new StringInputStream(scriptAsString, StandardCharsets.UTF_8.name()), resourceSet.getLoadOptions());
} catch (IOException e) {
throw new ScriptParsingException("Unexpected IOException; from close() of a String-based ByteArrayInputStream, no real I/O; how is that possible???", scriptAsString, e);
}
List<Diagnostic> errors = resource.getErrors();
if (errors.size() != 0) {
throw new ScriptParsingException("Failed to parse expression (due to managed SyntaxError/s)", scriptAsString).addDiagnosticErrors(errors);
}
EList<EObject> contents = resource.getContents();
if (!contents.isEmpty()) {
Iterable<Issue> validationErrors = getValidationErrors(contents.get(0));
if (!validationErrors.iterator().hasNext()) {
return (XExpression) contents.get(0);
} else {
throw new ScriptParsingException("Failed to parse expression (due to managed ValidationError/s)", scriptAsString).addValidationIssues(validationErrors);
}
} else {
return null;
}
}
use of org.eclipse.smarthome.model.script.engine.ScriptParsingException in project smarthome by eclipse.
the class ScriptEngineConsoleCommandExtension method execute.
@Override
public void execute(String[] args, Console console) {
if (scriptEngine != null) {
String scriptString = Arrays.stream(args).collect(Collectors.joining(" "));
Script script;
try {
script = scriptEngine.newScriptFromString(scriptString);
Object result = script.execute();
if (result != null) {
console.println(result.toString());
} else {
console.println("OK");
}
} catch (ScriptParsingException e) {
console.println(e.getMessage());
} catch (ScriptExecutionException e) {
console.println(e.getMessage());
}
} else {
console.println("Script engine is not available.");
}
}
Aggregations