PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 380 lines | 307 code | 43 blank | 30 comment | 58 complexity | c7bb0dd0e21d4684d2185e2b2150878c 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
  3. * Copyright (C) 2001 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package org.gjt.sp.jedit.pluginmgr;
  20. import com.microstar.xml.*;
  21. import java.io.*;
  22. import java.net.URL;
  23. import java.util.Hashtable;
  24. import java.util.Vector;
  25. import org.gjt.sp.util.Log;
  26. import org.gjt.sp.jedit.*;
  27. /**
  28. * Plugin list downloaded from server.
  29. * @since jEdit 3.2pre2
  30. */
  31. class PluginList
  32. {
  33. Vector plugins;
  34. Hashtable pluginHash;
  35. Vector pluginSets;
  36. PluginList() throws Exception
  37. {
  38. plugins = new Vector();
  39. pluginHash = new Hashtable();
  40. pluginSets = new Vector();
  41. String path = jEdit.getProperty("plugin-manager.url");
  42. PluginListHandler handler = new PluginListHandler(this,path);
  43. XmlParser parser = new XmlParser();
  44. parser.setHandler(handler);
  45. parser.parse(null,null,new BufferedReader(new InputStreamReader(
  46. new URL(path).openStream(),"UTF8")));
  47. }
  48. void addPlugin(Plugin plugin)
  49. {
  50. plugin.checkIfInstalled();
  51. plugins.addElement(plugin);
  52. pluginHash.put(plugin.name,plugin);
  53. }
  54. void addPluginSet(PluginSet set)
  55. {
  56. pluginSets.addElement(set);
  57. }
  58. void finished()
  59. {
  60. // after the entire list is loaded, fill out plugin field
  61. // in dependencies
  62. for(int i = 0; i < plugins.size(); i++)
  63. {
  64. Plugin plugin = (Plugin)plugins.elementAt(i);
  65. for(int j = 0; j < plugin.branches.size(); j++)
  66. {
  67. Branch branch = (Branch)plugin.branches.elementAt(j);
  68. for(int k = 0; k < branch.deps.size(); k++)
  69. {
  70. Dependency dep = (Dependency)branch.deps.elementAt(k);
  71. if(dep.what.equals("plugin"))
  72. dep.plugin = (Plugin)pluginHash.get(dep.pluginName);
  73. }
  74. }
  75. }
  76. }
  77. void dump()
  78. {
  79. for(int i = 0; i < plugins.size(); i++)
  80. {
  81. System.err.println((Plugin)plugins.elementAt(i));
  82. System.err.println();
  83. }
  84. }
  85. static class PluginSet
  86. {
  87. String name;
  88. String description;
  89. Vector plugins = new Vector();
  90. void install(Roster roster, String installDirectory,
  91. boolean downloadSource)
  92. {
  93. for(int i = 0; i < plugins.size(); i++)
  94. {
  95. Plugin plugin = (Plugin)plugins.elementAt(i);
  96. if(plugin.canBeInstalled())
  97. {
  98. plugin.install(roster,installDirectory,
  99. downloadSource);
  100. }
  101. }
  102. }
  103. public String toString()
  104. {
  105. return plugins.toString();
  106. }
  107. }
  108. static class Plugin
  109. {
  110. String jar;
  111. String name;
  112. String description;
  113. String author;
  114. Vector branches = new Vector();
  115. String installed;
  116. String installedVersion;
  117. void checkIfInstalled()
  118. {
  119. // check if the plugin is already installed.
  120. // this is a bit of hack
  121. EditPlugin.JAR[] jars = jEdit.getPluginJARs();
  122. for(int i = 0; i < jars.length; i++)
  123. {
  124. String path = jars[i].getPath();
  125. if(!new File(path).exists())
  126. continue;
  127. if(MiscUtilities.getFileName(path).equals(jar))
  128. {
  129. installed = path;
  130. EditPlugin[] plugins = jars[i].getPlugins();
  131. if(plugins.length >= 1)
  132. {
  133. installedVersion = jEdit.getProperty(
  134. "plugin." + plugins[0].getClassName()
  135. + ".version");
  136. }
  137. break;
  138. }
  139. }
  140. String[] notLoaded = jEdit.getNotLoadedPluginJARs();
  141. for(int i = 0; i < notLoaded.length; i++)
  142. {
  143. String path = notLoaded[i];
  144. if(MiscUtilities.getFileName(path).equals(jar))
  145. {
  146. installed = path;
  147. break;
  148. }
  149. }
  150. }
  151. /**
  152. * Find the first branch compatible with the running jEdit release.
  153. */
  154. Branch getCompatibleBranch()
  155. {
  156. for(int i = 0; i < branches.size(); i++)
  157. {
  158. Branch branch = (Branch)branches.elementAt(i);
  159. if(branch.canSatisfyDependencies())
  160. return branch;
  161. }
  162. return null;
  163. }
  164. boolean canBeInstalled()
  165. {
  166. Branch branch = getCompatibleBranch();
  167. return branch != null && !branch.obsolete;
  168. }
  169. void install(Roster roster, String installDirectory, boolean downloadSource)
  170. {
  171. if(installed != null)
  172. roster.addOperation(new Roster.Remove(installed));
  173. Branch branch = getCompatibleBranch();
  174. if(branch.obsolete)
  175. return;
  176. branch.satisfyDependencies(roster,installDirectory,
  177. downloadSource);
  178. if(installed != null)
  179. {
  180. installDirectory = MiscUtilities.getParentOfPath(
  181. installed);
  182. }
  183. roster.addOperation(new Roster.Install((downloadSource
  184. ? branch.downloadSource : branch.download),
  185. installDirectory));
  186. }
  187. public String toString()
  188. {
  189. return name;
  190. }
  191. }
  192. static class Branch
  193. {
  194. String version;
  195. String date;
  196. int downloadSize;
  197. String download;
  198. int downloadSourceSize;
  199. String downloadSource;
  200. boolean obsolete;
  201. Vector deps = new Vector();
  202. boolean canSatisfyDependencies()
  203. {
  204. for(int i = 0; i < deps.size(); i++)
  205. {
  206. Dependency dep = (Dependency)deps.elementAt(i);
  207. if(!dep.canSatisfy())
  208. return false;
  209. }
  210. return true;
  211. }
  212. void satisfyDependencies(Roster roster, String installDirectory,
  213. boolean downloadSource)
  214. {
  215. for(int i = 0; i < deps.size(); i++)
  216. {
  217. Dependency dep = (Dependency)deps.elementAt(i);
  218. dep.satisfy(roster,installDirectory,downloadSource);
  219. }
  220. }
  221. public String toString()
  222. {
  223. return "[version=" + version + ",download=" + download
  224. + ",obsolete=" + obsolete + ",deps=" + deps + "]";
  225. }
  226. }
  227. static class Dependency
  228. {
  229. String what;
  230. String from;
  231. String to;
  232. // only used if what is "plugin"
  233. String pluginName;
  234. Plugin plugin;
  235. Dependency(String what, String from, String to, String pluginName)
  236. {
  237. this.what = what;
  238. this.from = from;
  239. this.to = to;
  240. this.pluginName = pluginName;
  241. }
  242. boolean isSatisfied()
  243. {
  244. if(what.equals("plugin"))
  245. {
  246. for(int i = 0; i < plugin.branches.size(); i++)
  247. {
  248. Branch branch = (Branch)plugin.branches
  249. .elementAt(i);
  250. if(plugin.installedVersion != null
  251. &&
  252. (from == null || MiscUtilities.compareStrings(
  253. plugin.installedVersion,from,false) >= 0)
  254. &&
  255. (to == null || MiscUtilities.compareStrings(
  256. plugin.installedVersion,to,false) <= 0))
  257. {
  258. return true;
  259. }
  260. }
  261. return false;
  262. }
  263. else if(what.equals("jdk"))
  264. {
  265. String javaVersion = System.getProperty("java.version").substring(0,3);
  266. if((from == null || MiscUtilities.compareStrings(
  267. javaVersion,from,false) >= 0)
  268. &&
  269. (to == null || MiscUtilities.compareStrings(
  270. javaVersion,to,false) <= 0))
  271. return true;
  272. else
  273. return false;
  274. }
  275. else if(what.equals("jedit"))
  276. {
  277. String build = jEdit.getBuild();
  278. if((from == null || MiscUtilities.compareStrings(
  279. build,from,false) >= 0)
  280. &&
  281. (to == null || MiscUtilities.compareStrings(
  282. build,to,false) <= 0))
  283. return true;
  284. else
  285. return false;
  286. }
  287. else
  288. {
  289. Log.log(Log.ERROR,this,"Invalid dependency: " + what);
  290. return false;
  291. }
  292. }
  293. boolean canSatisfy()
  294. {
  295. if(isSatisfied())
  296. return true;
  297. else if(what.equals("plugin"))
  298. {
  299. return plugin.canBeInstalled();
  300. }
  301. else
  302. return false;
  303. }
  304. void satisfy(Roster roster, String installDirectory,
  305. boolean downloadSource)
  306. {
  307. if(what.equals("plugin"))
  308. {
  309. for(int i = 0; i < plugin.branches.size(); i++)
  310. {
  311. Branch branch = (Branch)plugin.branches
  312. .elementAt(i);
  313. if((plugin.installedVersion == null
  314. ||
  315. MiscUtilities.compareStrings(
  316. plugin.installedVersion,branch.version,false) < 0)
  317. &&
  318. (from == null || MiscUtilities.compareStrings(
  319. branch.version,from,false) >= 0)
  320. &&
  321. (to == null || MiscUtilities.compareStrings(
  322. branch.version,to,false) <= 0))
  323. {
  324. plugin.install(roster,installDirectory,
  325. downloadSource);
  326. return;
  327. }
  328. }
  329. }
  330. }
  331. public String toString()
  332. {
  333. return "[what=" + what + ",from=" + from
  334. + ",to=" + to + ",plugin=" + plugin + "]";
  335. }
  336. }
  337. }