53
53
*/
54
54
public class XMLDBExtractTask extends AbstractXMLDBTask {
55
55
private String resource = null ;
56
- private File destFile = null ;
57
- private File destDir = null ;
56
+ private Path destFile = null ;
57
+ private Path destDir = null ;
58
58
private boolean createdirectories = false ;
59
59
private boolean subcollections = false ;
60
60
private boolean overwrite = false ;
@@ -81,7 +81,7 @@ public void execute() throws BuildException {
81
81
if ((resource != null ) && (destDir == null )) {
82
82
83
83
// 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 );
85
85
final Resource res = base .getResource (resource );
86
86
87
87
if (res == null ) {
@@ -139,23 +139,23 @@ public void execute() throws BuildException {
139
139
private void extractResources (final Collection base , final String path ) throws XMLDBException , IOException {
140
140
final String [] resources = base .listResources ();
141
141
if (resources != null ) {
142
- File dir = destDir ;
142
+ Path dir = destDir ;
143
143
144
- log ("Extracting to directory " + destDir .getAbsolutePath (), Project .MSG_DEBUG );
144
+ log ("Extracting to directory " + destDir .toAbsolutePath (). toString (), Project .MSG_DEBUG );
145
145
146
146
if (path != null ) {
147
- dir = new File ( destDir , path );
147
+ dir = destDir . resolve ( path );
148
148
}
149
149
150
150
for (final String resource : resources ) {
151
151
final Resource res = base .getResource (resource );
152
152
log ("Extracting resource: " + res .getId (), Project .MSG_DEBUG );
153
153
154
- if (! dir . exists ( ) && createdirectories ) {
155
- dir . mkdirs ( );
154
+ if (Files . notExists ( dir ) && createdirectories ) {
155
+ Files . createDirectories ( dir );
156
156
}
157
157
158
- if (dir .exists ()) {
158
+ if (Files .exists (dir )) {
159
159
writeResource (res , dir );
160
160
}
161
161
@@ -181,18 +181,18 @@ private void extractSubCollections(final Collection base, final String path) thr
181
181
182
182
if (col != null ) {
183
183
log ("Extracting collection: " + col .getName (), Project .MSG_DEBUG );
184
- File dir = destDir ;
184
+ Path dir = destDir ;
185
185
final String subdir ;
186
186
187
187
if (path != null ) {
188
- dir = new File ( destDir , path + File . separator + childCol );
188
+ dir = destDir . resolve ( path ). resolve ( childCol );
189
189
subdir = path + File .separator + childCol ;
190
190
} else {
191
191
subdir = childCol ;
192
192
}
193
193
194
- if (! dir . exists ( ) && createdirectories ) {
195
- dir . mkdirs ( );
194
+ if (Files . notExists ( dir ) && createdirectories ) {
195
+ Files . createDirectories ( dir );
196
196
}
197
197
198
198
extractResources (col , subdir );
@@ -214,7 +214,7 @@ private void extractSubCollections(final Collection base, final String path) thr
214
214
* @throws XMLDBException if a database error occurs
215
215
* @throws IOException if an I/O error occurs
216
216
*/
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 {
218
218
if (res instanceof XMLResource ) {
219
219
writeXMLResource ((XMLResource ) res , dest );
220
220
} else if (res instanceof ExtendedResource ) {
@@ -231,11 +231,11 @@ private void writeResource(final Resource res, final File dest) throws XMLDBExce
231
231
* @throws XMLDBException if a database error occurs
232
232
* @throws IOException if an I/O error occurs
233
233
*/
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 {
235
235
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 );
239
239
}
240
240
}
241
241
@@ -245,14 +245,14 @@ private void writeXMLResource(final XMLResource res, final File dest) throws IOE
245
245
final SAXSerializer serializer = (SAXSerializer ) SerializerPool .getInstance ().borrowObject (SAXSerializer .class );
246
246
247
247
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 );
249
249
serializer .setOutput (writer , outputProperties );
250
250
res .getContentAsSAX (serializer );
251
251
SerializerPool .getInstance ().returnObject (serializer );
252
252
}
253
253
254
254
} 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." ;
256
256
257
257
if (failonerror ) {
258
258
throw (new BuildException (msg ));
@@ -262,14 +262,14 @@ private void writeXMLResource(final XMLResource res, final File dest) throws IOE
262
262
}
263
263
}
264
264
265
- private Writer getWriter (XMLResource res , File dest ) throws XMLDBException , IOException {
265
+ private Writer getWriter (XMLResource res , Path dest ) throws XMLDBException , IOException {
266
266
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 ());
269
269
writer = Files .newBufferedWriter (file , UTF_8 );
270
270
271
271
} else {
272
- writer = Files .newBufferedWriter (destFile . toPath () , UTF_8 );
272
+ writer = Files .newBufferedWriter (destFile , UTF_8 );
273
273
}
274
274
return writer ;
275
275
}
@@ -283,28 +283,27 @@ private Writer getWriter(XMLResource res, File dest) throws XMLDBException, IOEx
283
283
* @throws XMLDBException if a database error occurs
284
284
* @throws IOException if an I/O error occurs
285
285
*/
286
- private void writeBinaryResource (final Resource res , File dest ) throws XMLDBException , IOException {
286
+ private void writeBinaryResource (final Resource res , Path dest ) throws XMLDBException , IOException {
287
287
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 );
291
291
}
292
292
}
293
293
294
294
//dest != null && ( !dest.exists() ||
295
295
if (dest != null || overwrite == true ) {
296
- if (dest .isDirectory ()) {
296
+ if (Files .isDirectory (dest )) {
297
297
final String fname = res .getId ();
298
- dest = new File ( dest , fname );
298
+ dest = dest . resolve ( fname );
299
299
}
300
- final FileOutputStream os ;
301
- os = new FileOutputStream (dest );
302
-
303
- ((ExtendedResource ) res ).getContentIntoAStream (os );
304
300
301
+ try (final OutputStream os = Files .newOutputStream (dest )) {
302
+ ((ExtendedResource ) res ).getContentIntoAStream (os );
303
+ }
305
304
306
305
} 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." ;
308
307
309
308
if (failonerror ) {
310
309
throw (new BuildException (msg ));
@@ -319,11 +318,11 @@ public void setResource(final String resource) {
319
318
}
320
319
321
320
public void setDestFile (final File destFile ) {
322
- this .destFile = destFile ;
321
+ this .destFile = destFile . toPath () ;
323
322
}
324
323
325
324
public void setDestDir (final File destDir ) {
326
- this .destDir = destDir ;
325
+ this .destDir = destDir . toPath () ;
327
326
}
328
327
329
328
0 commit comments