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() {
118 public void startInBackground() {
119 new Thread(this).start();
125 * Scans all available files in the project’s local path and emits an event
128 * @see FileScannerListener#fileScannerFinished(FileScanner)
132 files = new ArrayList<ScannedFile>();
136 scanFiles(new File(project.getLocalPath()), files);
137 Collections.sort(files);
138 } catch (IOException ioe1) {
141 fireFileScannerFinished();
145 * Returns whether there was an error scanning for files.
147 * @return <code>true</code> if there was an error, <code>false</code>
150 public boolean isError() {
155 * Returns the list of found files.
157 * @return The list of found files
159 public List<ScannedFile> getFiles() {
164 * Recursively scans a directory and adds all found files to the given list.
167 * The directory to scan
169 * The list to which to add the found files
170 * @throws IOException
171 * if an I/O error occurs
173 private void scanFiles(File rootDir, List<ScannedFile> fileList) throws IOException {
174 File[] files = rootDir.listFiles(new FileFilter() {
177 @SuppressWarnings("synthetic-access")
178 public boolean accept(File file) {
179 return !project.isIgnoreHiddenFiles() || !file.isHidden();
183 throw new IOException(I18n.getMessage("jsite.file-scanner.can-not-read-directory"));
185 for (File file : files) {
186 if (file.isDirectory()) {
187 scanFiles(file, fileList);
190 String filename = project.shortenFilename(file).replace('\\', '/');
191 String hash = hashFile(project.getLocalPath(), filename);
192 fileList.add(new ScannedFile(filename, hash));
193 lastFilename = filename;
198 * Hashes the given file.
201 * The path of the project
203 * The name of the file, relative to the project path
204 * @return The hash of the file
206 private static String hashFile(String path, String filename) {
207 InputStream fileInputStream = null;
208 DigestOutputStream digestOutputStream = null;
209 File file = new File(path, filename);
211 fileInputStream = new FileInputStream(file);
212 digestOutputStream = new DigestOutputStream(new NullOutputStream(), MessageDigest.getInstance("SHA-256"));
213 StreamCopier.copy(fileInputStream, digestOutputStream, file.length());
214 return toHex(digestOutputStream.getMessageDigest().digest());
215 } catch (NoSuchAlgorithmException nsae1) {
216 logger.log(Level.WARNING, "Could not get SHA-256 digest!", nsae1);
217 } catch (IOException ioe1) {
218 logger.log(Level.WARNING, "Could not read file!", ioe1);
220 Closer.close(digestOutputStream);
221 Closer.close(fileInputStream);
223 return toHex(new byte[32]);
227 * Converts the given byte array into a hexadecimal string.
230 * The array to convert
231 * @return The hexadecimal string
233 private static String toHex(byte[] array) {
234 StringBuilder hexString = new StringBuilder(array.length * 2);
235 for (byte b : array) {
236 hexString.append("0123456789abcdef".charAt((b >>> 4) & 0x0f)).append("0123456789abcdef".charAt(b & 0xf));
238 return hexString.toString();
242 * Container for a scanned file, consisting of the name of the file and its
245 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
247 public static class ScannedFile implements Comparable<ScannedFile> {
249 /** The name of the file. */
250 private final String filename;
252 /** The hash of the file. */
253 private final String hash;
256 * Creates a new scanned file.
259 * The name of the file
261 * The hash of the file
263 public ScannedFile(String filename, String hash) {
264 this.filename = filename;
273 * Returns the name of the file.
275 * @return The name of the file
277 public String getFilename() {
282 * Returns the hash of the file.
284 * @return The hash of the file
286 public String getHash() {
298 public int hashCode() {
299 return filename.hashCode();
306 public boolean equals(Object obj) {
307 return filename.equals(obj);
314 public String toString() {
319 // COMPARABLE METHODS
326 public int compareTo(ScannedFile scannedFile) {
327 return filename.compareTo(scannedFile.filename);