1 package fr.in2p3.jsaga.impl.url;
2
3 import java.io.File;
4 import java.io.IOException;
5
6
7
8
9
10
11
12
13
14
15
16
17
18 public class UniversalFile {
19 private File m_file;
20
21
22
23 private boolean m_isAbsolute;
24
25
26
27
28 private boolean m_isDir;
29
30
31
32
33
34
35
36
37
38 public UniversalFile(String path) {
39
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
50
51
52
53 public String getPath() {
54 return m_file.getPath().replace("\\", "/") + (m_isDir?"/":"");
55 }
56
57
58
59
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
75
76
77
78
79
80
81
82
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
91 if (path.empty() || path.peek().equals("..")) {
92 path.add(pathElement);
93 } else {
94 path.pop();
95 }
96 } else if (! pathElement.equals(".") && !pathElement.equals("")) {
97 path.add(pathElement);
98 }
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) {
108 canon += m_isDir?"/":"";
109 } else {
110 canon += "/";
111 }
112 }
113
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 }