KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > pluginmgr > MirrorListHandler


1 /*
2  * MirrorListHandler.java - XML handler for the mirrors list
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2002 Kris Kopicki (parts copied from 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.pluginmgr;
24
25 import java.util.*;
26
27 import org.xml.sax.Attributes JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29 import org.xml.sax.helpers.DefaultHandler JavaDoc;
30
31 import org.gjt.sp.util.XMLUtilities;
32 import org.gjt.sp.util.Log;
33 import org.gjt.sp.jedit.options.PluginOptions;
34
35 /**
36  * @version $Id: MirrorListHandler.java 8314 2007-01-06 09:50:04Z kpouer $
37  */

38 class MirrorListHandler extends DefaultHandler JavaDoc
39 {
40     //{{{ Constructor
41
MirrorListHandler(MirrorList mirrors, String JavaDoc path)
42     {
43         this.mirrors = mirrors;
44         this.path = path;
45     } //}}}
46

47     //{{{ resolveEntity() method
48
public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
49     {
50         return XMLUtilities.findEntity(systemId, "mirrors.dtd",
51                     PluginOptions.class);
52     } //}}}
53

54     //{{{ characters() method
55
public void characters(char[] c, int off, int len)
56     {
57         String JavaDoc tag = peekElement();
58
59         if(tag == "DESCRIPTION")
60             description.append(c, off, len);
61         else if(tag == "LOCATION")
62             location.append(c, off, len);
63         else if(tag == "COUNTRY")
64             country.append(c, off, len);
65         else if(tag == "CONTINENT")
66             continent.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
75         if (tag.equals("MIRROR"))
76         {
77             mirror = new MirrorList.Mirror();
78             id = attrs.getValue("ID");
79         }
80     } //}}}
81

82     //{{{ endElement() method
83
public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc tag)
84     {
85         popElement();
86
87         if(tag.equals("MIRROR"))
88         {
89             mirror.id = id;
90             mirror.description = description.toString();
91             mirror.location = location.toString();
92             mirror.country = country.toString();
93             mirror.continent = continent.toString();
94             mirrors.add(mirror);
95             description.setLength(0);
96             location.setLength(0);
97             country.setLength(0);
98             continent.setLength(0);
99         }
100     } //}}}
101

102     //{{{ startDocument() method
103
public void startDocument()
104     {
105         try
106         {
107             pushElement(null);
108         }
109         catch (Exception JavaDoc e)
110         {
111             Log.log(Log.ERROR, this, e);
112         }
113     } //}}}
114

115     //{{{ endDocument() method
116
public void endDocument()
117     {
118         mirrors.finished();
119     } //}}}
120

121     //{{{ Private members
122

123     //{{{ Variables
124
private String JavaDoc id;
125     private final StringBuilder JavaDoc description = new StringBuilder JavaDoc();
126     private final StringBuilder JavaDoc location = new StringBuilder JavaDoc();
127     private final StringBuilder JavaDoc country = new StringBuilder JavaDoc();
128     private final StringBuilder JavaDoc continent = new StringBuilder JavaDoc();
129
130     private final MirrorList mirrors;
131     private MirrorList.Mirror mirror;
132
133     private final Stack<String JavaDoc> stateStack = new Stack<String JavaDoc>();
134     private final String JavaDoc path;
135     //}}}
136

137     private String JavaDoc pushElement(String JavaDoc name)
138     {
139         name = name == null ? null : name.intern();
140         stateStack.push(name);
141         return name;
142     }
143
144     private String JavaDoc peekElement()
145     {
146         return stateStack.peek();
147     }
148
149     private void popElement()
150     {
151         stateStack.pop();
152     }
153
154     //}}}
155
}
156
Popular Tags