SAX2 Namespace Support

Toby Speight Toby.Speight at streapadair.freeserve.co.uk
Thu Jan 13 10:57:05 GMT 2000


David> David Megginson <URL:mailto:david at megginson.com>

0> In article <m3r9fwtuv8.fsf at localhost.localdomain>, David wrote:

David> In principle (the principle of least surprise), it's very bad
David> behaviour for two objects to be == in C++ or equals() in Java
David> if any of their publicly-accessible fields differ.  Think of
David> sets, for example.

You could follow the example of java.lang.String and define equals() to
compare all public data (as usual), and define an additional method
equalsIgnorePrefix() which doesn't.  The problem with this is that
hashCode() has to be consistent with equals(), but sometimes you want
to use a hashCode() based on equalsIgnorePrefix().  I've solved this in
the past (for a case-insensitive String class) by making a wrapper that
defines equals() in terms of equalsIgnoreCase(), and a corresponding
hashCode().  I'm not sure I like this for SAX2 though, as we should be
making the common case - ignoring the prefix - the simplest.

public class QName {
  public String getPrefix();
  public String getUri();
  public String getLocal();

  public int hashCode(); // based on prefix, URI, local
  public boolean hashCodeIgnorePrefix(Object o); // based on URI, local

  public boolean equals(Object o); // based on prefix, URI, local
  public boolean equalsIgnorePrefix(Object o); // based on URI, local
}


public class QNameIgnorePrefix {
  private QName name;
  public QNameIgnorePrefix(QName name) { this.name = name; }

  public String getUri() { return name.getUri(); }
  public String getLocal() { return name.getLocal(); }

  public int hashCode() { return name.hashCodeIgnorePrefix(); }
  public boolean equals(Object o); { return name.equalsIgnorePrefix(o); }
}

-- 


xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ or CD-ROM/ISBN 981-02-3594-1
Please note: New list subscriptions now closed in preparation for transfer to OASIS.





More information about the Xml-dev mailing list