001: /*
002: * MirrorListHandler.java - XML handler for the mirrors list
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2002 Kris Kopicki (parts copied from 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.pluginmgr;
024:
025: import java.util.*;
026:
027: import org.xml.sax.Attributes;
028: import org.xml.sax.InputSource;
029: import org.xml.sax.helpers.DefaultHandler;
030:
031: import org.gjt.sp.util.XMLUtilities;
032: import org.gjt.sp.util.Log;
033: import org.gjt.sp.jedit.options.PluginOptions;
034:
035: /**
036: * @version $Id: MirrorListHandler.java 8314 2007-01-06 09:50:04Z kpouer $
037: */
038: class MirrorListHandler extends DefaultHandler {
039: //{{{ Constructor
040: MirrorListHandler(MirrorList mirrors, String path) {
041: this .mirrors = mirrors;
042: this .path = path;
043: } //}}}
044:
045: //{{{ resolveEntity() method
046: public InputSource resolveEntity(String publicId, String systemId) {
047: return XMLUtilities.findEntity(systemId, "mirrors.dtd",
048: PluginOptions.class);
049: } //}}}
050:
051: //{{{ characters() method
052: public void characters(char[] c, int off, int len) {
053: String tag = peekElement();
054:
055: if (tag == "DESCRIPTION")
056: description.append(c, off, len);
057: else if (tag == "LOCATION")
058: location.append(c, off, len);
059: else if (tag == "COUNTRY")
060: country.append(c, off, len);
061: else if (tag == "CONTINENT")
062: continent.append(c, off, len);
063: } //}}}
064:
065: //{{{ startElement() method
066: public void startElement(String uri, String localName, String tag,
067: Attributes attrs) {
068: tag = pushElement(tag);
069:
070: if (tag.equals("MIRROR")) {
071: mirror = new MirrorList.Mirror();
072: id = attrs.getValue("ID");
073: }
074: } //}}}
075:
076: //{{{ endElement() method
077: public void endElement(String uri, String localName, String tag) {
078: popElement();
079:
080: if (tag.equals("MIRROR")) {
081: mirror.id = id;
082: mirror.description = description.toString();
083: mirror.location = location.toString();
084: mirror.country = country.toString();
085: mirror.continent = continent.toString();
086: mirrors.add(mirror);
087: description.setLength(0);
088: location.setLength(0);
089: country.setLength(0);
090: continent.setLength(0);
091: }
092: } //}}}
093:
094: //{{{ startDocument() method
095: public void startDocument() {
096: try {
097: pushElement(null);
098: } catch (Exception e) {
099: Log.log(Log.ERROR, this , e);
100: }
101: } //}}}
102:
103: //{{{ endDocument() method
104: public void endDocument() {
105: mirrors.finished();
106: } //}}}
107:
108: //{{{ Private members
109:
110: //{{{ Variables
111: private String id;
112: private final StringBuilder description = new StringBuilder();
113: private final StringBuilder location = new StringBuilder();
114: private final StringBuilder country = new StringBuilder();
115: private final StringBuilder continent = new StringBuilder();
116:
117: private final MirrorList mirrors;
118: private MirrorList.Mirror mirror;
119:
120: private final Stack<String> stateStack = new Stack<String>();
121: private final String path;
122:
123: //}}}
124:
125: private String pushElement(String name) {
126: name = name == null ? null : name.intern();
127: stateStack.push(name);
128: return name;
129: }
130:
131: private String peekElement() {
132: return stateStack.peek();
133: }
134:
135: private void popElement() {
136: stateStack.pop();
137: }
138:
139: //}}}
140: }
|