PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/pluginmgr/PluginList.java

#
Java | 477 lines | 333 code | 48 blank | 96 comment | 63 complexity | 3276cffc93b5cd2a3ead1ebd98cb3cd2 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * PluginList.java - Plugin list downloaded from server
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001, 2003 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. package org.gjt.sp.jedit.pluginmgr;
  23. //{{{ Imports
  24. import java.io.*;
  25. import java.net.URL;
  26. import java.util.Hashtable;
  27. import java.util.Vector;
  28. import java.util.zip.GZIPInputStream;
  29. import org.xml.sax.XMLReader;
  30. import org.xml.sax.InputSource;
  31. import org.xml.sax.helpers.XMLReaderFactory;
  32. import org.gjt.sp.util.Log;
  33. import org.gjt.sp.jedit.*;
  34. //}}}
  35. /**
  36. * Plugin list downloaded from server.
  37. * @since jEdit 3.2pre2
  38. * @version $Id: PluginList.java 5445 2006-06-19 01:09:51Z vanza $
  39. */
  40. class PluginList
  41. {
  42. /**
  43. * Magic numbers used for auto-detecting GZIP files.
  44. */
  45. public static final int GZIP_MAGIC_1 = 0x1f;
  46. public static final int GZIP_MAGIC_2 = 0x8b;
  47. Vector plugins;
  48. Hashtable pluginHash;
  49. Vector pluginSets;
  50. /**
  51. * The mirror id.
  52. * @since jEdit 4.3pre3
  53. */
  54. private final String id;
  55. //{{{ PluginList constructor
  56. PluginList() throws Exception
  57. {
  58. plugins = new Vector();
  59. pluginHash = new Hashtable();
  60. pluginSets = new Vector();
  61. String path = jEdit.getProperty("plugin-manager.export-url");
  62. id = jEdit.getProperty("plugin-manager.mirror.id");
  63. if (!id.equals(MirrorList.Mirror.NONE))
  64. path += "?mirror="+id;
  65. PluginListHandler handler = new PluginListHandler(this,path);
  66. XMLReader parser = XMLReaderFactory.createXMLReader();
  67. InputStream in = new BufferedInputStream(new URL(path).openStream());
  68. try
  69. {
  70. if(in.markSupported())
  71. {
  72. in.mark(2);
  73. int b1 = in.read();
  74. int b2 = in.read();
  75. in.reset();
  76. if(b1 == GZIP_MAGIC_1 && b2 == GZIP_MAGIC_2)
  77. in = new GZIPInputStream(in);
  78. }
  79. InputSource isrc = new InputSource(new InputStreamReader(in,"UTF8"));
  80. isrc.setSystemId("jedit.jar");
  81. parser.setContentHandler(handler);
  82. parser.setDTDHandler(handler);
  83. parser.setEntityResolver(handler);
  84. parser.setErrorHandler(handler);
  85. parser.parse(isrc);
  86. }
  87. finally
  88. {
  89. in.close();
  90. }
  91. } //}}}
  92. //{{{ addPlugin() method
  93. void addPlugin(Plugin plugin)
  94. {
  95. plugin.checkIfInstalled();
  96. plugins.addElement(plugin);
  97. pluginHash.put(plugin.name,plugin);
  98. } //}}}
  99. //{{{ addPluginSet() method
  100. void addPluginSet(PluginSet set)
  101. {
  102. pluginSets.addElement(set);
  103. } //}}}
  104. //{{{ finished() method
  105. void finished()
  106. {
  107. // after the entire list is loaded, fill out plugin field
  108. // in dependencies
  109. for(int i = 0; i < plugins.size(); i++)
  110. {
  111. Plugin plugin = (Plugin)plugins.elementAt(i);
  112. for(int j = 0; j < plugin.branches.size(); j++)
  113. {
  114. Branch branch = (Branch)plugin.branches.elementAt(j);
  115. for(int k = 0; k < branch.deps.size(); k++)
  116. {
  117. Dependency dep = (Dependency)branch.deps.elementAt(k);
  118. if(dep.what.equals("plugin"))
  119. dep.plugin = (Plugin)pluginHash.get(dep.pluginName);
  120. }
  121. }
  122. }
  123. } //}}}
  124. //{{{ dump() method
  125. void dump()
  126. {
  127. for(int i = 0; i < plugins.size(); i++)
  128. {
  129. System.err.println(plugins.elementAt(i));
  130. System.err.println();
  131. }
  132. } //}}}
  133. //{{{ getMirrorId() method
  134. /**
  135. * Returns the mirror ID.
  136. *
  137. * @return the mirror ID
  138. * @since jEdit 4.3pre3
  139. */
  140. String getMirrorId()
  141. {
  142. return id;
  143. } //}}}
  144. //{{{ PluginSet class
  145. static class PluginSet
  146. {
  147. String name;
  148. String description;
  149. Vector plugins = new Vector();
  150. public String toString()
  151. {
  152. return plugins.toString();
  153. }
  154. } //}}}
  155. //{{{ Plugin class
  156. static public class Plugin
  157. {
  158. String jar;
  159. String name;
  160. String description;
  161. String author;
  162. Vector branches = new Vector();
  163. //String installed;
  164. //String installedVersion;
  165. void checkIfInstalled()
  166. {
  167. /* // check if the plugin is already installed.
  168. // this is a bit of hack
  169. PluginJAR[] jars = jEdit.getPluginJARs();
  170. for(int i = 0; i < jars.length; i++)
  171. {
  172. String path = jars[i].getPath();
  173. if(!new File(path).exists())
  174. continue;
  175. if(MiscUtilities.getFileName(path).equals(jar))
  176. {
  177. installed = path;
  178. EditPlugin plugin = jars[i].getPlugin();
  179. if(plugin != null)
  180. {
  181. installedVersion = jEdit.getProperty(
  182. "plugin." + plugin.getClassName()
  183. + ".version");
  184. }
  185. break;
  186. }
  187. }
  188. String[] notLoaded = jEdit.getNotLoadedPluginJARs();
  189. for(int i = 0; i < notLoaded.length; i++)
  190. {
  191. String path = notLoaded[i];
  192. if(MiscUtilities.getFileName(path).equals(jar))
  193. {
  194. installed = path;
  195. break;
  196. }
  197. } */
  198. }
  199. String getInstalledVersion()
  200. {
  201. PluginJAR[] jars = jEdit.getPluginJARs();
  202. for(int i = 0; i < jars.length; i++)
  203. {
  204. String path = jars[i].getPath();
  205. if(MiscUtilities.getFileName(path).equals(jar))
  206. {
  207. EditPlugin plugin = jars[i].getPlugin();
  208. if(plugin != null)
  209. {
  210. return jEdit.getProperty(
  211. "plugin." + plugin.getClassName()
  212. + ".version");
  213. }
  214. else
  215. return null;
  216. }
  217. }
  218. return null;
  219. }
  220. String getInstalledPath()
  221. {
  222. PluginJAR[] jars = jEdit.getPluginJARs();
  223. for(int i = 0; i < jars.length; i++)
  224. {
  225. String path = jars[i].getPath();
  226. if(MiscUtilities.getFileName(path).equals(jar))
  227. return path;
  228. }
  229. return null;
  230. }
  231. /**
  232. * Find the first branch compatible with the running jEdit release.
  233. */
  234. Branch getCompatibleBranch()
  235. {
  236. for(int i = 0; i < branches.size(); i++)
  237. {
  238. Branch branch = (Branch)branches.elementAt(i);
  239. if(branch.canSatisfyDependencies())
  240. return branch;
  241. }
  242. return null;
  243. }
  244. boolean canBeInstalled()
  245. {
  246. Branch branch = getCompatibleBranch();
  247. return branch != null && !branch.obsolete
  248. && branch.canSatisfyDependencies();
  249. }
  250. void install(Roster roster, String installDirectory, boolean downloadSource)
  251. {
  252. String installed = getInstalledPath();
  253. Branch branch = getCompatibleBranch();
  254. if(branch.obsolete)
  255. {
  256. if(installed != null)
  257. roster.addRemove(installed);
  258. return;
  259. }
  260. //branch.satisfyDependencies(roster,installDirectory,
  261. // downloadSource);
  262. if(installed != null)
  263. {
  264. installDirectory = MiscUtilities.getParentOfPath(
  265. installed);
  266. }
  267. roster.addInstall(
  268. installed,
  269. (downloadSource ? branch.downloadSource : branch.download),
  270. installDirectory,
  271. (downloadSource ? branch.downloadSourceSize : branch.downloadSize));
  272. }
  273. public String toString()
  274. {
  275. return name;
  276. }
  277. } //}}}
  278. //{{{ Branch class
  279. static class Branch
  280. {
  281. String version;
  282. String date;
  283. int downloadSize;
  284. String download;
  285. int downloadSourceSize;
  286. String downloadSource;
  287. boolean obsolete;
  288. Vector deps = new Vector();
  289. boolean canSatisfyDependencies()
  290. {
  291. for(int i = 0; i < deps.size(); i++)
  292. {
  293. Dependency dep = (Dependency)deps.elementAt(i);
  294. if(!dep.canSatisfy())
  295. return false;
  296. }
  297. return true;
  298. }
  299. void satisfyDependencies(Roster roster, String installDirectory,
  300. boolean downloadSource)
  301. {
  302. for(int i = 0; i < deps.size(); i++)
  303. {
  304. Dependency dep = (Dependency)deps.elementAt(i);
  305. dep.satisfy(roster,installDirectory,downloadSource);
  306. }
  307. }
  308. public String toString()
  309. {
  310. return "[version=" + version + ",download=" + download
  311. + ",obsolete=" + obsolete + ",deps=" + deps + "]";
  312. }
  313. } //}}}
  314. //{{{ Dependency class
  315. static class Dependency
  316. {
  317. String what;
  318. String from;
  319. String to;
  320. // only used if what is "plugin"
  321. String pluginName;
  322. Plugin plugin;
  323. Dependency(String what, String from, String to, String pluginName)
  324. {
  325. this.what = what;
  326. this.from = from;
  327. this.to = to;
  328. this.pluginName = pluginName;
  329. }
  330. boolean isSatisfied()
  331. {
  332. if(what.equals("plugin"))
  333. {
  334. for(int i = 0; i < plugin.branches.size(); i++)
  335. {
  336. String installedVersion = plugin.getInstalledVersion();
  337. if(installedVersion != null
  338. &&
  339. (from == null || MiscUtilities.compareStrings(
  340. installedVersion,from,false) >= 0)
  341. &&
  342. (to == null || MiscUtilities.compareStrings(
  343. installedVersion,to,false) <= 0))
  344. {
  345. return true;
  346. }
  347. }
  348. return false;
  349. }
  350. else if(what.equals("jdk"))
  351. {
  352. String javaVersion = System.getProperty("java.version").substring(0,3);
  353. if((from == null || MiscUtilities.compareStrings(
  354. javaVersion,from,false) >= 0)
  355. &&
  356. (to == null || MiscUtilities.compareStrings(
  357. javaVersion,to,false) <= 0))
  358. return true;
  359. else
  360. return false;
  361. }
  362. else if(what.equals("jedit"))
  363. {
  364. String build = jEdit.getBuild();
  365. if((from == null || MiscUtilities.compareStrings(
  366. build,from,false) >= 0)
  367. &&
  368. (to == null || MiscUtilities.compareStrings(
  369. build,to,false) <= 0))
  370. return true;
  371. else
  372. return false;
  373. }
  374. else
  375. {
  376. Log.log(Log.ERROR,this,"Invalid dependency: " + what);
  377. return false;
  378. }
  379. }
  380. boolean canSatisfy()
  381. {
  382. if(isSatisfied())
  383. return true;
  384. else if(what.equals("plugin"))
  385. {
  386. return plugin.canBeInstalled();
  387. }
  388. else
  389. return false;
  390. }
  391. void satisfy(Roster roster, String installDirectory,
  392. boolean downloadSource)
  393. {
  394. if(what.equals("plugin"))
  395. {
  396. String installedVersion = plugin.getInstalledVersion();
  397. for(int i = 0; i < plugin.branches.size(); i++)
  398. {
  399. Branch branch = (Branch)plugin.branches
  400. .elementAt(i);
  401. if((installedVersion == null
  402. ||
  403. MiscUtilities.compareStrings(
  404. installedVersion,branch.version,false) < 0)
  405. &&
  406. (from == null || MiscUtilities.compareStrings(
  407. branch.version,from,false) >= 0)
  408. &&
  409. (to == null || MiscUtilities.compareStrings(
  410. branch.version,to,false) <= 0))
  411. {
  412. plugin.install(roster,installDirectory,
  413. downloadSource);
  414. return;
  415. }
  416. }
  417. }
  418. }
  419. public String toString()
  420. {
  421. return "[what=" + what + ",from=" + from
  422. + ",to=" + to + ",plugin=" + plugin + "]";
  423. }
  424. } //}}}
  425. }