KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > wsdl > util > RelativePath


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.ui.wsdl.util;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * this class provides functions used to generate a relative path
29  * from two absolute paths
30  * @author David M. Howard
31  */

32 public class RelativePath {
33     /**
34      * break a path down into individual elements and add to a list.
35      * example : if a path is /a/b/c/d.txt, the breakdown will be [d.txt,c,b,a]
36      * @param f input file
37      * @return a List collection with the individual elements of the path in
38        reverse order
39      */

40     private static List JavaDoc getPathList(File JavaDoc f) {
41         List JavaDoc l = new ArrayList JavaDoc();
42         File JavaDoc r;
43         try {
44             r = f.getCanonicalFile();
45             while(r != null) {
46                 l.add(r.getName());
47                 r = r.getParentFile();
48             }
49         }
50         catch (IOException JavaDoc e) {
51             e.printStackTrace();
52             l = null;
53         }
54         return l;
55     }
56
57     /**
58      * figure out a string representing the relative path of
59      * 'f' with respect to 'r'
60      * @param r home path
61      * @param f path of file
62      */

63     private static String JavaDoc matchPathLists(List JavaDoc r,List JavaDoc f) {
64         int i;
65         int j;
66         StringBuffer JavaDoc s;
67         // start at the beginning of the lists
68
// iterate while both lists are equal
69
s = new StringBuffer JavaDoc();
70         i = r.size()-1;
71         j = f.size()-1;
72
73         // first eliminate common root
74
while((i >= 0)&&(j >= 0)&&(r.get(i).equals(f.get(j)))) {
75             i--;
76             j--;
77         }
78
79         // for each remaining level in the home path, add a ..
80
for(;i>=0;i--) {
81             //s += ".." + File.separator;
82
//ritesh: always use forward slash since using backward slash for windows
83
//create problem creating URI object in engine. URI always expect forwaard slash
84
s.append("..").append("/");
85         }
86
87         // for each level in the file path, add the path
88
for(;j>=1;j--) {
89             //s += f.get(j) + File.separator;
90
//ritesh: always use forward slash since using backward slash for windows
91
//create problem creating URI object in engine. URI always expect forwaard slash
92
s.append(f.get(j)).append("/");
93         }
94
95         // file name
96
s.append(f.get(j));
97         return s.toString();
98     }
99
100     /**
101      * get relative path of File 'f' with respect to 'home' directory
102      * example : home = /a/b/c
103      * f = /a/d/e/x.txt
104      * s = getRelativePath(home,f) = ../../d/e/x.txt
105      * @param home base path, should be a directory, not a file, or it doesn't
106          make sense
107      * @param f file to generate path for
108      * @return path from home to f as a string
109      */

110     public static String JavaDoc getRelativePath(File JavaDoc home,File JavaDoc f){
111         File JavaDoc r;
112         List JavaDoc homelist;
113         List JavaDoc filelist;
114         String JavaDoc s;
115
116         homelist = getPathList(home);
117         filelist = getPathList(f);
118         s = matchPathLists(homelist,filelist);
119
120         return s;
121     }
122
123     /**
124        * test the function
125        */

126     public static void main(String JavaDoc args[]) {
127         if (args.length != 2) {
128             System.out.println("RelativePath <home> <file>");
129             return;
130         }
131         System.out.println("home = " + args[0]);
132         System.out.println("file = " + args[1]);
133         System.out.println("path = " + getRelativePath(new File JavaDoc(args[0]),new
134                 File JavaDoc(args[1])));
135     }
136 }
137
138
Popular Tags