View Javadoc
1   package fr.ifremer.tutti.ui.swing.updater;
2   
3   /*
4    * #%L
5    * Tutti :: UI Updater
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import java.io.IOException;
28  import java.nio.file.FileVisitResult;
29  import java.nio.file.Files;
30  import java.nio.file.Path;
31  import java.nio.file.PathMatcher;
32  import java.nio.file.SimpleFileVisitor;
33  import java.nio.file.attribute.BasicFileAttributes;
34  
35  /**
36   * Created on 1/26/15.
37   *
38   * @author Tony Chemit - chemit@codelutin.com
39   * @since 3.12
40   */
41  public class DeleteHelper {
42  
43      public static void deleteDirectories(Path path, String glob) throws IOException {
44  
45          PathMatcher matcher = path.getFileSystem().getPathMatcher("glob:" + glob);
46          if (Files.isDirectory(path)) {
47  
48              DeleteDirectories deleteDirectories = new DeleteDirectories(matcher);
49              Files.walkFileTree(path, deleteDirectories);
50          }
51  
52      }
53  
54      public static void deleteFiles(Path path, String glob) throws IOException {
55          if (Files.isDirectory(path)) {
56  
57              PathMatcher matcher = path.getFileSystem().getPathMatcher("glob:" + glob);
58              DeleteFiles deleteFiles = new DeleteFiles(matcher);
59              Files.walkFileTree(path, deleteFiles);
60          }
61  
62      }
63  
64      public static void deleteDirectory(Path path) throws IOException {
65          if (Files.isDirectory(path)) {
66              DeleteDirectory deleteDirectory = new DeleteDirectory();
67              Files.walkFileTree(path, deleteDirectory);
68          }
69      }
70  
71      public static void deleteDirectoryOnExit(Path path) throws IOException {
72          if (Files.isDirectory(path)) {
73              DeleteDirectoryOnExit deleteDirectory = new DeleteDirectoryOnExit();
74              Files.walkFileTree(path, deleteDirectory);
75          }
76      }
77  
78      /**
79       * To delete the given directory.
80       */
81      public static class DeleteDirectory extends SimpleFileVisitor<Path> {
82  
83          @Override
84          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
85              Files.deleteIfExists(file);
86              return FileVisitResult.CONTINUE;
87          }
88  
89          @Override
90          public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
91              System.out.println("Delete directory: " + dir);
92              Files.deleteIfExists(dir);
93              return FileVisitResult.CONTINUE;
94          }
95  
96      }
97  
98      /**
99       * To delete the given directory on exit.
100      */
101     public static class DeleteDirectoryOnExit extends SimpleFileVisitor<Path> {
102 
103         @Override
104         public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
105             System.out.println("Delete directory on exit: " + dir);
106             dir.toFile().deleteOnExit();
107             return FileVisitResult.CONTINUE;
108         }
109 
110         @Override
111         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
112             file.toFile().deleteOnExit();
113             return FileVisitResult.CONTINUE;
114         }
115 
116     }
117 
118     /**
119      * To delete all files that are matching the given matcher.
120      */
121     public static class DeleteFiles extends SimpleFileVisitor<Path> {
122 
123         private final PathMatcher matcher;
124 
125         protected DeleteFiles(PathMatcher matcher) {
126             this.matcher = matcher;
127         }
128 
129         @Override
130         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
131 
132             // If the file name matches the glob or if no matcher, delete the file
133             if (matcher.matches(file.getFileName())) {
134                 System.out.println("Delete file: " + file);
135                 Files.deleteIfExists(file);
136             }
137             return FileVisitResult.CONTINUE;
138         }
139 
140     }
141 
142     /**
143      * To delete all directories which names are matching the given matcher.
144      */
145     public static class DeleteDirectories extends SimpleFileVisitor<Path> {
146 
147         private final PathMatcher matcher;
148 
149         protected DeleteDirectories(PathMatcher matcher) {
150             this.matcher = matcher;
151         }
152 
153         private Path deleteDir;
154 
155         @Override
156         public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
157 
158             if (deleteDir == null) {
159                 if (matcher.matches(dir.getFileName())) {
160                     System.out.println("Delete directory: " + dir);
161                     deleteDir = dir;
162                 }
163             }
164             return FileVisitResult.CONTINUE;
165 
166         }
167 
168         @Override
169         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
170 
171             if (deleteDir != null) {
172                 Files.deleteIfExists(file);
173             }
174             return FileVisitResult.CONTINUE;
175 
176         }
177 
178         @Override
179         public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
180 
181             if (deleteDir != null) {
182 
183                 Files.deleteIfExists(dir);
184 
185                 if (dir.equals(deleteDir)) {
186                     deleteDir = null;
187                 }
188 
189             }
190 
191             return FileVisitResult.CONTINUE;
192 
193         }
194 
195     }
196 }