Skip to content

Commit 2125be8

Browse files
XCF import: Handle duplicated function names by renaming.
In Version 4,, support pages; they now map to CommandSets. Some cleanup. Resolves #538.
1 parent 74ae9bf commit 2125be8

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

src/main/java/org/harctoolbox/irscrutinizer/importer/XcfImporter.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2009, 2013, 2014 Bengt Martensson.
2+
Copyright (C) 2009, 2013, 2014, 2024 Bengt Martensson.
33
44
This program is free software: you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -22,9 +22,11 @@
2222
import java.io.InputStream;
2323
import java.io.Reader;
2424
import java.text.ParseException;
25+
import java.util.ArrayList;
2526
import java.util.Date;
2627
import java.util.HashMap;
2728
import java.util.LinkedHashMap;
29+
import java.util.List;
2830
import java.util.Map;
2931
import java.util.logging.Level;
3032
import java.util.logging.Logger;
@@ -36,6 +38,9 @@
3638
import org.harctoolbox.girr.Remote;
3739
import org.harctoolbox.girr.RemoteSet;
3840
import org.harctoolbox.ircore.InvalidArgumentException;
41+
import org.harctoolbox.ircore.IrCoreException;
42+
import org.harctoolbox.ircore.ThisCannotHappenException;
43+
import org.harctoolbox.irp.IrpException;
3944
import org.harctoolbox.irscrutinizer.Version;
4045
import org.harctoolbox.xml.XmlUtils;
4146
import org.w3c.dom.Document;
@@ -107,6 +112,23 @@ private static String childContent(Element element, String tagName) {
107112
return nl.item(0).getTextContent();
108113
}
109114

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+
110132
private boolean translateProntoFont = true;
111133

112134
private transient Map<String, String> nameIndex;
@@ -197,10 +219,6 @@ private void load4(Element root) throws ParseException {
197219
case "Modules":
198220
moduleIndex = mkIndex(element, "MODULE");
199221
break;
200-
//else if (element.getTagName().equals("Strings"))
201-
// setupNamesIndex(element);
202-
//else if (element.getTagName().equals("Items"))
203-
// itemIndex = mkIndex(element, "ITEM");
204222
default:
205223
break;
206224
}
@@ -230,34 +248,38 @@ private Remote loadModule(Element module) {
230248
NodeList names = module.getElementsByTagName("Name");
231249
String nameId = ((Element) names.item(0)).getAttribute("id");
232250
String name = nameIndex.get(nameId);
233-
//System.out.println(id + "\t" + nameId + "\t" + name);
234251
Map<String,Command> cmds = new HashMap<>(32);
252+
List<CommandSet> commandSets = new ArrayList<>(16);
235253
NodeList firstPages = module.getElementsByTagName("FirstPage");
236254
Element page = firstPages.getLength() > 0 ? pageIndex.get(((Element) firstPages.item(0)).getAttribute("id")) : null;
237255
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);
240259
NodeList nextpages = page.getElementsByTagName("Next");
241260
page = nextpages.getLength() > 0 ? pageIndex.get(((Element) nextpages.item(0)).getAttribute("id")) : null;
242261
}
243262
return new Remote(new Remote.MetaData(name),
244263
null, //java.lang.String comment,
245264
null, //java.lang.String notes,
246-
cmds,
265+
commandSets,
247266
null //java.util.HashMap<java.lang.String,java.util.HashMap<java.lang.String,java.lang.String>> applicationParameters)
248267
);
249268
}
250269

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);
253272
NodeList items = page.getElementsByTagName("Item");
254273
for (int i = 0; i < items.getLength(); i++) {
255274
Element item = (Element) items.item(i);
256275
Command command = loadItem(item);
257276
if (command != null)
258-
cmds.put(command.getName(), command);
277+
putUnique(cmds, command);
259278
}
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);
261283
}
262284

263285
private Command loadItem(Element item) {
@@ -382,7 +404,7 @@ private Remote parseDevice(Element device) {
382404
for (int i = 0; i < functions.getLength(); i++) {
383405
Command command = parseFunction((Element) functions.item(i));
384406
if (command != null)
385-
commands.put(command.getName(), command);
407+
putUnique(commands, command);
386408
}
387409

388410
Remote.MetaData metaData = parseMetaData(device);

0 commit comments

Comments
 (0)