PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/pluginmgr/PluginList.java

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