@@ -78,96 +78,89 @@ public Configuration load() {
7878 config .setRecentLoadDirectory (recentSaveDirectory );
7979 config .setRecentSaveDirectory (recentSaveDirectory );
8080
81- XMLInputFactory factory = XMLInputFactory .newInstance ();
82- XMLStreamReader reader = null ;
83-
84- try (BufferedInputStream bis = new BufferedInputStream (new FileInputStream (FILE ))) {
85- // Load values
86- reader = factory .createXMLStreamReader (bis );
87-
88- String name = "" ;
89- Stack <String > names = new Stack <>();
90- List <File > recentFiles = new ArrayList <>();
91- boolean maximize = false ;
92- Map <String , String > preferences = config .getPreferences ();
93-
94- while (reader .hasNext ()) {
95- switch (reader .next ()) {
96- case XMLStreamConstants .START_ELEMENT :
97- names .push (name );
98- name += '/' + reader .getLocalName ();
99- switch (name ) {
100- case "/configuration/gui/mainWindow/location" :
101- x = Integer .parseInt (reader .getAttributeValue (null , "x" ));
102- y = Integer .parseInt (reader .getAttributeValue (null , "y" ));
103- break ;
104- case "/configuration/gui/mainWindow/size" :
105- w = Integer .parseInt (reader .getAttributeValue (null , "w" ));
106- h = Integer .parseInt (reader .getAttributeValue (null , "h" ));
107- break ;
108- }
109- break ;
110- case XMLStreamConstants .END_ELEMENT :
111- name = names .pop ();
112- break ;
113- case XMLStreamConstants .CHARACTERS :
114- switch (name ) {
115- case "/configuration/recentFilePaths/filePath" :
116- File file = new File (reader .getText ().trim ());
117- if (file .exists ()) {
118- recentFiles .add (file );
119- }
120- break ;
121- case "/configuration/recentDirectories/loadPath" :
122- file = new File (reader .getText ().trim ());
123- if (file .exists ()) {
124- config .setRecentLoadDirectory (file );
125- }
126- break ;
127- case "/configuration/recentDirectories/savePath" :
128- file = new File (reader .getText ().trim ());
129- if (file .exists ()) {
130- config .setRecentSaveDirectory (file );
131- }
132- break ;
133- case "/configuration/gui/lookAndFeel" :
134- config .setLookAndFeel (reader .getText ().trim ());
135- break ;
136- case "/configuration/gui/mainWindow/maximize" :
137- maximize = Boolean .parseBoolean (reader .getText ().trim ());
138- break ;
139- default :
140- if (name .startsWith ("/configuration/preferences/" )) {
141- String key = name .substring ("/configuration/preferences/" .length ());
142- preferences .put (key , reader .getText ().trim ());
143- }
144- break ;
145- }
146- break ;
81+ if (FILE .exists ()) {
82+ try (FileInputStream fis = new FileInputStream (FILE )) {
83+ XMLStreamReader reader = XMLInputFactory .newInstance ().createXMLStreamReader (fis );
84+
85+ // Load values
86+ String name = "" ;
87+ Stack <String > names = new Stack <>();
88+ List <File > recentFiles = new ArrayList <>();
89+ boolean maximize = false ;
90+ Map <String , String > preferences = config .getPreferences ();
91+
92+ while (reader .hasNext ()) {
93+ switch (reader .next ()) {
94+ case XMLStreamConstants .START_ELEMENT :
95+ names .push (name );
96+ name += '/' + reader .getLocalName ();
97+ switch (name ) {
98+ case "/configuration/gui/mainWindow/location" :
99+ x = Integer .parseInt (reader .getAttributeValue (null , "x" ));
100+ y = Integer .parseInt (reader .getAttributeValue (null , "y" ));
101+ break ;
102+ case "/configuration/gui/mainWindow/size" :
103+ w = Integer .parseInt (reader .getAttributeValue (null , "w" ));
104+ h = Integer .parseInt (reader .getAttributeValue (null , "h" ));
105+ break ;
106+ }
107+ break ;
108+ case XMLStreamConstants .END_ELEMENT :
109+ name = names .pop ();
110+ break ;
111+ case XMLStreamConstants .CHARACTERS :
112+ switch (name ) {
113+ case "/configuration/recentFilePaths/filePath" :
114+ File file = new File (reader .getText ().trim ());
115+ if (file .exists ()) {
116+ recentFiles .add (file );
117+ }
118+ break ;
119+ case "/configuration/recentDirectories/loadPath" :
120+ file = new File (reader .getText ().trim ());
121+ if (file .exists ()) {
122+ config .setRecentLoadDirectory (file );
123+ }
124+ break ;
125+ case "/configuration/recentDirectories/savePath" :
126+ file = new File (reader .getText ().trim ());
127+ if (file .exists ()) {
128+ config .setRecentSaveDirectory (file );
129+ }
130+ break ;
131+ case "/configuration/gui/lookAndFeel" :
132+ config .setLookAndFeel (reader .getText ().trim ());
133+ break ;
134+ case "/configuration/gui/mainWindow/maximize" :
135+ maximize = Boolean .parseBoolean (reader .getText ().trim ());
136+ break ;
137+ default :
138+ if (name .startsWith ("/configuration/preferences/" )) {
139+ String key = name .substring ("/configuration/preferences/" .length ());
140+ preferences .put (key , reader .getText ().trim ());
141+ }
142+ break ;
143+ }
144+ break ;
145+ }
147146 }
148- }
149147
150- if (recentFiles .size () > Constants .MAX_RECENT_FILES ) {
151- // Truncate
152- recentFiles = recentFiles .subList (0 , Constants .MAX_RECENT_FILES );
153- }
154- config .setRecentFiles (recentFiles );
148+ if (recentFiles .size () > Constants .MAX_RECENT_FILES ) {
149+ // Truncate
150+ recentFiles = recentFiles .subList (0 , Constants .MAX_RECENT_FILES );
151+ }
152+ config .setRecentFiles (recentFiles );
155153
156- if ((x >= 0 ) && (y >= 0 ) && (x + w < screenSize .width ) && (y + h < screenSize .height )) {
157- // Update preferences
158- config .setMainWindowLocation (new Point (x , y ));
159- config .setMainWindowSize (new Dimension (w , h ));
160- config .setMainWindowMaximize (maximize );
161- }
162- } catch (Exception e ) {
163- assert ExceptionUtil .printStackTrace (e );
164- } finally {
165- if (reader != null ) {
166- try {
167- reader .close ();
168- } catch (XMLStreamException e ) {
169- assert ExceptionUtil .printStackTrace (e );
154+ if ((x >= 0 ) && (y >= 0 ) && (x + w < screenSize .width ) && (y + h < screenSize .height )) {
155+ // Update preferences
156+ config .setMainWindowLocation (new Point (x , y ));
157+ config .setMainWindowSize (new Dimension (w , h ));
158+ config .setMainWindowMaximize (maximize );
170159 }
160+
161+ reader .close ();
162+ } catch (Exception e ) {
163+ assert ExceptionUtil .printStackTrace (e );
171164 }
172165 }
173166
@@ -182,12 +175,10 @@ public void save(Configuration configuration) {
182175 Point l = configuration .getMainWindowLocation ();
183176 Dimension s = configuration .getMainWindowSize ();
184177
185- XMLOutputFactory factory = XMLOutputFactory . newInstance ();
186- XMLStreamWriter writer = null ;
178+ try ( FileOutputStream fos = new FileOutputStream ( FILE )) {
179+ XMLStreamWriter writer = XMLOutputFactory . newInstance (). createXMLStreamWriter ( fos ) ;
187180
188- try (BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (FILE ))) {
189181 // Load values
190- writer = factory .createXMLStreamWriter (bos );
191182 writer .writeStartDocument ();
192183 writer .writeCharacters ("\n " );
193184 writer .writeStartElement ("configuration" );
@@ -261,16 +252,9 @@ public void save(Configuration configuration) {
261252
262253 writer .writeEndElement ();
263254 writer .writeEndDocument ();
255+ writer .close ();
264256 } catch (Exception e ) {
265257 assert ExceptionUtil .printStackTrace (e );
266- } finally {
267- if (writer != null ) {
268- try {
269- writer .close ();
270- } catch (XMLStreamException e ) {
271- assert ExceptionUtil .printStackTrace (e );
272- }
273- }
274258 }
275259 }
276260}
0 commit comments