awesome assets from gffs code
[feisty_meow.git] / kona / src / org / gffs / filesystem / FileSystemHelper.java
1 package org.gffs.filesystem;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 public class FileSystemHelper
12 {
13         private static Log _logger = LogFactory.getLog(FileSystemHelper.class);
14
15         /**
16          * returns true if the path contains a symbolic link anywhere within it.
17          */
18         public static boolean pathContainsLink(String path) throws FileNotFoundException
19         {
20                 // replace any backslashes with forward slashes.
21                 path = path.replaceAll("\\+", "/");
22
23                 // make sure path is absolute.
24                 if (!path.startsWith("/")) {
25                         String msg = "path passed in was not absolute: '" + path + "'";
26                         _logger.error(msg);
27                         throw new FileNotFoundException(msg);
28                 }
29
30                 // replace any double slashes with single ones.
31                 path = path.replaceAll("//", "/");
32                 String[] components = path.split("/");
33
34                 String currentPath = ""; // never expected to be a link.
35                 for (String component : components) {
36                         currentPath = currentPath + "/" + component;
37                         if (isFileSymLink(new File(currentPath))) {
38                                 return true;
39                         }
40                 }
41                 return false;
42
43                 /*
44                  * future: this could be more useful if it returned the position of the link as a component in path, but then we also need to accept a
45                  * starting point for the link searching so they can find all of them.
46                  */
47         }
48
49         /**
50          * returns true if the path specified is actually a symbolic link.
51          */
52         public static boolean isFileSymLink(File path)
53         {
54                 Path nioPath = path.toPath();
55                 return Files.isSymbolicLink(nioPath);
56         }
57
58         /**
59          * makes the string a more friendly filesystem path for java. this includes turning backslashes into forward slashes.
60          */
61         public static String sanitizeFilename(String toClean)
62         {
63                 if (toClean == null)
64                         return toClean; // can't fix nothing.
65                 // _logger.debug("before='" + toClean + "'");
66
67                 String toReturn = toClean.replace('\\', '/');
68
69                 // future: any other cleanings we should do on the path?
70
71                 // _logger.debug("after='" + toReturn + "'");
72                 return toReturn;
73         }
74
75         static public void main(String[] args)
76         {
77                 String uglyPath = "C:\\Program Files\\GenesisII\\";
78                 String fixedPath = sanitizeFilename(uglyPath);
79                 String expectedPath = "C:/Program Files/GenesisII/";
80                 if (!fixedPath.equals(expectedPath)) {
81                         System.err.println("FAILURE IN PARSING!  result is not right: '" + fixedPath + "' when it should be '" + expectedPath);
82                         System.exit(1);
83                 } else {
84                         System.err.println("parsing occurred as expected.");
85                 }
86                 System.exit(0);
87         }
88
89 }