Author: bodewig Date: Thu Nov 3 14:48:04 2011 New Revision: 1197165 URL: http://svn.apache.org/viewvc?rev=1197165&view=rev Log: extract common file name munging code from the *Utils classes
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/FileNameUtil.java - copied, changed from r1197084, commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2Utils.java commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/BZip2UtilsTestCase.java Copied: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/FileNameUtil.java (from r1197084, commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java) URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/FileNameUtil.java?p2=commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/FileNameUtil.java&p1=commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java&r1=1197084&r2=1197165&rev=1197165&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/FileNameUtil.java Thu Nov 3 14:48:04 2011 @@ -16,73 +16,125 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.commons.compress.compressors.gzip; +package org.apache.commons.compress.compressors; +import java.util.Collections; import java.util.HashMap; import java.util.Locale; import java.util.Map; /** - * Utility code for the gzip compression format. + * File name mapping code for the compression formats. * @ThreadSafe + * @since Apache Commons Compress 1.4 */ -public class GzipUtils { +public class FileNameUtil { /** - * Map from common filename suffixes to the suffixes that identify gzipped + * Map from common filename suffixes to the suffixes that identify compressed * versions of those file types. For example: from ".tar" to ".tgz". */ - private static final Map<String, String> compressSuffix = - new HashMap<String, String>(); + private final Map<String, String> compressSuffix = + new HashMap<String, String>(); /** - * Map from common filename suffixes of gzipped files to the corresponding - * suffixes of uncompressed files. For example: from ".tgz" to ".tar". + * Map from common filename suffixes of compressed files to the + * corresponding suffixes of uncompressed files. For example: from + * ".tgz" to ".tar". * <p> - * This map also contains gzip-specific suffixes like ".gz" and "-z". + * This map also contains format-specific suffixes like ".gz" and "-z". * These suffixes are mapped to the empty string, as they should simply * be removed from the filename when the file is uncompressed. */ - private static final Map<String, String> uncompressSuffix = - new HashMap<String, String>(); + private final Map<String, String> uncompressSuffix; - static { - compressSuffix.put(".tar", ".tgz"); - compressSuffix.put(".svg", ".svgz"); - compressSuffix.put(".cpio", ".cpgz"); - compressSuffix.put(".wmf", ".wmz"); - compressSuffix.put(".emf", ".emz"); - - uncompressSuffix.put(".tgz", ".tar"); - uncompressSuffix.put(".taz", ".tar"); - uncompressSuffix.put(".svgz", ".svg"); - uncompressSuffix.put(".cpgz", ".cpio"); - uncompressSuffix.put(".wmz", ".wmf"); - uncompressSuffix.put(".emz", ".emf"); - uncompressSuffix.put(".gz", ""); - uncompressSuffix.put(".z", ""); - uncompressSuffix.put("-gz", ""); - uncompressSuffix.put("-z", ""); - uncompressSuffix.put("_z", ""); - } - // N.B. if any shorter or longer keys are added, ensure the for loop limits are changed + /** + * Length of the longest compressed suffix. + */ + private final int longestCompressedSuffix; - /** Private constructor to prevent instantiation of this utility class. */ - private GzipUtils() { + /** + * Length of the shortest compressed suffix. + */ + private final int shortestCompressedSuffix; + + /** + * Length of the longest uncompressed suffix. + */ + private final int longestUncompressedSuffix; + + /** + * Length of the shortest uncompressed suffix longer than the + * empty string. + */ + private final int shortestUncompressedSuffix; + + /** + * The format's default extension. + */ + private final String defaultExtension; + + /** + * sets up the utility with a map of known compressed to + * uncompressed suffix mappings and the default extension of the + * format. + * + * @param uncompressSuffix Map from common filename suffixes of + * compressed files to the corresponding suffixes of uncompressed + * files. For example: from ".tgz" to ".tar". This map also + * contains format-specific suffixes like ".gz" and "-z". These + * suffixes are mapped to the empty string, as they should simply + * be removed from the filename when the file is uncompressed. + * + * @param defaultExtension the format's default extension like ".gz" + */ + public FileNameUtil(Map<String, String> uncompressSuffix, + String defaultExtension) { + this.uncompressSuffix = Collections.unmodifiableMap(uncompressSuffix); + int lc = Integer.MIN_VALUE, sc = Integer.MAX_VALUE; + int lu = Integer.MIN_VALUE, su = Integer.MAX_VALUE; + for (Map.Entry<String, String> ent : uncompressSuffix.entrySet()) { + int cl = ent.getKey().length(); + if (cl > lc) { + lc = cl; + } + if (cl < sc) { + sc = cl; + } + + String u = ent.getValue(); + int ul = u.length(); + if (ul > 0) { + if (!compressSuffix.containsKey(u)) { + compressSuffix.put(u, ent.getKey()); + } + if (ul > lu) { + lu = ul; + } + if (ul < su) { + su = ul; + } + } + } + longestCompressedSuffix = lc; + longestUncompressedSuffix = lu; + shortestCompressedSuffix = sc; + shortestUncompressedSuffix = su; + this.defaultExtension = defaultExtension; } /** - * Detects common gzip suffixes in the given filename. + * Detects common format suffixes in the given filename. * * @param filename name of a file - * @return <code>true</code> if the filename has a common gzip suffix, + * @return <code>true</code> if the filename has a common format suffix, * <code>false</code> otherwise */ - public static boolean isCompressedFilename(String filename) { - String lower = filename.toLowerCase(Locale.ENGLISH); - int n = lower.length(); - // Shortest suffix is two letters (_z), longest is five (.svgz) - for (int i = 2; i <= 5 && i < n; i++) { + public boolean isCompressedFilename(String filename) { + final String lower = filename.toLowerCase(Locale.ENGLISH); + final int n = lower.length(); + for (int i = shortestCompressedSuffix; + i <= longestCompressedSuffix && i < n; i++) { if (uncompressSuffix.containsKey(lower.substring(n - i))) { return true; } @@ -91,24 +143,24 @@ public class GzipUtils { } /** - * Maps the given name of a gzip-compressed file to the name that the + * Maps the given name of a compressed file to the name that the * file should have after uncompression. Commonly used file type specific * suffixes like ".tgz" or ".svgz" are automatically detected and * correctly mapped. For example the name "package.tgz" is mapped to * "package.tar". And any filenames with the generic ".gz" suffix * (or any other generic gzip suffix) is mapped to a name without that - * suffix. If no gzip suffix is detected, then the filename is returned + * suffix. If no format suffix is detected, then the filename is returned * unmapped. * * @param filename name of a file * @return name of the corresponding uncompressed file */ - public static String getUncompressedFilename(String filename) { - String lower = filename.toLowerCase(Locale.ENGLISH); - int n = lower.length(); - // Shortest suffix is two letters (_z), longest is five (.svgz) - for (int i = 2; i <= 5 && i < n; i++) { - Object suffix = uncompressSuffix.get(lower.substring(n - i)); + public String getUncompressedFilename(String filename) { + final String lower = filename.toLowerCase(Locale.ENGLISH); + final int n = lower.length(); + for (int i = shortestCompressedSuffix; + i <= longestCompressedSuffix && i < n; i++) { + String suffix = uncompressSuffix.get(lower.substring(n - i)); if (suffix != null) { return filename.substring(0, n - i) + suffix; } @@ -118,7 +170,7 @@ public class GzipUtils { /** * Maps the given filename to the name that the file should have after - * compression with gzip. Common file types with custom suffixes for + * compressio. Common file types with custom suffixes for * compressed versions are automatically detected and correctly mapped. * For example the name "package.tar" is mapped to "package.tgz". If no * custom mapping is applicable, then the default ".gz" suffix is appended @@ -127,18 +179,18 @@ public class GzipUtils { * @param filename name of a file * @return name of the corresponding compressed file */ - public static String getCompressedFilename(String filename) { - String lower = filename.toLowerCase(Locale.ENGLISH); - int n = lower.length(); - // Shortest suffix is four letters (.svg), longest is five (.cpio) - for (int i = 4; i <= 5 && i < n; i++) { - Object suffix = compressSuffix.get(lower.substring(n - i)); + public String getCompressedFilename(String filename) { + final String lower = filename.toLowerCase(Locale.ENGLISH); + final int n = lower.length(); + for (int i = shortestUncompressedSuffix; + i <= longestUncompressedSuffix && i < n; i++) { + String suffix = compressSuffix.get(lower.substring(n - i)); if (suffix != null) { return filename.substring(0, n - i) + suffix; } } - // No custom suffix found, just append the default .gz - return filename + ".gz"; + // No custom suffix found, just append the default + return filename + defaultExtension; } } Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2Utils.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2Utils.java?rev=1197165&r1=1197164&r2=1197165&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2Utils.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2Utils.java Thu Nov 3 14:48:04 2011 @@ -18,9 +18,9 @@ */ package org.apache.commons.compress.compressors.bzip2; -import java.util.HashMap; -import java.util.Locale; +import java.util.LinkedHashMap; import java.util.Map; +import org.apache.commons.compress.compressors.FileNameUtil; /** * Utility code for the BZip2 compression format. @@ -29,24 +29,20 @@ import java.util.Map; */ public abstract class BZip2Utils { - /** - * Map from common filename suffixes of bzip2ed files to the corresponding - * suffixes of uncompressed files. For example: from ".tbz2" to ".tar". - * <p> - * This map also contains bzip2-specific suffixes like ".bz2". These - * suffixes are mapped to the empty string, as they should simply be - * removed from the filename when the file is uncompressed. - */ - private static final Map<String, String> uncompressSuffix = - new HashMap<String, String>(); + private static final FileNameUtil fileNameUtil; static { + Map<String, String> uncompressSuffix = + new LinkedHashMap<String, String>(); + // backwards compatibilty: BZip2Utils never created the short + // tbz form, so .tar.bz2 has to be added explicitly + uncompressSuffix.put(".tar.bz2", ".tar"); uncompressSuffix.put(".tbz2", ".tar"); uncompressSuffix.put(".tbz", ".tar"); uncompressSuffix.put(".bz2", ""); uncompressSuffix.put(".bz", ""); + fileNameUtil = new FileNameUtil(uncompressSuffix, ".bz2"); } - // N.B. if any shorter or longer keys are added, ensure the for loop limits are changed /** Private constructor to prevent instantiation of this utility class. */ private BZip2Utils() { @@ -60,15 +56,7 @@ public abstract class BZip2Utils { * <code>false</code> otherwise */ public static boolean isCompressedFilename(String filename) { - String lower = filename.toLowerCase(Locale.ENGLISH); - int n = lower.length(); - // Shortest suffix is three letters (.bz), longest is five (.tbz2) - for (int i = 3; i <= 5 && i < n; i++) { - if (uncompressSuffix.containsKey(lower.substring(n - i))) { - return true; - } - } - return false; + return fileNameUtil.isCompressedFilename(filename); } /** @@ -85,16 +73,7 @@ public abstract class BZip2Utils { * @return name of the corresponding uncompressed file */ public static String getUncompressedFilename(String filename) { - String lower = filename.toLowerCase(Locale.ENGLISH); - int n = lower.length(); - // Shortest suffix is three letters (.bz), longest is five (.tbz2) - for (int i = 3; i <= 5 && i < n; i++) { - Object suffix = uncompressSuffix.get(lower.substring(n - i)); - if (suffix != null) { - return filename.substring(0, n - i) + suffix; - } - } - return filename; + return fileNameUtil.getUncompressedFilename(filename); } /** @@ -108,7 +87,7 @@ public abstract class BZip2Utils { * @return name of the corresponding compressed file */ public static String getCompressedFilename(String filename) { - return filename + ".bz2"; + return fileNameUtil.getCompressedFilename(filename); } } Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java?rev=1197165&r1=1197164&r2=1197165&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/gzip/GzipUtils.java Thu Nov 3 14:48:04 2011 @@ -18,9 +18,9 @@ */ package org.apache.commons.compress.compressors.gzip; -import java.util.HashMap; -import java.util.Locale; +import java.util.LinkedHashMap; import java.util.Map; +import org.apache.commons.compress.compressors.FileNameUtil; /** * Utility code for the gzip compression format. @@ -28,31 +28,14 @@ import java.util.Map; */ public class GzipUtils { - /** - * Map from common filename suffixes to the suffixes that identify gzipped - * versions of those file types. For example: from ".tar" to ".tgz". - */ - private static final Map<String, String> compressSuffix = - new HashMap<String, String>(); - - /** - * Map from common filename suffixes of gzipped files to the corresponding - * suffixes of uncompressed files. For example: from ".tgz" to ".tar". - * <p> - * This map also contains gzip-specific suffixes like ".gz" and "-z". - * These suffixes are mapped to the empty string, as they should simply - * be removed from the filename when the file is uncompressed. - */ - private static final Map<String, String> uncompressSuffix = - new HashMap<String, String>(); + private static final FileNameUtil fileNameUtil; static { - compressSuffix.put(".tar", ".tgz"); - compressSuffix.put(".svg", ".svgz"); - compressSuffix.put(".cpio", ".cpgz"); - compressSuffix.put(".wmf", ".wmz"); - compressSuffix.put(".emf", ".emz"); - + // using LinkedHashMap so .tgz is preferred over .taz as + // compressed extension of .tar as FileNameUtil will use the + // first one found + Map<String, String> uncompressSuffix = + new LinkedHashMap<String, String>(); uncompressSuffix.put(".tgz", ".tar"); uncompressSuffix.put(".taz", ".tar"); uncompressSuffix.put(".svgz", ".svg"); @@ -64,8 +47,8 @@ public class GzipUtils { uncompressSuffix.put("-gz", ""); uncompressSuffix.put("-z", ""); uncompressSuffix.put("_z", ""); + fileNameUtil = new FileNameUtil(uncompressSuffix, ".gz"); } - // N.B. if any shorter or longer keys are added, ensure the for loop limits are changed /** Private constructor to prevent instantiation of this utility class. */ private GzipUtils() { @@ -79,15 +62,7 @@ public class GzipUtils { * <code>false</code> otherwise */ public static boolean isCompressedFilename(String filename) { - String lower = filename.toLowerCase(Locale.ENGLISH); - int n = lower.length(); - // Shortest suffix is two letters (_z), longest is five (.svgz) - for (int i = 2; i <= 5 && i < n; i++) { - if (uncompressSuffix.containsKey(lower.substring(n - i))) { - return true; - } - } - return false; + return fileNameUtil.isCompressedFilename(filename); } /** @@ -104,16 +79,7 @@ public class GzipUtils { * @return name of the corresponding uncompressed file */ public static String getUncompressedFilename(String filename) { - String lower = filename.toLowerCase(Locale.ENGLISH); - int n = lower.length(); - // Shortest suffix is two letters (_z), longest is five (.svgz) - for (int i = 2; i <= 5 && i < n; i++) { - Object suffix = uncompressSuffix.get(lower.substring(n - i)); - if (suffix != null) { - return filename.substring(0, n - i) + suffix; - } - } - return filename; + return fileNameUtil.getUncompressedFilename(filename); } /** @@ -128,17 +94,7 @@ public class GzipUtils { * @return name of the corresponding compressed file */ public static String getCompressedFilename(String filename) { - String lower = filename.toLowerCase(Locale.ENGLISH); - int n = lower.length(); - // Shortest suffix is four letters (.svg), longest is five (.cpio) - for (int i = 4; i <= 5 && i < n; i++) { - Object suffix = compressSuffix.get(lower.substring(n - i)); - if (suffix != null) { - return filename.substring(0, n - i) + suffix; - } - } - // No custom suffix found, just append the default .gz - return filename + ".gz"; + return fileNameUtil.getCompressedFilename(filename); } } Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java?rev=1197165&r1=1197164&r2=1197165&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java Thu Nov 3 14:48:04 2011 @@ -19,8 +19,8 @@ package org.apache.commons.compress.compressors.xz; import java.util.HashMap; -import java.util.Locale; import java.util.Map; +import org.apache.commons.compress.compressors.FileNameUtil; /** * Utility code for the xz compression format. @@ -29,32 +29,15 @@ import java.util.Map; */ public class XZUtils { - /** - * Map from common filename suffixes to the suffixes that identify xzped - * versions of those file types. For example: from ".tar" to ".txz". - */ - private static final Map<String, String> compressSuffix = - new HashMap<String, String>(); - - /** - * Map from common filename suffixes of xzped files to the corresponding - * suffixes of uncompressed files. For example: from ".tgz" to ".tar". - * <p> - * This map also contains xz-specific suffixes like ".gz" and "-z". - * These suffixes are mapped to the empty string, as they should simply - * be removed from the filename when the file is uncompressed. - */ - private static final Map<String, String> uncompressSuffix = - new HashMap<String, String>(); + private static final FileNameUtil fileNameUtil; static { - compressSuffix.put(".tar", ".txz"); - + Map<String, String> uncompressSuffix = new HashMap<String, String>(); uncompressSuffix.put(".txz", ".tar"); uncompressSuffix.put(".xz", ""); uncompressSuffix.put("-xz", ""); + fileNameUtil = new FileNameUtil(uncompressSuffix, ".xz"); } - // N.B. if any shorter or longer keys are added, ensure the for loop limits are changed /** Private constructor to prevent instantiation of this utility class. */ private XZUtils() { @@ -68,15 +51,7 @@ public class XZUtils { * <code>false</code> otherwise */ public static boolean isCompressedFilename(String filename) { - String lower = filename.toLowerCase(Locale.ENGLISH); - int n = lower.length(); - // Shortest suffix is three letters (.xz), longest is four (.txz) - for (int i = 3; i <= 4 && i < n; i++) { - if (uncompressSuffix.containsKey(lower.substring(n - i))) { - return true; - } - } - return false; + return fileNameUtil.isCompressedFilename(filename); } /** @@ -93,16 +68,7 @@ public class XZUtils { * @return name of the corresponding uncompressed file */ public static String getUncompressedFilename(String filename) { - String lower = filename.toLowerCase(Locale.ENGLISH); - int n = lower.length(); - // Shortest suffix is three letters (.xz), longest is four (.txz) - for (int i = 3; i <= 4 && i < n; i++) { - Object suffix = uncompressSuffix.get(lower.substring(n - i)); - if (suffix != null) { - return filename.substring(0, n - i) + suffix; - } - } - return filename; + return fileNameUtil.getUncompressedFilename(filename); } /** @@ -117,17 +83,7 @@ public class XZUtils { * @return name of the corresponding compressed file */ public static String getCompressedFilename(String filename) { - String lower = filename.toLowerCase(Locale.ENGLISH); - int n = lower.length(); - // Shortest suffix is three letters (.xz), longest is four (.txz) - for (int i = 3; i <= 4 && i < n; i++) { - Object suffix = compressSuffix.get(lower.substring(n - i)); - if (suffix != null) { - return filename.substring(0, n - i) + suffix; - } - } - // No custom suffix found, just append the default .gz - return filename + ".xz"; + return fileNameUtil.getCompressedFilename(filename); } } Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/BZip2UtilsTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/BZip2UtilsTestCase.java?rev=1197165&r1=1197164&r2=1197165&view=diff ============================================================================== --- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/BZip2UtilsTestCase.java (original) +++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/BZip2UtilsTestCase.java Thu Nov 3 14:48:04 2011 @@ -69,7 +69,7 @@ public class BZip2UtilsTestCase extends assertEquals("x.bz2", BZip2Utils.getCompressedFilename("x")); assertEquals("X.bz2", BZip2Utils.getCompressedFilename("X")); assertEquals("x.tar.bz2", BZip2Utils.getCompressedFilename("x.tar")); - assertEquals("x.TAR.bz2", BZip2Utils.getCompressedFilename("x.TAR")); + assertEquals("x.tar.bz2", BZip2Utils.getCompressedFilename("x.TAR")); } }