001: /*
002: * MirrorList.java - Mirrors list
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2002 Kris Kopicki
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.io.*;
026: import java.net.*;
027: import java.util.*;
028:
029: import org.xml.sax.XMLReader;
030: import org.xml.sax.InputSource;
031: import org.xml.sax.helpers.XMLReaderFactory;
032:
033: import org.gjt.sp.jedit.*;
034: import org.gjt.sp.util.IOUtilities;
035: import org.gjt.sp.util.ProgressObserver;
036: import org.gjt.sp.util.Log;
037:
038: /**
039: * @version $Id: MirrorList.java 7062 2006-09-18 09:55:39Z kpouer $
040: */
041: public class MirrorList {
042: public List<Mirror> mirrors;
043: /** The xml mirror list. */
044: public String xml;
045:
046: //{{{ MirrorList constructor
047: public MirrorList(boolean download, ProgressObserver observer)
048: throws Exception {
049: mirrors = new ArrayList<Mirror>();
050:
051: Mirror none = new Mirror();
052: none.id = Mirror.NONE;
053: none.description = none.location = none.country = none.continent = "";
054: mirrors.add(none);
055:
056: String path = jEdit.getProperty("plugin-manager.mirror-url");
057: MirrorListHandler handler = new MirrorListHandler(this , path);
058: if (download) {
059: Log.log(Log.NOTICE, this ,
060: "Loading mirror list from internet");
061: downloadXml(path);
062: } else {
063: Log.log(Log.NOTICE, this , "Loading mirror list from cache");
064: readXml();
065: }
066: observer.setValue(1);
067: Reader in = new BufferedReader(new StringReader(xml));
068:
069: InputSource isrc = new InputSource(in);
070: isrc.setSystemId("jedit.jar");
071: XMLReader parser = XMLReaderFactory.createXMLReader();
072: parser.setContentHandler(handler);
073: parser.setDTDHandler(handler);
074: parser.setEntityResolver(handler);
075: parser.setErrorHandler(handler);
076: parser.parse(isrc);
077: observer.setValue(2);
078: } //}}}
079:
080: //{{{ Private members
081:
082: //{{{ readXml() method
083: /**
084: * Read and store the mirror list xml.
085: * @throws IOException exception if it was not possible to read the
086: * xml or if the url was invalid
087: */
088: private void readXml() throws IOException {
089: String settingsDirectory = jEdit.getSettingsDirectory();
090: if (settingsDirectory == null)
091: return;
092:
093: File mirrorList = new File(MiscUtilities.constructPath(
094: settingsDirectory, "mirrorList.xml"));
095: if (!mirrorList.exists())
096: return;
097: InputStream inputStream = null;
098: try {
099: inputStream = new BufferedInputStream(new FileInputStream(
100: mirrorList));
101:
102: ByteArrayOutputStream out = new ByteArrayOutputStream();
103: IOUtilities.copyStream(null, inputStream, out, false);
104: xml = out.toString();
105: } finally {
106: IOUtilities.closeQuietly(inputStream);
107: }
108: } //}}}
109:
110: //{{{ downloadXml() method
111: /**
112: * Read and store the mirror list xml.
113: *
114: * @param path the url
115: * @throws IOException exception if it was not possible to read the
116: * xml or if the url was invalid
117: */
118: private void downloadXml(String path) throws IOException {
119: InputStream inputStream = null;
120: try {
121: inputStream = new URL(path).openStream();
122:
123: ByteArrayOutputStream out = new ByteArrayOutputStream();
124: IOUtilities.copyStream(null, inputStream, out, false);
125: xml = out.toString();
126: } finally {
127: IOUtilities.closeQuietly(inputStream);
128: }
129: } //}}}
130:
131: //{{{ add() method
132: void add(Mirror mirror) {
133: mirrors.add(mirror);
134: } //}}}
135:
136: //{{{ finished() method
137: void finished() {
138: Collections.sort(mirrors, new MirrorCompare());
139: } //}}}
140:
141: //}}}
142:
143: //{{{ Inner classes
144:
145: //{{{ Mirror class
146: public static class Mirror {
147: public static final String NONE = "NONE";
148:
149: public String id;
150: public String description;
151: public String location;
152: public String country;
153: public String continent;
154: } //}}}
155:
156: //{{{ MirrorCompare class
157: private class MirrorCompare implements Comparator<Mirror> {
158: public int compare(Mirror m1, Mirror m2) {
159: int result;
160: if ((result = m1.continent
161: .compareToIgnoreCase(m2.continent)) == 0)
162: if ((result = m1.country
163: .compareToIgnoreCase(m2.country)) == 0)
164: if ((result = m1.location
165: .compareToIgnoreCase(m2.location)) == 0)
166: return m1.description
167: .compareToIgnoreCase(m2.description);
168: return result;
169: }
170:
171: public boolean equals(Object obj) {
172: return obj instanceof MirrorCompare;
173: }
174: } //}}}
175:
176: //}}}
177: }
|