Catching warnings during transformation
Added by Michael Koch about 10 years ago
Hi everyone,
I have a question about catching warnings of transformation with the .net API. My library successfully runs a transformation. When a console application uses this library, the concsole window shows messages like "Recoverable error XTRE0540: Ambiguous rule match for ....", but my desktop application doesnt. How can I catch messages of the XsltTransformer.Run() method like these to log or to save? I tried the XslMessageListener, this works perfect with xsl:message. And I catched other warnings like "javax.xml.transform.TransformerCofigurationException: The child axis starting at an attribute node....", when I use the ErrorList of the XsltCompile. But I dont find a way to catch the warnings and messages during XsltTransformer.Run().
Can somebody help?
Thx and Greetz Michael
Replies (3)
Please register to reply
RE: Catching warnings during transformation - Added by Michael Kay about 10 years ago
At present you can only do this by digging down to the underlying Java classes. The XsltTransformer.Implementation property takes you to the net.sf.saxon.Controller object, and this has methods getErrorListener() and setErrorListener(). You can write and register an ErrorListener that implements the interface javax.xml.transform.ErrorListener, or you can get the StandardErrorListener which is currently registered, and call its setLogger() method to change the destination of messages; you could use an instance of net.sf.saxon.lib.StandardLogger initialized to write to a specified file.
I'm afraid this all involves rather low-level grubbing around the innards, but it can be done.
RE: Catching warnings during transformation - Added by Michael Koch about 10 years ago
Ahhh, okay. I supposed a solution like this, but it seems interesting. :) I think, I will try and if I am successful, I will post my solution. Thanks a lot.
RE: Catching warnings during transformation - Added by Michael Koch about 10 years ago
I just implement the Interface. I think, it is very easy.
To use the classes and interfaces, you have to reference the libraries IKVM.OpenJDK.ClassLibrary.dll and of course the saxon9.dll. First I create a class "JavaErrorClass" and implements the "ErrorListener", using javax.xml.transform.
public class JavaErrorListener : ErrorListener
{
private Logger _logger; //this is my Logger to save all messages in a SQL-Database
private string _source; //this is the current file to transform
public JavaErrorListener(Logger logger, string source)
{
_logger = logger;
_source = source;
}
public void warning(TransformerException te)
{
if (te.getException() != null)
_logger.LogException(te.getException(), TraceEventType.Warning);
else
_logger.LogMessage(string.Format("Warning for {0}: {1}", _source, te.Message), TraceEventType.Warning);
}
public void fatalError(TransformerException te)
{
if (te.getException() != null)
_logger.LogException(te.getException(), TraceEventType.Error);
else
_logger.LogMessage(string.Format("Error for {0}: {1}", _source, te.Message), TraceEventType.Error);
}
public void error(TransformerException te)
{
if (te.getException() != null)
_logger.LogException(te.getException(), TraceEventType.Error);
else
_logger.LogMessage(string.Format("Warning (JavaError) for {0}: {1}", _source, te.Message), TraceEventType.Warning);
}
}
before running the XsltTransformer, the ErrorListener of the transformer must be set.
JavaErrorListener listener = new JavaErrorListener(Logger, sourcePath);
transformer.Implementation.setErrorListener(listener);
Thats all! And it works really fine. Thanks a lot for the advice. And for the Saxon .Net API! :) I am really impressed about the features like creating my own .Net extensions and the listeners.
Please register to reply