Re: Conversion from an absolute URL to relative
Re: Conversion from an absolute URL to relative
- Subject: Re: Conversion from an absolute URL to relative
- From: "Louis C. Sacha" <email@hidden>
- Date: Wed, 10 Mar 2004 03:12:09 -0800
Hello...
I knew I should have waited until I could test that code before posting it :)
It turns out that using the NSString method
stringByDeletingLastPathComponent on a url won't actually work, since
it causes the second slash after the colon in the url to be lost. For
example
http://www.somesite.com/index.html turns into
http:/www.somesite.com and so the common prefix of the clipped
baseURLString and the absoluteURLString would end up being http:/
since the second slash was still present in the absoluteURLString but
not the baseURLString. That doesn't work too well when it's used to
figure out the relative url...
Also, if the urls for the absolute and base urls are from different
sites, the relative url returned should be the absolute url.
So here's an updated version, now that I've had a chance to test it
and make sure it basically works (although there might still be other
bugs floating around).
- (NSString *)relativeURLStringForAbsoluteURLString:(NSString
*)absoluteURLString withBaseURLString:(NSString *)baseURLString
{
if ((baseURLString)&&(![baseURLString isEqualToString:@""]))
{
/* make sure that baseURLString ends in a directory */
if (![[baseURLString
substringFromIndex:([baseURLString length] - 1)]
isEqualToString:@"/"])
{
unsigned endOfLastDirectoryIndex =
[baseURLString rangeOfString:@"/"
options:(NSLiteralSearch|NSBackwardsSearch)
range:NSMakeRange(0,[baseURLString length]-1)].location;
if (endOfLastDirectoryIndex != NSNotFound)
{baseURLString = [baseURLString
substringToIndex:(endOfLastDirectoryIndex+1)];}
else {baseURLString = @"";}
}
NSString *commonString = [absoluteURLString
commonPrefixWithString:baseURLString options:NSLiteralSearch];
if ((commonString)&&(![commonString isEqualToString:@""]))
{
/* make sure that the common url path ends in
a directory */
if (![[commonString
substringFromIndex:([commonString length] - 1)] isEqualToString:@"/"])
{
unsigned endOfLastDirectoryIndex =
[commonString rangeOfString:@"/"
options:(NSLiteralSearch|NSBackwardsSearch)
range:NSMakeRange(0,[commonString length]-1)].location;
if (endOfLastDirectoryIndex !=
NSNotFound) {commonString = [commonString
substringToIndex:(endOfLastDirectoryIndex+1)];}
else {commonString = @"";}
}
if (([commonString
length]>2)&&(![[commonString substringFromIndex:([commonString
length] - 2)] isEqualToString:@"//"]))
{
NSString *remainderString;
if ([commonString
isEqualToString:absoluteURLString]) {remainderString = @"";}
else
{
NSRange commonRange =
[absoluteURLString rangeOfString:commonString];
remainderString =
[absoluteURLString substringFromIndex:commonRange.length];
}
/* add ../ to back up to parent
directory as many times as required */
while (![commonString
isEqualToString:baseURLString])
{
unsigned truncateAfterIndex =
[baseURLString rangeOfString:@"/"
options:(NSLiteralSearch|NSBackwardsSearch)
range:NSMakeRange(0,[baseURLString length]-1)].location;
baseURLString = [baseURLString
substringToIndex:(truncateAfterIndex+1)];
remainderString = [NSString
stringWithFormat:@"../%@",remainderString];
}
return remainderString;
}
}
}
/* no baseURLString, empty base url, or base url does not match */
return absoluteURLString;
}
With apologies to digest readers who aren't interested, but still get
to scroll past my code twice :)
Louis
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.