1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.io.InputStreamReader;
|
---|
11 | import java.io.Reader;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 |
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
20 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
|
---|
21 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
22 | import org.openstreetmap.josm.tools.Utils;
|
---|
23 | import org.openstreetmap.josm.tools.XmlObjectParser;
|
---|
24 | import org.xml.sax.SAXException;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * The tagging presets reader.
|
---|
28 | * @since 6068
|
---|
29 | */
|
---|
30 | public final class TaggingPresetReader {
|
---|
31 |
|
---|
32 | private TaggingPresetReader() {
|
---|
33 | // Hide default constructor for utils classes
|
---|
34 | }
|
---|
35 |
|
---|
36 | private static File zipIcons = null;
|
---|
37 |
|
---|
38 | public static List<String> getPresetSources() {
|
---|
39 | LinkedList<String> sources = new LinkedList<String>();
|
---|
40 |
|
---|
41 | for (SourceEntry e : (new TaggingPresetPreference.PresetPrefHelper()).get()) {
|
---|
42 | sources.add(e.url);
|
---|
43 | }
|
---|
44 |
|
---|
45 | return sources;
|
---|
46 | }
|
---|
47 |
|
---|
48 | public static List<TaggingPreset> readAll(Reader in, boolean validate) throws SAXException {
|
---|
49 | XmlObjectParser parser = new XmlObjectParser();
|
---|
50 | parser.mapOnStart("item", TaggingPreset.class);
|
---|
51 | parser.mapOnStart("separator", TaggingPresetSeparator.class);
|
---|
52 | parser.mapBoth("group", TaggingPresetMenu.class);
|
---|
53 | parser.map("text", TaggingPresetItems.Text.class);
|
---|
54 | parser.map("link", TaggingPresetItems.Link.class);
|
---|
55 | parser.mapOnStart("optional", TaggingPresetItems.Optional.class);
|
---|
56 | parser.mapOnStart("roles", TaggingPresetItems.Roles.class);
|
---|
57 | parser.map("role", TaggingPresetItems.Role.class);
|
---|
58 | parser.map("checkgroup", TaggingPresetItems.CheckGroup.class);
|
---|
59 | parser.map("check", TaggingPresetItems.Check.class);
|
---|
60 | parser.map("combo", TaggingPresetItems.Combo.class);
|
---|
61 | parser.map("multiselect", TaggingPresetItems.MultiSelect.class);
|
---|
62 | parser.map("label", TaggingPresetItems.Label.class);
|
---|
63 | parser.map("space", TaggingPresetItems.Space.class);
|
---|
64 | parser.map("key", TaggingPresetItems.Key.class);
|
---|
65 | parser.map("list_entry", TaggingPresetItems.PresetListEntry.class);
|
---|
66 | parser.map("item_separator", TaggingPresetItems.ItemSeparator.class);
|
---|
67 |
|
---|
68 | LinkedList<TaggingPreset> all = new LinkedList<TaggingPreset>();
|
---|
69 | TaggingPresetMenu lastmenu = null;
|
---|
70 | TaggingPresetItems.Roles lastrole = null;
|
---|
71 | final List<TaggingPresetItems.Check> checks = new LinkedList<TaggingPresetItems.Check>();
|
---|
72 | List<TaggingPresetItems.PresetListEntry> listEntries = new LinkedList<TaggingPresetItems.PresetListEntry>();
|
---|
73 |
|
---|
74 | if (validate) {
|
---|
75 | parser.startWithValidation(in, Main.JOSM_WEBSITE+"/tagging-preset-1.0", "resource://data/tagging-preset.xsd");
|
---|
76 | } else {
|
---|
77 | parser.start(in);
|
---|
78 | }
|
---|
79 | while (parser.hasNext()) {
|
---|
80 | Object o = parser.next();
|
---|
81 | if (!(o instanceof TaggingPresetItem) && !checks.isEmpty()) {
|
---|
82 | all.getLast().data.addAll(checks);
|
---|
83 | checks.clear();
|
---|
84 | }
|
---|
85 | if (o instanceof TaggingPresetMenu) {
|
---|
86 | TaggingPresetMenu tp = (TaggingPresetMenu) o;
|
---|
87 | if (tp == lastmenu) {
|
---|
88 | lastmenu = tp.group;
|
---|
89 | } else {
|
---|
90 | tp.group = lastmenu;
|
---|
91 | tp.setDisplayName();
|
---|
92 | lastmenu = tp;
|
---|
93 | all.add(tp);
|
---|
94 | }
|
---|
95 | lastrole = null;
|
---|
96 | } else if (o instanceof TaggingPresetSeparator) {
|
---|
97 | TaggingPresetSeparator tp = (TaggingPresetSeparator) o;
|
---|
98 | tp.group = lastmenu;
|
---|
99 | all.add(tp);
|
---|
100 | lastrole = null;
|
---|
101 | } else if (o instanceof TaggingPreset) {
|
---|
102 | TaggingPreset tp = (TaggingPreset) o;
|
---|
103 | tp.group = lastmenu;
|
---|
104 | tp.setDisplayName();
|
---|
105 | all.add(tp);
|
---|
106 | lastrole = null;
|
---|
107 | } else {
|
---|
108 | if (!all.isEmpty()) {
|
---|
109 | if (o instanceof TaggingPresetItems.Roles) {
|
---|
110 | all.getLast().data.add((TaggingPresetItem) o);
|
---|
111 | if (all.getLast().roles != null) {
|
---|
112 | throw new SAXException(tr("Roles cannot appear more than once"));
|
---|
113 | }
|
---|
114 | all.getLast().roles = (TaggingPresetItems.Roles) o;
|
---|
115 | lastrole = (TaggingPresetItems.Roles) o;
|
---|
116 | } else if (o instanceof TaggingPresetItems.Role) {
|
---|
117 | if (lastrole == null)
|
---|
118 | throw new SAXException(tr("Preset role element without parent"));
|
---|
119 | lastrole.roles.add((TaggingPresetItems.Role) o);
|
---|
120 | } else if (o instanceof TaggingPresetItems.Check) {
|
---|
121 | checks.add((TaggingPresetItems.Check) o);
|
---|
122 | } else if (o instanceof TaggingPresetItems.PresetListEntry) {
|
---|
123 | listEntries.add((TaggingPresetItems.PresetListEntry) o);
|
---|
124 | } else if (o instanceof TaggingPresetItems.CheckGroup) {
|
---|
125 | all.getLast().data.add((TaggingPresetItem) o);
|
---|
126 | ((TaggingPresetItems.CheckGroup) o).checks.addAll(checks);
|
---|
127 | checks.clear();
|
---|
128 | } else {
|
---|
129 | if (!checks.isEmpty()) {
|
---|
130 | all.getLast().data.addAll(checks);
|
---|
131 | checks.clear();
|
---|
132 | }
|
---|
133 | all.getLast().data.add((TaggingPresetItem) o);
|
---|
134 | if (o instanceof TaggingPresetItems.ComboMultiSelect) {
|
---|
135 | ((TaggingPresetItems.ComboMultiSelect) o).addListEntries(listEntries);
|
---|
136 | } else if (o instanceof TaggingPresetItems.Key) {
|
---|
137 | if (((TaggingPresetItems.Key) o).value == null) {
|
---|
138 | ((TaggingPresetItems.Key) o).value = ""; // Fix #8530
|
---|
139 | }
|
---|
140 | }
|
---|
141 | listEntries = new LinkedList<TaggingPresetItems.PresetListEntry>();
|
---|
142 | lastrole = null;
|
---|
143 | }
|
---|
144 | } else
|
---|
145 | throw new SAXException(tr("Preset sub element without parent"));
|
---|
146 | }
|
---|
147 | }
|
---|
148 | if (!all.isEmpty() && !checks.isEmpty()) {
|
---|
149 | all.getLast().data.addAll(checks);
|
---|
150 | checks.clear();
|
---|
151 | }
|
---|
152 | return all;
|
---|
153 | }
|
---|
154 |
|
---|
155 | public static Collection<TaggingPreset> readAll(String source, boolean validate) throws SAXException, IOException {
|
---|
156 | Collection<TaggingPreset> tp;
|
---|
157 | MirroredInputStream s = new MirroredInputStream(source);
|
---|
158 | try {
|
---|
159 | InputStream zip = s.findZipEntryInputStream("xml","preset");
|
---|
160 | if(zip != null) {
|
---|
161 | zipIcons = s.getFile();
|
---|
162 | }
|
---|
163 | InputStreamReader r = new InputStreamReader(zip == null ? s : zip, Utils.UTF_8);
|
---|
164 | try {
|
---|
165 | tp = readAll(new BufferedReader(r), validate);
|
---|
166 | } finally {
|
---|
167 | Utils.close(r);
|
---|
168 | }
|
---|
169 | } finally {
|
---|
170 | Utils.close(s);
|
---|
171 | }
|
---|
172 | return tp;
|
---|
173 | }
|
---|
174 |
|
---|
175 | public static Collection<TaggingPreset> readAll(Collection<String> sources, boolean validate) {
|
---|
176 | LinkedList<TaggingPreset> allPresets = new LinkedList<TaggingPreset>();
|
---|
177 | for(String source : sources) {
|
---|
178 | try {
|
---|
179 | allPresets.addAll(readAll(source, validate));
|
---|
180 | } catch (IOException e) {
|
---|
181 | Main.error(e);
|
---|
182 | Main.error(source);
|
---|
183 | JOptionPane.showMessageDialog(
|
---|
184 | Main.parent,
|
---|
185 | tr("Could not read tagging preset source: {0}",source),
|
---|
186 | tr("Error"),
|
---|
187 | JOptionPane.ERROR_MESSAGE
|
---|
188 | );
|
---|
189 | } catch (SAXException e) {
|
---|
190 | Main.error(e);
|
---|
191 | Main.error(source);
|
---|
192 | JOptionPane.showMessageDialog(
|
---|
193 | Main.parent,
|
---|
194 | "<html>" + tr("Error parsing {0}: ", source) + "<br><br><table width=600>" + e.getMessage() + "</table></html>",
|
---|
195 | tr("Error"),
|
---|
196 | JOptionPane.ERROR_MESSAGE
|
---|
197 | );
|
---|
198 | }
|
---|
199 | }
|
---|
200 | return allPresets;
|
---|
201 | }
|
---|
202 |
|
---|
203 | public static Collection<TaggingPreset> readFromPreferences(boolean validate) {
|
---|
204 | return readAll(getPresetSources(), validate);
|
---|
205 | }
|
---|
206 |
|
---|
207 | public static File getZipIcons() {
|
---|
208 | return zipIcons;
|
---|
209 | }
|
---|
210 | }
|
---|