PageRenderTime 71ms CodeModel.GetById 51ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/ServiceListHandler.java

#
Java | 161 lines | 94 code | 24 blank | 43 comment | 5 complexity | e13f04c0f9809bf37cfe80461d84e3fc 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 java.io.*;
 27import java.net.URL;
 28import java.util.*;
 29
 30import org.xml.sax.Attributes;
 31import org.xml.sax.InputSource;
 32import org.xml.sax.helpers.DefaultHandler;
 33
 34import org.gjt.sp.util.Log;
 35//}}}
 36
 37/**
 38 * @since jEdit 4.2pre1
 39 * @author Slava Pestov
 40 * @version $Id: ServiceListHandler.java 5443 2006-06-18 18:51:40Z vanza $
 41 */
 42class ServiceListHandler extends DefaultHandler
 43{
 44	//{{{ ServiceListHandler constructor
 45	ServiceListHandler(PluginJAR plugin, URL uri)
 46	{
 47		this.plugin = plugin;
 48		this.uri = uri;
 49		code = new StringBuffer();
 50		stateStack = new Stack();
 51		cachedServices = new LinkedList();
 52	} //}}}
 53
 54	//{{{ resolveEntity() method
 55	public InputSource resolveEntity(String publicId, String systemId)
 56	{
 57		return MiscUtilities.findEntity(systemId, "services.dtd", getClass());
 58	} //}}}
 59
 60	//{{{ characters() method
 61	public void characters(char[] c, int off, int len)
 62	{
 63		String tag = peekElement();
 64		if (tag == "SERVICE")
 65			code.append(c, off, len);
 66	} //}}}
 67
 68	//{{{ startElement() method
 69	public void startElement(String uri, String localName,
 70				 String tag, Attributes attrs)
 71	{
 72		tag = pushElement(tag);
 73		serviceName = attrs.getValue("NAME");
 74		serviceClass = attrs.getValue("CLASS");
 75	} //}}}
 76
 77	//{{{ endElement() method
 78	public void endElement(String uri, String localName, String name)
 79	{
 80		String tag = peekElement();
 81
 82		if(name.equals(tag))
 83		{
 84			if (tag.equals("SERVICE"))
 85			{
 86				ServiceManager.Descriptor d =
 87					new ServiceManager.Descriptor(
 88					serviceClass,serviceName,code.toString(),plugin);
 89				ServiceManager.registerService(d);
 90				cachedServices.add(d);
 91				code.setLength(0);
 92			}
 93
 94			popElement();
 95		}
 96		else
 97		{
 98			// can't happen
 99			throw new InternalError();
100		}
101	} //}}}
102
103	//{{{ startDocument() method
104	public void startDocument()
105	{
106		try
107		{
108			pushElement(null);
109		}
110		catch (Exception e)
111		{
112			e.printStackTrace();
113		}
114	} //}}}
115
116	//{{{ getCachedServices() method
117	public ServiceManager.Descriptor[] getCachedServices()
118	{
119		return (ServiceManager.Descriptor[])cachedServices.toArray(
120			new ServiceManager.Descriptor[cachedServices.size()]);
121	} //}}}
122
123	//{{{ Private members
124
125	//{{{ Instance variables
126	private PluginJAR plugin;
127	private URL uri;
128
129	private String serviceName;
130	private String serviceClass;
131	private StringBuffer code;
132
133	private Stack stateStack;
134
135	private List cachedServices;
136	//}}}
137
138	//{{{ pushElement() method
139	private String pushElement(String name)
140	{
141		name = (name == null) ? null : name.intern();
142
143		stateStack.push(name);
144
145		return name;
146	} //}}}
147
148	//{{{ peekElement() method
149	private String peekElement()
150	{
151		return (String) stateStack.peek();
152	} //}}}
153
154	//{{{ popElement() method
155	private String popElement()
156	{
157		return (String) stateStack.pop();
158	} //}}}
159
160	//}}}
161}