source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetReader.java@ 7847

Last change on this file since 7847 was 7847, checked in by bastiK, 9 years ago

fixed #10872 - handle BOM for preset files

File size: 13.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedReader;
7import java.io.File;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.InputStreamReader;
11import java.io.Reader;
12import java.nio.charset.StandardCharsets;
13import java.util.ArrayList;
14import java.util.Collection;
15import java.util.HashMap;
16import java.util.Iterator;
17import java.util.LinkedList;
18import java.util.List;
19import java.util.Map;
20import java.util.Set;
21import java.util.Stack;
22
23import javax.swing.JOptionPane;
24
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
27import org.openstreetmap.josm.io.CachedFile;
28import org.openstreetmap.josm.io.UTFInputStreamReader;
29import org.openstreetmap.josm.tools.XmlObjectParser;
30import org.xml.sax.SAXException;
31
32/**
33 * The tagging presets reader.
34 * @since 6068
35 */
36public final class TaggingPresetReader {
37
38 /**
39 * The accepted MIME types sent in the HTTP Accept header.
40 * @since 6867
41 */
42 public static final String PRESET_MIME_TYPES = "application/xml, text/xml, text/plain; q=0.8, application/zip, application/octet-stream; q=0.5";
43
44 private TaggingPresetReader() {
45 // Hide default constructor for utils classes
46 }
47
48 private static File zipIcons = null;
49
50 /**
51 * Returns the set of preset source URLs.
52 * @return The set of preset source URLs.
53 */
54 public static Set<String> getPresetSources() {
55 return new TaggingPresetPreference.PresetPrefHelper().getActiveUrls();
56 }
57
58 /**
59 * Holds a reference to a chunk of items/objects.
60 */
61 public static class Chunk {
62 /** The chunk id, can be referenced later */
63 public String id;
64 }
65
66 /**
67 * Holds a reference to an earlier item/object.
68 */
69 public static class Reference {
70 /** Reference matching a chunk id defined earlier **/
71 public String ref;
72 }
73
74 public static List<TaggingPreset> readAll(Reader in, boolean validate) throws SAXException {
75 XmlObjectParser parser = new XmlObjectParser();
76 parser.mapOnStart("item", TaggingPreset.class);
77 parser.mapOnStart("separator", TaggingPresetSeparator.class);
78 parser.mapBoth("group", TaggingPresetMenu.class);
79 parser.map("text", TaggingPresetItems.Text.class);
80 parser.map("link", TaggingPresetItems.Link.class);
81 parser.map("preset_link", TaggingPresetItems.PresetLink.class);
82 parser.mapOnStart("optional", TaggingPresetItems.Optional.class);
83 parser.mapOnStart("roles", TaggingPresetItems.Roles.class);
84 parser.map("role", TaggingPresetItems.Role.class);
85 parser.map("checkgroup", TaggingPresetItems.CheckGroup.class);
86 parser.map("check", TaggingPresetItems.Check.class);
87 parser.map("combo", TaggingPresetItems.Combo.class);
88 parser.map("multiselect", TaggingPresetItems.MultiSelect.class);
89 parser.map("label", TaggingPresetItems.Label.class);
90 parser.map("space", TaggingPresetItems.Space.class);
91 parser.map("key", TaggingPresetItems.Key.class);
92 parser.map("list_entry", TaggingPresetItems.PresetListEntry.class);
93 parser.map("item_separator", TaggingPresetItems.ItemSeparator.class);
94 parser.mapBoth("chunk", Chunk.class);
95 parser.map("reference", Reference.class);
96
97 LinkedList<TaggingPreset> all = new LinkedList<>();
98 TaggingPresetMenu lastmenu = null;
99 TaggingPresetItems.Roles lastrole = null;
100 final List<TaggingPresetItems.Check> checks = new LinkedList<>();
101 List<TaggingPresetItems.PresetListEntry> listEntries = new LinkedList<>();
102 final Map<String, List<Object>> byId = new HashMap<>();
103 final Stack<String> lastIds = new Stack<>();
104 /** lastIdIterators contains non empty iterators of items to be handled before obtaining the next item from the XML parser */
105 final Stack<Iterator<Object>> lastIdIterators = new Stack<>();
106
107 if (validate) {
108 parser.startWithValidation(in, Main.getXMLBase()+"/tagging-preset-1.0", "resource://data/tagging-preset.xsd");
109 } else {
110 parser.start(in);
111 }
112 while (parser.hasNext() || !lastIdIterators.isEmpty()) {
113 final Object o;
114 if (!lastIdIterators.isEmpty()) {
115 // obtain elements from lastIdIterators with higher priority
116 o = lastIdIterators.peek().next();
117 if (!lastIdIterators.peek().hasNext()) {
118 // remove iterator if is empty
119 lastIdIterators.pop();
120 }
121 } else {
122 o = parser.next();
123 }
124 if (o instanceof Chunk) {
125 if (!lastIds.isEmpty() && ((Chunk) o).id.equals(lastIds.peek())) {
126 // pop last id on end of object, don't process further
127 lastIds.pop();
128 ((Chunk) o).id = null;
129 continue;
130 } else {
131 // if preset item contains an id, store a mapping for later usage
132 String lastId = ((Chunk) o).id;
133 lastIds.push(lastId);
134 byId.put(lastId, new ArrayList<>());
135 continue;
136 }
137 } else if (!lastIds.isEmpty()) {
138 // add object to mapping for later usage
139 byId.get(lastIds.peek()).add(o);
140 continue;
141 }
142 if (o instanceof Reference) {
143 // if o is a reference, obtain the corresponding objects from the mapping,
144 // and iterate over those before consuming the next element from parser.
145 final String ref = ((Reference) o).ref;
146 if (byId.get(ref) == null) {
147 throw new SAXException(tr("Reference {0} is being used before it was defined", ref));
148 }
149 Iterator<Object> it = byId.get(ref).iterator();
150 if (it.hasNext()) {
151 lastIdIterators.push(it);
152 } else {
153 Main.warn("Ignoring reference '"+ref+"' denoting an empty chunk");
154 }
155 continue;
156 }
157 if (!(o instanceof TaggingPresetItem) && !checks.isEmpty()) {
158 all.getLast().data.addAll(checks);
159 checks.clear();
160 }
161 if (o instanceof TaggingPresetMenu) {
162 TaggingPresetMenu tp = (TaggingPresetMenu) o;
163 if (tp == lastmenu) {
164 lastmenu = tp.group;
165 } else {
166 tp.group = lastmenu;
167 tp.setDisplayName();
168 lastmenu = tp;
169 all.add(tp);
170 }
171 lastrole = null;
172 } else if (o instanceof TaggingPresetSeparator) {
173 TaggingPresetSeparator tp = (TaggingPresetSeparator) o;
174 tp.group = lastmenu;
175 all.add(tp);
176 lastrole = null;
177 } else if (o instanceof TaggingPreset) {
178 TaggingPreset tp = (TaggingPreset) o;
179 tp.group = lastmenu;
180 tp.setDisplayName();
181 all.add(tp);
182 lastrole = null;
183 } else {
184 if (!all.isEmpty()) {
185 if (o instanceof TaggingPresetItems.Roles) {
186 all.getLast().data.add((TaggingPresetItem) o);
187 if (all.getLast().roles != null) {
188 throw new SAXException(tr("Roles cannot appear more than once"));
189 }
190 all.getLast().roles = (TaggingPresetItems.Roles) o;
191 lastrole = (TaggingPresetItems.Roles) o;
192 } else if (o instanceof TaggingPresetItems.Role) {
193 if (lastrole == null)
194 throw new SAXException(tr("Preset role element without parent"));
195 lastrole.roles.add((TaggingPresetItems.Role) o);
196 } else if (o instanceof TaggingPresetItems.Check) {
197 checks.add((TaggingPresetItems.Check) o);
198 } else if (o instanceof TaggingPresetItems.PresetListEntry) {
199 listEntries.add((TaggingPresetItems.PresetListEntry) o);
200 } else if (o instanceof TaggingPresetItems.CheckGroup) {
201 all.getLast().data.add((TaggingPresetItem) o);
202 ((TaggingPresetItems.CheckGroup) o).checks.addAll(checks);
203 checks.clear();
204 } else {
205 if (!checks.isEmpty()) {
206 all.getLast().data.addAll(checks);
207 checks.clear();
208 }
209 all.getLast().data.add((TaggingPresetItem) o);
210 if (o instanceof TaggingPresetItems.ComboMultiSelect) {
211 ((TaggingPresetItems.ComboMultiSelect) o).addListEntries(listEntries);
212 } else if (o instanceof TaggingPresetItems.Key) {
213 if (((TaggingPresetItems.Key) o).value == null) {
214 ((TaggingPresetItems.Key) o).value = ""; // Fix #8530
215 }
216 }
217 listEntries = new LinkedList<>();
218 lastrole = null;
219 }
220 } else
221 throw new SAXException(tr("Preset sub element without parent"));
222 }
223 }
224 if (!all.isEmpty() && !checks.isEmpty()) {
225 all.getLast().data.addAll(checks);
226 checks.clear();
227 }
228 return all;
229 }
230
231 public static Collection<TaggingPreset> readAll(String source, boolean validate) throws SAXException, IOException {
232 Collection<TaggingPreset> tp;
233 CachedFile cf = new CachedFile(source).setHttpAccept(PRESET_MIME_TYPES);
234 try (
235 // zip may be null, but Java 7 allows it: https://blogs.oracle.com/darcy/entry/project_coin_null_try_with
236 InputStream zip = cf.findZipEntryInputStream("xml", "preset")
237 ) {
238 if (zip != null) {
239 zipIcons = cf.getFile();
240 }
241 try (InputStreamReader r = UTFInputStreamReader.create(zip == null ? cf.getInputStream() : zip)) {
242 tp = readAll(new BufferedReader(r), validate);
243 }
244 }
245 return tp;
246 }
247
248 /**
249 * Reads all tagging presets from the given sources.
250 * @param sources Collection of tagging presets sources.
251 * @param validate if {@code true}, presets will be validated against XML schema
252 * @return Collection of all presets successfully read
253 */
254 public static Collection<TaggingPreset> readAll(Collection<String> sources, boolean validate) {
255 return readAll(sources, validate, true);
256 }
257
258 /**
259 * Reads all tagging presets from the given sources.
260 * @param sources Collection of tagging presets sources.
261 * @param validate if {@code true}, presets will be validated against XML schema
262 * @param displayErrMsg if {@code true}, a blocking error message is displayed in case of I/O exception.
263 * @return Collection of all presets successfully read
264 */
265 public static Collection<TaggingPreset> readAll(Collection<String> sources, boolean validate, boolean displayErrMsg) {
266 LinkedList<TaggingPreset> allPresets = new LinkedList<>();
267 for(String source : sources) {
268 try {
269 allPresets.addAll(readAll(source, validate));
270 } catch (IOException e) {
271 Main.error(e, false);
272 Main.error(source);
273 if (source.startsWith("http")) {
274 Main.addNetworkError(source, e);
275 }
276 if (displayErrMsg) {
277 JOptionPane.showMessageDialog(
278 Main.parent,
279 tr("Could not read tagging preset source: {0}",source),
280 tr("Error"),
281 JOptionPane.ERROR_MESSAGE
282 );
283 }
284 } catch (SAXException e) {
285 Main.error(e);
286 Main.error(source);
287 JOptionPane.showMessageDialog(
288 Main.parent,
289 "<html>" + tr("Error parsing {0}: ", source) + "<br><br><table width=600>" + e.getMessage() + "</table></html>",
290 tr("Error"),
291 JOptionPane.ERROR_MESSAGE
292 );
293 }
294 }
295 return allPresets;
296 }
297
298 /**
299 * Reads all tagging presets from sources stored in preferences.
300 * @param validate if {@code true}, presets will be validated against XML schema
301 * @param displayErrMsg if {@code true}, a blocking error message is displayed in case of I/O exception.
302 * @return Collection of all presets successfully read
303 */
304 public static Collection<TaggingPreset> readFromPreferences(boolean validate, boolean displayErrMsg) {
305 return readAll(getPresetSources(), validate, displayErrMsg);
306 }
307
308 public static File getZipIcons() {
309 return zipIcons;
310 }
311}
Note: See TracBrowser for help on using the repository browser.