KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * MirrorList.java - Mirrors list
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2002 Kris Kopicki
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.io.*;
26 import java.net.*;
27 import java.util.*;
28
29 import org.xml.sax.XMLReader JavaDoc;
30 import org.xml.sax.InputSource JavaDoc;
31 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
32
33 import org.gjt.sp.jedit.*;
34 import org.gjt.sp.util.IOUtilities;
35 import org.gjt.sp.util.ProgressObserver;
36 import org.gjt.sp.util.Log;
37
38 /**
39  * @version $Id: MirrorList.java 7062 2006-09-18 09:55:39Z kpouer $
40  */

41 public class MirrorList
42 {
43     public List<Mirror> mirrors;
44     /** The xml mirror list. */
45     public String JavaDoc xml;
46
47     //{{{ MirrorList constructor
48
public MirrorList(boolean download, ProgressObserver observer) throws Exception JavaDoc
49     {
50         mirrors = new ArrayList<Mirror>();
51
52         Mirror none = new Mirror();
53         none.id = Mirror.NONE;
54         none.description = none.location = none.country = none.continent = "";
55         mirrors.add(none);
56
57         String JavaDoc path = jEdit.getProperty("plugin-manager.mirror-url");
58         MirrorListHandler handler = new MirrorListHandler(this,path);
59         if (download)
60         {
61             Log.log(Log.NOTICE, this, "Loading mirror list from internet");
62             downloadXml(path);
63         }
64         else
65         {
66             Log.log(Log.NOTICE, this, "Loading mirror list from cache");
67             readXml();
68         }
69         observer.setValue(1);
70         Reader JavaDoc in = new BufferedReader(new StringReader(xml));
71
72         InputSource JavaDoc isrc = new InputSource JavaDoc(in);
73         isrc.setSystemId("jedit.jar");
74         XMLReader JavaDoc parser = XMLReaderFactory.createXMLReader();
75         parser.setContentHandler(handler);
76         parser.setDTDHandler(handler);
77         parser.setEntityResolver(handler);
78         parser.setErrorHandler(handler);
79         parser.parse(isrc);
80         observer.setValue(2);
81     } //}}}
82

83     //{{{ Private members
84

85     //{{{ readXml() method
86
/**
87      * Read and store the mirror list xml.
88      * @throws IOException exception if it was not possible to read the
89      * xml or if the url was invalid
90      */

91     private void readXml() throws IOException
92     {
93         String JavaDoc settingsDirectory = jEdit.getSettingsDirectory();
94         if(settingsDirectory == null)
95             return;
96
97         File mirrorList = new File(MiscUtilities.constructPath(
98             settingsDirectory,"mirrorList.xml"));
99         if(!mirrorList.exists())
100             return;
101         InputStream inputStream = null;
102         try
103         {
104             inputStream = new BufferedInputStream(new FileInputStream(mirrorList));
105
106             ByteArrayOutputStream out = new ByteArrayOutputStream();
107             IOUtilities.copyStream(null,inputStream,out, false);
108             xml = out.toString();
109         }
110         finally
111         {
112             IOUtilities.closeQuietly(inputStream);
113         }
114     } //}}}
115

116     //{{{ downloadXml() method
117
/**
118      * Read and store the mirror list xml.
119      *
120      * @param path the url
121      * @throws IOException exception if it was not possible to read the
122      * xml or if the url was invalid
123      */

124     private void downloadXml(String JavaDoc path) throws IOException
125     {
126         InputStream inputStream = null;
127         try
128         {
129             inputStream = new URL(path).openStream();
130
131             ByteArrayOutputStream out = new ByteArrayOutputStream();
132             IOUtilities.copyStream(null,inputStream,out, false);
133             xml = out.toString();
134         }
135         finally
136         {
137             IOUtilities.closeQuietly(inputStream);
138         }
139     } //}}}
140

141     //{{{ add() method
142
void add(Mirror mirror)
143     {
144         mirrors.add(mirror);
145     } //}}}
146

147     //{{{ finished() method
148
void finished()
149     {
150         Collections.sort(mirrors,new MirrorCompare());
151     } //}}}
152

153     //}}}
154

155     //{{{ Inner classes
156

157     //{{{ Mirror class
158
public static class Mirror
159     {
160         public static final String JavaDoc NONE = "NONE";
161
162         public String JavaDoc id;
163         public String JavaDoc description;
164         public String JavaDoc location;
165         public String JavaDoc country;
166         public String JavaDoc continent;
167     } //}}}
168

169     //{{{ MirrorCompare class
170
private class MirrorCompare implements Comparator<Mirror>
171     {
172         public int compare(Mirror m1,Mirror m2)
173         {
174             int result;
175             if ((result = m1.continent.compareToIgnoreCase(m2.continent)) == 0)
176                 if ((result = m1.country.compareToIgnoreCase(m2.country)) == 0)
177                     if ((result = m1.location.compareToIgnoreCase(m2.location)) == 0)
178                         return m1.description.compareToIgnoreCase(m2.description);
179             return result;
180         }
181
182         public boolean equals(Object JavaDoc obj)
183         {
184             return obj instanceof MirrorCompare;
185         }
186     } //}}}
187

188     //}}}
189
}
190
Popular Tags