本文整理匯總了Java中groovy.lang.GroovyShell.parse方法的典型用法代碼示例。如果您正苦於以下問題:Java GroovyShell.parse方法的具體用法?Java GroovyShell.parse怎麽用?Java GroovyShell.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類groovy.lang.GroovyShell
的用法示例。
在下文中一共展示了GroovyShell.parse方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: substitute
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
/**
* performs groovy substitutions with this as delegate and inherited bindings
*/
protected String substitute(String before) {
if (before.indexOf("${") == -1)
return before;
// escape all $ not followed by curly braces on same line
before = before.replaceAll("\\$(?!\\{)", "\\\\\\$");
// escape all regex -> ${~
before = before.replaceAll("\\$\\{~", "\\\\\\$\\{~");
// escape all escaped newlines
before = before.replaceAll("\\\\n", "\\\\\\\\\\n");
before = before.replaceAll("\\\\r", "\\\\\\\\\\r");
// escape all escaped quotes
before = before.replaceAll("\\\"", "\\\\\"");
CompilerConfiguration compilerCfg = new CompilerConfiguration();
compilerCfg.setScriptBaseClass(DelegatingScript.class.getName());
GroovyShell shell = new GroovyShell(TestCaseScript.class.getClassLoader(), getBinding(), compilerCfg);
DelegatingScript script = (DelegatingScript) shell.parse("return \"\"\"" + before + "\"\"\"");
script.setDelegate(TestCaseScript.this);
// restore escaped \$ to $ for comparison
return script.run().toString().replaceAll("\\\\$", "\\$");
}
示例2: runScript
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
/**
* Returns the builder object for creating new output variable value.
*/
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.setProperty("out", getRuntimeContext().get);
gScript.run();
}
示例3: run
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
@Override
public Object run(String dsl, Binding binding) {
CompilerConfiguration compilerConfiguration = prepareCompilerConfiguration();
ClassLoader classLoader = prepareClassLoader(AbstractDSLLauncher.class.getClassLoader());
GroovyCodeSource groovyCodeSource = prepareGroovyCodeSource(dsl);
// Groovy shell
GroovyShell shell = new GroovyShell(
classLoader,
new Binding(),
compilerConfiguration
);
// Groovy script
Script groovyScript = shell.parse(groovyCodeSource);
// Binding
groovyScript.setBinding(binding);
// Runs the script
return run(groovyScript);
}
示例4: runGroovyDslScript
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
/**
* Runs a Groovy DSL script and returns the value returned by the script.
* @param scriptReader For reading the script text
* @param expectedType The expected type of the return value
* @param parameters Parameters used by the script, null or empty if the script doesn't need any
* @param <T> The expected type of the return value
* @return The return value of the script, not null
*/
private static <T> T runGroovyDslScript(Reader scriptReader, Class<T> expectedType, Map<String, Object> parameters) {
Map<String, Object> timeoutArgs = ImmutableMap.<String, Object>of("value", 2);
ASTTransformationCustomizer customizer = new ASTTransformationCustomizer(timeoutArgs, TimedInterrupt.class);
CompilerConfiguration config = new CompilerConfiguration();
config.addCompilationCustomizers(customizer);
config.setScriptBaseClass(SimulationScript.class.getName());
Map<String, Object> bindingMap = parameters == null ? Collections.<String, Object>emptyMap() : parameters;
//copy map to ensure that binding is mutable (for use in registerAliases)
Binding binding = new Binding(Maps.newHashMap(bindingMap));
registerAliases(binding);
GroovyShell shell = new GroovyShell(binding, config);
Script script = shell.parse(scriptReader);
Object scriptOutput = script.run();
if (scriptOutput == null) {
throw new IllegalArgumentException("Script " + scriptReader + " didn't return an object");
}
if (expectedType.isInstance(scriptOutput)) {
return expectedType.cast(scriptOutput);
} else {
throw new IllegalArgumentException("Script '" + scriptReader + "' didn't create an object of the expected type. " +
"expected type: " + expectedType.getName() + ", " +
"actual type: " + scriptOutput.getClass().getName() + ", " +
"actual value: " + scriptOutput);
}
}
示例5: processFiles
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
/**
* Process the input files.
*/
private void processFiles() throws CompilationFailedException, IOException, URISyntaxException {
GroovyShell groovy = new GroovyShell(conf);
setupContextClassLoader(groovy);
Script s = groovy.parse(getScriptSource(isScriptFile, script));
if (args.isEmpty()) {
try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out)) {
processReader(s, reader, writer);
}
} else {
Iterator i = args.iterator();
while (i.hasNext()) {
String filename = (String) i.next();
//TODO: These are the arguments for -p and -i. Why are we searching using Groovy script extensions?
// Where is this documented?
File file = huntForTheScriptFile(filename);
processFile(s, file);
}
}
}
示例6: getComponent
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
@Override
public Component getComponent(int width, int height) throws IOException {
final JPanel base = new JPanel();
base.setOpaque(false);
base.setPreferredSize(new Dimension(width, height));
base.setLayout(new BoxLayout(base, BoxLayout.Y_AXIS));
final Binding sharedData = new Binding();
final GroovyShell shell = new GroovyShell(sharedData);
sharedData.setProperty("slidePanel", base);
final Script script = shell.parse(new InputStreamReader(GroovySlide.class.getResourceAsStream("test.groovy")));
script.run();
// final RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60);
// textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_GROOVY);
// textArea.setCodeFoldingEnabled(true);
// final RTextScrollPane sp = new RTextScrollPane(textArea);
// base.add(sp);
return base;
}
示例7: testGroovyShell
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
@Test
public void testGroovyShell() throws Exception {
GroovyShell groovyShell = new GroovyShell();
final String s = "x > 2 && y > 1";
Script script = groovyShell.parse(s);
Binding binding = new Binding();
binding.setProperty("x",5);
binding.setProperty("y",3);
script.setBinding(binding);
Object result = script.run();
Assert.assertEquals(true, result);
log.debug("evaluating [{}] with (x,y)=({},{}) => {}\n", s,
script.getBinding().getProperty("x"), script.getBinding().getProperty("y"), result);
binding.setProperty("y",0);
result = script.run();
Assert.assertEquals(false, result);
log.debug("evaluating [{}] with (x,y)=({},{}) => {}\n", s,
binding.getProperty("x"), binding.getProperty("y"), result);
}
示例8: testGroovyShell_goodBindingFollowedByBadBinding_Exception
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
@Test(expected = groovy.lang.MissingPropertyException.class)
public void testGroovyShell_goodBindingFollowedByBadBinding_Exception() throws Exception {
GroovyShell groovyShell = new GroovyShell();
final String s = "x > 2 && y > 1";
Script script = groovyShell.parse(s);
Binding binding = new Binding();
binding.setProperty("x", 5);
binding.setProperty("y", 3);
script.setBinding(binding);
Object result = script.run();
Assert.assertEquals(true, result);
Assert.assertTrue(binding.hasVariable("x"));
binding = new Binding();
binding.setProperty("x1", 5);
binding.setProperty("y1", 3);
script.setBinding(binding);
Assert.assertFalse(binding.hasVariable("x"));
script.run(); // throws exception because no bindings for x, y
}
示例9: runScript
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
private void runScript(String script) throws Exception {
Vertx vertx = Vertx.vertx();
try {
GroovyShell gcl = new GroovyShell();
Script s = gcl.parse(new File(script));
Binding binding = new Binding();
binding.setProperty("test", this);
binding.setProperty("vertx", vertx);
s.setBinding(binding);
s.run();
} finally {
CountDownLatch latch = new CountDownLatch(1);
vertx.close(v -> {
latch.countDown();
});
latch.await();
}
}
示例10: getScript
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
public Script getScript() {
syncLock.r.lock();
try {
if (script != null)
return script;
} finally {
syncLock.r.unlock();
}
String v = getValue();
if (StringUtils.isEmpty(v))
return null;
GroovyShell shell = new GroovyShell();
Script s = shell.parse(getValue());
setScript(s);
return s;
}
示例11: main
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception
{
Binding binding = new Binding();
binding.setVariable("language", "Groovy");
GroovyShell shell = new GroovyShell(binding);
GroovyScriptEngine engine = new GroovyScriptEngine(new URL[]{GroovyTest.class.getClassLoader().getResource("/")});
Script script = shell.parse(GroovyTest.class.getClassLoader().getResource("random.groovy").toURI());
// System.out.println(script);
// script.invokeMethod("new SuRenRandom()", null);
// script.evaluate("new SuRenRandom()");
// engine.run("random.groovy", binding);
InputStream stream = GroovyTest.class.getClassLoader().getResource("random.groovy").openStream();
StringBuffer buf = new StringBuffer();
byte[] bf = new byte[1024];
int len = -1;
while((len = stream.read(bf)) != -1)
{
buf.append(new String(bf, 0, len));
}
buf.append("\n");
for(int i = 0; i < 30; i++)
{
System.out.println(shell.evaluate(buf.toString() + "new SuRenRandom().randomPhoneNum()"));
System.out.println(shell.evaluate(buf.toString() + "new SuRenRandom().randomEmail()"));
System.out.println(shell.evaluate(buf.toString() + "new SuRenRandom().randomZipCode()"));
}
}
示例12: setScript
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
public GroovyRunner<T> setScript(String source) throws ScriptException {
try {
GroovyShell shell = new GroovyShell(binding);
script = shell.parse(source);
return this;
} catch (CompilationFailedException e) {
throw new ScriptException("Groovy script exception: " + e.getMessage());
}
}
示例13: loadByPath
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
@Override
public void loadByPath(Path path) throws Throwable {
String id;
if (Files.isDirectory(path)) {
id = path.getFileName().toString();
path = path.resolve("plugin.groovy");
} else {
if(path.getFileName().toString().equals("plugin.groovy"))
id = path.getParent().getFileName().toString();
else {
id = path.getFileName().toString();
id = id.substring(0, id.length() - 7);
}
}
System.setProperty("groovy.grape.report.downloads", "true");
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setSourceEncoding("UTF-8");
compilerConfiguration.setScriptBaseClass("info.deskchan.groovy_support.GroovyPlugin");
compilerConfiguration.setClasspath(path.getParent().toString());
GroovyShell groovyShell = new GroovyShell(compilerConfiguration);
Script script = groovyShell.parse(path.toFile());
GroovyPlugin plugin = (GroovyPlugin) script;
plugin.setPluginDirPath(path.getParent());
PluginConfig config = new PluginConfig("Groovy");
path = path.getParent().resolve("manifest.json");
config.appendFromJson(path);
PluginManager.getInstance().initializePlugin(id, plugin, config);
}
示例14: run
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
public void run() {
startExecution();
try {
if (testCase.getAsset().isFormat(Asset.POSTMAN)) {
String runnerClass = NODE_PACKAGE + ".TestRunner";
Package pkg = PackageCache.getPackage(testCase.getPackage());
Class<?> testRunnerClass = CompiledJavaCache.getResourceClass(runnerClass, getClass().getClassLoader(), pkg);
Object runner = testRunnerClass.newInstance();
Method runMethod = testRunnerClass.getMethod("run", TestCase.class);
runMethod.invoke(runner, testCase);
finishExecution(null);
}
else {
String groovyScript = testCase.getText();
CompilerConfiguration compilerConfig = new CompilerConfiguration();
compilerConfig.setScriptBaseClass(TestCaseScript.class.getName());
Binding binding = new Binding();
binding.setVariable("testCaseRun", this);
ClassLoader classLoader = this.getClass().getClassLoader();
Package testPkg = PackageCache.getPackage(testCase.getPackage());
if (testPkg != null)
classLoader = testPkg.getCloudClassLoader();
GroovyShell shell = new GroovyShell(classLoader, binding, compilerConfig);
Script gScript = shell.parse(groovyScript);
gScript.setProperty("out", log);
gScript.run();
finishExecution(null);
}
}
catch (Throwable ex) {
finishExecution(ex);
}
}
示例15: executeGPath
import groovy.lang.GroovyShell; //導入方法依賴的package包/類
private void executeGPath() throws ActivityException {
String transform = (String) getAttributeValue(RULE);
if (StringHelper.isEmpty(transform)) {
logger.info("No transform defined for activity: " + getActivityName());
return;
}
GroovyShell shell = new GroovyShell(getClass().getClassLoader());
Script script = shell.parse(transform);
Binding binding = new Binding();
Variable inputVar = getMainProcessDefinition().getVariable(inputDocument);
if (inputVar == null)
throw new ActivityException("Input document variable not found: " + inputDocument);
String inputVarName = inputVar.getName();
Object inputVarValue = getGPathParamValue(inputVarName, inputVar.getType());
binding.setVariable(inputVarName, inputVarValue);
Variable outputVar = getMainProcessDefinition().getVariable(getOutputDocuments()[0]);
String outputVarName = outputVar.getName();
Object outputVarValue = getGPathParamValue(outputVarName, outputVar.getType());
binding.setVariable(outputVarName, outputVarValue);
script.setBinding(binding);
script.run();
Object groovyVarValue = binding.getVariable(outputVarName);
setGPathParamValue(outputVarName, outputVar.getType(), groovyVarValue);
}