1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | /**
|
---|
3 | *
|
---|
4 | */
|
---|
5 | package org.openstreetmap.josm.plugins;
|
---|
6 |
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
8 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
9 |
|
---|
10 | import java.io.BufferedReader;
|
---|
11 | import java.io.File;
|
---|
12 | import java.io.FileNotFoundException;
|
---|
13 | import java.io.FileOutputStream;
|
---|
14 | import java.io.FileWriter;
|
---|
15 | import java.io.IOException;
|
---|
16 | import java.io.InputStream;
|
---|
17 | import java.io.InputStreamReader;
|
---|
18 | import java.io.OutputStream;
|
---|
19 | import java.net.MalformedURLException;
|
---|
20 | import java.net.URL;
|
---|
21 | import java.util.Collection;
|
---|
22 | import java.util.regex.Matcher;
|
---|
23 | import java.util.regex.Pattern;
|
---|
24 |
|
---|
25 | import javax.swing.JOptionPane;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
29 | import org.openstreetmap.josm.gui.preferences.PluginPreference.PluginDescription;
|
---|
30 | import org.xml.sax.SAXException;
|
---|
31 |
|
---|
32 | public 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(" <description>"+escape(m.group(3))+"</description>\n");
|
---|
112 | b.append(" <version>"+escape(m.group(4))+"</version>\n");
|
---|
113 | b.append(" </plugin>\n");
|
---|
114 | }
|
---|
115 | b.append("</plugins>\n");
|
---|
116 | return b;
|
---|
117 | }
|
---|
118 |
|
---|
119 | private static String escape(String s) {
|
---|
120 | return s.replaceAll("<", "<").replaceAll(">", ">");
|
---|
121 | }
|
---|
122 |
|
---|
123 | public static boolean downloadPlugin(PluginDescription pd) {
|
---|
124 | File file = new File(Main.pref.getPreferencesDir()+"plugins/"+pd.name+".jar");
|
---|
125 | if (!download(pd.resource, file)) {
|
---|
126 | JOptionPane.showMessageDialog(Main.parent, tr("Could not download plugin: {0} from {1}", pd.name, pd.resource));
|
---|
127 | } else {
|
---|
128 | try {
|
---|
129 | PluginInformation.findPlugin(pd.name);
|
---|
130 | return true;
|
---|
131 | } catch (Exception e) {
|
---|
132 | e.printStackTrace();
|
---|
133 | JOptionPane.showMessageDialog(Main.parent, tr("The plugin {0} seem to be broken or could not be downloaded automatically.", pd.name));
|
---|
134 | }
|
---|
135 | }
|
---|
136 | if (file.exists())
|
---|
137 | file.delete();
|
---|
138 | return false;
|
---|
139 | }
|
---|
140 |
|
---|
141 | private static boolean download(String url, File file) {
|
---|
142 | try {
|
---|
143 | InputStream in = new URL(url).openStream();
|
---|
144 | OutputStream out = new FileOutputStream(file);
|
---|
145 | byte[] buffer = new byte[8192];
|
---|
146 | for (int read = in.read(buffer); read != -1; read = in.read(buffer))
|
---|
147 | out.write(buffer, 0, read);
|
---|
148 | out.close();
|
---|
149 | in.close();
|
---|
150 | return true;
|
---|
151 | } catch (MalformedURLException e) {
|
---|
152 | e.printStackTrace();
|
---|
153 | } catch (FileNotFoundException e) {
|
---|
154 | e.printStackTrace();
|
---|
155 | } catch (IOException e) {
|
---|
156 | e.printStackTrace();
|
---|
157 | }
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 |
|
---|
161 | public static void update(Collection<PluginDescription> update) {
|
---|
162 | Main.worker.execute(new UpdateTask(update));
|
---|
163 | }
|
---|
164 | }
|
---|