/plugins/JCompiler/trunk/jcompiler/JCompilerShell.java

# · Java · 235 lines · 150 code · 33 blank · 52 comment · 16 complexity · abff5471d593a372c4e4768b07384089 MD5 · raw file

  1. /*
  2. * JCompilerShell.java - ConsolePlugin shell for JCompiler
  3. * Copyright (C) 2000 Dirk Moebius
  4. *
  5. * :tabSize=4:indentSize=4:noTabs=false:maxLineLen=0:
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package jcompiler;
  22. import java.io.*;
  23. import java.util.Vector;
  24. import org.gjt.sp.jedit.EditBus;
  25. import org.gjt.sp.jedit.jEdit;
  26. import org.gjt.sp.jedit.View;
  27. import org.gjt.sp.util.Log;
  28. import errorlist.DefaultErrorSource;
  29. import console.Console;
  30. import console.ConsolePane;
  31. import console.Output;
  32. import console.Shell;
  33. /**
  34. * a JCompiler shell for the Console plugin.
  35. */
  36. public class JCompilerShell extends Shell
  37. {
  38. public JCompilerShell()
  39. {
  40. super("JCompiler");
  41. }
  42. // ----- Begin Shell implementation -----
  43. /**
  44. * print an information message.
  45. *
  46. * @param output where to put the information
  47. */
  48. public void printInfoMessage(Output output)
  49. {
  50. output.print(null, jEdit.getProperty("jcompiler.msg.info"));
  51. }
  52. /**
  53. * prints a prompt to the specified console.
  54. *
  55. * @param console the console.
  56. * @param output the output.
  57. */
  58. public void printPrompt(Console console, Output output)
  59. {
  60. output.writeAttrs(ConsolePane.colorAttributes(console.getInfoColor()),
  61. jEdit.getProperty("jcompiler.msg.prompt"));
  62. output.writeAttrs(null," ");
  63. }
  64. /**
  65. * execute a command.
  66. *
  67. * @param console the Console where the command was entered.
  68. * @param output where the output should go.
  69. * @param command the entered command.
  70. */
  71. public void execute(Console console, String input, Output output, Output error, String command)
  72. {
  73. stop(console); // stop last command
  74. String cmd = command.trim();
  75. DefaultErrorSource errorSource = console.getErrorSource();
  76. if ("compile".equals(cmd))
  77. {
  78. errorSource.clear();
  79. compileTask = new JCompilerTask(false, false, console, output, errorSource);
  80. }
  81. else if ("compilepkg".equals(cmd))
  82. {
  83. errorSource.clear();
  84. compileTask = new JCompilerTask(true, false, console, output, errorSource);
  85. }
  86. else if ("rebuildpkg".equals(cmd))
  87. {
  88. errorSource.clear();
  89. compileTask = new JCompilerTask(true, true, console, output, errorSource);
  90. }
  91. else if ("javac".equals(cmd))
  92. {
  93. // command "javac" invoked without arguments,
  94. // prints javac usage
  95. compileTask = new JCompilerTask(new String[] {}, console, output, errorSource);
  96. }
  97. else if (cmd.startsWith("javac "))
  98. {
  99. // command "javac" with arguments
  100. String[] args;
  101. try
  102. {
  103. args = parseCmdLineArguments(cmd.substring(6), console.getView().getBuffer().getPath());
  104. }
  105. catch (IOException ex)
  106. {
  107. output.print(console.getErrorColor(),
  108. jEdit.getProperty("jcompiler.msg.errorCommandLine",
  109. new Object[] { ex }));
  110. return;
  111. }
  112. errorSource.clear();
  113. compileTask = new JCompilerTask(args, console, output, errorSource);
  114. }
  115. else if ("help".equals(cmd))
  116. {
  117. printInfoMessage(output);
  118. output.commandDone();
  119. }
  120. else
  121. {
  122. output.print(console.getInfoColor(),
  123. jEdit.getProperty("jcompiler.msg.errorUnknownCommand",
  124. new Object[] { cmd }));
  125. output.commandDone();
  126. }
  127. }
  128. public void stop(Console console)
  129. {
  130. if (compileTask != null)
  131. {
  132. if (compileTask.isAlive())
  133. {
  134. Output output = console.getOutput();
  135. output.print(console.getErrorColor(), jEdit.getProperty("jcompiler.msg.stopping"));
  136. compileTask.stop();
  137. output.commandDone();
  138. }
  139. compileTask = null;
  140. }
  141. }
  142. public boolean waitFor(Console console)
  143. {
  144. if (compileTask != null)
  145. {
  146. try
  147. {
  148. synchronized(compileTask)
  149. {
  150. compileTask.wait();
  151. }
  152. }
  153. catch (InterruptedException ie)
  154. {
  155. return false;
  156. }
  157. compileTask = null;
  158. }
  159. return true;
  160. }
  161. // ----- End Shell implementation -----
  162. private String[] parseCmdLineArguments(String cmd, String bufferFileName) throws IOException
  163. {
  164. // Expand any variables in the command line arguments:
  165. cmd = JCompiler.expandVariables(cmd, bufferFileName);
  166. // The following is stolen from Slava Pestov's SystemShell.java (Console plugin):
  167. // We replace \ with a non-printable char because StreamTokenizer
  168. // handles \ specially, which causes problems on Windows as \ is the
  169. // file separator there. After parsing is done, the non printable
  170. // char is changed to \ once again.
  171. cmd = cmd.replace('\\', NON_PRINTABLE);
  172. StreamTokenizer st = new StreamTokenizer(new StringReader(cmd));
  173. st.resetSyntax();
  174. st.wordChars('!',255);
  175. st.whitespaceChars(0,' ');
  176. st.quoteChar('"');
  177. st.quoteChar('\'');
  178. Vector args = new Vector();
  179. loop:
  180. for(;;)
  181. {
  182. switch(st.nextToken())
  183. {
  184. case StreamTokenizer.TT_EOF:
  185. break loop;
  186. case StreamTokenizer.TT_WORD:
  187. case '"':
  188. case '\'':
  189. args.addElement(st.sval.replace(NON_PRINTABLE, '\\'));
  190. break;
  191. }
  192. }
  193. Log.log(Log.DEBUG, this, "arguments=" + args);
  194. String[] array = new String[args.size()];
  195. args.copyInto(array);
  196. return array;
  197. }
  198. private JCompilerTask compileTask;
  199. private static final char NON_PRINTABLE = 127;
  200. }