package textbender.g.io; // Copyright 2004-2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.io.*; import java.util.regex.*; import textbender.g.lang.*; import textbender.g.util.logging.*; /** File utilities. */ public @ThreadSafe final class FileX { private FileX() {} /** Pattern to split a filename (or path) into two groups: body and dot-extension. * The body group (1) may include separators '/'. * It will be neither empty nor null. *

* The extension group (2) will either include the preceding dot '.', * or it will be empty. It will not be null. *

*/ public static final Pattern BODY_DOTX_PATTERN = Pattern.compile( "^(.+?)((?:\\.[^.]*)?)$" ); /** @param target pathname to create as copy (will be overwritten if it already exists) * @param source file or directory to copy * (directory contents are recursively copied using {@link #copyTo(File,File) copyTo}) */ public static void copyAs( File target, File source ) throws IOException { copyAs( target, source, FileFilterX.TRANSPARENT ); } /** Copies a file to a new file named as specified. * No checking is done to prevent self-copying. * * @param target pathname to create as copy (will be overwritten if it already exists) * @param source file or directory to copy * (directory contents are recursively copied * using {@link #copyTo(File,File,FileFilter) copyTo}) * @param fileFilter to use, if the source is a directory * (filter is applied to contents of directory, * and recursively to contents of any sub-directories that themselves pass the filter) */ public static void copyAs( File target, File source, FileFilter fileFilter ) throws IOException { if( source.isDirectory() ) { // First the directory itself. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if( target.isDirectory() ) // clean it out, in case not empty: { if( !deleteRecursiveFrom(target) ) throw new IOException( "unable to delete directory contents of " + target ); } else if( !target.mkdir() ) throw new IOException( "unable to create directory " + target ); // Then its contents, if any. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - File[] subSourceArray = source.listFiles( fileFilter ); for( int i=0; i