| 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 | 0 | private PathUtil() { |
| 27 | |
|
| 28 | 0 | } |
| 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 | 92 | basePath = replaceSeparator(basePath); |
| 62 | 92 | relPath = replaceSeparator(relPath); |
| 63 | 92 | String path = ""; |
| 64 | 92 | if (relPath.charAt(0) == SLASH_CHAR) { |
| 65 | 64 | basePath = NULL_STRING; |
| 66 | |
} |
| 67 | 92 | if (!StringUtil.isEmpty(basePath)) { |
| 68 | 24 | if (!relPath.startsWith(basePath)) { |
| 69 | 20 | path += basePath; |
| 70 | 20 | if (!path.endsWith(SLASH)) { |
| 71 | 16 | path += SLASH; |
| 72 | |
} |
| 73 | |
} |
| 74 | |
} |
| 75 | 92 | path += relPath; |
| 76 | 92 | return path; |
| 77 | |
} |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public static String replaceSeparator(final String path) { |
| 87 | 192 | if (path != null) { |
| 88 | 188 | return StringUtil.replace(path, YEN_SIGN, SLASH); |
| 89 | |
} else { |
| 90 | 4 | 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 | 20 | AssertionUtil.assertNotNull("basePath", basePath); |
| 105 | 16 | AssertionUtil.assertNotNull("path", path); |
| 106 | |
|
| 107 | 12 | if (path.startsWith(basePath)) { |
| 108 | 8 | return path.substring(basePath.length(), path.length()); |
| 109 | |
} else { |
| 110 | 4 | return path; |
| 111 | |
} |
| 112 | |
} |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public static String getParent(final String path) { |
| 124 | 32 | if (path != null) { |
| 125 | 28 | int slashIndex = path.lastIndexOf(SLASH); |
| 126 | 28 | int yenIndex = path.lastIndexOf(YEN_SIGN); |
| 127 | 28 | int pos = (slashIndex > yenIndex) ? slashIndex : yenIndex; |
| 128 | 28 | if (pos >= 0) { |
| 129 | 24 | return path.substring(0, pos); |
| 130 | |
} else { |
| 131 | 4 | return path; |
| 132 | |
} |
| 133 | |
} else { |
| 134 | 4 | return null; |
| 135 | |
} |
| 136 | |
} |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public static String getFileName(final String path) { |
| 148 | 256 | if (path != null) { |
| 149 | 252 | int slashIndex = path.lastIndexOf(SLASH); |
| 150 | 252 | int yenIndex = path.lastIndexOf(YEN_SIGN); |
| 151 | 252 | int pos = (slashIndex > yenIndex) ? slashIndex : yenIndex; |
| 152 | 252 | return path.substring(pos + 1, path.length()); |
| 153 | |
} else { |
| 154 | 4 | return null; |
| 155 | |
} |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
public static String getBaseName(final String fileName) { |
| 166 | 244 | if (fileName != null) { |
| 167 | 240 | return StringUtil.substringFromLast(fileName, PERIOD); |
| 168 | |
} else { |
| 169 | 4 | return null; |
| 170 | |
} |
| 171 | |
} |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
public static String getExt(final String path) { |
| 181 | 20 | if (path != null) { |
| 182 | 16 | if (path.indexOf(PERIOD) > 0) { |
| 183 | 8 | return StringUtil.substringToLast(path, PERIOD); |
| 184 | |
} else { |
| 185 | 8 | return NULL_STRING; |
| 186 | |
} |
| 187 | |
} else { |
| 188 | 4 | return null; |
| 189 | |
} |
| 190 | |
} |
| 191 | |
} |