Capture xsl: message output in java

I am trying to capture an xsl: message message in java when invoking my conversion. Below is a snippet of my code.

final ArrayList<TransformerException> errorList = new ArrayList<TransformerException>(); ErrorListener errorListener = new ErrorListener() { @Override public void warning(TransformerException e) throws TransformerException { //To change body of implemented methods use File | Settings | File Templates. log.error(e.getMessage()); errorList.add(e); } @Override public void error(TransformerException e) throws TransformerException { //To change body of implemented methods use File | Settings | File Templates. log.error(e.getMessage()); errorList.add(e); } @Override public void fatalError(TransformerException e) throws TransformerException { //To change body of implemented methods use File | Settings | File Templates. errorList.add(e); throw e; } }; ... try { transformer.setErrorListener(errorListener); newDoc = transform(transformer, oldDoc); } catch (TransformerException e) { log.error("Problem transforming normalized document into PUBS-XML", e); throw e; } 

Unfortunately this does not work.

Is there a better way?

Thanks in advance!

+8
java xslt
source share
1 answer

If you use Saxon, you may need to set the message emitter using setMessageEmitter () .

http://www.saxonica.com/documentation/javadoc/net/sf/saxon/Controller.html

public void setMessageEmitter(Receiver receiver)

Set the receiver to output xsl: message.

Recent versions of the JAXP interface indicate that, by default, xsl:message output is sent to the registered ErrorListener. Saxon does not comply with this agreement. Instead, the output is sent to the default error message, which is a slightly customized implementation of the standard Saxon Emitter interface.

This interface can be used to change the way in which the saxon outputs xsl:message .

Michael Kay explained why Saxon does not output xsl:message according to the JAXP interface and suggested two options for getting the results

ErrorListener was something that was introduced by JAXP at a fairly late ErrorListener (one of many regrettable cases where the specification was changed unilaterally to conform to Xalan), and I decided not to implement this change in the default behavior, as this would undermine the existing ones. applications .

In Saxon, the output xsl:message directed to a receiver, which you can assign to a transformer:

((net.sf.saxon.Controller)transformer).setMessageEmitter(....)

If you want to follow the JAXP model of sending output to the ErrorListener, you can designate a Receiver that does this:

((net.sf.saxon.Controller)transformer).setMessageEmitter(new net.sf.saxon.event.MessageWarner())

+11
source share

Source: https://habr.com/ru/post/651085/


All Articles