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