编译脚本

@param script 脚本内容
@return {@link CompiledScript}
@throws ScriptRuntimeException 脚本异常
@since 3.2.0
421666
public static CompiledScript compile(String script) throws ScriptRuntimeException {
		try {
			return compile(getJavaScriptEngine(), script);
		} catch (ScriptException e) {
			throw new ScriptRuntimeException(e);
		}
	}
280594
public CompiledScript compile(String scriptSource) throws ScriptException {
        try {
            return new GroovyCompiledScript(this,
                    getScriptClass(scriptSource));
        } catch (SyntaxException e) {
            throw new ScriptException(e.getMessage(),
                    e.getSourceLocator(), e.getLine());
        } catch (IOException e) {
            throw new ScriptException(e);
        } catch (CompilationFailedException ee) {
            throw new ScriptException(ee);
        }
    }
786251
public GroovyActionBuilder script(Resource scriptResource, Charset charset) {
		try {
			action.setScript(FileUtils.readToString(scriptResource, charset));
		} catch (IOException e) {
			throw new CitrusRuntimeException("Failed to read script resource file", e);
		}
		return this;
	}
842758
protected void runScript(String mapperScript, Slurper slurper, Builder builder)
            throws ActivityException, TransformerException {

        CompilerConfiguration compilerConfig = new CompilerConfiguration();
        compilerConfig.setScriptBaseClass(CrossmapScript.class.getName());

        Binding binding = new Binding();
        binding.setVariable("runtimeContext", getRuntimeContext());
        binding.setVariable(slurper.getName(), slurper.getInput());
        binding.setVariable(builder.getName(), builder);
        GroovyShell shell = new GroovyShell(getPackage().getCloudClassLoader(), binding, compilerConfig);
        Script gScript = shell.parse(mapperScript);
        gScript.run();
    }
1358047
public void setPostTransScript( final String script) {
		setPostTransProgram(readProgram(script));
		if ( script != null) format.getHeader().createEntry( POSTTRANSSCRIPT, script);
	}
421692
public static <T> T executeGroovyShellScript(final String script,
                                                 final Map<String, Object> variables,
                                                 final Class<T> clazz) {
        try {
            val binding = new Binding();
            val shell = new GroovyShell(binding);
            if (variables != null && !variables.isEmpty()) {
                variables.forEach(binding::setVariable);
            }
            if (!binding.hasVariable("logger")) {
                binding.setVariable("logger", LOGGER);
            }
            LOGGER.debug("Executing groovy script [{}] with variables [{}]", script, binding.getVariables());

            val result = shell.evaluate(script);
            return getGroovyScriptExecutionResultOrThrow(clazz, result);
        } catch (final Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return null;
    }
1070287
protected void _generate(SarlScript script, IExtraLanguageGeneratorContext context) {
		if (script != null) {
			for (final XtendTypeDeclaration content : script.getXtendTypes()) {
				if (context.getCancelIndicator().isCanceled()) {
					return;
				}
				try {
					generate(content, context);
				} finally {
					context.clearData();
				}
			}
		}
	}
477597
public static Object eval(String script, ScriptContext context) throws ScriptRuntimeException {
		try {
			return compile(script).eval(context);
		} catch (ScriptException e) {
			throw new ScriptRuntimeException(e);
		}
	}
1154943
protected final void registStaticScript(Class<? extends T> scriptClass)
	{
		try {
			boolean isAbstractOrInterface = Modifier.isAbstract(scriptClass.getModifiers())
					|| Modifier.isInterface(scriptClass.getModifiers());// 是否是抽象类
			if(isAbstractOrInterface)
			{
				throw new UnsupportedOperationException(scriptClass +"can not newInstance");
			}
			T script = scriptClass.newInstance();
			int code=script.getMessageCode();
			Class<? extends T> old=staticCodeMap.get(code);
			staticCodeMap.put(code,scriptClass);
			if(old==null)
			{
				_log.info("regist Static codeScript,code="+code+"(0x"+Integer.toHexString(code)+"),class="+scriptClass);
			}else
			{
				_log.info("regist Static codeScript,code="+code+"(0x"+Integer.toHexString(code)+"),class="+scriptClass+",Override script="+old);
			}
		} catch (Exception e) {
			_log.error(e.getMessage(),e);
		}
	}
553408
public void runScript() {
		if (script.trim().length() > 0) {
			BufferedReader reader = new BufferedReader(new StringReader(script));
			try {
				List results = runScript(reader);
				System.out.println(new LogEntry(Level.CRITICAL, "completed run of script in device '\" + this.deviceId + \"'\""));
			} catch (Throwable t) {
				throw new ConfigurationException("failed to complete run of script", t);
			}
		}
	}
258481
public JavaScriptActionBuilder javascript(Resource script, Charset charset) {
        JavaScriptAction action = new JavaScriptAction();
        try {
            action.setScript(FileUtils.readToString(script, charset));
        } catch (IOException e) {
            throw new CitrusRuntimeException("Failed to read script resource", e);
        }
        action(action);
        return new JavaScriptActionBuilder(action);
    }
1309555
public static void putScript(String name, KotlinCompiledScript script) {
        getInstance().scripts.put(name, script);
    }
1228897
@Override
    public <T> T runScript(final Reader reader, final String sourceName) throws IOException {
        return runWithContext(new RhinoCallable<T, IOException>() {
            @SuppressWarnings("unchecked")
            @Override
            protected T doCall(Context cx, Scriptable scope) throws IOException {
                Object val = cx.evaluateReader(scope, reader, sourceName, 1, null);
                val = unwrappedValue(val);
                return (T) val;
            }
        });
    }
865906
private static String addScriptDirectives(Element scriptElement) throws IOException, DocumentTemplateException {
    String scriptReplacement = "";

    List scriptParts = parseScriptParts(scriptElement.getValue());
    for (int index = 0; index < scriptParts.size(); index++) {
      ScriptPart scriptPart = (ScriptPart) scriptParts.get(index);
      if (scriptPart.getLocation() == null) {
        scriptReplacement = scriptPart.getText();
      }
      else {
        Element enclosingElement = findEnclosingElement(scriptElement, scriptPart.getLocation());
        if (scriptPart.isTagAttribute()) {
          String[] nameValue = scriptPart.getText().split("=", 2);
          if (nameValue.length != 2) {
            throw new DocumentTemplateException("script error: # attribute name=value not found");
          }

          String attributeNamespace = enclosingElement.getNamespaceURI();
          if (nameValue[0].contains(":")) {
            String prefix = nameValue[0].split(":")[0];
            if (!prefix.equals(enclosingElement.getNamespacePrefix())) {
              attributeNamespace = XPATH_CONTEXT.lookup(prefix);
              if (attributeNamespace == null) {
                throw new DocumentTemplateException("unsupported attribute namespace: " + prefix);
              }
            }
          }

          enclosingElement.addAttribute(new Attribute(nameValue[0], attributeNamespace, nameValue[1]));
        }
        else {
          ParentNode parent = enclosingElement.getParent();
          int parentIndex = parent.indexOf(enclosingElement);
          if (scriptPart.afterEndTag()) {
            parentIndex++;
          }
          parent.insertChild(newNode(scriptPart.getText()), parentIndex);
        }
      }
    }

    return scriptReplacement;
  }
420639
@RequestMapping(value = "/api/scripts/{scriptIdentifier}", method = RequestMethod.GET)
    public
    @ResponseBody
    Script getScript(Model model, @PathVariable String scriptIdentifier) throws Exception {
        return ScriptService.getInstance().getScript(Integer.parseInt(scriptIdentifier));
    }
129821
public List<?> runGroovy(String script, Object... args) throws Exception {
        List<?> answer;
        BSFManager manager = new BSFManager();

        for (int x = 0; x < args.length; x++) {
            manager.declareBean("arg" + x, args[x], String.class);
        }
        answer = (List) manager.eval("groovy", "script.groovy", 0, 0, script);

        return answer;
    }
60894
public static String aggregateScript(List<String> scriptNames, ClassLoader scriptLoader)
            throws URISyntaxException, IOException {
        final StringBuilder aggregatedScript = new StringBuilder();
        for (String scriptName : scriptNames) {
            String scriptNameWithExtension = format("%s.rpt", scriptName);
            Path scriptPath = Paths.get(scriptNameWithExtension);
            scriptNameWithExtension = URI.create(scriptNameWithExtension).normalize().getPath();
            String script = null;

            assert !scriptPath.isAbsolute();

            // resolve relative scripts in local file system
            if (scriptLoader != null) {
                // resolve relative scripts from class loader to support
                // separated specification projects that include Robot scripts only
                URL resource = scriptLoader.getResource(scriptNameWithExtension);
                if (resource != null) {
                    URI resourceURI = resource.toURI();
                    if ("file".equals(resourceURI.getScheme())) {
                        Path resourcePath = Paths.get(resourceURI);
                        script = readScript(resourcePath);
                    } else {
                        try (FileSystem fileSystem = newFileSystem(resourceURI, EMPTY_ENVIRONMENT)) {
                            Path resourcePath = Paths.get(resourceURI);
                            script = readScript(resourcePath);
                        }
                    }
                }
            }

            if (script == null) {
                throw new RuntimeException("Script not found: " + scriptPath);
            }

            aggregatedScript.append(script);
        }
        return aggregatedScript.toString();
    }