Skip to content

Commit

Permalink
Allow opening ConfigEdit.xml directly. Cleanup.
Browse files Browse the repository at this point in the history
Use try-with-resources for opening zip file. Preparation for #537
  • Loading branch information
bengtmartensson committed Oct 24, 2024
1 parent 8482845 commit e3efc44
Showing 1 changed file with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.harctoolbox.girr.Command;
import org.harctoolbox.girr.CommandSet;
import org.harctoolbox.girr.GirrException;
import org.harctoolbox.girr.Remote;
import org.harctoolbox.girr.RemoteSet;
Expand All @@ -48,24 +51,23 @@ public class XcfImporter extends RemoteSetImporter implements IReaderImporter {

private static final String XCF_XML_FILENAME = "ConfigEdit.xml";
private static final String DEFAULT_CHARSETNAME = "WINDOWS-1252";

private final static Logger logger = Logger.getLogger(XcfImporter.class.getName());

private static Document openConfig(File filename) throws SAXException, IOException {
ZipFile zipFile = null;
Document doc = null;
try {
zipFile = new ZipFile(filename);
ZipEntry entry = zipFile.getEntry(XCF_XML_FILENAME);
if (entry == null)
entry = zipFile.getEntry("/" + XCF_XML_FILENAME);
if (entry == null)
throw new IOException("Cannot read " + filename.getCanonicalPath() + " as XCF file." );
InputStream stream = zipFile.getInputStream(entry);
doc = XmlUtils.openXmlStream(stream, null, false, false);
} finally {
if (zipFile != null)
zipFile.close();
if (filename.getName().endsWith(".xml"))
return XmlUtils.openXmlFile(filename);
else {
try (ZipFile zipFile = new ZipFile(filename)) {
ZipEntry entry = zipFile.getEntry(XCF_XML_FILENAME);
if (entry == null)
entry = zipFile.getEntry("/" + XCF_XML_FILENAME);
if (entry == null)
throw new IOException("Cannot read " + filename.getCanonicalPath() + " as XCF file.");
InputStream stream = zipFile.getInputStream(entry);
return XmlUtils.openXmlStream(stream, null, false, false);
}
}
return doc;
}

private static LinkedHashMap<String, Element> mkIndex(Element element, String tagId) {
Expand All @@ -88,10 +90,11 @@ public static RemoteSet importXcf(String filename) throws IOException, SAXExcept
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public static void main(String args[]) {
try {
RemoteSet buttons = importXcf(args[0]);
buttons.getRemotes().forEach((button) -> {
System.out.println(button.toString());
});
RemoteSet remoteSet = importXcf(args[0]);
for (Remote remote : remoteSet)
for (CommandSet commandSet : remote)
for (Command command : commandSet)
System.out.println(command.toString());
} catch (IOException | ParseException | InvalidArgumentException | SAXException ex) {
System.err.println(ex.getMessage());
}
Expand Down Expand Up @@ -139,7 +142,6 @@ private String transmogrify(String s) {
: s;
}

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

Element root = doc.getDocumentElement();
String version = root.getAttribute("Version");
if (version.charAt(0) == '5')
load5(root);
else
load4(root);
logger.log(Level.INFO, "Loaded XCF file with Version {0}", version);

switch (version.charAt(0)) {
case '4':
load4(root);
break;
case '5':
load5(root);
break;
default:
throw new IllegalArgumentException("Unsupported XCF version " + version);
}
}

@SuppressWarnings("empty-statement")
Expand Down Expand Up @@ -405,7 +415,7 @@ public boolean canImportDirectories() {

@Override
public String[][] getFileExtensions() {
return new String[][]{ new String[]{ "Pronto professional files (*.xcf)", "xcf" }};
return new String[][]{ new String[]{ "Pronto professional files (*.xcf)", "xcf" }, new String[]{"ConfigEdit.xml files (*.xml)", "xml" }};
}

@Override
Expand Down

0 comments on commit e3efc44

Please sign in to comment.