views:

39

answers:

1

I would like to open an html file locally (not fetch it from a server) and somehow ensure that the relative hrefs are turned correctly resolved. I suppose I need to convert them into absolute paths.

I played around with the html element, in the head of the document, to resolve any relative links on the page, but only had limited success (often the resources could not be found).

I can convert the html source file anyway I wish with java, but ideally I would like to modify the html source as little as possible, for example insert a function that modifies the result of the hrf attribute.

How would I go about this? Do any of the java html parsers do this?

Thanks

+1  A: 

If you want to do it on your own, I recommend you to read the chapter Reference Resolution in RFC 3986. It explains how to establish the base URI and how to resolve relative URIs by giving an algorithm.

But there’s probably already a library in Java that does that for you.

Gumbo
ideally I would like to find a java program that does it for me.But thanks the link will surely help.
I suppose I just need to create a uri out of the address, then call URI#resolve(relative).
@user273096: Yes, I think `(new URI(base)).resolve(relative)` will do it.
Gumbo
alrighty thanks.