View Javadoc

1   package fr.in2p3.jsaga.impl.url;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   /* ***************************************************
7    * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
8    * ***             http://cc.in2p3.fr/             ***
9    * ***************************************************
10   * File:   UniversalFile
11   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
12   * ***************************************************
13   * Description:                                      */
14  
15  /**
16   *
17   */
18  public class UniversalFile {
19      private File m_file;
20      /**
21       * Indicate if this file is absolute
22       */
23      private boolean m_isAbsolute;
24      
25      /**
26       * Indicate if this file is a directory
27       */
28      private boolean m_isDir;
29  
30      /**
31       * Build an universal file (Windows and Unix)
32       * @param path
33       * 
34       * If the path ends with / or \, it is a directory
35       * On Unix, if the path starts with /, the file is considered as absolute
36       * On Windows, if the path starts with / or \ or X:/, the file is considered as absolute
37       */
38      public UniversalFile(String path) {
39  		// remove leading duplicated / 
40          while (path.startsWith("//")) {
41          	path = path.substring(1);
42          }
43          m_file = new File(path);
44          m_isAbsolute = m_file.isAbsolute() || path.startsWith("/") || path.startsWith("\\");
45          m_isDir = path.endsWith("/") || path.endsWith("\\");
46      }
47  
48      /**
49       * returns the path of the file with all separators converted as / 
50       * and with a trailing / if the file is a directory 
51       * @return the path of the file
52       */
53      public String getPath() {
54          return m_file.getPath().replace("\\", "/") + (m_isDir?"/":"");
55      }
56  
57      /**
58       * returns the parent directory of the file with all separators converted as / and with a trailing /
59       * @return the path of the parent directory
60       */
61      public String getParent() {
62          String parent = m_file.getParent();
63          if (parent == null) {
64              return "./";
65          }
66          parent = parent.replace("\\", "/");
67          if (! "/".equals(parent)) {
68              parent = parent + "/";
69          }
70          return parent;
71      }
72  
73      /**
74       * returns the canonical path of the file with all separators converted as / 
75       * and with a trailing / if the file is a directory. 
76       * It removes all "./" and remove recursively all "dir/../" sets
77       * @return the canonical path of the file
78       * @throws IOException
79       * 
80       * To compute the canonical path, the method java.io.File.getCanonicalPath() is not used
81       * because this method first converts this pathname to absolute form if necessary, as if by 
82       * invoking the getAbsolutePath() method and some characters are not supported by Windows
83       * 
84       */
85      public String getCanonicalPath() throws IOException {
86      	String canon;
87      	java.util.Stack path = new java.util.Stack();
88      	for (String pathElement : getPath().split("/")) {
89      		if (pathElement.equals("..")) {  
90      			// remove last element if stack is not empty and last element is not ".." itself
91      			if (path.empty() || path.peek().equals("..")) {
92      				path.add(pathElement);
93      			} else {
94      				path.pop();
95      			}
96      		} else if (! pathElement.equals(".") && !pathElement.equals("")) { // NOT "." and not ""
97      			path.add(pathElement);
98      		} // otherwise (".." or ""): nothing to do
99      	}
100     	if (getPath().startsWith("/")) {
101     		canon = "/";
102     	} else {
103     		canon = "";
104     	}
105     	for (int i = 0; i < path.size(); i++) {
106     		canon += (String)path.get(i);
107     		if (i == path.size()-1) { // last element : add / only for Dir
108     			canon += m_isDir?"/":"";
109     		} else {
110     			canon += "/";
111     		}
112     	}
113     	// if everything was removed, it means the path is "./"
114         if (canon.equals("")) { canon = "./";}
115         return canon;
116     }
117 
118     public boolean isAbsolute() {
119         return m_isAbsolute;
120     }
121 
122     public boolean isDirectory() {
123         return m_isDir;
124     }
125 
126 }