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

Last change on this file since 6867 was 6643, checked in by Don-vip, 10 years ago

global replacement of e.printStackTrace() by Main.error(e)

  • Property svn:eol-style set to native
File size: 4.2 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.util.LinkedList;
12import java.util.List;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.tools.Utils;
16
17/**
18 * A parser for the plugin list provided by a JOSM Plugin Download Site.
19 *
20 * See <a href="http://josm.openstreetmap.de/plugin">http://josm.openstreetmap.de/plugin</a>
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 */
25public class PluginListParser {
26
27 /**
28 * Creates the plugin information object
29 *
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
34 * @throws PluginListParseException
35 */
36 protected static PluginInformation createInfo(String name, String url, String manifest) throws PluginListParseException{
37 try {
38 return new PluginInformation(
39 new ByteArrayInputStream(manifest.getBytes(Utils.UTF_8)),
40 name.substring(0, name.length() - 4),
41 url
42 );
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, Utils.UTF_8));
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);
70 while (line.length() > 70) {
71 manifest.append(line.substring(0, 70)).append("\n");
72 line = " " + line.substring(70);
73 }
74 manifest.append(line).append("\n");
75 continue;
76 }
77 addPluginInformation(ret, name, url, manifest.toString());
78 String[] x = line.split(";");
79 if(x.length != 2)
80 throw new IOException(tr("Illegal entry in plugin list."));
81 name = x[0];
82 url = x[1];
83 manifest = new StringBuilder();
84
85 }
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 {
95 if (name != null) {
96 PluginInformation info = createInfo(name, url, manifest);
97 if (info != null) {
98 for (PluginProxy plugin : PluginHandler.pluginList) {
99 if (plugin.getPluginInformation().name.equals(info.getName())) {
100 info.localversion = plugin.getPluginInformation().localversion;
101 }
102 }
103 ret.add(info);
104 }
105 }
106 } catch (PluginListParseException ex) {
107 Main.error(ex);
108 }
109 }
110
111}
Note: See TracBrowser for help on using the repository browser.