Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programming Diversions and the fly likes Comparing URLS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Comparing URLS" Watch "Comparing URLS" New topic
Author

Comparing URLS

Mark Fletcher
Ranch Hand

Joined: Dec 08, 2001
Posts: 897
Say you have a page with the url

http://foo/bar/bar1/bar2/page.htm

and it has a link

http://foo/bar3/bar4/page2.htm

Suggest an method of producing a relative URL to url1 for url2

ie

../../../bar3/bar4/page2.htm


Mark Fletcher - http://www.markfletcher.org/blog
I had some Java certs, but they're too old now...
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2049
Originally posted by Mark Fletcher:
Say you have a page with the url

http://foo/bar/bar1/bar2/page.htm

and it has a link

http://foo/bar3/bar4/page2.htm

Suggest an method of producing a relative URL to url1 for url2

ie

../../../bar3/bar4/page2.htm



i have thought about it, but doesnt have time yet to code and test my pseudocode:

given: url1(the one containing the link to url2), url2

1. count tokens(folders) for url1
e.g. for "http://foo/bar3/bar4/page2.htm", there are 3 tokens, foo, bar3 and bar4

2.
dots="";
extract the filename and put it to webPageFilename(e.g. page2.htm)

for (i=tokens;i>0;i--)
{
get substring of url1 from start of url1 up to token i;
get substring of url2 from start of url2 up to token i;
if both substring are same
{
return dots+remaining tokens for url2+webPageFilename;
}
dots=dots+'../';
}
return 'must use absolute url';
}
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Put the two into a stack (reverse the second), pop from the first and append "../" until it exists in the second stack, pop from the second stack till you get the common element, then start appending till you run out.

This assumes the lengths are small. If the common parts are much larger than the separate bits it may be faster not to reverse the second, pop off the second and prepend them to a separate string then join the two Strings together.

Then again you can start by popping and prepending from the second till you reach the common element, then pop from the first and prepend the "../" till you again reach the common element. This is a little more direct but probably harder to read after the fact.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

This is not correct, I'll leave the reason why as an excersize for readers. I'll update when I get a moment
Sania Marsh
Ranch Hand

Joined: Jul 12, 2004
Posts: 469
I'm bit confused about second solution,
(are we putting first element at the bottom of the stack or last... but we are still looking for same element, when we shoould reather look for different one)
but I think we cannot compare from end. foo may have foo within itself. or 3 levels down. You are never guranteed to have different folder names.

We should rather go from front, until hit different elelments, then count the remaining "tokens" and prepend them as dots.

Very interesting. I'll try to write it once get time
[ August 20, 2005: Message edited by: Sania Marsh ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

foo may have foo within itself.
Yep, this is the problem I was referring to.
You are quite correct.
Nimai Etheridge
Greenhorn

Joined: Feb 16, 2005
Posts: 16
I split the strings into array's and then compared each element of the array. I guess it's the same as tokenizing the string



the relativeDir boolean ensures you don't get output like
../bar/../foo.html

it's not the funkiest code, but it works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing URLS
 
Similar Threads
Using CSS float for positioning page elements
class within interface and interface within class
doubt in instanceof
Interface assignments
Better JUnit Tests with PMD - free book chapter