|
1 | 1 | /*
|
2 |
| -Copyright (C) 2009, 2013, 2014 Bengt Martensson. |
| 2 | +Copyright (C) 2009, 2013, 2014, 2024 Bengt Martensson. |
3 | 3 |
|
4 | 4 | This program is free software: you can redistribute it and/or modify
|
5 | 5 | it under the terms of the GNU General Public License as published by
|
|
22 | 22 | import java.io.InputStream;
|
23 | 23 | import java.io.Reader;
|
24 | 24 | import java.text.ParseException;
|
| 25 | +import java.util.ArrayList; |
25 | 26 | import java.util.Date;
|
26 | 27 | import java.util.HashMap;
|
27 | 28 | import java.util.LinkedHashMap;
|
| 29 | +import java.util.List; |
28 | 30 | import java.util.Map;
|
29 | 31 | import java.util.logging.Level;
|
30 | 32 | import java.util.logging.Logger;
|
|
36 | 38 | import org.harctoolbox.girr.Remote;
|
37 | 39 | import org.harctoolbox.girr.RemoteSet;
|
38 | 40 | import org.harctoolbox.ircore.InvalidArgumentException;
|
| 41 | +import org.harctoolbox.ircore.IrCoreException; |
| 42 | +import org.harctoolbox.ircore.ThisCannotHappenException; |
| 43 | +import org.harctoolbox.irp.IrpException; |
39 | 44 | import org.harctoolbox.irscrutinizer.Version;
|
40 | 45 | import org.harctoolbox.xml.XmlUtils;
|
41 | 46 | import org.w3c.dom.Document;
|
@@ -107,6 +112,23 @@ private static String childContent(Element element, String tagName) {
|
107 | 112 | return nl.item(0).getTextContent();
|
108 | 113 | }
|
109 | 114 |
|
| 115 | + private static void putUnique(Map<String, Command> map, Command command) { |
| 116 | + String origName = command.getName(); |
| 117 | + String name = origName; |
| 118 | + int i = 0; |
| 119 | + while (map.containsKey(name)) { |
| 120 | + i++; |
| 121 | + name = origName + "$" + Integer.toString(i); |
| 122 | + } |
| 123 | + |
| 124 | + try { |
| 125 | + Command newCommand = new Command(name, i == 0 ? null : "Renamed from " + origName, command.getProntoHex()); |
| 126 | + map.put(name, newCommand); |
| 127 | + } catch (GirrException | IrpException | IrCoreException ex) { |
| 128 | + throw new ThisCannotHappenException(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
110 | 132 | private boolean translateProntoFont = true;
|
111 | 133 |
|
112 | 134 | private transient Map<String, String> nameIndex;
|
@@ -197,10 +219,6 @@ private void load4(Element root) throws ParseException {
|
197 | 219 | case "Modules":
|
198 | 220 | moduleIndex = mkIndex(element, "MODULE");
|
199 | 221 | break;
|
200 |
| - //else if (element.getTagName().equals("Strings")) |
201 |
| - // setupNamesIndex(element); |
202 |
| - //else if (element.getTagName().equals("Items")) |
203 |
| - // itemIndex = mkIndex(element, "ITEM"); |
204 | 222 | default:
|
205 | 223 | break;
|
206 | 224 | }
|
@@ -230,34 +248,38 @@ private Remote loadModule(Element module) {
|
230 | 248 | NodeList names = module.getElementsByTagName("Name");
|
231 | 249 | String nameId = ((Element) names.item(0)).getAttribute("id");
|
232 | 250 | String name = nameIndex.get(nameId);
|
233 |
| - //System.out.println(id + "\t" + nameId + "\t" + name); |
234 | 251 | Map<String,Command> cmds = new HashMap<>(32);
|
| 252 | + List<CommandSet> commandSets = new ArrayList<>(16); |
235 | 253 | NodeList firstPages = module.getElementsByTagName("FirstPage");
|
236 | 254 | Element page = firstPages.getLength() > 0 ? pageIndex.get(((Element) firstPages.item(0)).getAttribute("id")) : null;
|
237 | 255 | while (page != null) {
|
238 |
| - //System.out.print(page.getAttribute("id") + " "); |
239 |
| - cmds.putAll(loadPage(page)); |
| 256 | + CommandSet pageCommands = loadPage(page); |
| 257 | + if (!pageCommands.isEmpty()) |
| 258 | + commandSets.add(pageCommands); |
240 | 259 | NodeList nextpages = page.getElementsByTagName("Next");
|
241 | 260 | page = nextpages.getLength() > 0 ? pageIndex.get(((Element) nextpages.item(0)).getAttribute("id")) : null;
|
242 | 261 | }
|
243 | 262 | return new Remote(new Remote.MetaData(name),
|
244 | 263 | null, //java.lang.String comment,
|
245 | 264 | null, //java.lang.String notes,
|
246 |
| - cmds, |
| 265 | + commandSets, |
247 | 266 | null //java.util.HashMap<java.lang.String,java.util.HashMap<java.lang.String,java.lang.String>> applicationParameters)
|
248 | 267 | );
|
249 | 268 | }
|
250 | 269 |
|
251 |
| - private Map<String,Command> loadPage(Element page) { |
252 |
| - Map<String,Command> cmds = new HashMap<>(16); |
| 270 | + private CommandSet loadPage(Element page) { |
| 271 | + Map<String,Command> cmds = new LinkedHashMap<>(64); |
253 | 272 | NodeList items = page.getElementsByTagName("Item");
|
254 | 273 | for (int i = 0; i < items.getLength(); i++) {
|
255 | 274 | Element item = (Element) items.item(i);
|
256 | 275 | Command command = loadItem(item);
|
257 | 276 | if (command != null)
|
258 |
| - cmds.put(command.getName(), command); |
| 277 | + putUnique(cmds, command); |
259 | 278 | }
|
260 |
| - return cmds; |
| 279 | + NodeList nl = page.getElementsByTagName("Name"); |
| 280 | + String nameId = ((Element) nl.item(0)).getAttribute("id"); |
| 281 | + String pageName = nameIndex.get(nameId); |
| 282 | + return new CommandSet(pageName, null, cmds, null, null); |
261 | 283 | }
|
262 | 284 |
|
263 | 285 | private Command loadItem(Element item) {
|
@@ -382,7 +404,7 @@ private Remote parseDevice(Element device) {
|
382 | 404 | for (int i = 0; i < functions.getLength(); i++) {
|
383 | 405 | Command command = parseFunction((Element) functions.item(i));
|
384 | 406 | if (command != null)
|
385 |
| - commands.put(command.getName(), command); |
| 407 | + putUnique(commands, command); |
386 | 408 | }
|
387 | 409 |
|
388 | 410 | Remote.MetaData metaData = parseMetaData(device);
|
|
0 commit comments