1 10 11 package org.mmbase.util; 12 13 import org.mmbase.util.logging.Logger; 14 import org.mmbase.util.logging.Logging; 15 16 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); 30 36 static public String makeRelative(final String basePath, final String path ) { 37 if (log.isDebugEnabled()) { 38 log.debug("converting " + path + " relative to " + basePath); 39 } 40 final int basePathLen = basePath.length(); 42 final int pathLen = path.length(); 43 44 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 return "."; 59 } else if ( pos == basePathLen && pos < pathLen && path.charAt( pos ) == separatorChar ) { 60 return path.substring( pos + 1 ); 62 } 63 64 final StringBuffer buffer = new StringBuffer (); 66 if ( pathLen > 1 && ( pos < pathLen || basePath.charAt( pos ) != separatorChar ) ) { 67 pos = basePath.lastIndexOf( separatorChar, pos ); 69 buffer.append( path.substring( pos ) ); 70 } 71 72 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 |