Skip to content

Commit c12ec72

Browse files
authored
Merge pull request #3630 from dizzzz/improvements/misc
Fixing some resource management issues
2 parents 78a4369 + 23cdcf0 commit c12ec72

File tree

2 files changed

+51
-56
lines changed

2 files changed

+51
-56
lines changed

exist-ant/src/main/java/org/exist/ant/XMLDBExtractTask.java

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
*/
5454
public class XMLDBExtractTask extends AbstractXMLDBTask {
5555
private String resource = null;
56-
private File destFile = null;
57-
private File destDir = null;
56+
private Path destFile = null;
57+
private Path destDir = null;
5858
private boolean createdirectories = false;
5959
private boolean subcollections = false;
6060
private boolean overwrite = false;
@@ -81,7 +81,7 @@ public void execute() throws BuildException {
8181
if ((resource != null) && (destDir == null)) {
8282

8383
// extraction of a single resource
84-
log("Extracting resource: " + resource + " to " + destFile.getAbsolutePath(), Project.MSG_INFO);
84+
log("Extracting resource: " + resource + " to " + destFile.toAbsolutePath().toString(), Project.MSG_INFO);
8585
final Resource res = base.getResource(resource);
8686

8787
if (res == null) {
@@ -139,23 +139,23 @@ public void execute() throws BuildException {
139139
private void extractResources(final Collection base, final String path) throws XMLDBException, IOException {
140140
final String[] resources = base.listResources();
141141
if (resources != null) {
142-
File dir = destDir;
142+
Path dir = destDir;
143143

144-
log("Extracting to directory " + destDir.getAbsolutePath(), Project.MSG_DEBUG);
144+
log("Extracting to directory " + destDir.toAbsolutePath().toString(), Project.MSG_DEBUG);
145145

146146
if (path != null) {
147-
dir = new File(destDir, path);
147+
dir = destDir.resolve(path);
148148
}
149149

150150
for (final String resource : resources) {
151151
final Resource res = base.getResource(resource);
152152
log("Extracting resource: " + res.getId(), Project.MSG_DEBUG);
153153

154-
if (!dir.exists() && createdirectories) {
155-
dir.mkdirs();
154+
if (Files.notExists(dir) && createdirectories) {
155+
Files.createDirectories(dir);
156156
}
157157

158-
if (dir.exists()) {
158+
if (Files.exists(dir)) {
159159
writeResource(res, dir);
160160
}
161161

@@ -181,18 +181,18 @@ private void extractSubCollections(final Collection base, final String path) thr
181181

182182
if (col != null) {
183183
log("Extracting collection: " + col.getName(), Project.MSG_DEBUG);
184-
File dir = destDir;
184+
Path dir = destDir;
185185
final String subdir;
186186

187187
if (path != null) {
188-
dir = new File(destDir, path + File.separator + childCol);
188+
dir = destDir.resolve(path).resolve(childCol);
189189
subdir = path + File.separator + childCol;
190190
} else {
191191
subdir = childCol;
192192
}
193193

194-
if (!dir.exists() && createdirectories) {
195-
dir.mkdirs();
194+
if (Files.notExists(dir) && createdirectories) {
195+
Files.createDirectories(dir);
196196
}
197197

198198
extractResources(col, subdir);
@@ -214,7 +214,7 @@ private void extractSubCollections(final Collection base, final String path) thr
214214
* @throws XMLDBException if a database error occurs
215215
* @throws IOException if an I/O error occurs
216216
*/
217-
private void writeResource(final Resource res, final File dest) throws XMLDBException, IOException {
217+
private void writeResource(final Resource res, final Path dest) throws XMLDBException, IOException {
218218
if (res instanceof XMLResource) {
219219
writeXMLResource((XMLResource) res, dest);
220220
} else if (res instanceof ExtendedResource) {
@@ -231,11 +231,11 @@ private void writeResource(final Resource res, final File dest) throws XMLDBExce
231231
* @throws XMLDBException if a database error occurs
232232
* @throws IOException if an I/O error occurs
233233
*/
234-
private void writeXMLResource(final XMLResource res, final File dest) throws IOException, XMLDBException {
234+
private void writeXMLResource(final XMLResource res, final Path dest) throws IOException, XMLDBException {
235235
if (createdirectories) {
236-
final File parentDir = new File(dest.getParent());
237-
if (!parentDir.exists()) {
238-
parentDir.mkdirs();
236+
final Path parentDir = dest.getParent();
237+
if (Files.notExists(parentDir)) {
238+
Files.createDirectories(parentDir);
239239
}
240240
}
241241

@@ -245,14 +245,14 @@ private void writeXMLResource(final XMLResource res, final File dest) throws IOE
245245
final SAXSerializer serializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
246246

247247
try (final Writer writer = getWriter(res, dest)) {
248-
log("Writing resource " + res.getId() + " to destination " + dest.getAbsolutePath(), Project.MSG_DEBUG);
248+
log("Writing resource " + res.getId() + " to destination " + dest.toAbsolutePath().toString(), Project.MSG_DEBUG);
249249
serializer.setOutput(writer, outputProperties);
250250
res.getContentAsSAX(serializer);
251251
SerializerPool.getInstance().returnObject(serializer);
252252
}
253253

254254
} else {
255-
final String msg = "Destination xml file " + ((dest != null) ? (dest.getAbsolutePath() + " ") : "") + "exists. Use " + "overwrite property to overwrite this file.";
255+
final String msg = "Destination xml file " + ((dest != null) ? (dest.toAbsolutePath().toString() + " ") : "") + "exists. Use " + "overwrite property to overwrite this file.";
256256

257257
if (failonerror) {
258258
throw (new BuildException(msg));
@@ -262,14 +262,14 @@ private void writeXMLResource(final XMLResource res, final File dest) throws IOE
262262
}
263263
}
264264

265-
private Writer getWriter(XMLResource res, File dest) throws XMLDBException, IOException {
265+
private Writer getWriter(XMLResource res, Path dest) throws XMLDBException, IOException {
266266
final Writer writer;
267-
if (dest.isDirectory()) {
268-
final Path file = dest.toPath().resolve(res.getId());
267+
if (Files.isDirectory(dest)) {
268+
final Path file = dest.resolve(res.getId());
269269
writer = Files.newBufferedWriter(file, UTF_8);
270270

271271
} else {
272-
writer = Files.newBufferedWriter(destFile.toPath(), UTF_8);
272+
writer = Files.newBufferedWriter(destFile, UTF_8);
273273
}
274274
return writer;
275275
}
@@ -283,28 +283,27 @@ private Writer getWriter(XMLResource res, File dest) throws XMLDBException, IOEx
283283
* @throws XMLDBException if a database error occurs
284284
* @throws IOException if an I/O error occurs
285285
*/
286-
private void writeBinaryResource(final Resource res, File dest) throws XMLDBException, IOException {
286+
private void writeBinaryResource(final Resource res, Path dest) throws XMLDBException, IOException {
287287
if (createdirectories == true) {
288-
final File parentDir = new File(dest.getParent());
289-
if (!parentDir.exists()) {
290-
parentDir.mkdirs();
288+
final Path parentDir = dest.getParent();
289+
if (Files.notExists(parentDir)) {
290+
Files.createDirectories(parentDir);
291291
}
292292
}
293293

294294
//dest != null && ( !dest.exists() ||
295295
if (dest != null || overwrite == true) {
296-
if (dest.isDirectory()) {
296+
if (Files.isDirectory(dest)) {
297297
final String fname = res.getId();
298-
dest = new File(dest, fname);
298+
dest = dest.resolve(fname);
299299
}
300-
final FileOutputStream os;
301-
os = new FileOutputStream(dest);
302-
303-
((ExtendedResource) res).getContentIntoAStream(os);
304300

301+
try(final OutputStream os = Files.newOutputStream(dest)) {
302+
((ExtendedResource) res).getContentIntoAStream(os);
303+
}
305304

306305
} else {
307-
final String msg = "Dest binary file " + ((dest != null) ? (dest.getAbsolutePath() + " ") : "") + "exists. Use " + "overwrite property to overwrite file.";
306+
final String msg = "Dest binary file " + ((dest != null) ? (dest.toAbsolutePath().toString() + " ") : "") + "exists. Use " + "overwrite property to overwrite file.";
308307

309308
if (failonerror) {
310309
throw (new BuildException(msg));
@@ -319,11 +318,11 @@ public void setResource(final String resource) {
319318
}
320319

321320
public void setDestFile(final File destFile) {
322-
this.destFile = destFile;
321+
this.destFile = destFile.toPath();
323322
}
324323

325324
public void setDestDir(final File destDir) {
326-
this.destDir = destDir;
325+
this.destDir = destDir.toPath();
327326
}
328327

329328

exist-core/src/main/java/org/exist/client/DocumentView.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,11 @@
3030
import java.awt.event.KeyEvent;
3131
import java.awt.event.WindowAdapter;
3232
import java.awt.event.WindowEvent;
33-
import java.io.File;
34-
import java.io.FileOutputStream;
35-
import java.io.IOException;
36-
import java.io.OutputStreamWriter;
37-
import java.io.PrintWriter;
38-
import java.io.StringWriter;
33+
import java.io.*;
3934
import java.net.URISyntaxException;
4035
import java.net.URL;
4136
import java.nio.charset.Charset;
37+
import java.nio.file.Files;
4238
import java.nio.file.Paths;
4339
import java.util.Observable;
4440
import java.util.Observer;
@@ -79,6 +75,8 @@
7975
import org.xmldb.api.base.XMLDBException;
8076
import org.xmldb.api.modules.XMLResource;
8177

78+
import static java.nio.charset.StandardCharsets.UTF_8;
79+
8280
class DocumentView extends JFrame {
8381

8482
private static final long serialVersionUID = 1L;
@@ -410,33 +408,31 @@ private void saveAs() {
410408
}
411409

412410
private void export() throws XMLDBException {
413-
final String workDir = properties.getProperty("working-dir", System //$NON-NLS-1$
414-
.getProperty("user.dir")); //$NON-NLS-1$
411+
final String workDir = properties.getProperty("working-dir", System.getProperty("user.dir"));
415412
final JFileChooser chooser = new JFileChooser(workDir);
416413
chooser.setMultiSelectionEnabled(false);
417414
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
418415
chooser.setSelectedFile(Paths.get(resource.getId()).toFile());
419-
if (chooser.showDialog(this, Messages.getString("DocumentView.44")) == JFileChooser.APPROVE_OPTION) { //$NON-NLS-1$
416+
417+
if (chooser.showDialog(this, Messages.getString("DocumentView.44")) == JFileChooser.APPROVE_OPTION) {
420418
final File file = chooser.getSelectedFile();
421419
if (file.exists()
422420
&& JOptionPane.showConfirmDialog(this,
423-
Messages.getString("DocumentView.45"), Messages.getString("DocumentView.46"), //$NON-NLS-1$ //$NON-NLS-2$
421+
Messages.getString("DocumentView.45"), Messages.getString("DocumentView.46"),
424422
JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
425423
return;
426424
}
427-
try {
428-
final OutputStreamWriter writer = new OutputStreamWriter(
429-
new FileOutputStream(file), Charset.forName(properties
430-
.getProperty("encoding"))); //$NON-NLS-1$
425+
426+
final Charset encoding = Charset.forName(properties.getProperty("encoding"));
427+
try (final Writer writer = Files.newBufferedWriter(file.toPath(), encoding)) {
431428
writer.write(text.getText());
432-
writer.close();
429+
433430
} catch (final IOException e) {
434-
ClientFrame.showErrorMessage(Messages.getString("DocumentView.48") //$NON-NLS-1$
435-
+ e.getMessage(), e);
431+
ClientFrame.showErrorMessage(Messages.getString("DocumentView.48") + e.getMessage(), e);
436432
}
433+
437434
final File selectedDir = chooser.getCurrentDirectory();
438-
properties
439-
.setProperty("working-dir", selectedDir.getAbsolutePath()); //$NON-NLS-1$
435+
properties.setProperty("working-dir", selectedDir.getAbsolutePath());
440436
}
441437
}
442438

0 commit comments

Comments
 (0)