source: josm/trunk/src/org/openstreetmap/josm/plugins/PluginListParser.java@ 5508

Last change on this file since 5508 was 5307, checked in by simon04, 12 years ago

see #7818 - make plugin list parsing more robust.

In case of an error, resume with next item.

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedReader;
7import java.io.ByteArrayInputStream;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.InputStreamReader;
11import java.io.UnsupportedEncodingException;
12import java.util.LinkedList;
13import 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 */
23public 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 static 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 while (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 addPluginInformation(ret, name, url, manifest.toString());
87 String x[] = line.split(";");
88 if(x.length != 2)
89 throw new IOException(tr("Illegal entry in plugin list."));
90 name = x[0];
91 url = x[1];
92 manifest = new StringBuilder();
93
94 }
95 addPluginInformation(ret, name, url, manifest.toString());
96 return ret;
97 } catch (IOException e) {
98 throw new PluginListParseException(e);
99 }
100 }
101
102 private static void addPluginInformation(List<PluginInformation> ret, String name, String url, String manifest) {
103 try {
104 if (name != null) {
105 PluginInformation info = createInfo(name, url, manifest.toString());
106 if (info != null) {
107 for (PluginProxy plugin : PluginHandler.pluginList) {
108 if (plugin.getPluginInformation().name.equals(info.getName())) {
109 info.localversion = plugin.getPluginInformation().localversion;
110 }
111 }
112 ret.add(info);
113 }
114 }
115 } catch (PluginListParseException ex) {
116 ex.printStackTrace();
117 }
118 }
119
120}
Note: See TracBrowser for help on using the repository browser.