[3083] | 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;
|
---|
[7082] | 11 | import java.nio.charset.StandardCharsets;
|
---|
[3083] | 12 | import java.util.LinkedList;
|
---|
| 13 | import java.util.List;
|
---|
| 14 |
|
---|
[12620] | 15 | import org.openstreetmap.josm.tools.Logging;
|
---|
[6643] | 16 |
|
---|
[3083] | 17 | /**
|
---|
| 18 | * A parser for the plugin list provided by a JOSM Plugin Download Site.
|
---|
[3530] | 19 | *
|
---|
[6920] | 20 | * See <a href="https://josm.openstreetmap.de/plugin">https://josm.openstreetmap.de/plugin</a>
|
---|
[3083] | 21 | * for a sample of the document. The format is a custom format, kind of mix of CSV and RFC822 style
|
---|
| 22 | * name/value-pairs.
|
---|
| 23 | *
|
---|
| 24 | */
|
---|
| 25 | public class PluginListParser {
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Creates the plugin information object
|
---|
[3530] | 29 | *
|
---|
[3083] | 30 | * @param name the plugin name
|
---|
| 31 | * @param url the plugin download url
|
---|
| 32 | * @param manifest the plugin manifest
|
---|
| 33 | * @return a plugin information object
|
---|
[8470] | 34 | * @throws PluginListParseException if plugin manifest cannot be parsed
|
---|
[3083] | 35 | */
|
---|
[10123] | 36 | public static PluginInformation createInfo(String name, String url, String manifest) throws PluginListParseException {
|
---|
[3083] | 37 | try {
|
---|
| 38 | return new PluginInformation(
|
---|
[7082] | 39 | new ByteArrayInputStream(manifest.getBytes(StandardCharsets.UTF_8)),
|
---|
[3083] | 40 | name.substring(0, name.length() - 4),
|
---|
| 41 | url
|
---|
[5923] | 42 | );
|
---|
[3083] | 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.
|
---|
[3530] | 50 | *
|
---|
[6920] | 51 | * See <a href="https://josm.openstreetmap.de/plugin">https://josm.openstreetmap.de/plugin</a>
|
---|
[3083] | 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.
|
---|
[3530] | 54 | *
|
---|
[3083] | 55 | * @param in the input stream from which to parse
|
---|
| 56 | * @return the list of plugin information objects
|
---|
[8291] | 57 | * @throws PluginListParseException if something goes wrong while parsing
|
---|
[3083] | 58 | */
|
---|
[8510] | 59 | public List<PluginInformation> parse(InputStream in) throws PluginListParseException {
|
---|
[7005] | 60 | List<PluginInformation> ret = new LinkedList<>();
|
---|
[3083] | 61 | BufferedReader r = null;
|
---|
| 62 | try {
|
---|
[7082] | 63 | r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
|
---|
[3083] | 64 | String name = null;
|
---|
| 65 | String url = null;
|
---|
| 66 | StringBuilder manifest = new StringBuilder();
|
---|
| 67 | for (String line = r.readLine(); line != null; line = r.readLine()) {
|
---|
| 68 | if (line.startsWith("\t")) {
|
---|
| 69 | line = line.substring(1);
|
---|
[5306] | 70 | while (line.length() > 70) {
|
---|
[8390] | 71 | manifest.append(line.substring(0, 70)).append('\n');
|
---|
[8846] | 72 | line = ' ' + line.substring(70);
|
---|
[3083] | 73 | }
|
---|
[8390] | 74 | manifest.append(line).append('\n');
|
---|
[3083] | 75 | continue;
|
---|
| 76 | }
|
---|
[5307] | 77 | addPluginInformation(ret, name, url, manifest.toString());
|
---|
[6085] | 78 | String[] x = line.split(";");
|
---|
[8510] | 79 | if (x.length != 2)
|
---|
[5923] | 80 | throw new IOException(tr("Illegal entry in plugin list."));
|
---|
[3083] | 81 | name = x[0];
|
---|
| 82 | url = x[1];
|
---|
| 83 | manifest = new StringBuilder();
|
---|
| 84 |
|
---|
| 85 | }
|
---|
[5307] | 86 | addPluginInformation(ret, name, url, manifest.toString());
|
---|
| 87 | return ret;
|
---|
| 88 | } catch (IOException e) {
|
---|
| 89 | throw new PluginListParseException(e);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | private static void addPluginInformation(List<PluginInformation> ret, String name, String url, String manifest) {
|
---|
| 94 | try {
|
---|
[3083] | 95 | if (name != null) {
|
---|
[5923] | 96 | PluginInformation info = createInfo(name, url, manifest);
|
---|
[11381] | 97 | for (PluginProxy plugin : PluginHandler.pluginList) {
|
---|
| 98 | if (plugin.getPluginInformation().name.equals(info.getName())) {
|
---|
| 99 | info.localversion = plugin.getPluginInformation().localversion;
|
---|
[3083] | 100 | }
|
---|
| 101 | }
|
---|
[11381] | 102 | ret.add(info);
|
---|
[3083] | 103 | }
|
---|
[5307] | 104 | } catch (PluginListParseException ex) {
|
---|
[12620] | 105 | Logging.error(ex);
|
---|
[3083] | 106 | }
|
---|
| 107 | }
|
---|
[5307] | 108 |
|
---|
[3083] | 109 | }
|
---|