1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.uruma.util;
17
18 import org.seasar.framework.util.StringUtil;
19
20
21
22
23
24
25 public class PathUtil {
26 private PathUtil() {
27
28 }
29
30 private static final String YEN_SIGN = "\\";
31
32 private static final String SLASH = "/";
33
34 private static final char SLASH_CHAR = '/';
35
36 private static final String PERIOD = ".";
37
38 private static final String NULL_STRING = "";
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public static String createPath(String basePath, String relPath) {
61 basePath = replaceSeparator(basePath);
62 relPath = replaceSeparator(relPath);
63 String path = "";
64 if (relPath.charAt(0) == SLASH_CHAR) {
65 basePath = NULL_STRING;
66 }
67 if (!StringUtil.isEmpty(basePath)) {
68 if (!relPath.startsWith(basePath)) {
69 path += basePath;
70 if (!path.endsWith(SLASH)) {
71 path += SLASH;
72 }
73 }
74 }
75 path += relPath;
76 return path;
77 }
78
79
80
81
82
83
84
85
86 public static String replaceSeparator(final String path) {
87 if (path != null) {
88 return StringUtil.replace(path, YEN_SIGN, SLASH);
89 } else {
90 return NULL_STRING;
91 }
92 }
93
94
95
96
97
98
99
100
101
102 public static String getRelativePath(final String basePath,
103 final String path) {
104 AssertionUtil.assertNotNull("basePath", basePath);
105 AssertionUtil.assertNotNull("path", path);
106
107 if (path.startsWith(basePath)) {
108 return path.substring(basePath.length(), path.length());
109 } else {
110 return path;
111 }
112 }
113
114
115
116
117
118
119
120
121
122
123 public static String getParent(final String path) {
124 if (path != null) {
125 int slashIndex = path.lastIndexOf(SLASH);
126 int yenIndex = path.lastIndexOf(YEN_SIGN);
127 int pos = (slashIndex > yenIndex) ? slashIndex : yenIndex;
128 if (pos >= 0) {
129 return path.substring(0, pos);
130 } else {
131 return path;
132 }
133 } else {
134 return null;
135 }
136 }
137
138
139
140
141
142
143
144
145
146
147 public static String getFileName(final String path) {
148 if (path != null) {
149 int slashIndex = path.lastIndexOf(SLASH);
150 int yenIndex = path.lastIndexOf(YEN_SIGN);
151 int pos = (slashIndex > yenIndex) ? slashIndex : yenIndex;
152 return path.substring(pos + 1, path.length());
153 } else {
154 return null;
155 }
156 }
157
158
159
160
161
162
163
164
165 public static String getBaseName(final String fileName) {
166 if (fileName != null) {
167 return StringUtil.substringFromLast(fileName, PERIOD);
168 } else {
169 return null;
170 }
171 }
172
173
174
175
176
177
178
179
180 public static String getExt(final String path) {
181 if (path != null) {
182 if (path.indexOf(PERIOD) > 0) {
183 return StringUtil.substringToLast(path, PERIOD);
184 } else {
185 return NULL_STRING;
186 }
187 } else {
188 return null;
189 }
190 }
191 }