aspose file tools*
The moose likes Servlets and the fly likes Instantiating URL from Html Content Stored in a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Instantiating URL from Html Content Stored in a String" Watch "Instantiating URL from Html Content Stored in a String" New topic
Author

Instantiating URL from Html Content Stored in a String

Cyrus Taz
Greenhorn

Joined: Sep 27, 2010
Posts: 3
Is there anyway to instantiate a URL object based on html that is in a String? What I want to be able to do is something like the following:


where htmlContent is a string that holds the actual html content such as

This should easily be doable, but I don't seem to find any ways.

Thanks for taking time to read my question,
Cyrus
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
No, as should be clear from the documentation of java.net.URL, a URL is a Uniform Resource Locator, in other words it points to content.

Exactly what are you trying to accomplish?

Bill
Cyrus Taz
Greenhorn

Joined: Sep 27, 2010
Posts: 3
Hi Bill,

Thanks for the reply. I'm trying to feed my own content to a URL object for testing purposes. Basically, I want to load my own Html code to a URL, and then run testing on that URL. I know there are libraries such as Jtidy, or HtmlCleaner which can do parsing of the html and loading it, but I really want to be able to load Html to a URL object, rather than use these third party libraries.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What you're saying still doesn't make sense--you don't "load content into a URL"--a URL is used to *retrieve* content.

What, *precisely*, are you trying to do?
Cyrus Taz
Greenhorn

Joined: Sep 27, 2010
Posts: 3
It does make sense. I have thousands of pages of Html which are saved into database rather than on the file system, and this is not uncommon practice at all. Now, I want to read these html pages, process them, and then save them back to the database; and we have third party classes and methods which require URL objects to be passed to them. So, one solution is to save them to a temporary file, and create URL based on the path of that temporary file. In this situation (and there are many similar situations), it is of no material where the content comes from; and the process of saving the files of course is very slow compared to the rest of the tests. So, I'm doing the extra step of saving the html content to a file just cause the API of these third party libraries are asking for URL.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, it *doesn't* make sense.

URLs don't have content. They *read* content. I'd probably just write a URLStreamHandler, or subclass and override URL.openStream, depending on your requirements. Or just expose them with an internal server and pass a normal URL.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instantiating URL from Html Content Stored in a String