I'd like some feedback on a change I'd like to make to a small part of the JAXP implementation.

The API in question is the javax.xml.namespace.QName class, which is a common cause of developer error as its constructor uses plain Strings and you have to remember which order the arguments go in:

  QName(String namespaceUri, String localPart)

but

  QName(String localPart)

seems to confuse people a lot, and you often find something like

  new QName("foo", "http://bar.com/my/namespace";)

or worse, the arguments are variable names

  new QName(el, un)

- difficult to spot and debug.

The attached patch does some simple validation to ensure the consistency of its arguments at runtime. It should help to tie down exactly where a bogus QName is constructed, even if it doesn't catch every possible case. Note that it doesn't do exhaustive testing of the localPart or prefix arguments as the exact criteria depend on the version of XML the author intends.

Note that this is contentious, because Sun's QName *doesn't do any validation* of its input. However, I still think it's the right thing to do, and I'm not alone:

  http://cafe.elharo.com/java/why-methods-should-check-their-arguments/

Anyway, see below for the patch.
--
犬 Chris Burdess
  "They that can give up essential liberty to obtain a little safety
  deserve neither liberty nor safety." - Benjamin Franklin

Attachment: patch
Description: Binary data



Attachment: PGP.sig
Description: This is a digitally signed message part

Reply via email to