PageRenderTime 357ms CodeModel.GetById 88ms app.highlight 4ms RepoModel.GetById 264ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/ServiceListHandler.java

#
Java | 199 lines | 110 code | 29 blank | 60 comment | 13 complexity | f5a5e56ac8b3edeed40522369145b3c5 MD5 | raw file
  1/*
  2 * ServiceManager.java - Handles services.xml files in plugins
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2003 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit;
 24
 25//{{{ Imports
 26import com.microstar.xml.*;
 27import java.io.*;
 28import java.net.URL;
 29import java.util.*;
 30import org.gjt.sp.util.Log;
 31//}}}
 32
 33/**
 34 * @since jEdit 4.2pre1
 35 * @author Slava Pestov
 36 * @version $Id: ServiceListHandler.java 4667 2003-04-30 21:22:41Z spestov $
 37 */
 38class ServiceListHandler extends HandlerBase
 39{
 40	//{{{ ServiceListHandler constructor
 41	ServiceListHandler(PluginJAR plugin, URL uri)
 42	{
 43		this.plugin = plugin;
 44		this.uri = uri;
 45		stateStack = new Stack();
 46		cachedServices = new LinkedList();
 47	} //}}}
 48
 49	//{{{ resolveEntity() method
 50	public Object resolveEntity(String publicId, String systemId)
 51	{
 52		if("services.dtd".equals(systemId))
 53		{
 54			// this will result in a slight speed up, since we
 55			// don't need to read the DTD anyway, as AElfred is
 56			// non-validating
 57			return new StringReader("<!-- -->");
 58
 59			/* try
 60			{
 61				return new BufferedReader(new InputStreamReader(
 62					getClass().getResourceAsStream
 63					("/org/gjt/sp/jedit/services.dtd")));
 64			}
 65			catch(Exception e)
 66			{
 67				Log.log(Log.ERROR,this,"Error while opening"
 68					+ " dockables.dtd:");
 69				Log.log(Log.ERROR,this,e);
 70			} */
 71		}
 72
 73		return null;
 74	} //}}}
 75
 76	//{{{ attribute() method
 77	public void attribute(String aname, String value, boolean isSpecified)
 78	{
 79		if(aname.equals("NAME"))
 80			serviceName = value;
 81		else if(aname.equals("CLASS"))
 82			serviceClass = value;
 83	} //}}}
 84
 85	//{{{ doctypeDecl() method
 86	public void doctypeDecl(String name, String publicId,
 87		String systemId) throws Exception
 88	{
 89		if("SERVICES".equals(name))
 90			return;
 91
 92		Log.log(Log.ERROR,this,uri + ": DOCTYPE must be SERVICES");
 93	} //}}}
 94
 95	//{{{ charData() method
 96	public void charData(char[] c, int off, int len)
 97	{
 98		String tag = peekElement();
 99		String text = new String(c, off, len);
100
101		if (tag == "SERVICE")
102		{
103			code = text;
104		}
105	} //}}}
106
107	//{{{ startElement() method
108	public void startElement(String tag)
109	{
110		tag = pushElement(tag);
111	} //}}}
112
113	//{{{ endElement() method
114	public void endElement(String name)
115	{
116		if(name == null)
117			return;
118
119		String tag = peekElement();
120
121		if(name.equals(tag))
122		{
123			if(tag == "SERVICE")
124			{
125				ServiceManager.Descriptor d =
126					new ServiceManager.Descriptor(
127					serviceClass,serviceName,code,plugin);
128				ServiceManager.registerService(d);
129				cachedServices.add(d);
130			}
131
132			popElement();
133		}
134		else
135		{
136			// can't happen
137			throw new InternalError();
138		}
139	} //}}}
140
141	//{{{ startDocument() method
142	public void startDocument()
143	{
144		try
145		{
146			pushElement(null);
147		}
148		catch (Exception e)
149		{
150			e.printStackTrace();
151		}
152	} //}}}
153
154	//{{{ getCachedServices() method
155	public ServiceManager.Descriptor[] getCachedServices()
156	{
157		return (ServiceManager.Descriptor[])cachedServices.toArray(
158			new ServiceManager.Descriptor[cachedServices.size()]);
159	} //}}}
160
161	//{{{ Private members
162
163	//{{{ Instance variables
164	private PluginJAR plugin;
165	private URL uri;
166
167	private String serviceName;
168	private String serviceClass;
169	private String code;
170
171	private Stack stateStack;
172
173	private List cachedServices;
174	//}}}
175
176	//{{{ pushElement() method
177	private String pushElement(String name)
178	{
179		name = (name == null) ? null : name.intern();
180
181		stateStack.push(name);
182
183		return name;
184	} //}}}
185
186	//{{{ peekElement() method
187	private String peekElement()
188	{
189		return (String) stateStack.peek();
190	} //}}}
191
192	//{{{ popElement() method
193	private String popElement()
194	{
195		return (String) stateStack.pop();
196	} //}}}
197
198	//}}}
199}