| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.plugins; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 5 | |
|---|
| 6 | import java.io.BufferedReader; |
|---|
| 7 | import java.io.ByteArrayInputStream; |
|---|
| 8 | import java.io.IOException; |
|---|
| 9 | import java.io.InputStream; |
|---|
| 10 | import java.io.InputStreamReader; |
|---|
| 11 | import java.io.UnsupportedEncodingException; |
|---|
| 12 | import java.util.LinkedList; |
|---|
| 13 | import java.util.List; |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * A parser for the plugin list provided by a JOSM Plugin Download Site. |
|---|
| 17 | * |
|---|
| 18 | * See <a href="http://josm.openstreetmap.de/plugin">http://josm.openstreetmap.de/plugin</a> |
|---|
| 19 | * for a sample of the document. The format is a custom format, kind of mix of CSV and RFC822 style |
|---|
| 20 | * name/value-pairs. |
|---|
| 21 | * |
|---|
| 22 | */ |
|---|
| 23 | public class PluginListParser { |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Creates the plugin information object |
|---|
| 27 | * |
|---|
| 28 | * @param name the plugin name |
|---|
| 29 | * @param url the plugin download url |
|---|
| 30 | * @param manifest the plugin manifest |
|---|
| 31 | * @return a plugin information object |
|---|
| 32 | * @throws PluginListParseException |
|---|
| 33 | */ |
|---|
| 34 | protected PluginInformation createInfo(String name, String url, String manifest) throws PluginListParseException{ |
|---|
| 35 | try { |
|---|
| 36 | return new PluginInformation( |
|---|
| 37 | new ByteArrayInputStream(manifest.getBytes("utf-8")), |
|---|
| 38 | name.substring(0, name.length() - 4), |
|---|
| 39 | url |
|---|
| 40 | ); |
|---|
| 41 | } catch(UnsupportedEncodingException e) { |
|---|
| 42 | throw new PluginListParseException(tr("Failed to create plugin information from manifest for plugin ''{0}''", name), e); |
|---|
| 43 | } catch (PluginException e) { |
|---|
| 44 | throw new PluginListParseException(tr("Failed to create plugin information from manifest for plugin ''{0}''", name), e); |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * Parses a plugin information document and replies a list of plugin information objects. |
|---|
| 50 | * |
|---|
| 51 | * See <a href="http://josm.openstreetmap.de/plugin">http://josm.openstreetmap.de/plugin</a> |
|---|
| 52 | * for a sample of the document. The format is a custom format, kind of mix of CSV and RFC822 style |
|---|
| 53 | * name/value-pairs. |
|---|
| 54 | * |
|---|
| 55 | * @param in the input stream from which to parse |
|---|
| 56 | * @return the list of plugin information objects |
|---|
| 57 | * @throws PluginListParseException thrown if something goes wrong while parsing |
|---|
| 58 | */ |
|---|
| 59 | public List<PluginInformation> parse(InputStream in) throws PluginListParseException{ |
|---|
| 60 | List<PluginInformation> ret = new LinkedList<PluginInformation>(); |
|---|
| 61 | BufferedReader r = null; |
|---|
| 62 | try { |
|---|
| 63 | r = new BufferedReader(new InputStreamReader(in, "utf-8")); |
|---|
| 64 | String name = null; |
|---|
| 65 | String url = null; |
|---|
| 66 | StringBuilder manifest = new StringBuilder(); |
|---|
| 67 | /* |
|---|
| 68 | code structure: |
|---|
| 69 | for () { |
|---|
| 70 | A; |
|---|
| 71 | B; |
|---|
| 72 | C; |
|---|
| 73 | } |
|---|
| 74 | B; |
|---|
| 75 | */ |
|---|
| 76 | for (String line = r.readLine(); line != null; line = r.readLine()) { |
|---|
| 77 | if (line.startsWith("\t")) { |
|---|
| 78 | line = line.substring(1); |
|---|
| 79 | if (line.length() > 70) { |
|---|
| 80 | manifest.append(line.substring(0, 70)).append("\n"); |
|---|
| 81 | line = " " + line.substring(70); |
|---|
| 82 | } |
|---|
| 83 | manifest.append(line).append("\n"); |
|---|
| 84 | continue; |
|---|
| 85 | } |
|---|
| 86 | if (name != null) { |
|---|
| 87 | PluginInformation info = createInfo(name, url, manifest.toString()); |
|---|
| 88 | if (info != null) { |
|---|
| 89 | for (PluginProxy plugin : PluginHandler.pluginList) { |
|---|
| 90 | if (plugin.getPluginInformation().name.equals(info.getName())) { |
|---|
| 91 | info.localversion = plugin.getPluginInformation().localversion; |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | ret.add(info); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | String x[] = line.split(";"); |
|---|
| 98 | if(x.length != 2) |
|---|
| 99 | throw new IOException(tr("Illegal entry in plugin list.")); |
|---|
| 100 | name = x[0]; |
|---|
| 101 | url = x[1]; |
|---|
| 102 | manifest = new StringBuilder(); |
|---|
| 103 | |
|---|
| 104 | } |
|---|
| 105 | if (name != null) { |
|---|
| 106 | PluginInformation info = createInfo(name, url, manifest.toString()); |
|---|
| 107 | if (info != null) { |
|---|
| 108 | for (PluginProxy plugin : PluginHandler.pluginList) { |
|---|
| 109 | if (plugin.getPluginInformation().name.equals(info.getName())) { |
|---|
| 110 | info.localversion = plugin.getPluginInformation().localversion; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | ret.add(info); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | return ret; |
|---|
| 117 | } catch (IOException e) { |
|---|
| 118 | throw new PluginListParseException(e); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | } |
|---|