2009-01-24 19:08:57 -05:00
|
|
|
|
|
|
|
package net.sourceforge.tuned;
|
|
|
|
|
|
|
|
|
2011-09-04 19:50:54 -04:00
|
|
|
import java.io.BufferedInputStream;
|
2009-01-24 19:08:57 -05:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileFilter;
|
2009-10-20 17:16:34 -04:00
|
|
|
import java.io.FileInputStream;
|
2011-09-09 10:50:01 -04:00
|
|
|
import java.io.FileOutputStream;
|
2009-10-20 17:16:34 -04:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2011-09-04 19:50:54 -04:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.Reader;
|
2011-12-19 21:37:36 -05:00
|
|
|
import java.io.StringWriter;
|
2011-09-09 10:50:01 -04:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.channels.FileChannel;
|
2011-09-14 14:13:34 -04:00
|
|
|
import java.nio.charset.Charset;
|
2009-02-03 15:36:57 -05:00
|
|
|
import java.util.ArrayList;
|
2009-06-19 18:35:39 -04:00
|
|
|
import java.util.Collection;
|
2011-07-03 21:14:54 -04:00
|
|
|
import java.util.HashMap;
|
2009-10-28 21:22:00 -04:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedList;
|
2009-02-03 15:36:57 -05:00
|
|
|
import java.util.List;
|
2011-07-03 21:14:54 -04:00
|
|
|
import java.util.Map;
|
2011-11-26 04:50:31 -05:00
|
|
|
import java.util.SortedMap;
|
|
|
|
import java.util.TreeMap;
|
2009-07-26 12:54:24 -04:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2009-01-24 19:08:57 -05:00
|
|
|
|
2011-12-19 21:37:36 -05:00
|
|
|
import javax.xml.transform.OutputKeys;
|
|
|
|
import javax.xml.transform.Transformer;
|
|
|
|
import javax.xml.transform.TransformerException;
|
|
|
|
import javax.xml.transform.TransformerFactory;
|
|
|
|
import javax.xml.transform.dom.DOMSource;
|
|
|
|
import javax.xml.transform.stream.StreamResult;
|
|
|
|
|
|
|
|
import org.w3c.dom.Document;
|
|
|
|
|
2011-09-04 19:50:54 -04:00
|
|
|
import com.ibm.icu.text.CharsetDetector;
|
|
|
|
import com.ibm.icu.text.CharsetMatch;
|
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
|
|
|
|
public final class FileUtilities {
|
|
|
|
|
2012-02-10 11:43:09 -05:00
|
|
|
public static File moveRename(File source, File destination) throws IOException {
|
2011-09-09 10:50:01 -04:00
|
|
|
// resolve destination
|
2012-03-07 09:26:47 -05:00
|
|
|
destination = resolveDestination(source, destination);
|
2011-09-09 10:50:01 -04:00
|
|
|
|
2012-02-10 11:43:09 -05:00
|
|
|
if (source.isDirectory()) { // move folder
|
|
|
|
moveFolderIO(source, destination);
|
|
|
|
} else { // move file
|
|
|
|
try {
|
|
|
|
moveFileNIO2(source, destination);
|
|
|
|
} catch (LinkageError e) {
|
|
|
|
moveFileIO(source, destination);
|
|
|
|
}
|
2011-09-09 10:50:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2012-02-10 11:43:09 -05:00
|
|
|
private static void moveFileNIO2(File source, File destination) throws IOException {
|
2011-12-26 21:26:40 -05:00
|
|
|
java.nio.file.Files.move(source.toPath(), destination.toPath());
|
2011-11-20 16:32:24 -05:00
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2012-02-10 11:43:09 -05:00
|
|
|
private static void moveFileIO(File source, File destination) throws IOException {
|
2012-02-24 08:39:32 -05:00
|
|
|
// use "copy and delete" as fallback if standard rename fails
|
|
|
|
org.apache.commons.io.FileUtils.moveFile(source, destination);
|
2012-02-10 11:43:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void moveFolderIO(File source, File destination) throws IOException {
|
2012-02-24 08:39:32 -05:00
|
|
|
// use "copy and delete" as fallback if standard move/rename fails
|
|
|
|
org.apache.commons.io.FileUtils.moveDirectory(source, destination);
|
2011-11-20 16:32:24 -05:00
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2012-02-23 00:01:06 -05:00
|
|
|
public static File copyAs(File source, File destination) throws IOException {
|
2012-03-07 09:26:47 -05:00
|
|
|
// resolve destination
|
|
|
|
destination = resolveDestination(source, destination);
|
|
|
|
|
|
|
|
if (source.isDirectory()) { // copy folder
|
|
|
|
org.apache.commons.io.FileUtils.copyDirectory(source, destination);
|
|
|
|
} else { // copy file
|
|
|
|
org.apache.commons.io.FileUtils.copyFile(source, destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static File resolveDestination(File source, File destination) throws IOException {
|
2012-02-23 00:01:06 -05:00
|
|
|
// resolve destination
|
|
|
|
if (!destination.isAbsolute()) {
|
|
|
|
// same folder, different name
|
|
|
|
destination = new File(source.getParentFile(), destination.getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we that we can create the destination folder structure
|
|
|
|
File destinationFolder = destination.getParentFile();
|
|
|
|
|
|
|
|
// create parent folder if necessary
|
|
|
|
if (!destinationFolder.isDirectory() && !destinationFolder.mkdirs()) {
|
|
|
|
throw new IOException("Failed to create folder: " + destinationFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-09 10:50:01 -04:00
|
|
|
public static byte[] readFile(File source) throws IOException {
|
2009-10-20 17:16:34 -04:00
|
|
|
InputStream in = new FileInputStream(source);
|
|
|
|
|
|
|
|
try {
|
|
|
|
byte[] data = new byte[(int) source.length()];
|
|
|
|
|
|
|
|
int position = 0;
|
|
|
|
int read = 0;
|
|
|
|
|
|
|
|
while (position < data.length && (read = in.read(data, position, data.length - position)) >= 0) {
|
|
|
|
position += read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
} finally {
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-10-29 16:19:53 -04:00
|
|
|
public static String readAll(Reader source) throws IOException {
|
|
|
|
StringBuilder text = new StringBuilder();
|
|
|
|
char[] buffer = new char[2048];
|
|
|
|
|
|
|
|
int read = 0;
|
|
|
|
while ((read = source.read(buffer)) >= 0) {
|
|
|
|
text.append(buffer, 0, read);
|
|
|
|
}
|
|
|
|
|
|
|
|
return text.toString();
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-09-09 10:50:01 -04:00
|
|
|
public static void writeFile(ByteBuffer data, File destination) throws IOException {
|
|
|
|
FileChannel fileChannel = new FileOutputStream(destination).getChannel();
|
|
|
|
|
|
|
|
try {
|
|
|
|
fileChannel.write(data);
|
|
|
|
} finally {
|
|
|
|
fileChannel.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-09-04 19:50:54 -04:00
|
|
|
public static Reader createTextReader(File file) throws IOException {
|
|
|
|
CharsetDetector detector = new CharsetDetector();
|
|
|
|
detector.setDeclaredEncoding("UTF-8"); // small boost for UTF-8 as default encoding
|
|
|
|
detector.setText(new BufferedInputStream(new FileInputStream(file)));
|
|
|
|
|
|
|
|
CharsetMatch charset = detector.detect();
|
|
|
|
if (charset != null)
|
|
|
|
return charset.getReader();
|
|
|
|
|
|
|
|
// assume UTF-8 by default
|
|
|
|
return new InputStreamReader(new FileInputStream(file), "UTF-8");
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-09-14 14:13:34 -04:00
|
|
|
public static String getText(ByteBuffer data) throws IOException {
|
|
|
|
CharsetDetector detector = new CharsetDetector();
|
|
|
|
detector.setDeclaredEncoding("UTF-8"); // small boost for UTF-8 as default encoding
|
|
|
|
detector.setText(new ByteBufferInputStream(data));
|
|
|
|
|
|
|
|
CharsetMatch charset = detector.detect();
|
2011-11-21 07:24:51 -05:00
|
|
|
if (charset != null) {
|
|
|
|
try {
|
|
|
|
return charset.getString();
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
throw new IOException("Failed to read text", e);
|
|
|
|
}
|
|
|
|
}
|
2011-09-14 14:13:34 -04:00
|
|
|
|
|
|
|
// assume UTF-8 by default
|
|
|
|
return Charset.forName("UTF-8").decode(data).toString();
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-07-26 12:54:24 -04:00
|
|
|
/**
|
|
|
|
* Pattern used for matching file extensions.
|
|
|
|
*
|
|
|
|
* e.g. "file.txt" -> match "txt", ".hidden" -> no match
|
|
|
|
*/
|
2009-08-10 07:46:24 -04:00
|
|
|
public static final Pattern EXTENSION = Pattern.compile("(?<=.[.])\\p{Alnum}+$");
|
2009-07-26 12:54:24 -04:00
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public static String getExtension(File file) {
|
2009-04-07 14:33:05 -04:00
|
|
|
if (file.isDirectory())
|
|
|
|
return null;
|
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
return getExtension(file.getName());
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public static String getExtension(String name) {
|
2009-08-10 07:46:24 -04:00
|
|
|
Matcher matcher = EXTENSION.matcher(name);
|
2009-01-24 19:08:57 -05:00
|
|
|
|
2009-07-26 12:54:24 -04:00
|
|
|
if (matcher.find()) {
|
|
|
|
// extension without leading '.'
|
|
|
|
return matcher.group();
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
|
|
|
|
2009-07-26 12:54:24 -04:00
|
|
|
// no extension
|
2009-01-24 19:08:57 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public static boolean hasExtension(File file, String... extensions) {
|
2009-08-10 07:46:24 -04:00
|
|
|
// avoid native call for speed, if possible
|
|
|
|
return hasExtension(file.getName(), extensions) && !file.isDirectory();
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public static boolean hasExtension(String filename, String... extensions) {
|
|
|
|
String extension = getExtension(filename);
|
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
for (String value : extensions) {
|
|
|
|
if ((extension == null && value == null) || (extension != null && extension.equalsIgnoreCase(value)))
|
|
|
|
return true;
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public static String getNameWithoutExtension(String name) {
|
2009-08-10 07:46:24 -04:00
|
|
|
Matcher matcher = EXTENSION.matcher(name);
|
2009-01-24 19:08:57 -05:00
|
|
|
|
2009-07-26 12:54:24 -04:00
|
|
|
if (matcher.find()) {
|
|
|
|
return name.substring(0, matcher.start() - 1);
|
|
|
|
}
|
2009-01-24 19:08:57 -05:00
|
|
|
|
|
|
|
// no extension, return given name
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public static String getName(File file) {
|
2012-07-16 07:24:34 -04:00
|
|
|
if (file.getName().isEmpty())
|
2009-01-24 19:08:57 -05:00
|
|
|
return getFolderName(file);
|
|
|
|
|
|
|
|
return getNameWithoutExtension(file.getName());
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public static String getFolderName(File file) {
|
|
|
|
String name = file.getName();
|
|
|
|
|
|
|
|
if (!name.isEmpty())
|
|
|
|
return name;
|
|
|
|
|
|
|
|
// file might be a drive (only has a path, but no name)
|
2012-07-14 15:51:46 -04:00
|
|
|
return replacePathSeparators(file.toString(), "");
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-11-25 14:43:24 -05:00
|
|
|
public static boolean isDerived(File derivate, File prime) {
|
|
|
|
return isDerived(getName(derivate), prime);
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-11-24 12:27:39 -05:00
|
|
|
public static boolean isDerived(String derivate, File prime) {
|
2012-02-14 09:16:13 -05:00
|
|
|
String base = getName(prime).trim().toLowerCase();
|
2011-11-24 12:27:39 -05:00
|
|
|
derivate = derivate.trim().toLowerCase();
|
|
|
|
return derivate.startsWith(base);
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-11-25 14:43:24 -05:00
|
|
|
public static boolean isDerivedByExtension(File derivate, File prime) {
|
|
|
|
return isDerivedByExtension(getName(derivate), prime);
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-11-24 12:27:39 -05:00
|
|
|
public static boolean isDerivedByExtension(String derivate, File prime) {
|
|
|
|
String base = getName(prime).trim().toLowerCase();
|
|
|
|
derivate = derivate.trim().toLowerCase();
|
|
|
|
|
|
|
|
if (derivate.equals(base))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
while (derivate.length() > base.length() && getExtension(derivate) != null) {
|
|
|
|
derivate = getNameWithoutExtension(derivate);
|
|
|
|
|
|
|
|
if (derivate.equals(base))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public static boolean containsOnly(Iterable<File> files, FileFilter filter) {
|
|
|
|
for (File file : files) {
|
|
|
|
if (!filter.accept(file))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-02-03 15:36:57 -05:00
|
|
|
public static List<File> filter(Iterable<File> files, FileFilter... filters) {
|
|
|
|
List<File> accepted = new ArrayList<File>();
|
|
|
|
|
|
|
|
for (File file : files) {
|
|
|
|
for (FileFilter filter : filters) {
|
|
|
|
if (filter.accept(file)) {
|
|
|
|
accepted.add(file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return accepted;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2012-06-15 06:45:35 -04:00
|
|
|
public static FileFilter not(FileFilter filter) {
|
|
|
|
return new NotFileFilter(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-13 14:16:38 -04:00
|
|
|
public static List<File> flatten(Iterable<File> roots, int maxDepth, boolean listHiddenFiles) {
|
2010-10-22 23:51:19 -04:00
|
|
|
List<File> files = new ArrayList<File>();
|
|
|
|
|
|
|
|
// unfold/flatten file tree
|
|
|
|
for (File root : roots) {
|
|
|
|
if (root.isDirectory()) {
|
2011-09-13 14:16:38 -04:00
|
|
|
listFiles(root, 0, files, maxDepth, listHiddenFiles);
|
2010-10-22 23:51:19 -04:00
|
|
|
} else {
|
|
|
|
files.add(root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-10-28 21:22:00 -04:00
|
|
|
public static List<File> listPath(File file) {
|
|
|
|
LinkedList<File> nodes = new LinkedList<File>();
|
|
|
|
|
|
|
|
for (File node = file; node != null; node = node.getParentFile()) {
|
|
|
|
nodes.addFirst(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-09-13 14:16:38 -04:00
|
|
|
public static List<File> listFiles(Iterable<File> folders, int maxDepth, boolean listHiddenFiles) {
|
2009-10-21 18:39:02 -04:00
|
|
|
List<File> files = new ArrayList<File>();
|
|
|
|
|
|
|
|
// collect files from directory tree
|
|
|
|
for (File folder : folders) {
|
2011-09-13 14:16:38 -04:00
|
|
|
listFiles(folder, 0, files, maxDepth, listHiddenFiles);
|
2009-10-21 18:39:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-09-13 14:16:38 -04:00
|
|
|
private static void listFiles(File folder, int depth, List<File> files, int maxDepth, boolean listHiddenFiles) {
|
2009-10-21 18:39:02 -04:00
|
|
|
if (depth > maxDepth)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (File file : folder.listFiles()) {
|
2011-09-13 14:16:38 -04:00
|
|
|
if (!listHiddenFiles && file.isHidden()) // ignore hidden files
|
|
|
|
continue;
|
|
|
|
|
2009-10-21 18:39:02 -04:00
|
|
|
if (file.isDirectory()) {
|
2011-09-13 14:16:38 -04:00
|
|
|
listFiles(file, depth + 1, files, maxDepth, listHiddenFiles);
|
2009-10-21 18:39:02 -04:00
|
|
|
} else {
|
|
|
|
files.add(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-11-26 04:50:31 -05:00
|
|
|
public static SortedMap<File, List<File>> mapByFolder(Iterable<File> files) {
|
|
|
|
SortedMap<File, List<File>> map = new TreeMap<File, List<File>>();
|
|
|
|
|
|
|
|
for (File file : files) {
|
2011-11-28 10:29:51 -05:00
|
|
|
File key = file.getParentFile();
|
|
|
|
if (key == null) {
|
|
|
|
throw new IllegalArgumentException("Parent is null: " + file);
|
|
|
|
}
|
2011-11-26 04:50:31 -05:00
|
|
|
|
2011-11-28 10:29:51 -05:00
|
|
|
List<File> valueList = map.get(key);
|
2011-11-26 04:50:31 -05:00
|
|
|
if (valueList == null) {
|
|
|
|
valueList = new ArrayList<File>();
|
2011-11-28 10:29:51 -05:00
|
|
|
map.put(key, valueList);
|
2011-11-26 04:50:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
valueList.add(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-07-03 21:14:54 -04:00
|
|
|
public static Map<String, List<File>> mapByExtension(Iterable<File> files) {
|
2011-07-06 22:08:50 -04:00
|
|
|
Map<String, List<File>> map = new HashMap<String, List<File>>();
|
2011-07-03 21:14:54 -04:00
|
|
|
|
|
|
|
for (File file : files) {
|
|
|
|
String key = getExtension(file);
|
|
|
|
if (key != null) {
|
|
|
|
key = key.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<File> valueList = map.get(key);
|
|
|
|
if (valueList == null) {
|
|
|
|
valueList = new ArrayList<File>();
|
|
|
|
map.put(key, valueList);
|
|
|
|
}
|
|
|
|
|
|
|
|
valueList.add(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
/**
|
2009-10-28 21:22:00 -04:00
|
|
|
* Invalid file name characters: \, /, :, *, ?, ", <, >, |, \r and \n
|
2009-08-10 07:46:24 -04:00
|
|
|
*/
|
|
|
|
public static final Pattern ILLEGAL_CHARACTERS = Pattern.compile("[\\\\/:*?\"<>|\\r\\n]");
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
/**
|
2009-10-28 21:22:00 -04:00
|
|
|
* Strip file name of invalid characters
|
2009-08-10 07:46:24 -04:00
|
|
|
*
|
|
|
|
* @param filename original filename
|
2009-10-28 21:22:00 -04:00
|
|
|
* @return valid file name stripped of invalid characters
|
2009-08-10 07:46:24 -04:00
|
|
|
*/
|
|
|
|
public static String validateFileName(CharSequence filename) {
|
2009-10-28 21:22:00 -04:00
|
|
|
// strip invalid characters from file name
|
2009-08-10 07:46:24 -04:00
|
|
|
return ILLEGAL_CHARACTERS.matcher(filename).replaceAll("");
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
public static boolean isInvalidFileName(CharSequence filename) {
|
2009-10-28 21:22:00 -04:00
|
|
|
// check if file name contains any illegal characters
|
2009-08-10 07:46:24 -04:00
|
|
|
return ILLEGAL_CHARACTERS.matcher(filename).find();
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-10-28 21:22:00 -04:00
|
|
|
public static File validateFileName(File file) {
|
|
|
|
// windows drives (e.g. c:, d:, etc.) are never invalid because name will be an empty string
|
|
|
|
if (!isInvalidFileName(file.getName()))
|
|
|
|
return file;
|
|
|
|
|
|
|
|
// validate file name only
|
|
|
|
return new File(file.getParentFile(), validateFileName(file.getName()));
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-10-28 21:22:00 -04:00
|
|
|
public static File validateFilePath(File path) {
|
|
|
|
Iterator<File> nodes = listPath(path).iterator();
|
|
|
|
|
|
|
|
// initialize with root node, keep original root object if possible (so we don't loose the drive on windows)
|
|
|
|
File validatedPath = validateFileName(nodes.next());
|
|
|
|
|
|
|
|
// validate the rest of the path
|
|
|
|
while (nodes.hasNext()) {
|
|
|
|
validatedPath = new File(validatedPath, validateFileName(nodes.next().getName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return validatedPath;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-10-28 21:22:00 -04:00
|
|
|
public static boolean isInvalidFilePath(File path) {
|
|
|
|
// check if file name contains any illegal characters
|
|
|
|
for (File node = path; node != null; node = node.getParentFile()) {
|
|
|
|
if (isInvalidFileName(node.getName()))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-09-13 14:16:38 -04:00
|
|
|
public static String normalizePathSeparators(String path) {
|
|
|
|
return path.replace('\\', '/');
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2010-02-04 18:22:28 -05:00
|
|
|
public static String replacePathSeparators(CharSequence path) {
|
2011-12-18 01:37:59 -05:00
|
|
|
return replacePathSeparators(path, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String replacePathSeparators(CharSequence path, String replacement) {
|
|
|
|
return Pattern.compile("\\s*[\\\\/]+\\s*").matcher(path).replaceAll(replacement);
|
2010-02-03 17:51:18 -05:00
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-12-19 21:37:36 -05:00
|
|
|
public static String getXmlString(Document dom) throws TransformerException {
|
|
|
|
Transformer tr = TransformerFactory.newInstance().newTransformer();
|
|
|
|
tr.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
|
|
|
tr.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
|
|
|
|
|
|
//create string from dom
|
|
|
|
StringWriter buffer = new StringWriter();
|
|
|
|
tr.transform(new DOMSource(dom), new StreamResult(buffer));
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
public static final long KILO = 1024;
|
|
|
|
public static final long MEGA = KILO * 1024;
|
|
|
|
public static final long GIGA = MEGA * 1024;
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
public static String formatSize(long size) {
|
|
|
|
if (size >= MEGA)
|
|
|
|
return String.format("%,d MB", size / MEGA);
|
|
|
|
else if (size >= KILO)
|
|
|
|
return String.format("%,d KB", size / KILO);
|
|
|
|
else
|
|
|
|
return String.format("%,d Byte", size);
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public static final FileFilter FOLDERS = new FileFilter() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
|
|
|
return file.isDirectory();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public static final FileFilter FILES = new FileFilter() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
|
|
|
return file.isFile();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-22 10:21:19 -05:00
|
|
|
public static final FileFilter TEMPORARY = new FileFilter() {
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
private final String tmpdir = System.getProperty("java.io.tmpdir");
|
|
|
|
|
2011-11-22 10:21:19 -05:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
2011-12-09 00:37:03 -05:00
|
|
|
return file.getAbsolutePath().startsWith(tmpdir);
|
2011-11-22 10:21:19 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2012-04-29 01:59:12 -04:00
|
|
|
public static class ParentFilter implements FileFilter {
|
2012-02-15 07:40:18 -05:00
|
|
|
|
|
|
|
private final File folder;
|
|
|
|
|
|
|
|
|
2012-04-29 01:59:12 -04:00
|
|
|
public ParentFilter(File folder) {
|
2012-02-15 07:40:18 -05:00
|
|
|
this.folder = folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
2012-04-29 01:59:12 -04:00
|
|
|
return listPath(file).contains(folder);
|
2012-02-15 07:40:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public static class ExtensionFileFilter implements FileFilter {
|
|
|
|
|
|
|
|
private final String[] extensions;
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public ExtensionFileFilter(String... extensions) {
|
|
|
|
this.extensions = extensions;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-06-19 18:35:39 -04:00
|
|
|
public ExtensionFileFilter(Collection<String> extensions) {
|
|
|
|
this.extensions = extensions.toArray(new String[0]);
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
|
|
|
return hasExtension(file, extensions);
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-06-13 05:53:48 -04:00
|
|
|
public boolean accept(String name) {
|
|
|
|
return hasExtension(name, extensions);
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-06-30 08:57:09 -04:00
|
|
|
public boolean acceptExtension(String extension) {
|
|
|
|
for (String other : extensions) {
|
|
|
|
if (other.equalsIgnoreCase(extension))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2011-09-14 14:13:34 -04:00
|
|
|
public String extension() {
|
|
|
|
return extensions[0];
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
public String[] extensions() {
|
|
|
|
return extensions.clone();
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:37:03 -05:00
|
|
|
|
2012-06-15 06:45:35 -04:00
|
|
|
public static class NotFileFilter implements FileFilter {
|
|
|
|
|
|
|
|
public FileFilter filter;
|
|
|
|
|
|
|
|
|
|
|
|
public NotFileFilter(FileFilter filter) {
|
|
|
|
this.filter = filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
|
|
|
return !filter.accept(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
/**
|
|
|
|
* Dummy constructor to prevent instantiation.
|
|
|
|
*/
|
|
|
|
private FileUtilities() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|