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

Last change on this file since 626 was 509, checked in by gebner, 16 years ago

Fix parsing of Plugins.xml

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