Changeset 299 in josm for src/org/openstreetmap/josm/plugins
- Timestamp:
- 2007-08-08T02:59:36+02:00 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/plugins/PluginDownloader.java
r298 r299 1 // 1 //License: GPL. Copyright 2007 by Immanuel Scholz and others 2 2 /** 3 3 * … … 6 6 7 7 import static org.openstreetmap.josm.tools.I18n.tr; 8 import static org.openstreetmap.josm.tools.I18n.trn; 8 9 9 10 import java.io.BufferedReader; 10 11 import java.io.File; 12 import java.io.FileNotFoundException; 11 13 import java.io.FileOutputStream; 12 14 import java.io.FileWriter; … … 15 17 import java.io.InputStreamReader; 16 18 import java.io.OutputStream; 19 import java.net.MalformedURLException; 17 20 import java.net.URL; 21 import java.util.Collection; 18 22 import java.util.regex.Matcher; 19 23 import java.util.regex.Pattern; … … 22 26 23 27 import org.openstreetmap.josm.Main; 28 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 24 29 import org.openstreetmap.josm.gui.preferences.PluginPreference.PluginDescription; 30 import org.xml.sax.SAXException; 25 31 26 32 public class PluginDownloader { 27 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>(.*)"); 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>(.*)"); 28 68 29 69 public static int downloadDescription() { … … 66 106 b.append(" <resource>"+escape(m.group(1))+"</resource>\n"); 67 107 b.append(" <description>"+escape(m.group(3))+"</description>\n"); 108 b.append(" <version>"+escape(m.group(4))+"</version>\n"); 68 109 b.append(" </plugin>\n"); 69 110 } … … 73 114 74 115 private static String escape(String s) { 75 76 116 return s.replaceAll("<", "<").replaceAll(">", ">"); 117 } 77 118 78 119 public static boolean downloadPlugin(PluginDescription pd) { 79 120 File file = new File(Main.pref.getPreferencesDir()+"plugins/"+pd.name+".jar"); 80 try { 81 InputStream in = new URL(pd.resource).openStream(); 121 if (!download(pd.resource, file)) { 122 JOptionPane.showMessageDialog(Main.parent, tr("Could not download plugin: {0} from {1}", pd.name, pd.resource)); 123 } else { 124 try { 125 PluginInformation.findPlugin(pd.name); 126 return true; 127 } catch (Exception e) { 128 e.printStackTrace(); 129 JOptionPane.showMessageDialog(Main.parent, tr("The plugin {0} seem to be broken or could not be downloaded automatically.", pd.name)); 130 } 131 } 132 if (file.exists()) 133 file.delete(); 134 return false; 135 } 136 137 private static boolean download(String url, File file) { 138 try { 139 InputStream in = new URL(url).openStream(); 82 140 OutputStream out = new FileOutputStream(file); 83 84 85 86 87 88 try {89 PluginInformation.findPlugin(pd.name);90 return true;91 } catch (Exception e) {92 93 JOptionPane.showMessageDialog(Main.parent, tr("The plugin {0} seem to be broken or could not be downloaded automatically.", pd.name));94 } 95 } catch (Exception e) { 96 JOptionPane.showMessageDialog(Main.parent, tr("Could not download plugin: {0} from {1}", pd.name, pd.resource));97 98 if (file.exists()) 99 file.delete(); 100 return false;101 141 byte[] buffer = new byte[8192]; 142 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) 143 out.write(buffer, 0, read); 144 out.close(); 145 in.close(); 146 return true; 147 } catch (MalformedURLException e) { 148 e.printStackTrace(); 149 } catch (FileNotFoundException e) { 150 e.printStackTrace(); 151 } catch (IOException e) { 152 e.printStackTrace(); 153 } 154 return false; 155 } 156 157 public static void update(Collection<PluginDescription> update) { 158 Main.worker.execute(new UpdateTask(update)); 159 } 102 160 }
Note:
See TracChangeset
for help on using the changeset viewer.