001: package org.enhydra.server.util;
002:
003: import java.io.File;
004:
005: /**
006: * <p>Title: </p>
007: * <p>Description: </p>
008: * <p>Copyright: Copyright (c) 2002</p>
009: * <p>Company: </p>
010: * @author Damir Milovic
011: * @version 1.0
012: */
013:
014: public class PathUtil {
015:
016: private PathUtil() {
017: }
018:
019: /**
020: * Creates relative path from absolute. Resolve relative pathnames against
021: * parent directory.
022: * @param parentPath parent directory path.
023: * @param absolutePath absolute file path (directory or file).
024: * @return relative path if parent directory is included in absolutePath,
025: * otherwise return absolute path.
026: */
027: public static String makeRelativePath(String parentPath,
028: String absolutePath) {
029:
030: String relativePath = null;
031:
032: if (parentPath != null && absolutePath != null
033: && (parentPath.length() < absolutePath.length())) {
034: File absoluteFile = new File(absolutePath);
035: if (absoluteFile.isAbsolute()) {
036: File parentFile = new File(parentPath);
037: // parentFile must be absolute
038: if (parentFile.isAbsolute()) {
039: String dirPath = parentFile.getAbsolutePath()
040: .replace('\\', '/');
041: absolutePath = absolutePath.replace('\\', '/');
042: int dirLength = dirPath.length();
043: if (absolutePath.substring(0, dirLength)
044: .equalsIgnoreCase(dirPath)) {
045: //Cut parent path
046: relativePath = absolutePath
047: .substring(dirLength);
048: if (relativePath.startsWith("/")) {
049: relativePath = relativePath.substring(1);
050: }
051: }
052: } else {
053: relativePath = absoluteFile.getPath().replace('\\',
054: '/');
055: }
056: } else {
057: relativePath = absoluteFile.getPath()
058: .replace('\\', '/');
059: }
060: } else {
061: if (absolutePath != null) {
062: relativePath = absolutePath.replace('\\', '/');
063: }
064:
065: }
066: return relativePath;
067: }
068:
069: /**
070: * Creates absolute path from relative.
071: * @param parentPath root path (directory).
072: * @param relativePath relative path (directory or file).
073: * @return absolute path if <code>parentPath</code> is not <code>null</code> and it is absolute.
074: */
075: public static String makeAbsolutePath(String parentPath,
076: String relativePath) {
077: String absolutePath = relativePath;
078:
079: if (parentPath != null) {
080: parentPath = parentPath.replace('\\', '/');
081:
082: File parentFile = new File(parentPath);
083: File relativeFile = new File(relativePath);
084: File dir = null;
085: //Must create dirs if don't exist
086: parentFile.mkdirs();
087: if (parentFile.isAbsolute() && (!relativeFile.isAbsolute())) {
088: //Check dir
089: if (parentFile.isDirectory()) {
090: dir = parentFile;
091: } else {
092: dir = parentFile.getParentFile();
093: }
094: File absoluteFile = new File(dir, relativeFile
095: .getPath());
096: absolutePath = absoluteFile.getAbsolutePath();
097: absolutePath = absolutePath.replace('\\', '/');
098: }
099: }
100: return absolutePath;
101: }
102: }
|