/plugins/XInsert/tags/XInsert-2.6/src/XInsertHandler.java

# · Java · 145 lines · 89 code · 16 blank · 40 comment · 32 complexity · e5aee65114967369cda0acfcb8e79374 MD5 · raw file

  1. /*
  2. * 19:34:16 10/09/99
  3. *
  4. * XInsertHandler.java - Handles xml-insert files for Jext
  5. * Copyright (C) 1999 Romain Guy - powerteam@chez.com
  6. * Portions Copyright (C) 2000 Dominic Stolerman - dominic@sspd.org.uk
  7. * added REFERENCE_TYPE (c) 2006 Martin Raspe - hertzhaft@biblhertz.it
  8. * www.chez.com/powerteam
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. import java.util.Stack;
  25. import java.util.Vector;
  26. import org.xml.sax.helpers.DefaultHandler;
  27. import org.xml.sax.InputSource;
  28. import org.xml.sax.Attributes;
  29. import org.gjt.sp.util.Log;
  30. public class XInsertHandler extends DefaultHandler {
  31. // private members
  32. private XTree tree;
  33. private Stack stateStack;
  34. private int type = XTreeItem.TEXT_TYPE;
  35. private String lastAttr, lastName, lastAttrValue, propValue;
  36. private StringBuilder lastValue;
  37. public XInsertHandler(XTree tree) { this.tree = tree; }
  38. public void attribute(String aname, String value) {
  39. // TYPE attribute
  40. if (aname.equalsIgnoreCase("TYPE")) {
  41. if(value.equalsIgnoreCase("MACRO"))
  42. type = XTreeItem.MACRO_TYPE;
  43. else if(value.equalsIgnoreCase("XINSERT_SCRIPT"))
  44. type = XTreeItem.XINSERT_SCRIPT_TYPE;
  45. else if(value.equalsIgnoreCase("TEXT"))
  46. type = XTreeItem.TEXT_TYPE;
  47. // new data type added here
  48. else if(value.equalsIgnoreCase("NAMED_MACRO"))
  49. type = XTreeItem.NAMED_MACRO_TYPE;
  50. // new data types added: hertzhaft
  51. else if(value.equalsIgnoreCase("ACTION"))
  52. type = XTreeItem.ACTION_TYPE;
  53. else if(value.equalsIgnoreCase("REFERENCE"))
  54. type = XTreeItem.REFERENCE_TYPE;
  55. else {
  56. type = XTreeItem.UNKNOWN_TYPE;
  57. Log.log(Log.WARNING, this, "Invalid value for attribute \"type\": " + value);
  58. }
  59. }
  60. else if (aname.equalsIgnoreCase("NAME")) {
  61. // NAME attribute
  62. lastAttr = aname;
  63. lastAttrValue = value;
  64. }
  65. else if (aname.equalsIgnoreCase("VALUE"))
  66. // VALUE attribute
  67. propValue = value;
  68. }
  69. @Override
  70. public void characters(char[] c, int off, int len) {
  71. // ITEM content
  72. if ("ITEM".equalsIgnoreCase(((String) stateStack.peek())))
  73. // calling append since we get value one bit at a time
  74. lastValue.append(new String(c, off, len));
  75. }
  76. @Override
  77. public void startElement(String ns, String localName, String qName, Attributes attributes) {
  78. // handle the attributes before pushing the element
  79. // (this is what the old com.microstar.xml XmlParser implementation did)
  80. for(int i=0;i<attributes.getLength();i++)
  81. {
  82. String aname = attributes.getLocalName(i);
  83. String value = attributes.getValue(i);
  84. attribute(aname, value);
  85. }
  86. // reset String contents
  87. lastValue = new StringBuilder();
  88. if ("NAME".equalsIgnoreCase(lastAttr)) {
  89. // named MENU
  90. if ("MENU".equalsIgnoreCase(localName)) {
  91. //Log.log(Log.DEBUG, this, "adding Menu: " + lastAttrValue);
  92. tree.addMenu(lastAttrValue);
  93. }
  94. // named VARIABLE
  95. if ("VARIABLE".equalsIgnoreCase(localName)) {
  96. if(propValue == null || lastAttrValue == null)
  97. Log.log(Log.WARNING, this, "Can not set XInsert property");
  98. else
  99. tree.addVariable(lastAttrValue, propValue);
  100. }
  101. }
  102. stateStack.push(localName);
  103. }
  104. @Override
  105. public void endElement(String ns, String localName, String qName) {
  106. if (localName == null) return;
  107. String lastStartTag = (String) stateStack.peek();
  108. if (localName.equalsIgnoreCase(lastStartTag)) {
  109. // MENU end
  110. if (lastStartTag.equalsIgnoreCase("MENU"))
  111. tree.closeMenu();
  112. // ITEM end
  113. else if (lastStartTag.equalsIgnoreCase("ITEM")) {
  114. tree.addInsert(lastAttrValue, lastValue.toString(), type);
  115. type = XTreeItem.TEXT_TYPE;
  116. }
  117. stateStack.pop();
  118. } else
  119. System.err.println("Unclosed tag: " + stateStack.peek());
  120. lastAttr = null;
  121. lastAttrValue = null;
  122. }
  123. @Override
  124. public void startDocument() {
  125. stateStack = new Stack();
  126. stateStack.push(null);
  127. }
  128. }
  129. // End of XInsertHandler.java