/plugins/JavaScriptShell/tags/release-0.1/javascriptshell/JavaScriptShell.java

# · Java · 113 lines · 71 code · 21 blank · 21 comment · 12 complexity · 2a4c3963d3591c88f5f91e99371892ba MD5 · raw file

  1. /*
  2. * JavaScriptShell.java
  3. * Copyright (c) 2007 Jakub Roztocil <jakub@webkitchen.cz>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. */
  20. package javascriptshell;
  21. import java.io.File;
  22. import java.io.BufferedReader;
  23. import java.io.FileReader;
  24. import org.gjt.sp.jedit.*;
  25. import console.*;
  26. import javax.script.*;
  27. import org.gjt.sp.jedit.Macros.Handler;
  28. import org.gjt.sp.util.Log;
  29. import org.gjt.sp.jedit.gui.TextAreaDialog;
  30. public class JavaScriptShell extends Shell {
  31. private static ScriptEngine engine;
  32. public static void init() {
  33. if (engine == null) {
  34. ScriptEngineManager manager = new ScriptEngineManager();
  35. engine = manager.getEngineByName("JavaScript");
  36. engine.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
  37. }
  38. }
  39. public JavaScriptShell(String name) {
  40. super(name);
  41. }
  42. public void execute(Console console, String input, Output output, Output error, String command) {
  43. if (command == null || command.equals("")) {
  44. output.commandDone();
  45. return;
  46. }
  47. setGlobals(console.getView());
  48. try {
  49. Object retVal = engine.eval(command);
  50. String out = "";
  51. if (retVal != null) {
  52. out = retVal.toString();
  53. }
  54. output.print(console.getPlainColor(), out);
  55. output.commandDone();
  56. } catch (ScriptException e) {
  57. output.print(console.getErrorColor(), e.getMessage());
  58. output.commandDone();
  59. }
  60. }
  61. public void printInfoMessage(Output output) {
  62. }
  63. public void printPrompt(Console console, Output output) {
  64. output.writeAttrs(ConsolePane.colorAttributes(console.getInfoColor()), "JavaScript> ");
  65. }
  66. private static void setGlobals(View view) {
  67. engine.put("view", view);
  68. engine.put("textArea", view == null ? null : view.getTextArea());
  69. engine.put("buffer", view == null ? null : view.getTextArea().getBuffer());
  70. engine.put("engine", engine);
  71. }
  72. public static void runScript(String path, View view) {
  73. File file = new File(path);
  74. if (file.exists()) {
  75. try {
  76. BufferedReader reader = new BufferedReader(new FileReader(file));
  77. StringBuffer code = new StringBuffer();
  78. String line;
  79. while ((line = reader.readLine()) != null) {
  80. code.append(line+"\n");
  81. }
  82. //Log.log(Log.DEBUG, JavaScriptShell.class, code.toString());
  83. setGlobals(view);
  84. engine.eval(code.toString());
  85. } catch (Exception e) {
  86. Log.log(Log.ERROR, JavaScriptShell.class, e.toString());
  87. new TextAreaDialog(view, "javascript-error", e);
  88. }
  89. }
  90. }
  91. }
  92. /* :folding=explicit:collapseFolds=1:tabSize=4:indentSize=4:noTabs=false: */