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