KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > UriParser


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.util;
12
13 import org.mmbase.util.logging.Logger;
14 import org.mmbase.util.logging.Logging;
15
16 /**
17  * For an important part stolen from jakarta vfs (only one function).
18  * @javadoc
19  *
20  * @author Michiel Meeuwissen
21  * @since MMBase-1.7
22  * @version $Id: UriParser.java,v 1.3 2004/09/30 17:19:50 pierre Exp $
23  */

24 public class UriParser {
25
26     private static final Logger log = Logging.getLoggerInstance(UriParser.class);
27
28     final static char separatorChar = java.io.File.separator.charAt(0); // '\' for windows '/' for other oses.
29

30      /**
31      * Converts an absolute path into a relative path.
32      *
33      * @param basePath The base path.
34      * @param path The path to convert.
35      */

36     static public String JavaDoc makeRelative(final String JavaDoc basePath, final String JavaDoc path ) {
37         if (log.isDebugEnabled()) {
38             log.debug("converting " + path + " relative to " + basePath);
39         }
40         // Calculate the common prefix
41
final int basePathLen = basePath.length();
42         final int pathLen = path.length();
43
44         // Deal with root
45
if ( basePathLen == 1 && pathLen == 1 ) {
46             return ".";
47         } else if ( basePathLen == 1 ) {
48             return path.substring(1);
49         }
50
51         final int maxlen = Math.min( basePathLen, pathLen );
52         int pos = 0;
53         for ( ; pos < maxlen && basePath.charAt( pos ) == path.charAt( pos ); pos++ ) {
54         }
55
56         if ( pos == basePathLen && pos == pathLen ) {
57             // Same names
58
return ".";
59         } else if ( pos == basePathLen && pos < pathLen && path.charAt( pos ) == separatorChar ) {
60             // A descendent of the base path
61
return path.substring( pos + 1 );
62         }
63
64         // Strip the common prefix off the path
65
final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
66         if ( pathLen > 1 && ( pos < pathLen || basePath.charAt( pos ) != separatorChar ) ) {
67             // Not a direct ancestor, need to back up
68
pos = basePath.lastIndexOf( separatorChar, pos );
69             buffer.append( path.substring( pos ) );
70         }
71
72         // Prepend a '../' for each element in the base path past the common
73
// prefix
74
buffer.insert( 0, ".." );
75         pos = basePath.indexOf( separatorChar, pos + 1 );
76         while ( pos != -1 ) {
77             buffer.insert( 0, "../" );
78             pos = basePath.indexOf( separatorChar, pos + 1 );
79         }
80         if (log.isDebugEnabled()) {
81             log.debug("is: " + buffer);
82         }
83
84         return buffer.toString();
85     }
86 }
87
Popular Tags