PageRenderTime 103ms CodeModel.GetById 69ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

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