KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > ServiceListHandler


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
23 package org.gjt.sp.jedit;
24
25 //{{{ Imports
26
import java.io.*;
27 import java.net.URL JavaDoc;
28 import java.util.*;
29
30 import org.xml.sax.Attributes JavaDoc;
31 import org.xml.sax.InputSource JavaDoc;
32 import org.xml.sax.helpers.DefaultHandler JavaDoc;
33
34 import org.gjt.sp.util.Log;
35 import org.gjt.sp.util.XMLUtilities;
36 //}}}
37

38 /**
39  * @since jEdit 4.2pre1
40  * @author Slava Pestov
41  * @version $Id: ServiceListHandler.java 5573 2006-07-13 04:37:32Z vanza $
42  */

43 class ServiceListHandler extends DefaultHandler JavaDoc
44 {
45     //{{{ ServiceListHandler constructor
46
ServiceListHandler(PluginJAR plugin, URL JavaDoc uri)
47     {
48         this.plugin = plugin;
49         this.uri = uri;
50         code = new StringBuffer JavaDoc();
51         stateStack = new Stack();
52         cachedServices = new LinkedList();
53     } //}}}
54

55     //{{{ resolveEntity() method
56
public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
57     {
58         return XMLUtilities.findEntity(systemId, "services.dtd", getClass());
59     } //}}}
60

61     //{{{ characters() method
62
public void characters(char[] c, int off, int len)
63     {
64         String JavaDoc tag = peekElement();
65         if (tag == "SERVICE")
66             code.append(c, off, len);
67     } //}}}
68

69     //{{{ startElement() method
70
public void startElement(String JavaDoc uri, String JavaDoc localName,
71                  String JavaDoc tag, Attributes JavaDoc attrs)
72     {
73         tag = pushElement(tag);
74         serviceName = attrs.getValue("NAME");
75         serviceClass = attrs.getValue("CLASS");
76     } //}}}
77

78     //{{{ endElement() method
79
public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc name)
80     {
81         String JavaDoc tag = peekElement();
82
83         if(name.equals(tag))
84         {
85             if (tag.equals("SERVICE"))
86             {
87                 ServiceManager.Descriptor d =
88                     new ServiceManager.Descriptor(
89                     serviceClass,serviceName,code.toString(),plugin);
90                 ServiceManager.registerService(d);
91                 cachedServices.add(d);
92                 code.setLength(0);
93             }
94
95             popElement();
96         }
97         else
98         {
99             // can't happen
100
throw new InternalError JavaDoc();
101         }
102     } //}}}
103

104     //{{{ startDocument() method
105
public void startDocument()
106     {
107         try
108         {
109             pushElement(null);
110         }
111         catch (Exception JavaDoc e)
112         {
113             e.printStackTrace();
114         }
115     } //}}}
116

117     //{{{ getCachedServices() method
118
public ServiceManager.Descriptor[] getCachedServices()
119     {
120         return (ServiceManager.Descriptor[])cachedServices.toArray(
121             new ServiceManager.Descriptor[cachedServices.size()]);
122     } //}}}
123

124     //{{{ Private members
125

126     //{{{ Instance variables
127
private PluginJAR plugin;
128     private URL JavaDoc uri;
129
130     private String JavaDoc serviceName;
131     private String JavaDoc serviceClass;
132     private StringBuffer JavaDoc code;
133
134     private Stack stateStack;
135
136     private List cachedServices;
137     //}}}
138

139     //{{{ pushElement() method
140
private String JavaDoc pushElement(String JavaDoc name)
141     {
142         name = (name == null) ? null : name.intern();
143
144         stateStack.push(name);
145
146         return name;
147     } //}}}
148

149     //{{{ peekElement() method
150
private String JavaDoc peekElement()
151     {
152         return (String JavaDoc) stateStack.peek();
153     } //}}}
154

155     //{{{ popElement() method
156
private String JavaDoc popElement()
157     {
158         return (String JavaDoc) stateStack.pop();
159     } //}}}
160

161     //}}}
162
}
163
Popular Tags