PageRenderTime 161ms CodeModel.GetById 101ms app.highlight 31ms RepoModel.GetById 19ms app.codeStats 0ms

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

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