001: /*
002: * ServiceManager.java - Handles services.xml files in plugins
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit;
024:
025: //{{{ Imports
026: import java.io.*;
027: import java.net.URL;
028: import java.util.*;
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: /**
040: * @since jEdit 4.2pre1
041: * @author Slava Pestov
042: * @version $Id: ServiceListHandler.java 5573 2006-07-13 04:37:32Z vanza $
043: */
044: class ServiceListHandler extends DefaultHandler {
045: //{{{ ServiceListHandler constructor
046: ServiceListHandler(PluginJAR plugin, URL uri) {
047: this .plugin = plugin;
048: this .uri = uri;
049: code = new StringBuffer();
050: stateStack = new Stack();
051: cachedServices = new LinkedList();
052: } //}}}
053:
054: //{{{ resolveEntity() method
055: public InputSource resolveEntity(String publicId, String systemId) {
056: return XMLUtilities.findEntity(systemId, "services.dtd",
057: getClass());
058: } //}}}
059:
060: //{{{ characters() method
061: public void characters(char[] c, int off, int len) {
062: String tag = peekElement();
063: if (tag == "SERVICE")
064: code.append(c, off, len);
065: } //}}}
066:
067: //{{{ startElement() method
068: public void startElement(String uri, String localName, String tag,
069: Attributes attrs) {
070: tag = pushElement(tag);
071: serviceName = attrs.getValue("NAME");
072: serviceClass = attrs.getValue("CLASS");
073: } //}}}
074:
075: //{{{ endElement() method
076: public void endElement(String uri, String localName, String name) {
077: String tag = peekElement();
078:
079: if (name.equals(tag)) {
080: if (tag.equals("SERVICE")) {
081: ServiceManager.Descriptor d = new ServiceManager.Descriptor(
082: serviceClass, serviceName, code.toString(),
083: plugin);
084: ServiceManager.registerService(d);
085: cachedServices.add(d);
086: code.setLength(0);
087: }
088:
089: popElement();
090: } else {
091: // can't happen
092: throw new InternalError();
093: }
094: } //}}}
095:
096: //{{{ startDocument() method
097: public void startDocument() {
098: try {
099: pushElement(null);
100: } catch (Exception e) {
101: e.printStackTrace();
102: }
103: } //}}}
104:
105: //{{{ getCachedServices() method
106: public ServiceManager.Descriptor[] getCachedServices() {
107: return (ServiceManager.Descriptor[]) cachedServices
108: .toArray(new ServiceManager.Descriptor[cachedServices
109: .size()]);
110: } //}}}
111:
112: //{{{ Private members
113:
114: //{{{ Instance variables
115: private PluginJAR plugin;
116: private URL uri;
117:
118: private String serviceName;
119: private String serviceClass;
120: private StringBuffer code;
121:
122: private Stack stateStack;
123:
124: private List cachedServices;
125:
126: //}}}
127:
128: //{{{ pushElement() method
129: private String pushElement(String name) {
130: name = (name == null) ? null : name.intern();
131:
132: stateStack.push(name);
133:
134: return name;
135: } //}}}
136:
137: //{{{ peekElement() method
138: private String peekElement() {
139: return (String) stateStack.peek();
140: } //}}}
141:
142: //{{{ popElement() method
143: private String popElement() {
144: return (String) stateStack.pop();
145: } //}}}
146:
147: //}}}
148: }
|