Categories
Web Development

Cross-browser ‘Add Bookmark’ Link

Internet Explorer appears to offer an easy way to add a bookmark to the current page. The code is as simple as

window.external.AddFavorite(url, title);

But Firefox and Mozilla-based browser are more finicky. Dynamic Drive have an idea on how to do that, using the JavaScript call

window.sidebar.addPanel(title, url, "")

which works, eh, sometimes
See, in my Firefox 1.0.7 the code above works just fine, but in my co-worker’s Firefox 1.0.7, it does not. Like Mozilla 1.5, his Firefox simply adds a panel to the browser sidebar (click ‘F9’ if you never met your sidebar), where the browser simply loads the page. Not what I wanted.

Share
Categories
Java

Setting Transformer properties

So you are going to output an XML Document to a file using this canonical method. But how do you tell the Transformer object how the output should look like?

You use OutputKeys, that’s how.

For example:
transformer.setOutputProperty(OutputKeys.INDENT,"yes");

Share
Categories
Java

On Java Interfaces and Exception

While implementing an abstract factory pattern for my software engineering class I noticed something that seemed weird to me: I defined an interface with several methods, each method was declared to throw several exceptions using the standard throws keyword.

When I created the implementation class for that method, I noticed that I do not have to throw all of the exceptions the interface specified. Actually, I only needed to throw one exception for the method I was overriding for the specific implementation and Java let me. I tried researching more on the relationship between exceptions and interfaces finding nothing.

Turning into the real Java bible, the Java Language Specification (I recommend the free download), I encountered the following sentence in the spec (page 299):

The throws clause of an overriding method may not specify that this method will
result in throwing any checked exception which the overridden method has not permitted, by its throws clause, to throw. When interfaces are involved, more than one method declaration may be overridden by a single overriding declaration. In this case, the overriding declaration must have a throws clause that is compatible with all the overridden declarations.

So an implementing or overriding method (from an interface or a super class) may only throw exceptions that are among the ones the interface or super class specified in the original method declaration. By converse, this means that if an overriding method does not need to throw exceptions that are thrown by the interface or super class’ method, it does not have to. In other words:

An overriding method can throw any of the exceptions the original method throws, or none of them.

Share
Share