Overloading int considered harmful: part 2

Sunday, December 19th, 2004

After writing Overloading int considered harmful, three concerns were raised:

  • How does this affect object serialization?
  • What about ClassLoaders?
  • How about the new enum keyword in Java 1.5?

I’d like to address those now.

(more…)

HTML Labeled Components

Tuesday, December 14th, 2004

One of the coolest yet little known features of the Java Foundation Classes (aka Swing) is the ability to label components with HTML. For example, let’s suppose you want a button that contains both bold and non-bold text on separate lines like this:

Button text: Damn the Torpedoes! Full speed ahead!

This is quite complex to implement in Java code. You have to subclass JButton, override paint(), and perform many detailed calculations with FontMetrics objects just to decide where to draw the different text strings. On the other hand, it’s straight-forward code this in HTML:

(more…)

A Brief Introduction to XInclude

Wednesday, December 8th, 2004

It’s often convenient to divide long XML documents into multiple files. The classic example is a book, customarily divided in chapters. Each chapter may be further subdivided into sections. Traditionally this has implemented via external entity references. For example,

<?xml version="1.0"?>
<!DOCTYPE book SYSTEM "book.dtd"[
  <!ENTITY chapter1 SYSTEM "malapropisms.xml">
  <!ENTITY chapter2 SYSTEM "mispronunciations.xml">
  <!ENTITY chapter3 SYSTEM "madeupwords.xml">
]>
<book>
  <title>The Wit and Wisdom of George W. Bush</title>
  &chapter1;
  &chapter2;
  &chapter3;
</book>

However, external entity references have a number of limitations. Among them:

The individual component files cannot be treated in isolation. They often aren’t themselves full, well-formed XML documents. They cannot have document type declarations.

  • The document must have a DTD, and the parser must read the DTD. Not all parsers do.

  • If any of the pieces are missing, then the entire document is malformed. There’s no option for error recovery.

  • Only entire files can be included. You can’t include just one paragraph from a document.

  • There’s no way to include unparsed text such as an example Java program or XML document in a technical book. Only well-formed XML can be included, and all such XML is parsed. (SGML actually had this ability, but it was one of the features XML removed in the process of simplification.)

XInclude is an emerging specification from the W3C that endeavors to create a mechanism for building large XML documents out of their component parts which does not have these limitations. XInclude can combine multiple documents and parts thereof independently of validation. Each piece can be a complete XML document, a part of an XML document, or a non-XML text document like a Java program or an e-mail message.

(more…)

Upcoming Features

Thursday, December 2nd, 2004

Lots of people have been writing in to me with features they’d like to see here at the Cafes, especially with regard to the comment system. Some of these were already on my TODO list, but the site seemed to work and be useful now, so I didn’t want to wait to launch to implement absolutely everything. So herewith, in no particular order, is a peek at my TODO list:

(more…)

Overloading int considered harmful

Thursday, December 2nd, 2004

When I was writing JavaBeans: Developing Component Software in Java, I lost over a day hunting for one bug. The program compiled and ran; but a label that was supposed to appear in the GUI wouldn’t show up, no matter what I did! The problem turned out to be in this line of code:

Font f = new Font("Serif", 12, Font.BOLD);

Can you see the bug? It’s certainly not obvious. In fact it’s so inobvious that I’ve made this mistake probably a dozen times since then, and it’s been a hard one to find every single time. I almost guarantee you can’t spot the bug without looking at the declaration for the Font constructor:

(more…)