> Think this might work in Xalan?

I'm too tired tonight to know in detail, but:

      if (cause instanceof SAXParseException) {
                // details already reported, don't repeat them
                throw new TransformerConfigurationException("Failed to
parse stylesheet");

Is somewhat antithetical to the approach I've been trying to take lately:
The default ErrorListener doesn't report exceptions if it throws an
exception (it does report warning and non-fatal errors), it just passes
them on.  User ErrorListeners should also just pass a fatal error on,
though it may want to add to the exception.  Fatal errors should only be
reported by the exception catcher.

Do you think this is a reasonable approach?  I've only recently come to
this conclusion.

> I adapted it to work in my testbed ErrorListener as shown below. Seems to
be OK, but it's
> kinda kludgy since the logic really belongs in the application.

Well, the problem is, I think, that the creation of the new exception in
fatalError will create a new stack trace, which I don't think is what you
want.

-scott




                                                                                       
                            
                    Mike Brown                                                         
                            
                    <[EMAIL PROTECTED]       To:     [EMAIL PROTECTED]              
                            
                    >                    cc:     (bcc: Scott Boag/CAM/Lotus)           
                            
                                         Subject:     Re: [xsl] Xalan, errors and 
ErrorListener                    
                    08/09/2001                                                         
                            
                    06:16 PM                                                           
                            
                    Please respond                                                     
                            
                    to xalan-dev                                                       
                            
                                                                                       
                            
                                                                                       
                            




[EMAIL PROTECTED] wrote:
> The one problem I see in the above scenario is that the exception gets
> passed to the ErrorListener twice... once as an error and once as a fatal
> error.  I'm not sure how realistically clean this up without a
> hasBeenReported flag on Transformer exception, or just assuming that all
TransformerExceptions have been reported at the top level

OK, the situation is pretty much what I afraid it was.

Here's something I see in Saxon's PreparedStylesheet#prepare(SAXSource),
which does the work for TransformerFactoryImpl#newTemplates(Source):

    } catch (TransformerException err) {
        Throwable cause = err.getException();
        if (cause != null) {
            if (cause instanceof SAXParseException) {
                // details already reported, don't repeat them
                throw new TransformerConfigurationException("Failed to
parse stylesheet");
            } else if (cause instanceof TransformerConfigurationException)
{
                throw (TransformerConfigurationException)cause;
            } else {
                throw new TransformerConfigurationException(cause);
            }
        }
        throw new TransformerConfigurationException(err);
    }

Think this might work in Xalan?

I adapted it to work in my testbed ErrorListener as shown below. Seems to
be OK, but it's
kinda kludgy since the logic really belongs in the application.

import javax.xml.transform.TransformerException;
import org.xml.sax.SAXException;

public class TransformerErrorListener implements
javax.xml.transform.ErrorListener
{

  public void warning( TransformerException te ) throws
TransformerException
  {
    System.err.println( formatTransformerException( 0, te ) );
  }

  public void error( TransformerException te ) throws TransformerException
  {
    throw new TransformerException( formatTransformerException( 1, te ) );
  }

  public void fatalError( TransformerException te ) throws
TransformerException
  {
    Throwable cause = te.getException();
    if ( cause != null )
    {
      if ( cause instanceof SAXException )
      {
        throw te;
      }
      else
      {
        throw new TransformerException( formatTransformerException( 2, te )
);
      }
    }
    else
    {
      throw new TransformerException( formatTransformerException( 2, te )
);
    }
  }

  public static String formatTransformerException( int errType,
TransformerException te )
  {
    String[] errorTypes = { "WARNING", "ERROR", "FATAL ERROR" };
    String          msg = te.getMessageAndLocation();
    String msgout = "The XSLT processor reported the following " +
errorTypes[errType] + ":\n" + msg;
    return msgout;
  }

}





Reply via email to