2 * jSite - FileScanner.java - Copyright © 2006–2014 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.gui;
22 import java.io.FileFilter;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.security.DigestOutputStream;
27 import java.security.MessageDigest;
28 import java.security.NoSuchAlgorithmException;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
35 import net.pterodactylus.util.io.Closer;
36 import net.pterodactylus.util.io.NullOutputStream;
37 import net.pterodactylus.util.io.StreamCopier;
38 import de.todesbaum.jsite.application.Project;
39 import de.todesbaum.jsite.i18n.I18n;
42 * Scans the local path of a project anychronously and returns the list of found
45 * @see Project#getLocalPath()
46 * @see FileScannerListener#fileScannerFinished(FileScanner)
47 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
49 public class FileScanner implements Runnable {
52 private final static Logger logger = Logger.getLogger(FileScanner.class.getName());
54 /** The list of listeners. */
55 private final List<FileScannerListener> fileScannerListeners = new ArrayList<FileScannerListener>();
57 /** The project to scan. */
58 private final Project project;
60 /** The list of found files. */
61 private List<ScannedFile> files;
63 /** Wether there was an error. */
64 private boolean error = false;
66 /** The name of the last file scanned. */
67 private String lastFilename;
70 * Creates a new file scanner for the given project.
73 * The project whose files to scan
75 public FileScanner(Project project) {
76 this.project = project;
80 * Adds the given listener to the list of listeners.
82 * @param fileScannerListener
85 public void addFileScannerListener(FileScannerListener fileScannerListener) {
86 fileScannerListeners.add(fileScannerListener);
90 * Removes the given listener from the list of listeners.
92 * @param fileScannerListener
93 * The listener to remove
95 public void removeFileScannerListener(FileScannerListener fileScannerListener) {
96 fileScannerListeners.remove(fileScannerListener);
100 * Notifies all listeners that the file scan finished.
102 protected void fireFileScannerFinished() {
103 for (FileScannerListener fileScannerListener : new ArrayList<FileScannerListener>(fileScannerListeners)) {
104 fileScannerListener.fileScannerFinished(this);
109 * Returns the name of the last file scanned.
111 * @return The name of the last file scanned, or {@code null} if there was
112 * no file scanned yet
114 public String getLastFilename() {
121 * Scans all available files in the project’s local path and emits an event
124 * @see FileScannerListener#fileScannerFinished(FileScanner)
128 files = new ArrayList<ScannedFile>();
132 scanFiles(new File(project.getLocalPath()), files);
133 Collections.sort(files);
134 } catch (IOException ioe1) {
137 fireFileScannerFinished();
141 * Returns whether there was an error scanning for files.
143 * @return <code>true</code> if there was an error, <code>false</code>
146 public boolean isError() {
151 * Returns the list of found files.
153 * @return The list of found files
155 public List<ScannedFile> getFiles() {
160 * Recursively scans a directory and adds all found files to the given list.
163 * The directory to scan
165 * The list to which to add the found files
166 * @throws IOException
167 * if an I/O error occurs
169 private void scanFiles(File rootDir, List<ScannedFile> fileList) throws IOException {
170 File[] files = rootDir.listFiles(new FileFilter() {
173 @SuppressWarnings("synthetic-access")
174 public boolean accept(File file) {
175 return !project.isIgnoreHiddenFiles() || !file.isHidden();
179 throw new IOException(I18n.getMessage("jsite.file-scanner.can-not-read-directory"));
181 for (File file : files) {
182 if (file.isDirectory()) {
183 scanFiles(file, fileList);
186 String filename = project.shortenFilename(file).replace('\\', '/');
187 String hash = hashFile(project.getLocalPath(), filename);
188 fileList.add(new ScannedFile(filename, hash));
189 lastFilename = filename;
194 * Hashes the given file.
197 * The path of the project
199 * The name of the file, relative to the project path
200 * @return The hash of the file
202 private static String hashFile(String path, String filename) {
203 InputStream fileInputStream = null;
204 DigestOutputStream digestOutputStream = null;
205 File file = new File(path, filename);
207 fileInputStream = new FileInputStream(file);
208 digestOutputStream = new DigestOutputStream(new NullOutputStream(), MessageDigest.getInstance("SHA-256"));
209 StreamCopier.copy(fileInputStream, digestOutputStream, file.length());
210 return toHex(digestOutputStream.getMessageDigest().digest());
211 } catch (NoSuchAlgorithmException nsae1) {
212 logger.log(Level.WARNING, "Could not get SHA-256 digest!", nsae1);
213 } catch (IOException ioe1) {
214 logger.log(Level.WARNING, "Could not read file!", ioe1);
216 Closer.close(digestOutputStream);
217 Closer.close(fileInputStream);
219 return toHex(new byte[32]);
223 * Converts the given byte array into a hexadecimal string.
226 * The array to convert
227 * @return The hexadecimal string
229 private static String toHex(byte[] array) {
230 StringBuilder hexString = new StringBuilder(array.length * 2);
231 for (byte b : array) {
232 hexString.append("0123456789abcdef".charAt((b >>> 4) & 0x0f)).append("0123456789abcdef".charAt(b & 0xf));
234 return hexString.toString();
238 * Container for a scanned file, consisting of the name of the file and its
241 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
243 public static class ScannedFile implements Comparable<ScannedFile> {
245 /** The name of the file. */
246 private final String filename;
248 /** The hash of the file. */
249 private final String hash;
252 * Creates a new scanned file.
255 * The name of the file
257 * The hash of the file
259 public ScannedFile(String filename, String hash) {
260 this.filename = filename;
269 * Returns the name of the file.
271 * @return The name of the file
273 public String getFilename() {
278 * Returns the hash of the file.
280 * @return The hash of the file
282 public String getHash() {
294 public int hashCode() {
295 return filename.hashCode();
302 public boolean equals(Object obj) {
303 return filename.equals(obj);
310 public String toString() {
315 // COMPARABLE METHODS
322 public int compareTo(ScannedFile scannedFile) {
323 return filename.compareTo(scannedFile.filename);