Reputation: 32675
I've been reading through Programming Clojure, and I've been having some trouble understanding Stuarts primary Java Interop example. He extends DefaultHandler, and creates a startElement method, and then passes that handler to an XML parser. What I don't understand, is what exactly is happening. Does his implementation of startElement override the one defined in DefaultHandler? I'm confused. I have no experience with Java, and little with object orientation.
Thanks!
Upvotes: 3
Views: 2510
Reputation: 14514
I don't own the book, but I found the code and it looks like you're right. Here is the function (for others to see):
(def print-element-handler
(proxy [DefaultHandler] []
(startElement
[uri local qname atts]
(println (format "Saw element: %s" qname)))))
You're right about what it does. The proxy statement makes a new class, the equivilent of this Java code:
public class SomeNewClass extends DefaultHandler {
public void startElement(String uri,
String localName,
String qName,
Attributes attributes) {
System.out.println(*stuff*);
}
}
So the proxy statement defines that class, and gives you an instance, which is now held in print-element-handler.
Upvotes: 7
Reputation: 32675
Glancing over the Java documentation for DefaultHandler answered my own question. http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/helpers/DefaultHandler.html#startElement%28java.lang.String,%20java.lang.String,%20java.lang.String,%20org.xml.sax.Attributes%29
By default, do nothing. Application writers may override this method in a subclass to take specific actions at the start of each element (such as allocating a new tree node or writing output to a file).
Upvotes: 1