1 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 ; 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.helpers.XMLReaderFactory ; 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 41 public class MirrorList 42 { 43 public List<Mirror> mirrors; 44 45 public String xml; 46 47 public MirrorList(boolean download, ProgressObserver observer) throws Exception 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 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 in = new BufferedReader(new StringReader(xml)); 71 72 InputSource isrc = new InputSource (in); 73 isrc.setSystemId("jedit.jar"); 74 XMLReader 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 } 83 85 91 private void readXml() throws IOException 92 { 93 String 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 } 116 124 private void downloadXml(String 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 } 141 void add(Mirror mirror) 143 { 144 mirrors.add(mirror); 145 } 147 void finished() 149 { 150 Collections.sort(mirrors,new MirrorCompare()); 151 } 153 155 157 public static class Mirror 159 { 160 public static final String NONE = "NONE"; 161 162 public String id; 163 public String description; 164 public String location; 165 public String country; 166 public String continent; 167 } 169 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 obj) 183 { 184 return obj instanceof MirrorCompare; 185 } 186 } 188 } 190 | Popular Tags |