Revision: 13841
http://gate.svn.sourceforge.net/gate/?rev=13841&view=rev
Author: markagreenwood
Date: 2011-05-20 19:38:08 +0000 (Fri, 20 May 2011)
Log Message:
-----------
a new groovy vr
Modified Paths:
--------------
gate/trunk/plugins/Groovy/src/gate/groovy/ScriptPR.java
Added Paths:
-----------
gate/trunk/plugins/Groovy/src/gate/groovy/gui/ScriptPREditor.java
Modified: gate/trunk/plugins/Groovy/src/gate/groovy/ScriptPR.java
===================================================================
--- gate/trunk/plugins/Groovy/src/gate/groovy/ScriptPR.java 2011-05-20
17:01:38 UTC (rev 13840)
+++ gate/trunk/plugins/Groovy/src/gate/groovy/ScriptPR.java 2011-05-20
19:38:08 UTC (rev 13841)
@@ -1,16 +1,15 @@
/*
- * Copyright (c) 2010, The University of Sheffield.
- *
- * This file is part of the GATE/Groovy integration layer, and is free
- * software, released under the terms of the GNU Lesser General Public
- * Licence, version 2.1 (or any later version). A copy of this licence
- * is provided in the file LICENCE in the distribution.
- *
- * Groovy is developed by The Codehaus, details are available from
- * http://groovy.codehaus.org
+ * Copyright (c) 2010, The University of Sheffield.
+ *
+ * This file is part of the GATE/Groovy integration layer, and is free
software,
+ * released under the terms of the GNU Lesser General Public Licence, version
+ * 2.1 (or any later version). A copy of this licence is provided in the file
+ * LICENCE in the distribution.
+ *
+ * Groovy is developed by The Codehaus, details are available from
+ * http://groovy.codehaus.org
*/
-
package gate.groovy;
import gate.AnnotationSet;
@@ -43,19 +42,15 @@
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.runtime.InvokerInvocationException;
-
/**
* Groovy Script PR.
- *
+ *
* @author Angus Roberts, Ian Roberts
- *
*/
-@CreoleResource(name = "Groovy scripting PR",
- comment = "Runs a Groovy script as a processing resource",
- helpURL = "http://gate.ac.uk/userguide/sec:api:groovy",
- icon = "/gate/groovy/script-pr")
-public class ScriptPR extends AbstractLanguageAnalyser
- implements ProcessingResource, ControllerAwarePR {
+@CreoleResource(name = "Groovy scripting PR", comment = "Runs a Groovy script
as a processing resource", helpURL =
"http://gate.ac.uk/userguide/sec:api:groovy", icon = "/gate/groovy/script-pr")
+public class ScriptPR extends AbstractLanguageAnalyser implements
+ ProcessingResource,
+ ControllerAwarePR {
/**
* Groovy script file
@@ -73,6 +68,11 @@
private Script groovyScript;
/**
+ * the source of the loaded groovy script for use in the VR
+ */
+ private String groovySrc;
+
+ /**
* The character encoding of the script file.
*/
private String encoding;
@@ -90,9 +90,10 @@
/** Initialise this resource, and return it. */
public Resource init() throws ResourceInstantiationException {
- if (scriptURL == null)
- throw new ResourceInstantiationException("You must specify a Groovy
script to load");
-
+ if(scriptURL == null)
+ throw new ResourceInstantiationException(
+ "You must specify a Groovy script to load");
+
// Create the shell, with the GateClassLoader as its parent (so the script
// will have access to plugin classes)
GroovyShell groovyShell = new GroovyShell(Gate.getClassLoader());
@@ -101,34 +102,38 @@
char[] buf = new char[4096];
int charsRead = 0;
try {
- Reader reader = new BomStrippingInputStreamReader(scriptURL.openStream(),
- encoding);
+ Reader reader =
+ new BomStrippingInputStreamReader(scriptURL.openStream(),
+ encoding);
while((charsRead = reader.read(buf)) >= 0) {
scriptText.append(buf, 0, charsRead);
}
reader.close();
- }
- catch(IOException ioe) {
- throw new ResourceInstantiationException("Error loading Groovy script "
- + "from URL " + scriptURL, ioe);
- }
- // append a load of standard imports to the end of the script. We put them
- // at the end rather than the beginning because (in a script) imports work
- // anywhere, and putting them at the end means we don't mess up line
- // numbers in any compilation error messages.
- scriptText.append("\n\n\n");
- scriptText.append(GroovySupport.STANDARD_IMPORTS);
+ groovySrc = scriptText.toString();
- // determine the file name of the script
- String scriptFileName = scriptURL.toString();
- scriptFileName = scriptFileName.substring(scriptFileName.lastIndexOf('/'));
+ // append a load of standard imports to the end of the script. We put
them
+ // at the end rather than the beginning because (in a script) imports
work
+ // anywhere, and putting them at the end means we don't mess up line
+ // numbers in any compilation error messages.
+ scriptText.append("\n\n\n");
+ scriptText.append(GroovySupport.STANDARD_IMPORTS);
- try {
+ // determine the file name of the script
+ String scriptFileName = scriptURL.toString();
+ scriptFileName =
+ scriptFileName.substring(scriptFileName.lastIndexOf('/'));
+
groovyScript = groovyShell.parse(scriptText.toString(), scriptFileName);
+ } catch(IOException ioe) {
+ throw new ResourceInstantiationException("Error loading Groovy script "
+ + "from URL " + scriptURL, ioe);
}
+
catch(CompilationFailedException e) {
throw new ResourceInstantiationException("Script compilation failed", e);
+ } finally {
+ fireProcessFinished();
}
return this;
@@ -158,34 +163,31 @@
}
/**
- * Check whether the script declares a method with the given name that takes
- * a corpus parameter, and if so, call it passing the corpus from the given
- * controller. If the controller is not a CorpusController, do nothing.
- *
- * @throws ExecutionException if the script method throws an
- * ExecutionException we re-throw it
+ * Check whether the script declares a method with the given name that takes
a
+ * corpus parameter, and if so, call it passing the corpus from the given
+ * controller. If the controller is not a CorpusController, do nothing.
+ *
+ * @throws ExecutionException
+ * if the script method throws an ExecutionException we re-throw it
*/
protected void callControllerAwareMethod(String methodName, Controller c)
throws ExecutionException {
if(!(c instanceof CorpusController)) { return; }
- List<MetaMethod> metaMethods = groovyScript.getMetaClass().respondsTo(
- groovyScript, methodName, new Class[] {gate.Corpus.class});
+ List<MetaMethod> metaMethods =
+ groovyScript.getMetaClass().respondsTo(groovyScript, methodName,
+ new Class[]{gate.Corpus.class});
if(!metaMethods.isEmpty()) {
try {
- metaMethods.get(0).invoke(
- groovyScript, new Corpus[] { ((CorpusController)c).getCorpus() });
- }
- catch(InvokerInvocationException iie) {
+ metaMethods.get(0).invoke(groovyScript,
+ new Corpus[]{((CorpusController)c).getCorpus()});
+ } catch(InvokerInvocationException iie) {
if(iie.getCause() instanceof ExecutionException) {
throw (ExecutionException)iie.getCause();
- }
- else if(iie.getCause() instanceof RuntimeException) {
+ } else if(iie.getCause() instanceof RuntimeException) {
throw (RuntimeException)iie.getCause();
- }
- else if(iie.getCause() instanceof Error) {
+ } else if(iie.getCause() instanceof Error) {
throw (Error)iie.getCause();
- }
- else {
+ } else {
throw iie;
}
}
@@ -199,9 +201,8 @@
*/
public void execute() throws ExecutionException {
- if(document == null) {
- throw new ExecutionException("There is no loaded document");
- }
+ if(document == null) { throw new ExecutionException(
+ "There is no loaded document"); }
AnnotationSet outputAS = null;
if(outputASName == null || outputASName.trim().length() == 0)
@@ -217,7 +218,6 @@
fireStatusChanged("Groovy script PR running on " +
document.getSourceUrl());
fireProgressChanged(0);
-
// Create the variable bindings
Binding binding = groovyScript.getBinding();
binding.setVariable("doc", document);
@@ -246,11 +246,12 @@
// We've done
fireProgressChanged(100);
fireProcessFinished();
- fireStatusChanged( "Groovy script PR finished" );
+ fireStatusChanged("Groovy script PR finished");
}
/**
* gets name of the output annotation set
+ *
* @return
*/
public String getOutputASName() {
@@ -259,6 +260,7 @@
/**
* sets name of the output annotaiton set
+ *
* @param outputAS
*/
@Optional
@@ -270,6 +272,7 @@
/**
* gets name of the input annotation set
+ *
* @return
*/
public String getInputASName() {
@@ -278,6 +281,7 @@
/**
* sets name of the input annotaiton set
+ *
* @param inputAS
*/
@Optional
@@ -289,6 +293,7 @@
/**
* gets URL of the Groovy script
+ *
* @return
*/
public URL getScriptURL() {
@@ -297,10 +302,11 @@
/**
* sets File of the Groovy script
+ *
* @param script
*/
@CreoleParameter(comment = "Location of the Groovy script that will be "
- + "run for each document")
+ + "run for each document")
public void setScriptURL(URL script) {
this.scriptURL = script;
}
@@ -316,13 +322,14 @@
* Set the character encoding used to load the script.
*/
@CreoleParameter(defaultValue = "UTF-8", comment = "Character encoding used "
- + "to read the script")
+ + "to read the script")
public void setEncoding(String encoding) {
this.encoding = encoding;
}
/**
* Get Map of parameters for the Groovy script
+ *
* @return
*/
public FeatureMap getScriptParams() {
@@ -331,14 +338,24 @@
/**
* Set Map of parameters for the Groovy script
+ *
* @return
*/
@Optional
@RunTime
@CreoleParameter(comment = "Optional additional parameters to pass to the "
- + "script.")
+ + "script.")
public void setScriptParams(FeatureMap params) {
this.scriptParams = params;
}
+ /**
+ * Return the source of the loaded groovy script
+ *
+ * @return the source of the loaded groovy script
+ */
+ public String getGroovySrc() {
+ return groovySrc;
+ }
+
}
Added: gate/trunk/plugins/Groovy/src/gate/groovy/gui/ScriptPREditor.java
===================================================================
--- gate/trunk/plugins/Groovy/src/gate/groovy/gui/ScriptPREditor.java
(rev 0)
+++ gate/trunk/plugins/Groovy/src/gate/groovy/gui/ScriptPREditor.java
2011-05-20 19:38:08 UTC (rev 13841)
@@ -0,0 +1,142 @@
+package gate.groovy.gui;
+
+import gate.Resource;
+import gate.creole.AbstractVisualResource;
+import gate.creole.metadata.CreoleResource;
+import gate.creole.metadata.GuiType;
+import gate.event.ProgressListener;
+import gate.groovy.ScriptPR;
+import gate.gui.MainFrame;
+import gate.util.Files;
+import gate.util.GateRuntimeException;
+import groovy.ui.ConsoleTextEditor;
+
+import java.awt.BorderLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import javax.swing.Box;
+import javax.swing.JButton;
+import javax.swing.JToolBar;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
+
+@CreoleResource(name = "Script Editor", comment = "Editor for the Groovy
script behind this PR", guiType = GuiType.LARGE, mainViewer = true,
resourceDisplayed = "gate.groovy.ScriptPR")
+public class ScriptPREditor extends AbstractVisualResource implements
+ ProgressListener,
+ DocumentListener {
+
+ private ConsoleTextEditor editor;
+
+ private ScriptPR pr;
+
+ private File file;
+
+ private JButton btnSave, btnRevert;
+
+ public Resource init() {
+ initGuiComponents();
+
+ return this;
+ }
+
+ protected void initGuiComponents() {
+ setLayout(new BorderLayout());
+ editor = new ConsoleTextEditor();
+ editor.getTextEditor().getDocument().addDocumentListener(this);
+ add(editor, BorderLayout.CENTER);
+
+ btnSave = new JButton("Save and Reinitialize",
MainFrame.getIcon("crystal-clear-app-download-manager"));
+ btnSave.addActionListener(new ActionListener() {
+
+ public void actionPerformed(ActionEvent e) {
+ try {
+ FileWriter out = new FileWriter(file);
+ out.write(editor.getTextEditor().getText());
+ out.flush();
+ out.close();
+ pr.reInit();
+ btnRevert.setEnabled(false);
+ btnSave.setEnabled(false);
+ } catch(Exception ioe) {
+ ioe.printStackTrace();
+ }
+ }
+ });
+
+ btnRevert = new JButton("Revert Changes",
MainFrame.getIcon("crystal-clear-action-reload"));
+ btnRevert.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ editor.getTextEditor().setText(pr.getGroovySrc());
+ btnRevert.setEnabled(false);
+ btnSave.setEnabled(false);
+ }
+ });
+
+ JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
+ toolbar.setFloatable(false);
+ toolbar.add(btnSave);
+ toolbar.add(Box.createHorizontalStrut(5));
+ toolbar.add(btnRevert);
+
+ add(toolbar, BorderLayout.NORTH);
+ }
+
+ public void setTarget(Object target) {
+
+ if(target == null) return;
+ if(!(target instanceof ScriptPR)) { throw new GateRuntimeException(this
+ .getClass().getName()
+ + " can only be used to display "
+ + ScriptPR.class.getName()
+ + "\n"
+ + target.getClass().getName()
+ + " is not a " + ScriptPR.class.getName() + "!"); }
+
+ if(pr != null) {
+ pr.removeProgressListener(this);
+ }
+
+ pr = (ScriptPR)target;
+
+ try {
+ file = Files.fileFromURL(pr.getScriptURL());
+ } catch(Exception e) {
+ file = null;
+ }
+
+ editor.getTextEditor().setText(pr.getGroovySrc());
+
+ editor.getTextEditor().setEditable(file != null);
+ btnSave.setEnabled(false);
+ btnRevert.setEnabled(false);
+
+ pr.addProgressListener(this);
+ }
+
+ public void progressChanged(int i) {
+ // do nothing and wait until the progress has finished
+ }
+
+ public void processFinished() {
+ setTarget(pr);
+ }
+
+ public void changedUpdate(DocumentEvent e) {
+ // ignore these
+
+ }
+
+ public void insertUpdate(DocumentEvent e) {
+ btnRevert.setEnabled(true);
+ btnSave.setEnabled(file != null);
+ }
+
+ public void removeUpdate(DocumentEvent e) {
+ btnRevert.setEnabled(true);
+ btnSave.setEnabled(file != null);
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its
next-generation tools to help Windows* and Linux* C/C++ and Fortran
developers boost performance applications - including clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs