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

Last change on this file since 1017 was 1017, checked in by stoecker, 16 years ago

close bug #1627

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