You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Stephen Collyer <sc...@netspinner.co.uk> on 2008/02/12 15:11:31 UTC

Construct SAXParseException in start_element ?

Is it possible to retrieve the required id
and line/col info from within, say, start_element()
in order to construct a SAXParseException ?

-- 
Regards

Steve Collyer
Netspinner Ltd

Re: Construct SAXParseException in start_element ?

Posted by David Bertoni <db...@apache.org>.
Stephen Collyer wrote:
> David Bertoni wrote:
>> Stephen Collyer wrote:
> 
>>>> Yes, that's the right call, but you've reversed what you need to do.
>>>> Your ContentHandler implementation should override setDocumentLocator()
>>>> and store that pointer. 
>>> Right. So the only thing that needs to happen in the overridden
>>> method is the storing of the pointer that's passed in ?
>> Yes, that's it.  In fact, that's what the documentation for the function
>> states:
>>
>>     * By default, do nothing.  Application writers may override this
>>     * method in a subclass if they wish to store the locator for use
>>     * with other document events.
> 
> OK. Thanks for the help. BTW, is there any tutorial documentation
> that explains the sort of stuff I've been asking - all I can
> find that mentions this at all is the class level documentation,
> and it's not entirely easy to see how it fits together from that.
> 
A lot of this stuff is standard SAX behavior, so reading the SAX 
documentation should help.  Since we've implemented SAX in C++, there are 
some differences, but it shouldn't be too hard to reconcile them:

http://www.saxproject.org

Dave

Re: Construct SAXParseException in start_element ?

Posted by Stephen Collyer <sc...@netspinner.co.uk>.
David Bertoni wrote:
> Stephen Collyer wrote:

>>> Yes, that's the right call, but you've reversed what you need to do.
>>> Your ContentHandler implementation should override setDocumentLocator()
>>> and store that pointer. 
>>
>> Right. So the only thing that needs to happen in the overridden
>> method is the storing of the pointer that's passed in ?
> Yes, that's it.  In fact, that's what the documentation for the function
> states:
> 
>     * By default, do nothing.  Application writers may override this
>     * method in a subclass if they wish to store the locator for use
>     * with other document events.

OK. Thanks for the help. BTW, is there any tutorial documentation
that explains the sort of stuff I've been asking - all I can
find that mentions this at all is the class level documentation,
and it's not entirely easy to see how it fits together from that.

-- 
Regards

Steve Collyer
Netspinner Ltd

Re: Construct SAXParseException in start_element ?

Posted by David Bertoni <db...@apache.org>.
Stephen Collyer wrote:
> David Bertoni wrote:
>> Stephen Collyer wrote:
> 
>>> I guess you mean setDocumentLocator() ?
>>>
>>> So to use this, I create a Locator object, pass it to
>>> setDocumentLocator(). Then the Xerces parser code will
>>> set the appropriate error info, which I can retrieve
>>> in, say, the warning(), error() methods of an error
>>> handler class ?
>>>
>> Yes, that's the right call, but you've reversed what you need to do. 
>> Your ContentHandler implementation should override setDocumentLocator()
>> and store that pointer. 
> 
> Right. So the only thing that needs to happen in the overridden
> method is the storing of the pointer that's passed in ?
Yes, that's it.  In fact, that's what the documentation for the function 
states:

     * By default, do nothing.  Application writers may override this
     * method in a subclass if they wish to store the locator for use
     * with other document events.

> 
>> Then, during parsing callbacks (and only during
>> them), you can call the accessor functions to get the information you
>> need. 
> 
> By "parsing callbacks" I guess you mean start_document(),
> start_element() and so on ?
Yes.  startDocument, startElement, endElement, etc. are all parsing 
callback functions.

> 
>> The parser has to provide you with an instance of a Locator,
>> since it's parsing the document.
> 
> Well, I guess it could work the other way round - the
> implementation could expect the Locator* to be passed when
> the parser is created.

If you consider the information that the Locator provides, I think you'll 
agree it would be inefficient to have the parser report ever detail of the 
parse to it.  The way it's implemented, the application can poll for 
information only when it needs it.

>However, given that the parser creates
> the Locator, then I suppose it also deletes it ? i.e. the
> content handler need not do that ?
No, you must not.  The Locator belongs to the parser instance.

Dave

Re: Construct SAXParseException in start_element ?

Posted by Stephen Collyer <sc...@netspinner.co.uk>.
David Bertoni wrote:
> Stephen Collyer wrote:

>> I guess you mean setDocumentLocator() ?
>>
>> So to use this, I create a Locator object, pass it to
>> setDocumentLocator(). Then the Xerces parser code will
>> set the appropriate error info, which I can retrieve
>> in, say, the warning(), error() methods of an error
>> handler class ?
>>
> Yes, that's the right call, but you've reversed what you need to do. 
> Your ContentHandler implementation should override setDocumentLocator()
> and store that pointer. 

Right. So the only thing that needs to happen in the overridden
method is the storing of the pointer that's passed in ?

> Then, during parsing callbacks (and only during
> them), you can call the accessor functions to get the information you
> need. 

By "parsing callbacks" I guess you mean start_document(),
start_element() and so on ?

> The parser has to provide you with an instance of a Locator,
> since it's parsing the document.

Well, I guess it could work the other way round - the
implementation could expect the Locator* to be passed when
the parser is created. However, given that the parser creates
the Locator, then I suppose it also deletes it ? i.e. the
content handler need not do that ?

-- 
Regards

Steve Collyer
Netspinner Ltd

Re: Construct SAXParseException in start_element ?

Posted by David Bertoni <db...@apache.org>.
Stephen Collyer wrote:
> David Bertoni wrote:
>> Stephen Collyer wrote:
>>> Is it possible to retrieve the required id
>>> and line/col info from within, say, start_element()
>>> in order to construct a SAXParseException ?
>>>
>> You need to override ContentHandler::setLocator() and store the pointer
>> in your object.  During parsing event handlers, you can use the object
>> to get this information.
> 
> I guess you mean setDocumentLocator() ?
> 
> So to use this, I create a Locator object, pass it to
> setDocumentLocator(). Then the Xerces parser code will
> set the appropriate error info, which I can retrieve
> in, say, the warning(), error() methods of an error
> handler class ?
> 
Yes, that's the right call, but you've reversed what you need to do.  Your 
ContentHandler implementation should override setDocumentLocator() and 
store that pointer.  Then, during parsing callbacks (and only during them), 
you can call the accessor functions to get the information you need.  The 
parser has to provide you with an instance of a Locator, since it's parsing 
the document.

Dave

Re: Construct SAXParseException in start_element ?

Posted by Stephen Collyer <sc...@netspinner.co.uk>.
David Bertoni wrote:
> Stephen Collyer wrote:
>> Is it possible to retrieve the required id
>> and line/col info from within, say, start_element()
>> in order to construct a SAXParseException ?
>>
> You need to override ContentHandler::setLocator() and store the pointer
> in your object.  During parsing event handlers, you can use the object
> to get this information.

I guess you mean setDocumentLocator() ?

So to use this, I create a Locator object, pass it to
setDocumentLocator(). Then the Xerces parser code will
set the appropriate error info, which I can retrieve
in, say, the warning(), error() methods of an error
handler class ?

-- 
Regards

Steve Collyer
Netspinner Ltd

Re: Construct SAXParseException in start_element ?

Posted by David Bertoni <db...@apache.org>.
Stephen Collyer wrote:
> Is it possible to retrieve the required id
> and line/col info from within, say, start_element()
> in order to construct a SAXParseException ?
> 
You need to override ContentHandler::setLocator() and store the pointer in 
your object.  During parsing event handlers, you can use the object to get 
this information.

Dave