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