PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/Console/console/commando/CommandoHandler.java

#
Java | 366 lines | 267 code | 47 blank | 52 comment | 65 complexity | 6bfc74ffc10f62d429e8812f78492c01 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * CommandoHandler.java - Reads commando XML file
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001, 2005 Slava Pestov
  7. * Copyright (C) 2010 Eric Le Lay
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. package console.commando;
  24. //{{{ Imports
  25. import java.io.StringReader;
  26. import java.util.List;
  27. import java.util.Stack;
  28. import java.util.Vector;
  29. import javax.swing.border.TitledBorder;
  30. import org.gjt.sp.jedit.BeanShell;
  31. import org.gjt.sp.jedit.View;
  32. import org.gjt.sp.jedit.jEdit;
  33. import org.gjt.sp.util.Log;
  34. import org.gjt.sp.jedit.bsh.NameSpace;
  35. import org.gjt.sp.jedit.bsh.This;
  36. import org.xml.sax.helpers.DefaultHandler;
  37. import org.xml.sax.InputSource;
  38. import org.xml.sax.Attributes;
  39. //}}}
  40. public class CommandoHandler extends DefaultHandler
  41. {
  42. //{{{ CommandoHandler constructor
  43. CommandoHandler(View view, CommandoCommand command,
  44. CommandoDialog.SettingsPane settings,
  45. NameSpace nameSpace,
  46. List<This> components,
  47. List<Script> scripts)
  48. {
  49. this.view = view;
  50. this.command = command;
  51. this.settings = pane = settings;
  52. this.nameSpace = nameSpace;
  53. this.components = components;
  54. this.scripts = scripts;
  55. stateStack = new Stack<String>();
  56. options = new Vector<Option>();
  57. } //}}}
  58. //{{{ resolveEntity() method
  59. @Override
  60. public InputSource resolveEntity(String publicId, String systemId)
  61. {
  62. // could have used EntityResolver2.resolveEntity(4),
  63. // but it's not always available
  64. if(systemId.endsWith("commando.dtd"))
  65. {
  66. return new InputSource(new StringReader("<!-- -->"));
  67. }
  68. return null;
  69. }
  70. //}}}
  71. //{{{ attribute() method
  72. public void attribute(String aname, String value)
  73. {
  74. aname = (aname == null) ? null : aname.intern();
  75. value = (value == null) ? null : value.intern();
  76. if(aname == "LABEL")
  77. label = value;
  78. else if(aname == "VARNAME")
  79. varName = value;
  80. else if(aname == "DEFAULT")
  81. defaultValue = value;
  82. else if(aname == "EVAL")
  83. eval = value;
  84. else if(aname == "VALUE")
  85. optionValue = value;
  86. else if(aname == "CONFIRM")
  87. confirm = "TRUE".equals(value);
  88. else if(aname == "TO_BUFFER")
  89. toBuffer = "TRUE".equals(value);
  90. else if(aname == "BUFFER_MODE")
  91. mode = value;
  92. else if(aname == "SHELL")
  93. shell = value;
  94. } //}}}
  95. //{{{ charData() method
  96. @Override
  97. public void characters(char[] c, int off, int len)
  98. {
  99. String tag = peekElement();
  100. String text = new String(c, off, len);
  101. //code might be passed line by line, so concatenate
  102. if(tag == "COMMAND")
  103. code = code == null ? text : code+text;
  104. } //}}}
  105. //{{{ startElement() method
  106. @Override
  107. public void startElement(String ns, String localName, String qName, Attributes attributes)
  108. {
  109. // handle the attributes before pushing the element
  110. // (this is what the old com.microstar.xml XmlParser implementation did)
  111. for(int i=0;i<attributes.getLength();i++)
  112. {
  113. String aname = attributes.getLocalName(i);
  114. String value = attributes.getValue(i);
  115. attribute(aname, value);
  116. }
  117. pushElement(localName);
  118. String tag = peekElement();
  119. if(tag == "CAPTION")
  120. {
  121. pane = new CommandoDialog.SettingsPane();
  122. pane.setBorder(new TitledBorder(label));
  123. settings.addComponent(pane);
  124. label = null;
  125. }
  126. else if(tag == "CHOICE")
  127. {
  128. choiceLabel = label;
  129. options = new Vector<Option>();
  130. }
  131. } //}}}
  132. @Override
  133. public void endElement(String ns, String localName, String qName)
  134. {
  135. if(localName == null)
  136. return;
  137. String tag = peekElement();
  138. if(localName.equals(tag))
  139. {
  140. if(tag == "OPTION")
  141. {
  142. options.addElement(new Option(label,optionValue));
  143. label = optionValue = null;
  144. }
  145. else if(tag == "CAPTION")
  146. {
  147. pane = settings;
  148. }
  149. else if(tag == "COMMANDS" || tag == "UI"
  150. || tag == "COMMANDO")
  151. {
  152. // ignore these, they're syntax sugar
  153. }
  154. else if(tag == "COMMAND")
  155. {
  156. scripts.add(new Script(
  157. confirm,toBuffer,mode,
  158. shell,code));
  159. confirm = false;
  160. toBuffer = false;
  161. shell = code = null;
  162. }
  163. else
  164. {
  165. try
  166. {
  167. NameSpace tmp = new NameSpace(
  168. BeanShell.getNameSpace(),
  169. "commando");
  170. tmp.setVariable("pane",pane);
  171. tmp.setVariable("ns",nameSpace);
  172. if(tag == "CHOICE")
  173. tmp.setVariable("label",choiceLabel);
  174. else
  175. tmp.setVariable("label",label);
  176. tmp.setVariable("var",varName);
  177. tmp.setVariable("options",options);
  178. defaultValue = jEdit.getProperty(
  179. command.getPropertyPrefix()
  180. + varName,defaultValue);
  181. if(eval != null)
  182. {
  183. defaultValue =
  184. String.valueOf(
  185. BeanShell.eval(
  186. view,tmp,eval));
  187. }
  188. if(defaultValue == null)
  189. nameSpace.setVariable(varName,"");
  190. else
  191. nameSpace.setVariable(varName,defaultValue);
  192. // this stores This instances
  193. // we call valueChanged() on
  194. // them to update namespace
  195. String script = "commando" + tag + "(view,pane,ns,label,var,options)";
  196. Object value = BeanShell.eval(view, tmp, script);
  197. components.add((This)value);
  198. }
  199. catch(Exception e)
  200. {
  201. Log.log(Log.ERROR,this,e);
  202. }
  203. label = varName = defaultValue = eval = null;
  204. }
  205. popElement();
  206. }
  207. else
  208. {
  209. // can't happen
  210. throw new InternalError();
  211. }
  212. } //}}}
  213. //{{{ startDocument() method
  214. @Override
  215. public void startDocument()
  216. {
  217. pushElement(null);
  218. } //}}}
  219. //{{{ Private members
  220. //{{{ Instance variables
  221. private View view;
  222. private CommandoCommand command;
  223. private CommandoDialog.SettingsPane settings;
  224. private CommandoDialog.SettingsPane pane;
  225. private NameSpace nameSpace;
  226. private List<This> components;
  227. private List<Script> scripts;
  228. private String varName;
  229. private String defaultValue;
  230. private String eval;
  231. private String optionValue;
  232. private String choiceLabel;
  233. private String label;
  234. private boolean confirm;
  235. private boolean toBuffer;
  236. private String mode;
  237. private String shell;
  238. private String code;
  239. private Vector<Option> options;
  240. private Stack<String> stateStack;
  241. //}}}
  242. //{{{ pushElement() method
  243. private void pushElement(String name)
  244. {
  245. name = (name == null) ? null : name.intern();
  246. stateStack.push(name);
  247. } //}}}
  248. //{{{ peekElement() method
  249. private String peekElement()
  250. {
  251. return stateStack.peek();
  252. } //}}}
  253. //{{{ popElement() method
  254. private String popElement()
  255. {
  256. return stateStack.pop();
  257. } //}}}
  258. //}}}
  259. //{{{ Script class
  260. class Script
  261. {
  262. boolean confirm;
  263. boolean toBuffer;
  264. String mode;
  265. String shell;
  266. String code;
  267. Script(boolean confirm, boolean toBuffer, String mode,
  268. String shell, String code)
  269. {
  270. this.confirm = confirm;
  271. this.toBuffer = toBuffer;
  272. this.mode = mode;
  273. this.shell = shell;
  274. this.code = code;
  275. }
  276. Command getCommand()
  277. {
  278. Object command = BeanShell.eval(view,nameSpace,code);
  279. if(command == null)
  280. return null;
  281. return new Command(confirm,toBuffer,mode,
  282. shell,String.valueOf(command));
  283. }
  284. } //}}}
  285. //{{{ Command class
  286. // static for use by CommandoThread
  287. static class Command
  288. {
  289. boolean confirm;
  290. boolean toBuffer;
  291. String mode;
  292. String shell;
  293. String command;
  294. Command(boolean confirm, boolean toBuffer, String mode,
  295. String shell, String command)
  296. {
  297. this.confirm = confirm;
  298. this.toBuffer = toBuffer;
  299. this.mode = mode;
  300. this.shell = shell;
  301. this.command = command;
  302. }
  303. } //}}}
  304. //{{{ Option class
  305. public static class Option
  306. {
  307. public String label;
  308. public String value;
  309. Option(String label, String value)
  310. {
  311. this.label = label;
  312. this.value = value;
  313. }
  314. public String toString()
  315. {
  316. return label;
  317. }
  318. } //}}}
  319. }