26
26
import java .util .HashMap ;
27
27
import java .util .LinkedHashMap ;
28
28
import java .util .Map ;
29
+ import java .util .logging .Level ;
30
+ import java .util .logging .Logger ;
29
31
import java .util .zip .ZipEntry ;
30
32
import java .util .zip .ZipFile ;
31
33
import org .harctoolbox .girr .Command ;
34
+ import org .harctoolbox .girr .CommandSet ;
32
35
import org .harctoolbox .girr .GirrException ;
33
36
import org .harctoolbox .girr .Remote ;
34
37
import org .harctoolbox .girr .RemoteSet ;
@@ -48,24 +51,23 @@ public class XcfImporter extends RemoteSetImporter implements IReaderImporter {
48
51
49
52
private static final String XCF_XML_FILENAME = "ConfigEdit.xml" ;
50
53
private static final String DEFAULT_CHARSETNAME = "WINDOWS-1252" ;
54
+
55
+ private final static Logger logger = Logger .getLogger (XcfImporter .class .getName ());
51
56
52
57
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
+ }
67
70
}
68
- return doc ;
69
71
}
70
72
71
73
private static LinkedHashMap <String , Element > mkIndex (Element element , String tagId ) {
@@ -88,10 +90,11 @@ public static RemoteSet importXcf(String filename) throws IOException, SAXExcept
88
90
@ SuppressWarnings ("UseOfSystemOutOrSystemErr" )
89
91
public static void main (String args []) {
90
92
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 ());
95
98
} catch (IOException | ParseException | InvalidArgumentException | SAXException ex ) {
96
99
System .err .println (ex .getMessage ());
97
100
}
@@ -139,7 +142,6 @@ private String transmogrify(String s) {
139
142
: s ;
140
143
}
141
144
142
- @ SuppressWarnings ("empty-statement" )
143
145
private void load (Document doc ) throws ParseException {
144
146
learnedIrCodeIndex = 1 ;
145
147
nameIndex = null ;
@@ -150,10 +152,18 @@ private void load(Document doc) throws ParseException {
150
152
151
153
Element root = doc .getDocumentElement ();
152
154
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
+ }
157
167
}
158
168
159
169
@ SuppressWarnings ("empty-statement" )
@@ -405,7 +415,7 @@ public boolean canImportDirectories() {
405
415
406
416
@ Override
407
417
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" } };
409
419
}
410
420
411
421
@ Override
0 commit comments