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.ArrayList;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.Iterator;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Map;
|
---|
19 | import java.util.Stack;
|
---|
20 |
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
25 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
|
---|
26 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
27 | import org.openstreetmap.josm.tools.Utils;
|
---|
28 | import org.openstreetmap.josm.tools.XmlObjectParser;
|
---|
29 | import org.xml.sax.SAXException;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * The tagging presets reader.
|
---|
33 | * @since 6068
|
---|
34 | */
|
---|
35 | public final class TaggingPresetReader {
|
---|
36 |
|
---|
37 | private TaggingPresetReader() {
|
---|
38 | // Hide default constructor for utils classes
|
---|
39 | }
|
---|
40 |
|
---|
41 | private static File zipIcons = null;
|
---|
42 |
|
---|
43 | public static List<String> getPresetSources() {
|
---|
44 | LinkedList<String> sources = new LinkedList<String>();
|
---|
45 |
|
---|
46 | for (SourceEntry e : (new TaggingPresetPreference.PresetPrefHelper()).get()) {
|
---|
47 | sources.add(e.url);
|
---|
48 | }
|
---|
49 |
|
---|
50 | return sources;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Holds a reference to a chunk of items/objects.
|
---|
55 | */
|
---|
56 | public static class Chunk {
|
---|
57 | public String id;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Holds a reference to an earlier item/object.
|
---|
62 | */
|
---|
63 | public static class Reference {
|
---|
64 | public String ref;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public static List<TaggingPreset> readAll(Reader in, boolean validate) throws SAXException {
|
---|
68 | XmlObjectParser parser = new XmlObjectParser();
|
---|
69 | parser.mapOnStart("item", TaggingPreset.class);
|
---|
70 | parser.mapOnStart("separator", TaggingPresetSeparator.class);
|
---|
71 | parser.mapBoth("group", TaggingPresetMenu.class);
|
---|
72 | parser.map("text", TaggingPresetItems.Text.class);
|
---|
73 | parser.map("link", TaggingPresetItems.Link.class);
|
---|
74 | parser.mapOnStart("optional", TaggingPresetItems.Optional.class);
|
---|
75 | parser.mapOnStart("roles", TaggingPresetItems.Roles.class);
|
---|
76 | parser.map("role", TaggingPresetItems.Role.class);
|
---|
77 | parser.map("checkgroup", TaggingPresetItems.CheckGroup.class);
|
---|
78 | parser.map("check", TaggingPresetItems.Check.class);
|
---|
79 | parser.map("combo", TaggingPresetItems.Combo.class);
|
---|
80 | parser.map("multiselect", TaggingPresetItems.MultiSelect.class);
|
---|
81 | parser.map("label", TaggingPresetItems.Label.class);
|
---|
82 | parser.map("space", TaggingPresetItems.Space.class);
|
---|
83 | parser.map("key", TaggingPresetItems.Key.class);
|
---|
84 | parser.map("list_entry", TaggingPresetItems.PresetListEntry.class);
|
---|
85 | parser.map("item_separator", TaggingPresetItems.ItemSeparator.class);
|
---|
86 | parser.mapBoth("chunk", Chunk.class);
|
---|
87 | parser.map("reference", Reference.class);
|
---|
88 |
|
---|
89 | LinkedList<TaggingPreset> all = new LinkedList<TaggingPreset>();
|
---|
90 | TaggingPresetMenu lastmenu = null;
|
---|
91 | TaggingPresetItems.Roles lastrole = null;
|
---|
92 | final List<TaggingPresetItems.Check> checks = new LinkedList<TaggingPresetItems.Check>();
|
---|
93 | List<TaggingPresetItems.PresetListEntry> listEntries = new LinkedList<TaggingPresetItems.PresetListEntry>();
|
---|
94 | final Map<String, List<Object>> byId = new HashMap<String, List<Object>>();
|
---|
95 | final Stack<String> lastIds = new Stack<String>();
|
---|
96 | /** lastIdIterators contains non empty iterators of items to be handled before obtaining the next item from the XML parser */
|
---|
97 | final Stack<Iterator<Object>> lastIdIterators = new Stack<Iterator<Object>>();
|
---|
98 |
|
---|
99 | if (validate) {
|
---|
100 | parser.startWithValidation(in, Main.JOSM_WEBSITE+"/tagging-preset-1.0", "resource://data/tagging-preset.xsd");
|
---|
101 | } else {
|
---|
102 | parser.start(in);
|
---|
103 | }
|
---|
104 | while (parser.hasNext() || !lastIdIterators.isEmpty()) {
|
---|
105 | final Object o;
|
---|
106 | if (!lastIdIterators.isEmpty()) {
|
---|
107 | // obtain elements from lastIdIterators with higher priority
|
---|
108 | o = lastIdIterators.peek().next();
|
---|
109 | if (!lastIdIterators.peek().hasNext()) {
|
---|
110 | // remove iterator is is empty
|
---|
111 | lastIdIterators.pop();
|
---|
112 | }
|
---|
113 | } else {
|
---|
114 | o = parser.next();
|
---|
115 | }
|
---|
116 | if (o instanceof Chunk) {
|
---|
117 | if (!lastIds.isEmpty() && ((Chunk) o).id.equals(lastIds.peek())) {
|
---|
118 | // pop last id on end of object, don't process further
|
---|
119 | lastIds.pop();
|
---|
120 | ((Chunk) o).id = null;
|
---|
121 | continue;
|
---|
122 | } else {
|
---|
123 | // if preset item contains an id, store a mapping for later usage
|
---|
124 | String lastId = ((Chunk) o).id;
|
---|
125 | lastIds.push(lastId);
|
---|
126 | byId.put(lastId, new ArrayList<Object>());
|
---|
127 | continue;
|
---|
128 | }
|
---|
129 | } else if (!lastIds.isEmpty()) {
|
---|
130 | // add object to mapping for later usage
|
---|
131 | byId.get(lastIds.peek()).add(o);
|
---|
132 | continue;
|
---|
133 | }
|
---|
134 | if (o instanceof Reference) {
|
---|
135 | // if o is a reference, obtain the corresponding objects from the mapping,
|
---|
136 | // and iterate over those before consuming the next element from parser.
|
---|
137 | final String ref = ((Reference) o).ref;
|
---|
138 | if (byId.get(ref) == null) {
|
---|
139 | throw new SAXException(tr("Reference {0} is being used before it was defined", ref));
|
---|
140 | }
|
---|
141 | lastIdIterators.push(byId.get(ref).iterator());
|
---|
142 | continue;
|
---|
143 | }
|
---|
144 | if (!(o instanceof TaggingPresetItem) && !checks.isEmpty()) {
|
---|
145 | all.getLast().data.addAll(checks);
|
---|
146 | checks.clear();
|
---|
147 | }
|
---|
148 | if (o instanceof TaggingPresetMenu) {
|
---|
149 | TaggingPresetMenu tp = (TaggingPresetMenu) o;
|
---|
150 | if (tp == lastmenu) {
|
---|
151 | lastmenu = tp.group;
|
---|
152 | } else {
|
---|
153 | tp.group = lastmenu;
|
---|
154 | tp.setDisplayName();
|
---|
155 | lastmenu = tp;
|
---|
156 | all.add(tp);
|
---|
157 | }
|
---|
158 | lastrole = null;
|
---|
159 | } else if (o instanceof TaggingPresetSeparator) {
|
---|
160 | TaggingPresetSeparator tp = (TaggingPresetSeparator) o;
|
---|
161 | tp.group = lastmenu;
|
---|
162 | all.add(tp);
|
---|
163 | lastrole = null;
|
---|
164 | } else if (o instanceof TaggingPreset) {
|
---|
165 | TaggingPreset tp = (TaggingPreset) o;
|
---|
166 | tp.group = lastmenu;
|
---|
167 | tp.setDisplayName();
|
---|
168 | all.add(tp);
|
---|
169 | lastrole = null;
|
---|
170 | } else {
|
---|
171 | if (!all.isEmpty()) {
|
---|
172 | if (o instanceof TaggingPresetItems.Roles) {
|
---|
173 | all.getLast().data.add((TaggingPresetItem) o);
|
---|
174 | if (all.getLast().roles != null) {
|
---|
175 | throw new SAXException(tr("Roles cannot appear more than once"));
|
---|
176 | }
|
---|
177 | all.getLast().roles = (TaggingPresetItems.Roles) o;
|
---|
178 | lastrole = (TaggingPresetItems.Roles) o;
|
---|
179 | } else if (o instanceof TaggingPresetItems.Role) {
|
---|
180 | if (lastrole == null)
|
---|
181 | throw new SAXException(tr("Preset role element without parent"));
|
---|
182 | lastrole.roles.add((TaggingPresetItems.Role) o);
|
---|
183 | } else if (o instanceof TaggingPresetItems.Check) {
|
---|
184 | checks.add((TaggingPresetItems.Check) o);
|
---|
185 | } else if (o instanceof TaggingPresetItems.PresetListEntry) {
|
---|
186 | listEntries.add((TaggingPresetItems.PresetListEntry) o);
|
---|
187 | } else if (o instanceof TaggingPresetItems.CheckGroup) {
|
---|
188 | all.getLast().data.add((TaggingPresetItem) o);
|
---|
189 | ((TaggingPresetItems.CheckGroup) o).checks.addAll(checks);
|
---|
190 | checks.clear();
|
---|
191 | } else {
|
---|
192 | if (!checks.isEmpty()) {
|
---|
193 | all.getLast().data.addAll(checks);
|
---|
194 | checks.clear();
|
---|
195 | }
|
---|
196 | all.getLast().data.add((TaggingPresetItem) o);
|
---|
197 | if (o instanceof TaggingPresetItems.ComboMultiSelect) {
|
---|
198 | ((TaggingPresetItems.ComboMultiSelect) o).addListEntries(listEntries);
|
---|
199 | } else if (o instanceof TaggingPresetItems.Key) {
|
---|
200 | if (((TaggingPresetItems.Key) o).value == null) {
|
---|
201 | ((TaggingPresetItems.Key) o).value = ""; // Fix #8530
|
---|
202 | }
|
---|
203 | }
|
---|
204 | listEntries = new LinkedList<TaggingPresetItems.PresetListEntry>();
|
---|
205 | lastrole = null;
|
---|
206 | }
|
---|
207 | } else
|
---|
208 | throw new SAXException(tr("Preset sub element without parent"));
|
---|
209 | }
|
---|
210 | }
|
---|
211 | if (!all.isEmpty() && !checks.isEmpty()) {
|
---|
212 | all.getLast().data.addAll(checks);
|
---|
213 | checks.clear();
|
---|
214 | }
|
---|
215 | return all;
|
---|
216 | }
|
---|
217 |
|
---|
218 | public static Collection<TaggingPreset> readAll(String source, boolean validate) throws SAXException, IOException {
|
---|
219 | Collection<TaggingPreset> tp;
|
---|
220 | MirroredInputStream s = new MirroredInputStream(source);
|
---|
221 | try {
|
---|
222 | InputStream zip = s.findZipEntryInputStream("xml","preset");
|
---|
223 | if(zip != null) {
|
---|
224 | zipIcons = s.getFile();
|
---|
225 | }
|
---|
226 | InputStreamReader r = new InputStreamReader(zip == null ? s : zip, Utils.UTF_8);
|
---|
227 | try {
|
---|
228 | tp = readAll(new BufferedReader(r), validate);
|
---|
229 | } finally {
|
---|
230 | Utils.close(r);
|
---|
231 | }
|
---|
232 | } finally {
|
---|
233 | Utils.close(s);
|
---|
234 | }
|
---|
235 | return tp;
|
---|
236 | }
|
---|
237 |
|
---|
238 | public static Collection<TaggingPreset> readAll(Collection<String> sources, boolean validate) {
|
---|
239 | LinkedList<TaggingPreset> allPresets = new LinkedList<TaggingPreset>();
|
---|
240 | for(String source : sources) {
|
---|
241 | try {
|
---|
242 | allPresets.addAll(readAll(source, validate));
|
---|
243 | } catch (IOException e) {
|
---|
244 | Main.error(e);
|
---|
245 | Main.error(source);
|
---|
246 | JOptionPane.showMessageDialog(
|
---|
247 | Main.parent,
|
---|
248 | tr("Could not read tagging preset source: {0}",source),
|
---|
249 | tr("Error"),
|
---|
250 | JOptionPane.ERROR_MESSAGE
|
---|
251 | );
|
---|
252 | } catch (SAXException e) {
|
---|
253 | Main.error(e);
|
---|
254 | Main.error(source);
|
---|
255 | JOptionPane.showMessageDialog(
|
---|
256 | Main.parent,
|
---|
257 | "<html>" + tr("Error parsing {0}: ", source) + "<br><br><table width=600>" + e.getMessage() + "</table></html>",
|
---|
258 | tr("Error"),
|
---|
259 | JOptionPane.ERROR_MESSAGE
|
---|
260 | );
|
---|
261 | }
|
---|
262 | }
|
---|
263 | return allPresets;
|
---|
264 | }
|
---|
265 |
|
---|
266 | public static Collection<TaggingPreset> readFromPreferences(boolean validate) {
|
---|
267 | return readAll(getPresetSources(), validate);
|
---|
268 | }
|
---|
269 |
|
---|
270 | public static File getZipIcons() {
|
---|
271 | return zipIcons;
|
---|
272 | }
|
---|
273 | }
|
---|