/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/ServiceListHandler.java
Java | 161 lines | 94 code | 24 blank | 43 comment | 5 complexity | b0fefa15fc3e95e14158e0bc3eb0f87e 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 * 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.net.URL;
27import java.util.*;
28
29import org.xml.sax.Attributes;
30import org.xml.sax.InputSource;
31import org.xml.sax.helpers.DefaultHandler;
32
33import org.gjt.sp.util.XMLUtilities;
34import org.gjt.sp.util.Log;
35//}}}
36
37/**
38 * @since jEdit 4.2pre1
39 * @author Slava Pestov
40 * @version $Id: ServiceListHandler.java 16676 2009-12-18 14:44:32Z shlomy $
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 StringBuilder();
50 stateStack = new Stack<String>();
51 cachedServices = new LinkedList<ServiceManager.Descriptor>();
52 } //}}}
53
54 //{{{ resolveEntity() method
55 public InputSource resolveEntity(String publicId, String systemId)
56 {
57 return XMLUtilities.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 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 Log.log(Log.ERROR, e, e);
113 }
114 } //}}}
115
116 //{{{ getCachedServices() method
117 public ServiceManager.Descriptor[] getCachedServices()
118 {
119 return 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 StringBuilder code;
132
133 private Stack<String> stateStack;
134
135 private List<ServiceManager.Descriptor> 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 stateStack.peek();
152 } //}}}
153
154 //{{{ popElement() method
155 private String popElement()
156 {
157 return stateStack.pop();
158 } //}}}
159
160 //}}}
161}