View Javadoc

1   package twcsckernel.projectbase.io;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.regex.Pattern;
6   
7   /***
8    * Menedżer ścieżki root użytkownika.
9    * 
10   * @author VMD Group
11   * 
12   */
13  public class UserRootPathManager {
14  
15  	private String rootPath;
16  
17  	private int rootPathLength;
18  
19  	// wyr. reg. do zmieniania \ na /
20  	private static Pattern regex = Pattern.compile("////");
21  
22  	private static final String slash = "/";
23  
24  	/***
25  	 * Konstruktor menedżera ścieżki root. Podana ścieżka musi ustnieć. Ścieżka
26  	 * jest przekształcana na postać kononiczną (absolutną i unikalną) i w
27  	 * takiej postaci będzie używana.
28  	 * 
29  	 * @param rootPath -
30  	 *            ścieżka root
31  	 * @throws IOException -
32  	 *             wyjątek rzucany w przypadku niepoprawnej ścieżki root
33  	 */
34  	public void setUserRootPath(String rootPath) throws IOException {
35  		File rootFile = new File(rootPath);
36  		if (!rootFile.exists())
37  			throw new IOException("Incorrect root path: " + rootPath);
38  		this.rootPath = rootFile.getCanonicalPath();
39  		rootPathLength = this.rootPath.length();
40  	}
41  
42  	/***
43  	 * Metoda konwertuje ścieżkę bezwzględną systemu plików na ścieżkę
44  	 * bezwzględną użytkownika. Separatorem w zwracanej ścieżce zawsze
45  	 * jest "/".
46  	 * 
47  	 * @param globalPath -
48  	 *            ścieżka bezwzględna systemu plików
49  	 * @return - ścieżka bezwzględna użytkownika
50  	 */
51  	public String getUserAbsolutePath(String globalPath) {
52  		if (!globalPath.startsWith(rootPath))
53  			return null;
54  		String result = correctSlash(globalPath.substring(rootPathLength));
55  		if (!result.startsWith(slash))
56  			result = slash + result;
57  		return result;
58  	}
59  
60  	private static String correctSlash(String root) {
61  		return regex.matcher(root).replaceAll(slash);
62  	}
63  
64  	/***
65  	 * Metoda konwertuje ścieżkę bezwzględną systemu plików na ścieżkę
66  	 * bezwzględną użytkownika.
67  	 * 
68  	 * @param file -
69  	 *            plik
70  	 * @return - ścieżka bezwzględna użytkownika
71  	 */
72  	public String getUserAbsolutePath(File file) {
73  		return getUserAbsolutePath(file.getAbsolutePath());
74  	}
75  
76  	/***
77  	 * Metoda konwertuje ścieżkę bezwzględną użytkownika na ścieżkę bezwzględną
78  	 * systemu plików.
79  	 * 
80  	 * @param userPath -
81  	 *            ścieżka bezwzględna użytkownika
82  	 * @return - ścieżka bezwzględna systemu plików
83  	 */
84  	public String getGlobalAbsolutePath(String userPath) {
85  		return correctSlash(rootPath + slash + userPath);
86  	}
87  
88  	/***
89  	 * Metoda tworzy obiekt pliku mając do dyspozycji bezwzględną ścieżkę
90  	 * użytkownika.
91  	 * 
92  	 * @param userPath -
93  	 *            bzw. ścieżka użytkownika
94  	 * @return - obiekt pliku (java.io.File)
95  	 * @throws IOException -
96  	 *             wyjątek rzucany jeśli niemożliwe jest uzyskanie kanonicznej
97  	 *             postaci pliku
98  	 */
99  	public File getFile(String userPath) throws IOException {
100 
101 		return new File(getGlobalAbsolutePath(userPath)).getCanonicalFile();
102 	}
103 
104 	/***
105 	 * @return zwraca ścieżkę główną użytkownika
106 	 */
107 	public String getRootPath() {
108 		return rootPath;
109 	}
110 }