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