[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;
|
---|
| 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.
|
---|
[3530] | 17 | *
|
---|
[3083] | 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
|
---|
[3530] | 27 | *
|
---|
[3083] | 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.
|
---|
[3530] | 50 | *
|
---|
[3083] | 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.
|
---|
[3530] | 54 | *
|
---|
[3083] | 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 | name = x[0];
|
---|
| 99 | url = x[1];
|
---|
| 100 | manifest = new StringBuilder();
|
---|
| 101 |
|
---|
| 102 | }
|
---|
| 103 | if (name != null) {
|
---|
| 104 | PluginInformation info = createInfo(name, url, manifest.toString());
|
---|
| 105 | if (info != null) {
|
---|
| 106 | for (PluginProxy plugin : PluginHandler.pluginList) {
|
---|
| 107 | if (plugin.getPluginInformation().name.equals(info.getName())) {
|
---|
| 108 | info.localversion = plugin.getPluginInformation().localversion;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | ret.add(info);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | return ret;
|
---|
| 115 | } catch (IOException e) {
|
---|
| 116 | throw new PluginListParseException(e);
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|