|PartialURLs in VisualWorks 7.6|
December 14, 2007, 9:18:03 pm
Every now and then you get to do a minor improvement to an already exceptional area of the system. URIs are a really great idea and their APIs are pretty neat. Modelling a URI as an object gives you advantages when you're programming - it lets you manipulate the urls in object form, then serialize them out as encoded urls and suddenly you never have to worry about remembering to call URIEncoder.
Many moons ago I made a package called RelativeURL for WithStyle when we were doing anchor clicks. If you clicked on an anchor and the URL is relative - where should you go? ... VisualWorks had some basic functionality for this called #resolvePath: but in this instance, the API threw away the object on the right hand side of the equation and took a string.
Now with PartialURLs you can model not only a full URL but a URL that is just a path, query and fragment. For example, the following is now valid: '/images/test.png' asURI. Because they are real objects, you can now manipulate them like you would other kinds of URLs, except that these ones have no host or port.
Often when dealing with XML resources of one form or another, you'll be handed a relative URL that is meant to be relative to the xml:base URL. you can combine the two together with PartialURLs without having to know if the RHS of the equation is just a string or an actual URL. In fact, the RHS of the equation can be a full URL too - so now you don't even have to think about whether the "relative url" is a full url or not.
What do I mean by that? Well, take the following: 'http://localhost:7777/' aURI, '/images/test.png' asURI - this will output a URL of 'http://localhost:7777/images/test.png'. But if our program is more generic, eg: baseURL, optionURL then we may not know what kind of URL the optionURL is. If it ends up being something like this: 'http://localhost:7777/' asURI, 'https://secret:1234/files/things.zip' then the resultant URL will be 'https://secret:1234/files/things.zip'
The #resolvePath: methods (of which #, is an alias for) works the same way the web browsers do when combining the base with another url. This will be available in VisualWorks 7.6 and the next release of ObjectStudio 8.