Hi Peter

----- Original Message -----
From: "Peter Meulmeester" <[EMAIL PROTECTED]>
> Hello,
>
> Our app uses xml files for configuration. The syntax is checked during
> loading and linenumbers are then provided.
>
> After loading all xml file as dom4j documents, they are checked amongst
> themselves for references to eachother. If a reference error is found I
> would like to give an nice error message with line number like:
>
> conf1.xml:34: tag TAG1 references conf2.xml/TAG2 but does not exist in
> conf2.xml
>
> This means I have to remember the linenumber 34 (TAG1) to use later while
> doing the reference checks using the dom4j documents.
>
> This means I need to collect line number info during the SAX parsing and
> store it separate from the dom4j document in some datastructure.
>
> Can someone point me to a starting point on how to add callbacks to the
SAX
> parser which dom4j uses?
> Or maybe there is an easier way?

First off, we need to add extra information to Elements to store the line
number etc that it comes from. The org.dom4j.util.UserDataElement might be
useable as-is - you just need to write a 'user data' object that represents
the location of the element. Or you could provide your own Element and
DocumentFactory implementations if you wish.

Then if you use the org.dom4j.util.UserDataDocumentFactory (or your own
DocumentFactory) on the SAXReader, you'll create UserDataElements which you
can then associate the location information with.

Next you'll need to derive from the SAXContentHandler used by SAXReader and
add support to associate the location information (accessible via the SAX
Locator) with each new Element. You can do this in the startElement() method
I think fairly easily.

Then derive your own SAXReader to use your new SAXContentHandler (maybe
overloading the createContentHandler() factory method).

That should just about do it I think. Do let us know how you get on, it does
sound kinda useful. Maybe we could add your work to a contribution directory
in dom4j so others can share this stuff?

James


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


Reply via email to