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

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

see #8465 - replace Utils.UTF_8 by StandardCharsets.UTF_8, new in Java 7

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