[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;
}
}