All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.siwenyan.si.SiCommandExecutorRunScript Maven / Gradle / Ivy

There is a newer version: 1.25.1.0
Show newest version
package com.github.siwenyan.si;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SiCommandExecutorRunScript extends SiCommandExecutor {

    private static final Pattern p = Pattern.compile("\\s*document\\.setVar\\(\\s*'(.+)'\\s*,\\s*'(.+)'\\s*\\)\\s*;\\s*");

    @Override
    public void execute(SiCommand command, SiContext siContext) throws SiVerifyException {
        String script = command.getSiModelCommand().getTarget();
        script = siContext.dynamic(script, "'", "'");

        try {
            Matcher m = p.matcher(script);
            if (m.matches()) {
                String name = m.group(1);
                String value = m.group(2);
                siContext.addVar(name, value);
            } else {
                String name = command.getSiModelCommand().getValue();
                Object value = siContext.asJavascriptExecutor().executeScript(script);
                if (null != name && !name.trim().isEmpty()) {
                    siContext.getReporter().reportMessage("name: " + name);
                    siContext.getReporter().reportMessage("value: " + value);
                    siContext.addVar(name, value);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Fail to run script: " + script + " | " + e.getMessage());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy