001: /*
002: * ActionListHandler.java - XML handler for action files
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2001 Slava Pestov
007: * Portions copyright (C) 1999 mike dillon
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: */
023:
024: package org.gjt.sp.jedit;
025:
026: //{{{ Imports
027: import java.io.*;
028: import java.util.Stack;
029:
030: import org.xml.sax.Attributes;
031: import org.xml.sax.InputSource;
032: import org.xml.sax.helpers.DefaultHandler;
033:
034: import org.gjt.sp.util.Log;
035: import org.gjt.sp.util.XMLUtilities;
036:
037: //}}}
038:
039: class ActionListHandler extends DefaultHandler {
040: //{{{ ActionListHandler constructor
041: ActionListHandler(String path, ActionSet actionSet) {
042: this .path = path;
043: this .actionSet = actionSet;
044: stateStack = new Stack();
045: code = new StringBuffer();
046: isSelected = new StringBuffer();
047: } //}}}
048:
049: //{{{ resolveEntity() method
050: public InputSource resolveEntity(String publicId, String systemId) {
051: return XMLUtilities.findEntity(systemId, "actions.dtd",
052: getClass());
053: } //}}}
054:
055: //{{{ attribute() method
056: public void attribute(String aname, String value,
057: boolean isSpecified) {
058: aname = (aname == null) ? null : aname.intern();
059: value = (value == null) ? null : value.intern();
060:
061: if (aname == "NAME")
062: actionName = value;
063: else if (aname == "NO_REPEAT")
064: noRepeat = (value == "TRUE");
065: else if (aname == "NO_RECORD")
066: noRecord = (value == "TRUE");
067: else if (aname == "NO_REMEMBER_LAST")
068: noRememberLast = (value == "TRUE");
069: } //}}}
070:
071: //{{{ characters() method
072: public void characters(char[] c, int off, int len) {
073: String tag = peekElement();
074: if (tag.equals("CODE")) {
075: code.append(c, off, len);
076: } else if (tag.equals("IS_SELECTED")) {
077: isSelected.append(c, off, len);
078: }
079: } //}}}
080:
081: //{{{ startElement() method
082: public void startElement(String uri, String localName,
083: String qName, Attributes attrs) {
084: String tag = pushElement(qName);
085:
086: if (tag.equals("ACTION")) {
087: actionName = attrs.getValue("NAME");
088: noRepeat = "TRUE".equals(attrs.getValue("NO_REPEAT"));
089: noRecord = "TRUE".equals(attrs.getValue("NO_RECORD"));
090: noRememberLast = "TRUE".equals(attrs
091: .getValue("NO_REMEMBER_LAST"));
092: code.setLength(0);
093: isSelected.setLength(0);
094: }
095: } //}}}
096:
097: //{{{ endElement() method
098: public void endElement(String uri, String localName, String qName) {
099: String tag = peekElement();
100:
101: if (qName.equals(tag)) {
102: if (tag.equals("ACTION")) {
103: String selected = (isSelected.length() > 0) ? isSelected
104: .toString()
105: : null;
106: actionSet.addAction(new BeanShellAction(actionName,
107: code.toString(), selected, noRepeat, noRecord,
108: noRememberLast));
109: noRepeat = noRecord = noRememberLast = false;
110: code.setLength(0);
111: isSelected.setLength(0);
112: }
113:
114: popElement();
115: } else {
116: // can't happen
117: throw new InternalError();
118: }
119: } //}}}
120:
121: //{{{ startDocument() method
122: public void startDocument() {
123: try {
124: pushElement(null);
125: } catch (Exception e) {
126: e.printStackTrace();
127: }
128: } //}}}
129:
130: //{{{ Private members
131:
132: //{{{ Instance variables
133: private String path;
134: private ActionSet actionSet;
135:
136: private String actionName;
137: private StringBuffer code;
138: private StringBuffer isSelected;
139:
140: private boolean noRepeat;
141: private boolean noRecord;
142: private boolean noRememberLast;
143:
144: private Stack stateStack;
145:
146: //}}}
147:
148: //{{{ pushElement() method
149: private String pushElement(String name) {
150: name = (name == null) ? null : name.intern();
151:
152: stateStack.push(name);
153:
154: return name;
155: } //}}}
156:
157: //{{{ peekElement() method
158: private String peekElement() {
159: return (String) stateStack.peek();
160: } //}}}
161:
162: //{{{ popElement() method
163: private String popElement() {
164: return (String) stateStack.pop();
165: } //}}}
166:
167: //}}}
168:
169: }
|