Search in sources :

Example 1 with ScriptParsingException

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;
    }
}
Also used : StringInputStream(org.eclipse.xtext.util.StringInputStream) ScriptParsingException(org.eclipse.smarthome.model.script.engine.ScriptParsingException) Issue(org.eclipse.xtext.validation.Issue) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) XExpression(org.eclipse.xtext.xbase.XExpression) IOException(java.io.IOException)

Example 2 with ScriptParsingException

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.");
    }
}
Also used : Script(org.eclipse.smarthome.model.script.engine.Script) ScriptParsingException(org.eclipse.smarthome.model.script.engine.ScriptParsingException) ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException)

Aggregations

ScriptParsingException (org.eclipse.smarthome.model.script.engine.ScriptParsingException)2 IOException (java.io.IOException)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 Diagnostic (org.eclipse.emf.ecore.resource.Resource.Diagnostic)1 Script (org.eclipse.smarthome.model.script.engine.Script)1 ScriptExecutionException (org.eclipse.smarthome.model.script.engine.ScriptExecutionException)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)1 StringInputStream (org.eclipse.xtext.util.StringInputStream)1 Issue (org.eclipse.xtext.validation.Issue)1 XExpression (org.eclipse.xtext.xbase.XExpression)1