Clover coverage report - brownies library - 1.0-beta-1
Coverage timestamp: 月 8 16 2004 17:14:42 GMT+09:00
file stats: LOC: 391   Methods: 31
NCLOC: 231   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
FileNameUtils.java 79.7% 84.1% 83.9% 82.7%
coverage coverage
 1   
 /*
 2   
  * Joey and its relative products are published under the terms
 3   
  * of the Apache Software License.
 4   
  */
 5   
 package org.asyrinx.brownie.core.io;
 6   
 
 7   
 import java.io.File;
 8   
 import java.util.List;
 9   
 
 10   
 import org.asyrinx.brownie.core.lang.ArrayUtils;
 11   
 import org.asyrinx.brownie.core.lang.StringUtils;
 12   
 
 13   
 /**
 14   
  * @author Akima
 15   
  */
 16   
 public final class FileNameUtils {
 17   
 
 18   
     /**
 19   
      * ファイル名の拡張子からZIP圧縮されているものかどうかを判断します。
 20   
      * 
 21   
      * @param filename
 22   
      * @return
 23   
      */
 24  0
     public static boolean hasZipExtension(String filename) {
 25  0
         final String ext = StringUtils.nullTrim(getExtension(filename))
 26   
                 .toLowerCase();
 27  0
         return (ext.equals(FileConstants.EXT_ZIP)
 28   
                 || ext.equals(FileConstants.EXT_JAR)
 29   
                 || ext.equals(FileConstants.EXT_WAR) || ext
 30   
                 .equals(FileConstants.EXT_EAR));
 31   
     }
 32   
 
 33   
     /**
 34   
      * 引数のファイル名が絶対パスかどうか判定する
 35   
      * 
 36   
      * @param fileName
 37   
      * @return boolean
 38   
      */
 39  5
     public static boolean isAbsolutePath(String fileName) {
 40  5
         File f = new File(fileName);
 41  5
         return f.isAbsolute();
 42   
     }
 43   
 
 44   
     /**
 45   
      * 対象のパスを絶対パスにして返す <BR>
 46   
      * ルートパスと繋げる際に、セパレータの重複・欠落をチェックする。
 47   
      * 
 48   
      * @param path
 49   
      *            対象パス
 50   
      * @param rootPath
 51   
      *            ルートからのパス
 52   
      * @return String 絶対パス
 53   
      */
 54  4
     public static String toAbsolutePath(String rootPath, String path) {
 55  4
         if (isAbsolutePath(path)) {
 56  0
             return path;
 57  4
         } else if (StringUtils.isEmpty(rootPath)) {
 58  0
             return path;
 59   
         }
 60  4
         final PathPointer pointer = new PathPointer(rootPath);
 61  4
         pointer.moveTo(path);
 62  4
         return replaceSeparator(pointer.getPath(), getFileSeparator(rootPath),
 63   
                 getFileSeparator(path));
 64   
     }
 65   
 
 66   
     /**
 67   
      * 文字列の最後が区切り文字かどうかを判断する。
 68   
      */
 69  38
     public static boolean isSeparatorTail(String path) {
 70  38
         return isSeparator(path, File.separator, false)
 71   
                 || isSeparator(path, FileConstants.FILE_SPARATOR_SLASH, false);
 72   
     }
 73   
 
 74   
     /**
 75   
      * 文字列の先頭が区切り文字かどうかを判断する。
 76   
      */
 77  34
     public static boolean isSeparatorHead(String path) {
 78  34
         return isSeparator(path, File.separator, true)
 79   
                 || isSeparator(path, FileConstants.FILE_SPARATOR_SLASH, true);
 80   
     }
 81   
 
 82  136
     private static boolean isSeparator(String path, String separator,
 83   
             boolean isHead) {
 84  136
         if (path == null)
 85  0
             return false;
 86  136
         if (path.length() < File.separator.length())
 87  0
             return false;
 88  136
         final String target = isHead ? path.substring(0, File.separator
 89   
                 .length()) : path.substring(path.length()
 90   
                 - File.separator.length());
 91  136
         return separator.equals(target);
 92   
     }
 93   
 
 94  47
     public static boolean hasFileSeparator(String path) {
 95  47
         return hasFileSeparator(path, File.separator);
 96   
     }
 97   
 
 98  28
     public static boolean hasFileSeparatorSlash(String path) {
 99  28
         return hasFileSeparator(path, FileConstants.FILE_SPARATOR_SLASH);
 100   
     }
 101   
 
 102  75
     private static boolean hasFileSeparator(String path, String separator) {
 103  75
         return path.indexOf(separator) > -1;
 104   
     }
 105   
 
 106   
     /**
 107   
      * 
 108   
      * @param path
 109   
      * @return
 110   
      */
 111  0
     public static String adjustFileSeparator(String path) {
 112  0
         return toFileSeparatorSlash(path);
 113   
     }
 114   
 
 115  19
     public static String toFileSeparatorSlash(String path) {
 116  19
         return hasFileSeparator(path) ? replaceSeparator(path, File.separator,
 117   
                 FileConstants.FILE_SPARATOR_SLASH) : path;
 118   
     }
 119   
 
 120  0
     public static String toFileSeparator(String path) {
 121  0
         return hasFileSeparatorSlash(path) ? replaceSeparator(path,
 122   
                 FileConstants.FILE_SPARATOR_SLASH, File.separator) : path;
 123   
     }
 124   
 
 125  0
     public static String toFileSeparator(String path, String separator) {
 126  0
         return replaceSeparator(replaceSeparator(path,
 127   
                 FileConstants.FILE_SPARATOR_SLASH, separator), File.separator,
 128   
                 separator);
 129   
     }
 130   
 
 131   
     /**
 132   
      * 
 133   
      * @param path
 134   
      * @return
 135   
      */
 136  0
     public static boolean hasOneKindSeparator(String path) {
 137  0
         final boolean hasFS = hasFileSeparator(path);
 138  0
         final boolean hasFSS = hasFileSeparatorSlash(path);
 139  0
         return !(hasFS && hasFSS);
 140   
     }
 141   
 
 142   
     /**
 143   
      * 
 144   
      * @param path
 145   
      * @return
 146   
      */
 147  28
     public static String getFileSeparator(String path) {
 148  28
         final boolean hasFS = hasFileSeparator(path);
 149  28
         final boolean hasFSS = hasFileSeparatorSlash(path);
 150  28
         if (hasFS && !hasFSS)
 151  8
             return File.separator;
 152   
         else
 153  20
             return FileConstants.FILE_SPARATOR_SLASH;
 154   
     }
 155   
 
 156   
     /**
 157   
      * 文字列の先頭に区切り文字を付ける。 ただし、先頭が区切り文字である場合には引数を戻りちとして返す。
 158   
      */
 159  6
     public static String addSeparatorHead(String path) {
 160  6
         if (isSeparatorHead(path)) {
 161  4
             return path;
 162   
         } else {
 163  2
             return getFileSeparator(path) + path;
 164   
         }
 165   
     }
 166   
 
 167   
     /**
 168   
      * 文字列の末尾に区切り文字を付ける。 ただし、末尾が区切り文字である場合には引数を戻りちとして返す。
 169   
      */
 170  4
     public static String addSeparatorTail(String path) {
 171  4
         if (isSeparatorTail(path)) {
 172  2
             return path;
 173   
         } else {
 174  2
             return path + getFileSeparator(path);
 175   
         }
 176   
     }
 177   
 
 178   
     /**
 179   
      * 文字列の先頭の区切り文字を削除する。 ただし、先頭が区切り文字でない場合には引数を戻りちとして返す。
 180   
      */
 181  23
     public static String deleteSeparatorHead(String path) {
 182  23
         if (isSeparatorHead(path)) {
 183  3
             final String separator = FileNameUtils.getFileSeparator(path);
 184  3
             return path.substring(separator.length(), path.length()
 185   
                     - separator.length() + 1);
 186   
         } else {
 187  20
             return path;
 188   
         }
 189   
     }
 190   
 
 191   
     /**
 192   
      * 文字列の最後の区切り文字を削除する。 ただし、末尾が区切り文字でない場合には引数を戻り値として返す。
 193   
      */
 194  30
     public static String deleteSeparatorTail(String path) {
 195  30
         if (isSeparatorTail(path)) {
 196  3
             return path.substring(0, path.length() - File.separator.length());
 197   
         } else {
 198  27
             return path;
 199   
         }
 200   
     }
 201   
 
 202   
     /**
 203   
      * 対象ファイルの親のパス名文字列を返す
 204   
      * 
 205   
      * @param path
 206   
      *            対象ファイル
 207   
      * @return String 親のパス名文字列
 208   
      */
 209  8
     public static String getParentPath(String path) {
 210  8
         if (StringUtils.isEmpty(path))
 211  2
             return null;
 212  6
         return new File(path).getParent();
 213   
     }
 214   
 
 215   
     /**
 216   
      * ファイルセパレータを置換する
 217   
      * 
 218   
      * @param path
 219   
      *            置換対象パス
 220   
      * @param oldSeparator
 221   
      *            置換前のセパレータ
 222   
      * @param newSeparator
 223   
      *            置換後のセパレータ
 224   
      * @return String 置換後のパス
 225   
      */
 226  23
     public static String replaceSeparator(String path, String oldSeparator,
 227   
             String newSeparator) {
 228  23
         return StringUtils.replace(path, oldSeparator, newSeparator);
 229   
     }
 230   
 
 231  19
     public static String replaceSeparatorSlash(String path) {
 232  19
         return StringUtils.replace(path, FileConstants.FILE_SPARATOR_SLASH,
 233   
                 File.separator);
 234   
     }
 235   
 
 236   
     /**
 237   
      * パス中の先頭から指定されたパスを削除する。
 238   
      * 
 239   
      * @param targetPath
 240   
      *            編集対象
 241   
      * @param deletePath
 242   
      *            削除したいパス 先頭のパスが一致しない場合には、未編集の文字列を返却する。
 243   
      */
 244  1
     private static String deleteHeadPath(String targetPath, String deletePath,
 245   
             boolean addSeparator) {
 246  1
         if (addSeparator) {
 247  1
             deletePath = deleteSeparatorTail(deletePath);
 248   
         } else {
 249  0
             deletePath = addSeparatorTail(deletePath);
 250   
         }
 251  1
         int idx = targetPath.indexOf(deletePath);
 252  1
         if (idx != 0)
 253  0
             return targetPath;
 254  1
         String result = targetPath.substring(deletePath.length());
 255  1
         if (addSeparator) {
 256  1
             return addSeparatorHead(result);
 257   
         } else {
 258  0
             return deleteSeparatorHead(result);
 259   
         }
 260   
     }
 261   
 
 262   
     /**
 263   
      * Method toRelativePath.
 264   
      * 
 265   
      * @param filePath
 266   
      * @param basePath
 267   
      * @return
 268   
      */
 269  6
     public static String toRelativePath(String filePath, String basePath) {
 270  6
         if (StringUtils.isEmpty(filePath))
 271  0
             return null;
 272  6
         if (StringUtils.isEmpty(basePath))
 273  0
             return filePath;
 274  6
         basePath = replaceSeparator(deleteSeparatorTail(basePath),
 275   
                 File.separator, FileConstants.FILE_SPARATOR_SLASH);
 276  6
         filePath = replaceSeparator(deleteSeparatorTail(filePath),
 277   
                 File.separator, FileConstants.FILE_SPARATOR_SLASH);
 278  6
         if (filePath.equals(basePath))
 279  1
             return FileConstants.DIRECTORY_CURRENT;
 280  5
         if (filePath.startsWith(basePath)) {
 281  1
             return FileConstants.DIRECTORY_CURRENT
 282   
                     + addSeparatorHead(deleteHeadPath(filePath, basePath, true));
 283  4
         } else if (basePath.startsWith(filePath)) {
 284  2
             final String diff = StringUtils.difference(filePath, basePath);
 285  2
             return toParentPath(diff);
 286   
         } else {
 287  2
             final int diffIndex = StringUtils.indexOfDifference(filePath,
 288   
                     basePath);
 289  2
             final String baseDiff = basePath.substring(diffIndex);
 290  2
             final String fileDiff = filePath.substring(diffIndex);
 291  2
             return toParentPath(FileConstants.FILE_SPARATOR_SLASH + baseDiff)
 292   
                     + FileConstants.FILE_SPARATOR_SLASH + fileDiff;
 293   
         }
 294   
     }
 295   
 
 296  4
     private static String toParentPath(final String diff) {
 297  4
         final int slashCount = StringUtils.countMatches(diff,
 298   
                 FileConstants.FILE_SPARATOR_SLASH);
 299  4
         return StringUtils.repeat(FileConstants.DIRECTORY_PARENT
 300   
                 + FileConstants.FILE_SPARATOR_SLASH, slashCount - 1)
 301   
                 + FileConstants.DIRECTORY_PARENT;
 302   
     }
 303   
 
 304   
     /**
 305   
      * ファイルの拡張子を取得する。
 306   
      */
 307  25
     public static String getExtension(String fileName) {
 308  25
         if (fileName == null)
 309  1
             return null;
 310  24
         String[] parsed = StringUtils.tokenizeToArray(fileName,
 311   
                 FileConstants.DELIM_EXT_STR);
 312  24
         if (parsed.length < 2) {
 313  8
             return null;
 314   
         } else {
 315  16
             final String result = (String) ArrayUtils.getLast(parsed);
 316  16
             if (replaceSeparatorSlash(result).indexOf(File.separatorChar) > -1)
 317  0
                 return "";
 318   
             else
 319  16
                 return result;
 320   
         }
 321   
     }
 322   
 
 323   
     /**
 324   
      * ファイル名から拡張子を取り除く
 325   
      * 
 326   
      * @param fileName
 327   
      * @return
 328   
      */
 329  18
     public static String removeExtension(String fileName) {
 330  18
         if (fileName == null)
 331  1
             return null;
 332  17
         final String ext = getExtension(fileName);
 333  17
         if (ext == null) {
 334  6
             return fileName;
 335   
         } else {
 336  11
             return fileName.substring(0, fileName.length() - ext.length() - 1);
 337   
         }
 338   
     }
 339   
 
 340   
     /**
 341   
      * ファイルの拡張子を取得する。
 342   
      */
 343  12
     public static String changeExtension(String fileName, String newExtension) {
 344  12
         if (fileName == null)
 345  2
             return null;
 346  10
         final String baseFileName = removeExtension(fileName);
 347  10
         if (StringUtils.isEmpty(newExtension)) {
 348  5
             return baseFileName;
 349   
         } else {
 350  5
             if (newExtension.charAt(0) != FileConstants.DELIM_EXT)
 351  5
                 newExtension = FileConstants.DELIM_EXT + newExtension;
 352  5
             return baseFileName + newExtension;
 353   
         }
 354   
     }
 355   
 
 356   
     /**
 357   
      * URLかどうかを判断する。
 358   
      * 
 359   
      * @param string
 360   
      * @return
 361   
      */
 362  8
     public static boolean isURL(String string) {
 363  8
         return string.indexOf("://") > -1;
 364   
     }
 365   
 
 366   
     /**
 367   
      * 区切り文字(Windowsなら';'、Unix系なら':')でファイル名群を切り分ける
 368   
      * 
 369   
      * @param fileNames
 370   
      * @return
 371   
      */
 372  4
     public static List parseFileNames(String fileNames) {
 373  4
         return StringUtils.tokenize(fileNames, File.pathSeparator);
 374   
     }
 375   
 
 376   
     /**
 377   
      * オブジェクトをFileとして返します。
 378   
      * 
 379   
      * @param obj
 380   
      * @return
 381   
      */
 382  4
     public static File toFile(Object obj) {
 383  4
         if (obj == null)
 384  1
             return null;
 385  3
         if ("".equals(obj))
 386  1
             return null;
 387  2
         if (obj instanceof File)
 388  1
             return (File) obj;
 389  1
         return new File(String.valueOf(obj));
 390   
     }
 391   
 }