Changeset 1588 in josm for trunk/src/org


Ignore:
Timestamp:
2009-05-12T18:05:11+02:00 (15 years ago)
Author:
stoecker
Message:

fixed plugin handling. closes #2530 and #762

Location:
trunk/src/org/openstreetmap/josm/plugins
Files:
1 deleted
3 edited

Legend:

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

    r1326 r1588  
    3434
    3535    private static final class UpdateTask extends PleaseWaitRunnable {
    36         private final Collection<PluginDescription> toUpdate;
     36        private final Collection<PluginInformation> toUpdate;
    3737        private String errors = "";
    3838        private int count = 0;
    3939
    40         private UpdateTask(Collection<PluginDescription> toUpdate) {
     40        private UpdateTask(Collection<PluginInformation> toUpdate) {
    4141            super(tr("Update Plugins"));
    4242            this.toUpdate = toUpdate;
     
    5858            if (!pluginDir.exists())
    5959                pluginDir.mkdirs();
    60             for (PluginDescription d : toUpdate) {
     60            for (PluginInformation d : toUpdate) {
    6161                File pluginFile = new File(pluginDir, d.name + ".jar.new");
    62                 if (download(d.resource, pluginFile))
     62                if (download(d.downloadlink, pluginFile))
    6363                    count++;
    6464                else
     
    6969    }
    7070
    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     private final static String[] pluginSites = {"http://josm.openstreetmap.de/wiki/Plugins"};
     71    private final static String[] pluginSites = {"http://josm.openstreetmap.de/plugin"};
    7472
    7573    public static Collection<String> getSites() {
     
    8381        int count = 0;
    8482        for (String site : getSites()) {
     83        /* TODO: remove old site files (everything except .jar) */
    8584            try {
    8685                BufferedReader r = new BufferedReader(new InputStreamReader(new URL(site).openStream()));
    87                 CharSequence txt;
    88                 if (site.toLowerCase().endsWith(".xml"))
    89                     txt = readXml(r);
    90                 else
    91                     txt = readWiki(r);
     86                StringBuilder b = new StringBuilder();
     87                for (String line = r.readLine(); line != null; line = r.readLine())
     88                    b.append(line+"\n");
    9289                r.close();
    9390                new File(Main.pref.getPreferencesDir()+"plugins").mkdir();
    9491                FileWriter out = new FileWriter(new File(Main.pref
    9592                        .getPluginsDirFile(), count + "-site-"
    96                         + site.replaceAll("[/:\\\\ <>|]", "_") + ".xml"));
    97                 out.append(txt);
     93                        + site.replaceAll("[/:\\\\ <>|]", "_") + ".txt"));
     94                out.append(b);
    9895                out.close();
    9996                count++;
     
    104101    }
    105102
    106     private static CharSequence readXml(BufferedReader r) throws IOException {
    107         StringBuilder b = new StringBuilder();
    108         for (String line = r.readLine(); line != null; line = r.readLine())
    109             b.append(line+"\n");
    110         return b;
    111     }
    112 
    113     private static CharSequence readWiki(BufferedReader r) throws IOException {
    114         StringBuilder b = new StringBuilder("<plugins>\n");
    115         for (String line = r.readLine(); line != null; line = r.readLine()) {
    116             Matcher m = wiki.matcher(line);
    117             if (!m.matches())
    118                 continue;
    119             b.append("  <plugin>\n");
    120             b.append("    <name>"+escape(m.group(2))+"</name>\n");
    121             b.append("    <resource>"+escape(m.group(1))+"</resource>\n");
    122             b.append("    <author>"+escape(m.group(3))+"</author>\n");
    123             b.append("    <description>"+escape(m.group(4))+"</description>\n");
    124             b.append("    <version>"+escape(m.group(5))+"</version>\n");
    125             b.append("  </plugin>\n");
    126         }
    127         b.append("</plugins>\n");
    128         return b;
    129     }
    130 
    131103    private static String escape(String s) {
    132104        return s.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    133105    }
    134106
    135     public static boolean downloadPlugin(PluginDescription pd) {
     107    public static boolean downloadPlugin(PluginInformation pd) {
    136108        File file = new File(Main.pref.getPluginsDirFile(), pd.name + ".jar");
    137         if (!download(pd.resource, file)) {
    138             JOptionPane.showMessageDialog(Main.parent, tr("Could not download plugin: {0} from {1}", pd.name, pd.resource));
     109        if (!download(pd.downloadlink, file)) {
     110            JOptionPane.showMessageDialog(Main.parent, tr("Could not download plugin: {0} from {1}", pd.name, pd.downloadlink));
    139111        } else {
    140112            try {
     
    171143    }
    172144
    173     public static void update(Collection<PluginDescription> update) {
     145    public static void update(Collection<PluginInformation> update) {
    174146        Main.worker.execute(new UpdateTask(update));
    175147    }
  • trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java

    r1326 r1588  
    3535    public final String className;
    3636    public final String requires;
     37    public final String link;
    3738    public final String description;
    3839    public final boolean early;
     
    4041    public final int stage;
    4142    public final String version;
     43    public String downloadlink = null;
    4244    public final List<URL> libraries = new LinkedList<URL>();
    4345
     
    7678            }
    7779            if (manifest != null) {
     80                String s;
     81                String lang = Main.getLanguageCode()+"_";
    7882                Attributes attr = manifest.getMainAttributes();
    7983                className = attr.getValue("Plugin-Class");
     84                s = attr.getValue(lang+"Plugin-Link");
     85                if(s == null)
     86                    s = attr.getValue("Plugin-Link");
     87                link = s;
    8088                requires = attr.getValue("Plugin-Requires");
    81                 description = attr.getValue("Plugin-Description");
     89                s = attr.getValue(lang+"Plugin-Description");
     90                if(s == null)
     91                {
     92                    s = attr.getValue("Plugin-Description");
     93                    if(s != null)
     94                        s = tr(s);
     95                }
     96                description = s;
    8297                early = Boolean.parseBoolean(attr.getValue("Plugin-Early"));
    8398                String stageStr = attr.getValue("Plugin-Stage");
     
    106121                className = null;
    107122                mainversion = null;
    108                 description = tr("unknown");
     123                description = null;
    109124                early = false;
    110125                stage = 50;
    111126                requires = null;
     127                link = null;
    112128                version = null;
    113129                author = null;
     
    121137            throw new PluginException(null, name, e);
    122138        }
     139    }
     140
     141    public String getLinkDescription()
     142    {
     143        String d = description == null ? tr("no description available") : description;
     144        if(link != null)
     145            d += " <A HREF=\""+link+"\">"+tr("More details")+"</A>";
     146        return d;
    123147    }
    124148
  • trunk/src/org/openstreetmap/josm/plugins/PluginSelection.java

    r1415 r1588  
    1010import java.awt.event.ActionListener;
    1111
     12import java.io.BufferedReader;
     13import java.io.ByteArrayInputStream;
    1214import java.io.File;
    1315import java.io.FileReader;
     
    3941import org.openstreetmap.josm.gui.ExtendedDialog;
    4042import org.openstreetmap.josm.tools.OpenBrowser;
    41 import org.openstreetmap.josm.tools.XmlObjectParser.Uniform;
    4243
    4344public class PluginSelection {
    4445
    4546    private Map<String, Boolean> pluginMap;
    46     private Map<String, PluginDescription> availablePlugins;
     47    private Map<String, PluginInformation> availablePlugins;
    4748
    4849    public void updateDescription(JPanel pluginPanel) {
     
    6364        drawPanel(pluginPanel);
    6465
    65         Set<PluginDescription> toUpdate = new HashSet<PluginDescription>();
     66        Set<PluginInformation> toUpdate = new HashSet<PluginInformation>();
    6667        StringBuilder toUpdateStr = new StringBuilder();
    6768        for (PluginProxy proxy : PluginHandler.pluginList) {
    68             PluginDescription description = availablePlugins.get(proxy.info.name);
     69            PluginInformation description = availablePlugins.get(proxy.info.name);
    6970            if (description != null && (description.version == null || description.version.equals("")) ?
    7071            (proxy.info.version != null && proxy.info.version.equals("")) : !description.version.equals(proxy.info.version)) {
     
    7778            done = true;
    7879        } else {
    79             int answer = new ExtendedDialog(Main.parent, 
    80                         tr("Update"), 
     80            int answer = new ExtendedDialog(Main.parent,
     81                        tr("Update"),
    8182                        tr("Update the following plugins:\n\n{0}", toUpdateStr.toString()),
    82                         new String[] {tr("Update Plugins"), tr("Cancel")}, 
    83                         new String[] {"dialogs/refresh.png", "cancel.png"}).getValue(); 
     83                        new String[] {tr("Update Plugins"), tr("Cancel")},
     84                        new String[] {"dialogs/refresh.png", "cancel.png"}).getValue();
    8485            if (answer == 1) {
    8586                PluginDownloader.update(toUpdate);
     
    9394
    9495    public Boolean finish() {
    95         Collection<PluginDescription> toDownload = new LinkedList<PluginDescription>();
     96        Collection<PluginInformation> toDownload = new LinkedList<PluginInformation>();
    9697        String msg = "";
    9798        for (Entry<String, Boolean> entry : pluginMap.entrySet()) {
     
    102103        }
    103104        if (!toDownload.isEmpty()) {
    104             int answer = new ExtendedDialog(Main.parent, 
     105            int answer = new ExtendedDialog(Main.parent,
    105106                        tr("Download missing plugins"),
    106107                        tr("Download the following plugins?\n\n{0}", msg),
    107                         new String[] {tr("Download Plugins"), tr("Cancel")}, 
    108                         new String[] {"download.png", "cancel.png"}).getValue(); 
     108                        new String[] {tr("Download Plugins"), tr("Cancel")},
     109                        new String[] {"download.png", "cancel.png"}).getValue();
    109110            if (answer != 1)
    110                 for (PluginDescription pd : toDownload)
     111                for (PluginInformation pd : toDownload)
    111112                    pluginMap.put(pd.name, false);
    112113            else
    113                 for (PluginDescription pd : toDownload)
     114                for (PluginInformation pd : toDownload)
    114115                    if (!PluginDownloader.downloadPlugin(pd))
    115116                        pluginMap.put(pd.name, false);
     
    145146
    146147        int row = 0;
    147         for (final PluginDescription plugin : availablePlugins.values()) {
     148        for (final PluginInformation plugin : availablePlugins.values()) {
    148149            boolean enabled = (enabledPlugins != null) && enabledPlugins.contains(plugin.name);
    149150            if (pluginMap.get(plugin.name) == null)
     
    174175            pluginPanel.add(pluginCheck, gbc);
    175176
    176             pluginCheck.setToolTipText(plugin.resource != null ? ""+plugin.resource : tr("Plugin bundled with JOSM"));
     177            pluginCheck.setToolTipText(plugin.downloadlink != null ? ""+plugin.downloadlink : tr("Plugin bundled with JOSM"));
    177178
    178179            JEditorPane description = new JEditorPane();
    179180            description.setContentType("text/html");
    180181            description.setEditable(false);
    181             description.setText("<html><i>"+(plugin.description==null?tr("no description available"):plugin.description)+"</i></html>");
     182            description.setText("<html><i>"+plugin.getLinkDescription()+"</i></html>");
    182183            description.setBorder(BorderFactory.createEmptyBorder(0,20,0,0));
    183184            description.setBackground(UIManager.getColor("Panel.background"));
     
    205206                        if ((getLoaded(plugin.name) == null) && (plinfo != null)) {
    206207                            try {
    207                                 int answer = new ExtendedDialog(Main.parent, 
    208                                     tr("Plugin already exists"), 
     208                                int answer = new ExtendedDialog(Main.parent,
     209                                    tr("Plugin already exists"),
    209210                                    tr("Plugin archive already available. Do you want to download"
    210211                                        + " the current version by deleting existing archive?\n\n{0}",
    211212                                        plinfo.file.getCanonicalPath()),
    212                                     new String[] {tr("Delete and Download"), tr("Cancel")}, 
    213                                     new String[] {"download.png", "cancel.png"}).getValue();     
    214                                    
     213                                    new String[] {tr("Delete and Download"), tr("Cancel")},
     214                                    new String[] {"download.png", "cancel.png"}).getValue();
     215
    215216                                if (answer == 1) {
    216217                                    if (!plinfo.file.delete()) {
     
    247248    }
    248249
    249     private Map<String, PluginDescription> getAvailablePlugins() {
    250         SortedMap<String, PluginDescription> availablePlugins = new TreeMap<String, PluginDescription>(new Comparator<String>(){
     250    private Map<String, PluginInformation> getAvailablePlugins() {
     251        SortedMap<String, PluginInformation> availablePlugins = new TreeMap<String, PluginInformation>(new Comparator<String>(){
    251252            public int compare(String o1, String o2) {
    252253                return o1.compareToIgnoreCase(o2);
     
    260261                    if (!f.isFile())
    261262                        continue;
    262                     if (f.getName().endsWith(".jar")) {
     263                    String fname = f.getName();
     264                    if (fname.endsWith(".jar")) {
    263265                        try {
    264                             PluginInformation info = new PluginInformation(f);
     266                            PluginInformation info = new PluginInformation(f,fname.substring(0,fname.length()-4), null);
    265267                            if (!availablePlugins.containsKey(info.name))
    266                                 availablePlugins.put(info.name, new PluginDescription(
    267                                     info.name,
    268                                     info.description,
    269                                     PluginInformation.fileToURL(f).toString(),
    270                                     info.version));
     268                                availablePlugins.put(info.name, info);
    271269                        } catch (PluginException x) {
    272270                        }
    273                     } else if (f.getName().matches("^[0-9]+-site.*\\.xml$")) {
     271                    } else if (fname.matches("^[0-9]+-site.*\\.txt$")) {
    274272                        try {
    275                             Uniform<PluginDescription> parser = new Uniform<PluginDescription>(new FileReader(f), "plugin", PluginDescription.class);
    276                             for (PluginDescription pd : parser)
    277                                 if (!availablePlugins.containsKey(pd.name))
    278                                     availablePlugins.put(pd.name, pd);
     273                            BufferedReader r = new BufferedReader(new FileReader(f));
     274                            String name = null;
     275                            String url = null;
     276                            String manifest = null;
     277                            for (String line = r.readLine(); line != null; line = r.readLine())
     278                            {
     279                                if(line.startsWith("\t"))
     280                                {
     281                                    line = line.substring(1);
     282                                    if(line.length() > 70)
     283                                    {
     284                                        manifest += line.substring(0,70)+"\n";
     285                                        line = " " + line.substring(70);
     286                                    }
     287                                    manifest += line+"\n";
     288                                }
     289                                else
     290                                {
     291                                    if(name != null)
     292                                    {
     293                                        PluginInformation info = new PluginInformation(null, name.substring(0,name.length()-4),
     294                                        new ByteArrayInputStream(manifest.getBytes()));
     295                                        info.downloadlink = url;
     296                                        if(!availablePlugins.containsKey(info.name))
     297                                            availablePlugins.put(info.name, info);
     298                                        manifest = null;
     299                                    }
     300                                    String x[] = line.split(";");
     301                                    name = x[0];
     302                                    url = x[1];
     303                                }
     304                            }
     305                            if(name != null)
     306                            {
     307                                PluginInformation info = new PluginInformation(null, name.substring(0,name.length()-4),
     308                                new ByteArrayInputStream(manifest.getBytes()));
     309                                info.downloadlink = url;
     310                                if(!availablePlugins.containsKey(info.name))
     311                                    availablePlugins.put(info.name, info);
     312                            }
     313                            r.close();
    279314                        } catch (Exception e) {
    280315                            e.printStackTrace();
     
    287322        for (PluginProxy proxy : PluginHandler.pluginList)
    288323            if (!availablePlugins.containsKey(proxy.info.name))
    289                 availablePlugins.put(proxy.info.name, new PluginDescription(
    290                         proxy.info.name,
    291                         proxy.info.description,
    292                         proxy.info.file == null ? null :
    293                             PluginInformation.fileToURL(proxy.info.file).toString(),
    294                         proxy.info.version));
     324                availablePlugins.put(proxy.info.name, proxy.info);
    295325        return availablePlugins;
    296326    }
Note: See TracChangeset for help on using the changeset viewer.