Changeset 299 in josm for src/org/openstreetmap/josm/plugins


Ignore:
Timestamp:
2007-08-08T02:59:36+02:00 (17 years ago)
Author:
imi
Message:
  • added update of plugins
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/plugins/PluginDownloader.java

    r298 r299  
    1 // License: GPL. Copyright 2007 by Immanuel Scholz and others
     1//License: GPL. Copyright 2007 by Immanuel Scholz and others
    22/**
    33 *
     
    66
    77import static org.openstreetmap.josm.tools.I18n.tr;
     8import static org.openstreetmap.josm.tools.I18n.trn;
    89
    910import java.io.BufferedReader;
    1011import java.io.File;
     12import java.io.FileNotFoundException;
    1113import java.io.FileOutputStream;
    1214import java.io.FileWriter;
     
    1517import java.io.InputStreamReader;
    1618import java.io.OutputStream;
     19import java.net.MalformedURLException;
    1720import java.net.URL;
     21import java.util.Collection;
    1822import java.util.regex.Matcher;
    1923import java.util.regex.Pattern;
     
    2226
    2327import org.openstreetmap.josm.Main;
     28import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    2429import org.openstreetmap.josm.gui.preferences.PluginPreference.PluginDescription;
     30import org.xml.sax.SAXException;
    2531
    2632public 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>(.*)");
    2868
    2969        public static int downloadDescription() {
     
    66106                        b.append("    <resource>"+escape(m.group(1))+"</resource>\n");
    67107                        b.append("    <description>"+escape(m.group(3))+"</description>\n");
     108                        b.append("    <version>"+escape(m.group(4))+"</version>\n");
    68109                        b.append("  </plugin>\n");
    69110                }
     
    73114
    74115        private static String escape(String s) {
    75             return s.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    76     }
     116                return s.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
     117        }
    77118
    78119        public static boolean downloadPlugin(PluginDescription pd) {
    79120                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();
    82140                        OutputStream out = new FileOutputStream(file);
    83                 byte[] buffer = new byte[8192];
    84                 for (int read = in.read(buffer); read != -1; read = in.read(buffer))
    85                         out.write(buffer, 0, read);
    86                 out.close();
    87                 in.close();
    88                 try {
    89                     PluginInformation.findPlugin(pd.name);
    90                     return true;
    91             } catch (Exception e) {
    92                     e.printStackTrace();
    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        }
    102160}
Note: See TracChangeset for help on using the changeset viewer.