001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.project;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: public class PathParser {
027:
028: public static String getRelativeFile(File homeDirectory, File file) {
029: try {
030: if (file.isDirectory()) {
031: return getRelativePath(homeDirectory, file);
032: } else {
033: return getRelativePath(homeDirectory, file
034: .getParentFile())
035: + file.getCanonicalFile().getName();
036: }
037: } catch (Exception e) {
038: e.printStackTrace();
039: return file.getPath();
040: }
041: }
042:
043: public static String getRelativePath(File homeDirectory, File path) {
044: try {
045: if (!homeDirectory.exists()) {
046: homeDirectory = homeDirectory.getParentFile();
047: if (homeDirectory == null || !homeDirectory.exists())
048: return path.getPath();
049: }
050:
051: /**
052: * this handles the case where the Drive letters are different on
053: * windows
054: */
055: String homepath = homeDirectory.getCanonicalPath();
056: String filepath = path.getCanonicalPath();
057: if (homepath.length() == 0 || filepath.length() == 0)
058: return filepath;
059: if (homepath.charAt(0) != filepath.charAt(0))
060: return filepath;
061:
062: List home_elements = getPathList(homeDirectory);
063: List path_elements = getPathList(path);
064: return matchPathElements(home_elements, path_elements);
065: } catch (Exception e) {
066: e.printStackTrace();
067: return path.getPath();
068: }
069: }
070:
071: private static List getPathList(File f) {
072: ArrayList elements = new ArrayList();
073: try {
074: f = f.getCanonicalFile();
075: if (!f.isDirectory())
076: f = f.getParentFile();
077:
078: while (f != null) {
079: elements.add(0, f.getName());
080: f = f.getParentFile();
081: }
082: } catch (IOException e) {
083: e.printStackTrace();
084: elements = null;
085: }
086: return elements;
087: }
088:
089: private static String matchPathElements(List home, List file) {
090: int sz1 = home.size();
091: int sz2 = file.size();
092:
093: int count = 0;
094: while (count < sz1 && count < sz2) {
095: String e1 = (String) home.get(count);
096: String e2 = (String) file.get(count);
097:
098: if (e1 == null || e2 == null)
099: break;
100:
101: if (!e1.equals(e2))
102: break;
103:
104: count++;
105: }
106:
107: StringBuffer result = new StringBuffer();
108: if (count > 0) {
109: for (int index = (home.size() - count); index > 0; index--) {
110: result.append("..");
111: result.append(File.separatorChar);
112: }
113: }
114:
115: for (int index = count; index < file.size(); index++) {
116: result.append(file.get(index));
117: result.append(File.separatorChar);
118: }
119:
120: if (result.length() == 0)
121: result.append(".");
122:
123: return result.toString();
124: }
125: }
|