1 22 23 package org.gjt.sp.jedit.pluginmgr; 24 25 import java.io.*; 27 import java.net.URL ; 28 import java.util.*; 29 import java.util.zip.GZIPInputStream ; 30 import org.xml.sax.XMLReader ; 31 import org.xml.sax.InputSource ; 32 import org.xml.sax.helpers.XMLReaderFactory ; 33 import org.gjt.sp.util.Log; 34 import org.gjt.sp.util.StandardUtilities; 35 import org.gjt.sp.util.WorkRequest; 36 import org.gjt.sp.util.IOUtilities; 37 import org.gjt.sp.jedit.*; 38 40 45 class PluginList 46 { 47 50 public static final int GZIP_MAGIC_1 = 0x1f; 51 public static final int GZIP_MAGIC_2 = 0x8b; 52 53 final List<Plugin> plugins = new ArrayList<Plugin>(); 54 final Map<String , Plugin> pluginHash = new HashMap<String , Plugin>(); 55 final List<PluginSet> pluginSets = new ArrayList<PluginSet>(); 56 57 61 private final String id; 62 63 PluginList(WorkRequest workRequest) throws Exception 65 { 66 String path = jEdit.getProperty("plugin-manager.export-url"); 67 id = jEdit.getProperty("plugin-manager.mirror.id"); 68 if (!id.equals(MirrorList.Mirror.NONE)) 69 path += "?mirror="+id; 70 PluginListHandler handler = new PluginListHandler(this,path); 71 XMLReader parser = XMLReaderFactory.createXMLReader(); 72 InputStream in = null; 73 InputStream inputStream = null; 74 try 75 { 76 inputStream = new URL (path).openStream(); 77 workRequest.setStatus(jEdit.getProperty("plugin-manager.list-download")); 78 in = new BufferedInputStream(inputStream); 79 if(in.markSupported()) 80 { 81 in.mark(2); 82 int b1 = in.read(); 83 int b2 = in.read(); 84 in.reset(); 85 86 if(b1 == GZIP_MAGIC_1 && b2 == GZIP_MAGIC_2) 87 in = new GZIPInputStream (in); 88 } 89 90 InputSource isrc = new InputSource (new InputStreamReader(in,"UTF8")); 91 isrc.setSystemId("jedit.jar"); 92 parser.setContentHandler(handler); 93 parser.setDTDHandler(handler); 94 parser.setEntityResolver(handler); 95 parser.setErrorHandler(handler); 96 parser.parse(isrc); 97 } 98 finally 99 { 100 IOUtilities.closeQuietly(in); 101 IOUtilities.closeQuietly(inputStream); 102 } 103 } 105 void addPlugin(Plugin plugin) 107 { 108 plugin.checkIfInstalled(); 109 plugins.add(plugin); 110 pluginHash.put(plugin.name,plugin); 111 } 113 void addPluginSet(PluginSet set) 115 { 116 pluginSets.add(set); 117 } 119 void finished() 121 { 122 for(int i = 0; i < plugins.size(); i++) 125 { 126 Plugin plugin = plugins.get(i); 127 for(int j = 0; j < plugin.branches.size(); j++) 128 { 129 Branch branch = plugin.branches.get(j); 130 for(int k = 0; k < branch.deps.size(); k++) 131 { 132 Dependency dep = branch.deps.get(k); 133 if(dep.what.equals("plugin")) 134 dep.plugin = pluginHash.get(dep.pluginName); 135 } 136 } 137 } 138 } 140 void dump() 142 { 143 for(int i = 0; i < plugins.size(); i++) 144 { 145 System.err.println(plugins.get(i)); 146 System.err.println(); 147 } 148 } 150 157 String getMirrorId() 158 { 159 return id; 160 } 162 static class PluginSet 164 { 165 String name; 166 String description; 167 final List<String > plugins = new ArrayList<String >(); 168 169 public String toString() 170 { 171 return plugins.toString(); 172 } 173 } 175 public static class Plugin 177 { 178 String jar; 179 String name; 180 String description; 181 String author; 182 final List<Branch> branches = new ArrayList<Branch>(); 183 186 void checkIfInstalled() 187 { 188 223 } 224 225 String getInstalledVersion() 226 { 227 PluginJAR[] jars = jEdit.getPluginJARs(); 228 for(int i = 0; i < jars.length; i++) 229 { 230 String path = jars[i].getPath(); 231 232 if(MiscUtilities.getFileName(path).equals(jar)) 233 { 234 EditPlugin plugin = jars[i].getPlugin(); 235 if(plugin != null) 236 { 237 return jEdit.getProperty( 238 "plugin." + plugin.getClassName() 239 + ".version"); 240 } 241 else 242 return null; 243 } 244 } 245 246 return null; 247 } 248 249 String getInstalledPath() 250 { 251 PluginJAR[] jars = jEdit.getPluginJARs(); 252 for(int i = 0; i < jars.length; i++) 253 { 254 String path = jars[i].getPath(); 255 256 if(MiscUtilities.getFileName(path).equals(jar)) 257 return path; 258 } 259 260 return null; 261 } 262 263 266 Branch getCompatibleBranch() 267 { 268 for(int i = 0; i < branches.size(); i++) 269 { 270 Branch branch = branches.get(i); 271 if(branch.canSatisfyDependencies()) 272 return branch; 273 } 274 275 return null; 276 } 277 278 boolean canBeInstalled() 279 { 280 Branch branch = getCompatibleBranch(); 281 return branch != null && !branch.obsolete 282 && branch.canSatisfyDependencies(); 283 } 284 285 void install(Roster roster, String installDirectory, boolean downloadSource) 286 { 287 String installed = getInstalledPath(); 288 289 Branch branch = getCompatibleBranch(); 290 if(branch.obsolete) 291 { 292 if(installed != null) 293 roster.addRemove(installed); 294 return; 295 } 296 297 300 if(installed != null) 301 { 302 installDirectory = MiscUtilities.getParentOfPath( 303 installed); 304 } 305 306 roster.addInstall( 307 installed, 308 downloadSource ? branch.downloadSource : branch.download, 309 installDirectory, 310 downloadSource ? branch.downloadSourceSize : branch.downloadSize); 311 312 } 313 314 public String toString() 315 { 316 return name; 317 } 318 } 320 static class Branch 322 { 323 String version; 324 String date; 325 int downloadSize; 326 String download; 327 int downloadSourceSize; 328 String downloadSource; 329 boolean obsolete; 330 final List<Dependency> deps = new ArrayList<Dependency>(); 331 332 boolean canSatisfyDependencies() 333 { 334 for(int i = 0; i < deps.size(); i++) 335 { 336 Dependency dep = deps.get(i); 337 if(!dep.canSatisfy()) 338 return false; 339 } 340 341 return true; 342 } 343 344 void satisfyDependencies(Roster roster, String installDirectory, 345 boolean downloadSource) 346 { 347 for(int i = 0; i < deps.size(); i++) 348 { 349 Dependency dep = deps.get(i); 350 dep.satisfy(roster,installDirectory,downloadSource); 351 } 352 } 353 354 public String toString() 355 { 356 return "[version=" + version + ",download=" + download 357 + ",obsolete=" + obsolete + ",deps=" + deps + ']'; 358 } 359 } 361 static class Dependency 363 { 364 final String what; 365 final String from; 366 final String to; 367 final String pluginName; 369 Plugin plugin; 370 371 Dependency(String what, String from, String to, String pluginName) 372 { 373 this.what = what; 374 this.from = from; 375 this.to = to; 376 this.pluginName = pluginName; 377 } 378 379 boolean isSatisfied() 380 { 381 if(what.equals("plugin")) 382 { 383 for(int i = 0; i < plugin.branches.size(); i++) 384 { 385 String installedVersion = plugin.getInstalledVersion(); 386 if(installedVersion != null 387 && 388 (from == null || StandardUtilities.compareStrings( 389 installedVersion,from,false) >= 0) 390 && 391 (to == null || StandardUtilities.compareStrings( 392 installedVersion,to,false) <= 0)) 393 { 394 return true; 395 } 396 } 397 398 return false; 399 } 400 else if(what.equals("jdk")) 401 { 402 String javaVersion = System.getProperty("java.version").substring(0,3); 403 404 if((from == null || StandardUtilities.compareStrings( 405 javaVersion,from,false) >= 0) 406 && 407 (to == null || StandardUtilities.compareStrings( 408 javaVersion,to,false) <= 0)) 409 return true; 410 else 411 return false; 412 } 413 else if(what.equals("jedit")) 414 { 415 String build = jEdit.getBuild(); 416 417 if((from == null || StandardUtilities.compareStrings( 418 build,from,false) >= 0) 419 && 420 (to == null || StandardUtilities.compareStrings( 421 build,to,false) <= 0)) 422 return true; 423 else 424 return false; 425 } 426 else 427 { 428 Log.log(Log.ERROR,this,"Invalid dependency: " + what); 429 return false; 430 } 431 } 432 433 boolean canSatisfy() 434 { 435 if(isSatisfied()) 436 return true; 437 if (what.equals("plugin")) 438 return plugin.canBeInstalled(); 439 return false; 440 } 441 442 void satisfy(Roster roster, String installDirectory, 443 boolean downloadSource) 444 { 445 if(what.equals("plugin")) 446 { 447 String installedVersion = plugin.getInstalledVersion(); 448 for(int i = 0; i < plugin.branches.size(); i++) 449 { 450 Branch branch = plugin.branches.get(i); 451 if((installedVersion == null 452 || 453 StandardUtilities.compareStrings( 454 installedVersion,branch.version,false) < 0) 455 && 456 (from == null || StandardUtilities.compareStrings( 457 branch.version,from,false) >= 0) 458 && 459 (to == null || StandardUtilities.compareStrings( 460 branch.version,to,false) <= 0)) 461 { 462 plugin.install(roster,installDirectory, 463 downloadSource); 464 return; 465 } 466 } 467 } 468 } 469 470 public String toString() 471 { 472 return "[what=" + what + ",from=" + from 473 + ",to=" + to + ",plugin=" + plugin + ']'; 474 } 475 } } 477 | Popular Tags |