1 2 24 package org.enhydra.tool.common; 25 26 import java.io.File ; 28 29 public class PathHandle { 31 private String path = null; 32 33 public static PathHandle createPathHandle(File inFile) { 34 PathHandle handle = null; 35 36 if (inFile == null) { 37 handle = new PathHandle(); 38 } else { 39 handle = PathHandle.createPathHandle(inFile.getAbsolutePath()); 40 } 41 return handle; 42 } 43 44 public static PathHandle createPathHandle(String inPath) { 45 PathHandle handle = null; 46 47 handle = new PathHandle(); 48 handle.setPath(inPath); 49 return handle; 50 } 51 52 public static String createPathString(String inPath) { 53 PathHandle handle = null; 54 55 handle = PathHandle.createPathHandle(inPath); 56 return handle.getPath(); 57 } 58 59 public static String createPathString(File inFile) { 60 PathHandle handle = null; 61 62 handle = PathHandle.createPathHandle(inFile); 63 return handle.getPath(); 64 } 65 66 public PathHandle() {} 67 68 public boolean isFile() { 69 return FileUtil.isFile(getPath()); 70 } 71 72 public boolean isDirectory() { 73 return FileUtil.isDirectory(getPath()); 74 } 75 76 public PathHandle getParent() { 77 PathHandle parent = null; 78 File parentFile = null; 79 80 if (getFile() == null) { 81 parent = null; 82 } else { 83 parentFile = getFile().getParentFile(); 84 if (parentFile == null) { 85 parent = null; 86 } else { 87 parent = PathHandle.createPathHandle(parentFile); 88 } 89 } 90 return parent; 91 } 92 93 public File getFile() { 94 File file = null; 95 96 if (getPath() == null) { 97 98 } else { 100 file = new File (getPath()); 101 } 102 return file; 103 } 104 105 public void setPath(final String inPath) { 106 if (inPath == null) { 107 108 } else if (inPath.trim().length() == 0) { 110 path = new String (); 111 } else { 112 path = new String (inPath); 113 if ((path.indexOf('\\') == -1) && (path.indexOf('/') == -1)) { 114 path = "./" + path; 115 } 116 path = FileUtil.toCanonicalPath(path); 117 path = path.replace('\\', '/').trim(); 118 } 119 } 120 121 public String getPath() { 122 return path; 123 } 124 125 public String toString() { 126 return getPath(); 127 } 128 129 public boolean parentOf(File childFile) { 130 PathHandle childHandle = null; 131 132 childHandle = PathHandle.createPathHandle(childFile); 133 return parentOf(childHandle); 134 } 135 136 public boolean parentOf(String childPath) { 137 PathHandle childHandle = null; 138 139 childHandle = PathHandle.createPathHandle(childPath); 140 return parentOf(childHandle); 141 } 142 143 public boolean parentOf(PathHandle childHandle) { 144 boolean isParent = false; 145 146 if (getPath() == null || childHandle.getPath() == null) { 147 148 } else { 150 File parentFile = null; 151 152 parentFile = new File (getPath()); 153 if (parentFile.isDirectory()) { 154 isParent = 155 childHandle.getComparePath().startsWith(getComparePath() 156 + '/'); 157 } 158 } 159 return isParent; 160 } 161 162 public boolean equals(String comparePath) { 163 PathHandle compareHandle = null; 164 165 compareHandle = PathHandle.createPathHandle(comparePath); 166 return equals(compareHandle); 167 } 168 169 public boolean equals(PathHandle compare) { 170 boolean equal = false; 171 172 if (getPath() == null || compare == null 173 || compare.getPath() == null) { 174 175 } else { 177 equal = getComparePath().equals(compare.getComparePath()); 178 } 179 return equal; 180 } 181 182 public boolean hasExtension(String [] exts) { 183 boolean has = false; 184 185 for (int i = 0; i < exts.length; i++) { 186 if (hasExtension(exts[i])) { 187 has = true; 188 break; 189 } 190 } 191 return has; 192 } 193 194 public boolean hasExtension(String ext) { 195 boolean has = false; 196 197 if (getPath() == null || ext == null) { 198 199 } else { 201 if (isWindows()) { 202 has = ext.equalsIgnoreCase(getExtension()); 203 } else { 204 has = ext.equals(getExtension()); 205 } 206 } 207 return has; 208 } 209 210 public boolean endsWith(String [] inPaths) { 211 boolean endsWith = false; 212 213 if (inPaths == null) { 214 215 } else { 217 for (int i = 0; i < inPaths.length; i++) { 218 if (endsWith(inPaths[i])) { 219 endsWith = true; 220 break; 221 } 222 } 223 } 224 return endsWith; 225 } 226 227 public boolean endsWith(String inPath) { 228 boolean endsWith = false; 229 230 if (inPath == null || getPath() == null) { 231 232 } else { 234 inPath = inPath.replace('\\', '/'); 235 if (isWindows()) { 236 endsWith = getComparePath().endsWith(inPath.toLowerCase()); 237 } else { 238 endsWith = getComparePath().endsWith(inPath); 239 } 240 } 241 return endsWith; 242 } 243 244 public boolean isEmpty() { 245 boolean empty = false; 246 247 if (getPath() == null) { 248 empty = true; 249 } else { 250 empty = (getPath().trim().length() == 0); 251 } 252 return empty; 253 } 254 255 public String getExtension() { 256 String ext = new String (); 257 int index = -1; 258 259 if (getPath() == null) { 260 261 } else { 263 index = getPath().lastIndexOf('.'); 264 if (index > getPath().lastIndexOf('/')) { 265 if ((index > -1) && (getPath().length() > index)) { 266 ext = getPath().substring(index + 1); 267 } 268 } 269 } 270 return ext; 271 } 272 273 public void setExtension(String newExt) { 274 String ext = new String (); 275 276 if (getPath() == null) { 277 278 } else { 280 ext = getExtension(); 281 StringBuffer buf = new StringBuffer (); 282 283 if (ext.length() > 0) { 284 buf.append(path.substring(0, path.length() - ext.length())); 285 } else { 286 buf.append(path); 287 buf.append('.'); 288 } 289 buf.append(newExt); 290 setPath(buf.toString()); 291 } 292 } 293 294 public PathHandle expandRelativePath(String inPath) { 295 PathHandle out = null; 296 PathHandle dir = null; 297 298 if (isFile()) { 299 dir = getParent(); 300 } else { 301 dir = this; 302 } 303 if (inPath == null) { 304 out = null; 305 } else if (dir.getPath() == null) { 306 out = PathHandle.createPathHandle(inPath); 307 } else { 308 StringBuffer outPath = new StringBuffer (); 309 310 inPath = inPath.replace('\\', '/'); 311 if ((inPath.length() == 1) && (inPath.charAt(0) == '.')) { 312 outPath.append(dir.getPath()); 313 } else if ((inPath.length() > 0) && (inPath.charAt(0) == '.')) { 314 315 outPath.append(dir.getPath()); 317 outPath.append('/'); 318 if ((inPath.length() > 1) && (inPath.charAt(1) == '.')) { 319 outPath.append(inPath); 320 } else { 321 outPath.append(inPath.substring(2)); 322 } 323 } else { 324 outPath.append(inPath); 325 } 326 out = PathHandle.createPathHandle(outPath.toString()); 327 } 328 return out; 329 } 330 331 public String getRelativePath(String in) { 332 String relPath = new String (); 333 if ((in.length() > 0) && (in.charAt(0) == '.')) { 334 relPath = in.replace('\\','/'); 335 } else { 336 relPath = getRelativePath(PathHandle.createPathHandle(in)); 337 } 338 return relPath; 339 } 340 341 public String getRelativePath(PathHandle inPath) { 342 String relPath = new String (); 343 PathHandle dir = null; 344 345 if (isFile()) { 346 dir = getParent(); 347 } else { 348 dir = this; 349 } 350 if (dir.getPath() == null) { 351 relPath = inPath.getPath(); 352 } else { 353 StringBuffer relBuf = new StringBuffer (); 354 355 if (dir.equals(inPath)) { 356 relBuf.append('.'); 357 relBuf.append('/'); 358 } else if (dir.parentOf(inPath)) { 359 relBuf.append('.'); 360 relBuf.append('/'); 361 relBuf.append(inPath.getPath().substring(dir.getPath().length() 362 + 1)); 363 } else { 364 relBuf.append(inPath); 365 } 366 relPath = relBuf.toString(); 367 } 368 369 return relPath.replace('\\', '/'); 372 } 373 374 private String getComparePath() { 375 String thisPath = getPath(); 376 377 if (getPath() == null) { 378 379 } else if (isWindows()) { 381 thisPath = getPath().toLowerCase(); 382 } else { 383 thisPath = new String (getPath()); 384 } 385 return thisPath; 386 } 387 388 private boolean isWindows() { 389 return File.separatorChar == '\\'; 390 } 391 392 } 393 | Popular Tags |