Skip to content

Commit e3efc44

Browse files
Allow opening ConfigEdit.xml directly. Cleanup.
Use try-with-resources for opening zip file. Preparation for #537
1 parent 8482845 commit e3efc44

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

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

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
import java.util.HashMap;
2727
import java.util.LinkedHashMap;
2828
import java.util.Map;
29+
import java.util.logging.Level;
30+
import java.util.logging.Logger;
2931
import java.util.zip.ZipEntry;
3032
import java.util.zip.ZipFile;
3133
import org.harctoolbox.girr.Command;
34+
import org.harctoolbox.girr.CommandSet;
3235
import org.harctoolbox.girr.GirrException;
3336
import org.harctoolbox.girr.Remote;
3437
import org.harctoolbox.girr.RemoteSet;
@@ -48,24 +51,23 @@ public class XcfImporter extends RemoteSetImporter implements IReaderImporter {
4851

4952
private static final String XCF_XML_FILENAME = "ConfigEdit.xml";
5053
private static final String DEFAULT_CHARSETNAME = "WINDOWS-1252";
54+
55+
private final static Logger logger = Logger.getLogger(XcfImporter.class.getName());
5156

5257
private static Document openConfig(File filename) throws SAXException, IOException {
53-
ZipFile zipFile = null;
54-
Document doc = null;
55-
try {
56-
zipFile = new ZipFile(filename);
57-
ZipEntry entry = zipFile.getEntry(XCF_XML_FILENAME);
58-
if (entry == null)
59-
entry = zipFile.getEntry("/" + XCF_XML_FILENAME);
60-
if (entry == null)
61-
throw new IOException("Cannot read " + filename.getCanonicalPath() + " as XCF file." );
62-
InputStream stream = zipFile.getInputStream(entry);
63-
doc = XmlUtils.openXmlStream(stream, null, false, false);
64-
} finally {
65-
if (zipFile != null)
66-
zipFile.close();
58+
if (filename.getName().endsWith(".xml"))
59+
return XmlUtils.openXmlFile(filename);
60+
else {
61+
try (ZipFile zipFile = new ZipFile(filename)) {
62+
ZipEntry entry = zipFile.getEntry(XCF_XML_FILENAME);
63+
if (entry == null)
64+
entry = zipFile.getEntry("/" + XCF_XML_FILENAME);
65+
if (entry == null)
66+
throw new IOException("Cannot read " + filename.getCanonicalPath() + " as XCF file.");
67+
InputStream stream = zipFile.getInputStream(entry);
68+
return XmlUtils.openXmlStream(stream, null, false, false);
69+
}
6770
}
68-
return doc;
6971
}
7072

7173
private static LinkedHashMap<String, Element> mkIndex(Element element, String tagId) {
@@ -88,10 +90,11 @@ public static RemoteSet importXcf(String filename) throws IOException, SAXExcept
8890
@SuppressWarnings("UseOfSystemOutOrSystemErr")
8991
public static void main(String args[]) {
9092
try {
91-
RemoteSet buttons = importXcf(args[0]);
92-
buttons.getRemotes().forEach((button) -> {
93-
System.out.println(button.toString());
94-
});
93+
RemoteSet remoteSet = importXcf(args[0]);
94+
for (Remote remote : remoteSet)
95+
for (CommandSet commandSet : remote)
96+
for (Command command : commandSet)
97+
System.out.println(command.toString());
9598
} catch (IOException | ParseException | InvalidArgumentException | SAXException ex) {
9699
System.err.println(ex.getMessage());
97100
}
@@ -139,7 +142,6 @@ private String transmogrify(String s) {
139142
: s;
140143
}
141144

142-
@SuppressWarnings("empty-statement")
143145
private void load(Document doc) throws ParseException {
144146
learnedIrCodeIndex = 1;
145147
nameIndex = null;
@@ -150,10 +152,18 @@ private void load(Document doc) throws ParseException {
150152

151153
Element root = doc.getDocumentElement();
152154
String version = root.getAttribute("Version");
153-
if (version.charAt(0) == '5')
154-
load5(root);
155-
else
156-
load4(root);
155+
logger.log(Level.INFO, "Loaded XCF file with Version {0}", version);
156+
157+
switch (version.charAt(0)) {
158+
case '4':
159+
load4(root);
160+
break;
161+
case '5':
162+
load5(root);
163+
break;
164+
default:
165+
throw new IllegalArgumentException("Unsupported XCF version " + version);
166+
}
157167
}
158168

159169
@SuppressWarnings("empty-statement")
@@ -405,7 +415,7 @@ public boolean canImportDirectories() {
405415

406416
@Override
407417
public String[][] getFileExtensions() {
408-
return new String[][]{ new String[]{ "Pronto professional files (*.xcf)", "xcf" }};
418+
return new String[][]{ new String[]{ "Pronto professional files (*.xcf)", "xcf" }, new String[]{"ConfigEdit.xml files (*.xml)", "xml" }};
409419
}
410420

411421
@Override

0 commit comments

Comments
 (0)