PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/pluginmgr/MirrorListHandler.java

#
Java | 155 lines | 98 code | 22 blank | 35 comment | 14 complexity | 63f3f6bf8337b024446753cdfb6e4a40 MD5 | raw file
  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
 23package org.gjt.sp.jedit.pluginmgr;
 24
 25import java.util.*;
 26
 27import org.xml.sax.Attributes;
 28import org.xml.sax.InputSource;
 29import org.xml.sax.helpers.DefaultHandler;
 30
 31import org.gjt.sp.util.XMLUtilities;
 32import org.gjt.sp.util.Log;
 33import org.gjt.sp.jedit.options.PluginOptions;
 34
 35/**
 36 * @version $Id: MirrorListHandler.java 12504 2008-04-22 23:12:43Z ezust $
 37 */
 38class MirrorListHandler extends DefaultHandler
 39{
 40	//{{{ Constructor
 41	MirrorListHandler(MirrorList mirrors, String path)
 42	{
 43		this.mirrors = mirrors;
 44		this.path = path;
 45	} //}}}
 46
 47	//{{{ resolveEntity() method
 48	public InputSource resolveEntity(String publicId, String 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 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 uri, String localName,
 71				 String tag, Attributes 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 uri, String localName, String 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 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 id;
125	private final StringBuilder description = new StringBuilder();
126	private final StringBuilder location = new StringBuilder();
127	private final StringBuilder country = new StringBuilder();
128	private final StringBuilder continent = new StringBuilder();
129
130	private final MirrorList mirrors;
131	private MirrorList.Mirror mirror;
132
133	private final Stack<String> stateStack = new Stack<String>();
134	private final String path;
135	//}}}
136
137	private String pushElement(String name)
138	{
139		name = name == null ? null : name.intern();
140		stateStack.push(name);
141		return name;
142	}
143
144	private String peekElement()
145	{
146		return stateStack.peek();
147	}
148
149	private void popElement()
150	{
151		stateStack.pop();
152	}
153
154	//}}}
155}