mquigley avatar mquigley committed 8975126

New static method to get the current working directory. New method to simplify ('.'/'..' handling) paths.

Comments (0)

Files changed (2)

src/main/java/com/quigley/filesystem/FilesystemPath.java

 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.ListIterator;
 
 public class FilesystemPath implements Comparable<FilesystemPath> {
 	
     	
     	return outputPath;
     }
+    
+    public FilesystemPath simplify() {
+    	List<String> elementsCopy = new ArrayList<String>(elements);
+    	ListIterator<String> i = elementsCopy.listIterator();
+    	boolean saw = false;
+    	while(i.hasNext()) {
+    		String value = i.next();
+    		if(value.equals(".")) {
+    			i.remove();
+    		} else
+    		if(value.equals("..")) {
+    			if(saw) {
+    				if(i.hasPrevious()) {
+    					i.remove();
+    					i.previous();
+    					i.remove();
+    				}
+    			}
+    		} else {
+    			saw = true;
+    		}
+    	}
+    	return new FilesystemPath(elementsCopy);
+    }
 
     /*
      * Component Accessors
      * Static Operations
      */
     
+    public static FilesystemPath currentWorkingDirectory() {
+    	File cwd = new File(".");
+    	return new FilesystemPath(cwd.getAbsolutePath()).simplify();
+    }
+    
     public static String normalize(String pathString) {
         while(pathString.indexOf("\\") != -1) {
             pathString = pathString.replace('\\', '/');

src/test/java/com/quigley/filesystem/FilesystemPathTest.java

     	assertEquals("a", path.getLast());
     	assertEquals("/a", path.toString());
     }
+    
+    @Test
+    public void testSimplify() {
+    	FilesystemPath path = new FilesystemPath("a/b/../c");
+    	FilesystemPath simplified = path.simplify();
+    	assertNotNull(simplified);
+    	assertEquals("a/c", simplified.toString());
+    	
+    	path = new FilesystemPath("../../a");
+    	simplified = path.simplify();
+    	assertEquals(path.toString(), simplified.toString());
+    	
+    	path = new FilesystemPath("a/..");
+    	simplified = path.simplify();
+    	assertEquals("", simplified.toString());
+    	
+    	path = new FilesystemPath(".");
+    	simplified = path.simplify();
+    	assertEquals("", simplified.toString());
+    	
+    	path = new FilesystemPath("../a/b/../c/d/../e/f/../../g");
+    	simplified = path.simplify();
+    	assertEquals("../a/c/g", simplified.toString());
+    }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.