source: josm/trunk/src/org/openstreetmap/josm/plugins/PluginDownloader.java@ 1102

Last change on this file since 1102 was 1073, checked in by stoecker, 15 years ago

added some plugin checks

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2/**
3 *
4 */
5package org.openstreetmap.josm.plugins;
6
7import static org.openstreetmap.josm.tools.I18n.tr;
8import static org.openstreetmap.josm.tools.I18n.trn;
9
10import java.io.BufferedReader;
11import java.io.File;
12import java.io.FileNotFoundException;
13import java.io.FileOutputStream;
14import java.io.FileWriter;
15import java.io.FilenameFilter;
16import java.io.IOException;
17import java.io.InputStream;
18import java.io.InputStreamReader;
19import java.io.OutputStream;
20import java.net.MalformedURLException;
21import java.net.URL;
22import java.util.Arrays;
23import java.util.Collection;
24import java.util.regex.Matcher;
25import java.util.regex.Pattern;
26
27import javax.swing.JOptionPane;
28
29import org.openstreetmap.josm.Main;
30import org.openstreetmap.josm.gui.PleaseWaitRunnable;
31import org.openstreetmap.josm.gui.preferences.PluginPreference.PluginDescription;
32import org.xml.sax.SAXException;
33
34public class PluginDownloader {
35
36 private static final class UpdateTask extends PleaseWaitRunnable {
37 private final Collection<PluginDescription> toUpdate;
38 private String errors = "";
39 private int count = 0;
40
41 private UpdateTask(Collection<PluginDescription> toUpdate) {
42 super(tr("Update Plugins"));
43 this.toUpdate = toUpdate;
44 }
45
46 @Override protected void cancel() {
47 finish();
48 }
49
50 @Override protected void finish() {
51 if (errors.length() > 0)
52 JOptionPane.showMessageDialog(Main.parent, tr("There were problems with the following plugins:\n\n {0}",errors));
53 else
54 JOptionPane.showMessageDialog(Main.parent, trn("{0} Plugin successfully updated. Please restart JOSM.", "{0} Plugins successfully updated. Please restart JOSM.", count, count));
55 }
56
57 @Override protected void realRun() throws SAXException, IOException {
58 File pluginDir = Main.pref.getPluginsDirFile();
59 if (!pluginDir.exists())
60 pluginDir.mkdirs();
61 for (PluginDescription d : toUpdate) {
62 File pluginFile = new File(pluginDir, d.name + ".jar.new");
63 if (download(d.resource, pluginFile))
64 count++;
65 else
66 errors += d.name + "\n";
67 }
68 PluginDownloader.moveUpdatedPlugins();
69 }
70 }
71
72 private static final Pattern wiki = Pattern.compile("^</td></tr><tr><td><a class=\"ext-link\" href=\"([^\"]*)\"><span class=\"icon\">([^<]*)</span></a></td><td>([^<]*)</td><td>([^<].*)</td><td>(.*)");
73
74 private final static String[] pluginSites = {"http://josm.openstreetmap.de/wiki/Plugins"};
75
76 public static Collection<String> getSites() {
77 return Main.pref.getCollection("pluginmanager.sites", Arrays.asList(pluginSites));
78 }
79
80 public static int downloadDescription() {
81 int count = 0;
82 for (String site : getSites()) {
83 try {
84 BufferedReader r = new BufferedReader(new InputStreamReader(new URL(site).openStream()));
85 CharSequence txt;
86 if (site.toLowerCase().endsWith(".xml"))
87 txt = readXml(r);
88 else
89 txt = readWiki(r);
90 r.close();
91 new File(Main.pref.getPreferencesDir()+"plugins").mkdir();
92 FileWriter out = new FileWriter(new File(Main.pref
93 .getPluginsDirFile(), count + "-site-"
94 + site.replaceAll("[/:\\\\ <>|]", "_") + ".xml"));
95 out.append(txt);
96 out.close();
97 count++;
98 } catch (IOException x) {
99 }
100 }
101 return count;
102 }
103
104 private static CharSequence readXml(BufferedReader r) throws IOException {
105 StringBuilder b = new StringBuilder();
106 for (String line = r.readLine(); line != null; line = r.readLine())
107 b.append(line+"\n");
108 return b;
109 }
110
111 private static CharSequence readWiki(BufferedReader r) throws IOException {
112 StringBuilder b = new StringBuilder("<plugins>\n");
113 for (String line = r.readLine(); line != null; line = r.readLine()) {
114 Matcher m = wiki.matcher(line);
115 if (!m.matches())
116 continue;
117 b.append(" <plugin>\n");
118 b.append(" <name>"+escape(m.group(2))+"</name>\n");
119 b.append(" <resource>"+escape(m.group(1))+"</resource>\n");
120 b.append(" <author>"+escape(m.group(3))+"</author>\n");
121 b.append(" <description>"+escape(m.group(4))+"</description>\n");
122 b.append(" <version>"+escape(m.group(5))+"</version>\n");
123 b.append(" </plugin>\n");
124 }
125 b.append("</plugins>\n");
126 return b;
127 }
128
129 private static String escape(String s) {
130 return s.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
131 }
132
133 public static boolean downloadPlugin(PluginDescription pd) {
134 File file = new File(Main.pref.getPluginsDirFile(), pd.name + ".jar");
135 if (!download(pd.resource, file)) {
136 JOptionPane.showMessageDialog(Main.parent, tr("Could not download plugin: {0} from {1}", pd.name, pd.resource));
137 } else {
138 try {
139 PluginInformation.findPlugin(pd.name);
140 return true;
141 } catch (Exception e) {
142 e.printStackTrace();
143 JOptionPane.showMessageDialog(Main.parent, tr("The plugin {0} seem to be broken or could not be downloaded automatically.", pd.name));
144 }
145 }
146 if (file.exists())
147 file.delete();
148 return false;
149 }
150
151 private static boolean download(String url, File file) {
152 try {
153 InputStream in = new URL(url).openStream();
154 OutputStream out = new FileOutputStream(file);
155 byte[] buffer = new byte[8192];
156 for (int read = in.read(buffer); read != -1; read = in.read(buffer))
157 out.write(buffer, 0, read);
158 out.close();
159 in.close();
160 return true;
161 } catch (MalformedURLException e) {
162 e.printStackTrace();
163 } catch (FileNotFoundException e) {
164 e.printStackTrace();
165 } catch (IOException e) {
166 e.printStackTrace();
167 }
168 return false;
169 }
170
171 public static void update(Collection<PluginDescription> update) {
172 Main.worker.execute(new UpdateTask(update));
173 }
174
175 public static boolean moveUpdatedPlugins() {
176 File pluginDir = Main.pref.getPluginsDirFile();
177 boolean ok = true;
178 if (pluginDir.exists() && pluginDir.isDirectory() && pluginDir.canWrite()) {
179 final File[] files = pluginDir.listFiles(new FilenameFilter() {
180 public boolean accept(File dir, String name) {
181 return name.endsWith(".new");
182 }});
183 for (File updatedPlugin : files) {
184 final String filePath = updatedPlugin.getPath();
185 File plugin = new File(filePath.substring(0, filePath.length() - 4));
186 ok = (plugin.delete() || !plugin.exists()) && updatedPlugin.renameTo(plugin) && ok;
187 }
188 }
189 return ok;
190 }
191}
Note: See TracBrowser for help on using the repository browser.