OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

JAXP and SAX ErrorHandler stuff



I feel like I'm really just not grasping something here.

The javax.xml.parsers abstract classes seem to exist for the purpose of
making it so that I can code for them instead of for specific parsers.

Quoting from the ErrorHandler API docs, "WARNING: If an application does
not register an ErrorHandler, XML parsing errors will go unreported and
bizarre behaviour may result. For XML processing errors, a SAX driver 
must use this interface instead of throwing an exception."

I would think that registering an ErrorHandler is something that is so
essential to any SAX parsing operation that it would be a part of the
JAXP classes. So I don't understand why a setErrorHandler method is not
in the JAXP SAXParser abstract class.

I want to have my ErrorHandler output to a particular Writer, so my 
approach has been the following (try/catch notwithstanding):

  import java.io.*;
  import org.xml.sax.ErrorHandler;
  import javax.xml.parsers;

  ...

  Writer myWriter = new CharArrayWriter();
  ErrorHandler myErrorHandler = new ErrorHandler(myWriter);
  SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser parser = factory.newSAXParser();
  parser.setErrorHandler(myErrorHandler);  // breaks! no such method
  parser.parse(whatever);

...and then my ErrorHandler has a constructor that sets an output
destination for its error messages to be the Writer that was passed
in as an argument.

This doesn't work because setErrorHandler() doesn't exist in the abstract
SAXParserFactory class. It only exists in the implementation's class that
inherits from it, by virtue of that class's requirement to implement the
SAX2 XMLReader interface.

It seems that I need to either work with XMLReader implementations and
forget about using JAXP's SAXParserFactory and SAXParser, or import a
particular implementation's classes and call them directly. This makes me
wonder what the great advantage is to JAXP in SAX processing.

I thought perhaps the answer was that setErrorHandler() is not necessary, 
because I could have my ErrorHandler not only implement 
org.xml.sax.ErrorHandler, but also extend 
org.xml.sax.helpers.DefaultHandler. Then I would have to omit the 
ErrorHandler constructor and setErrorHandler call in the code above.

The problem with this is that I need to control the construction of the
ErrorHandler so that I can have it output the error messages to a
particular Writer. I have no idea what the implementation's SAXParser is
going to do when it constructs a DefaultHandler, but I'm pretty certain
there is no way for me to sneak my instance-specific Writer in there.

JAXP has an ErrorListener interface for XSLT processing errors. Why isn't
there an equivalent for the XML parsing? What am I not understanding
here?

   - Mike
_____________________________________________________________________________
mike j. brown, software engineer at  |  xml/xslt: http://skew.org/xml/
webb.net in denver, colorado, USA    |  personal: http://hyperreal.org/~mike/