001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.plugins; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.io.BufferedReader; 007import java.io.ByteArrayInputStream; 008import java.io.IOException; 009import java.io.InputStream; 010import java.io.InputStreamReader; 011import java.nio.charset.StandardCharsets; 012import java.util.LinkedList; 013import java.util.List; 014 015import org.openstreetmap.josm.tools.Logging; 016 017/** 018 * A parser for the plugin list provided by a JOSM Plugin Download Site. 019 * 020 * See <a href="https://josm.openstreetmap.de/plugin">https://josm.openstreetmap.de/plugin</a> 021 * for a sample of the document. The format is a custom format, kind of mix of CSV and RFC822 style 022 * name/value-pairs. 023 * 024 */ 025public class PluginListParser { 026 027 /** 028 * Creates the plugin information object 029 * 030 * @param name the plugin name 031 * @param url the plugin download url 032 * @param manifest the plugin manifest 033 * @return a plugin information object 034 * @throws PluginListParseException if plugin manifest cannot be parsed 035 */ 036 public static PluginInformation createInfo(String name, String url, String manifest) throws PluginListParseException { 037 try { 038 return new PluginInformation( 039 new ByteArrayInputStream(manifest.getBytes(StandardCharsets.UTF_8)), 040 name.substring(0, name.length() - 4), 041 url 042 ); 043 } catch (PluginException e) { 044 throw new PluginListParseException(tr("Failed to create plugin information from manifest for plugin ''{0}''", name), e); 045 } 046 } 047 048 /** 049 * Parses a plugin information document and replies a list of plugin information objects. 050 * 051 * See <a href="https://josm.openstreetmap.de/plugin">https://josm.openstreetmap.de/plugin</a> 052 * for a sample of the document. The format is a custom format, kind of mix of CSV and RFC822 style 053 * name/value-pairs. 054 * 055 * @param in the input stream from which to parse 056 * @return the list of plugin information objects 057 * @throws PluginListParseException if something goes wrong while parsing 058 */ 059 public List<PluginInformation> parse(InputStream in) throws PluginListParseException { 060 List<PluginInformation> ret = new LinkedList<>(); 061 try (BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) { 062 String name = null; 063 String url = null; 064 StringBuilder manifest = new StringBuilder(); 065 for (String line = r.readLine(); line != null; line = r.readLine()) { 066 if (line.startsWith("\t")) { 067 line = line.substring(1); 068 /* NOTE: Although manifest specification says line should not be longer than 72 bytes it 069 supports more than 500 bytes and thus even the longest possible 72 character UTF-8, so 070 this code correctly splits the text at 70 characters, not bytes. */ 071 while (line.length() > 70) { 072 manifest.append(line.substring(0, 70)).append('\n'); 073 line = ' ' + line.substring(70); 074 } 075 manifest.append(line).append('\n'); 076 continue; 077 } 078 addPluginInformation(ret, name, url, manifest.toString()); 079 String[] x = line.split(";"); 080 if (x.length != 2) 081 throw new IOException(tr("Illegal entry in plugin list.") + " " + line); 082 name = x[0]; 083 url = x[1]; 084 manifest = new StringBuilder(); 085 } 086 addPluginInformation(ret, name, url, manifest.toString()); 087 return ret; 088 } catch (IOException e) { 089 throw new PluginListParseException(e); 090 } 091 } 092 093 private static void addPluginInformation(List<PluginInformation> ret, String name, String url, String manifest) { 094 try { 095 if (name != null) { 096 PluginInformation info = createInfo(name, url, manifest); 097 for (PluginProxy plugin : PluginHandler.pluginList) { 098 if (plugin.getPluginInformation().name.equals(info.getName())) { 099 info.localversion = plugin.getPluginInformation().localversion; 100 } 101 } 102 ret.add(info); 103 } 104 } catch (PluginListParseException ex) { 105 Logging.error(ex); 106 } 107 } 108 109}