PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/ActionListHandler.java

#
Java | 197 lines | 128 code | 26 blank | 43 comment | 23 complexity | d04350caa1ac90b57d41777eb67f8dfd 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. * ActionListHandler.java - XML handler for action files
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2001 Slava Pestov
  7. * Portions copyright (C) 1999 mike dillon
  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 org.gjt.sp.jedit;
  24. //{{{ Imports
  25. import java.util.Stack;
  26. import org.xml.sax.Attributes;
  27. import org.xml.sax.InputSource;
  28. import org.xml.sax.helpers.DefaultHandler;
  29. import org.gjt.sp.util.Log;
  30. import org.gjt.sp.util.XMLUtilities;
  31. //}}}
  32. /**
  33. * This class loads the actions.xml files into a {@link JEditActionSet}. * @author Slava Pestov
  34. * @author Mike Dillon
  35. */
  36. class ActionListHandler extends DefaultHandler
  37. {
  38. //{{{ ActionListHandler constructor
  39. ActionListHandler(String path, JEditActionSet actionSet)
  40. {
  41. this.path = path;
  42. this.actionSet = actionSet;
  43. stateStack = new Stack<String>();
  44. code = new StringBuilder();
  45. isSelected = new StringBuilder();
  46. } //}}}
  47. //{{{ resolveEntity() method
  48. @Override
  49. public InputSource resolveEntity(String publicId, String systemId)
  50. {
  51. return XMLUtilities.findEntity(systemId, "actions.dtd", getClass());
  52. } //}}}
  53. //{{{ attribute() method
  54. public void attribute(String aname, String value, boolean isSpecified)
  55. {
  56. aname = (aname == null) ? null : aname.intern();
  57. value = (value == null) ? null : value.intern();
  58. if(aname == "NAME")
  59. actionName = value;
  60. else if(aname == "NO_REPEAT")
  61. noRepeat = (value == "TRUE");
  62. else if(aname == "NO_RECORD")
  63. noRecord = (value == "TRUE");
  64. else if(aname == "NO_REMEMBER_LAST")
  65. noRememberLast = (value == "TRUE");
  66. } //}}}
  67. //{{{ characters() method
  68. @Override
  69. public void characters(char[] c, int off, int len)
  70. {
  71. String tag = peekElement();
  72. if (tag.equals("CODE"))
  73. {
  74. code.append(c, off, len);
  75. }
  76. else if (tag.equals("IS_SELECTED"))
  77. {
  78. isSelected.append(c, off, len);
  79. }
  80. } //}}}
  81. //{{{ startElement() method
  82. @Override
  83. public void startElement(String uri, String localName,
  84. String qName, Attributes attrs)
  85. {
  86. String tag = pushElement(qName);
  87. if (tag.equals("ACTION"))
  88. {
  89. actionName = attrs.getValue("NAME");
  90. noRepeat = "TRUE".equals(attrs.getValue("NO_REPEAT"));
  91. noRecord = "TRUE".equals(attrs.getValue("NO_RECORD"));
  92. noRememberLast = "TRUE".equals(attrs.getValue("NO_REMEMBER_LAST"));
  93. code.setLength(0);
  94. isSelected.setLength(0);
  95. }
  96. } //}}}
  97. //{{{ endElement() method
  98. @Override
  99. public void endElement(String uri, String localName, String qName)
  100. {
  101. String tag = peekElement();
  102. if (qName.equals(tag))
  103. {
  104. if (tag.equals("ACTION"))
  105. {
  106. String selected = (isSelected.length() > 0) ?
  107. isSelected.toString() : null;
  108. JEditAbstractEditAction action =
  109. actionSet.createBeanShellAction(actionName,
  110. code.toString(),
  111. selected,
  112. noRepeat,
  113. noRecord,
  114. noRememberLast);
  115. actionSet.addAction(action);
  116. noRepeat = noRecord = noRememberLast = false;
  117. code.setLength(0);
  118. isSelected.setLength(0);
  119. }
  120. popElement();
  121. }
  122. else
  123. {
  124. // can't happen
  125. throw new InternalError();
  126. }
  127. } //}}}
  128. //{{{ startDocument() method
  129. @Override
  130. public void startDocument()
  131. {
  132. try
  133. {
  134. pushElement(null);
  135. }
  136. catch (Exception e)
  137. {
  138. Log.log(Log.ERROR,this, e);
  139. }
  140. } //}}}
  141. //{{{ Private members
  142. //{{{ Instance variables
  143. private String path;
  144. private JEditActionSet actionSet;
  145. private String actionName;
  146. private final StringBuilder code;
  147. private final StringBuilder isSelected;
  148. private boolean noRepeat;
  149. private boolean noRecord;
  150. private boolean noRememberLast;
  151. private final Stack<String> stateStack;
  152. //}}}
  153. //{{{ pushElement() method
  154. private String pushElement(String name)
  155. {
  156. name = (name == null) ? null : name.intern();
  157. stateStack.push(name);
  158. return name;
  159. } //}}}
  160. //{{{ peekElement() method
  161. private String peekElement()
  162. {
  163. return stateStack.peek();
  164. } //}}}
  165. //{{{ popElement() method
  166. private String popElement()
  167. {
  168. return stateStack.pop();
  169. } //}}}
  170. //}}}
  171. }