/* Copyright 2013 Nationale-Nederlanden Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package nl.nn.adapterframework.errormessageformatters; import java.io.IOException; import java.util.Map; import javax.xml.transform.Transformer; import nl.nn.adapterframework.configuration.ConfigurationException; import nl.nn.adapterframework.core.INamedObject; import nl.nn.adapterframework.core.ParameterException; import nl.nn.adapterframework.core.PipeLineSessionBase; import nl.nn.adapterframework.parameters.Parameter; import nl.nn.adapterframework.parameters.ParameterList; import nl.nn.adapterframework.parameters.ParameterResolutionContext; import nl.nn.adapterframework.util.ClassUtils; import nl.nn.adapterframework.util.XmlUtils; import org.apache.commons.lang.StringUtils; /** * Applies a XSLT-stylesheet to the standard error generated by * {@link ErrorMessageFormatter}. * <p>If the transformation does not succeed, this standard error message * is returned and an exception is logged. * <p><b>Configuration:</b> * <table border="1"> * <tr><th>attributes</th><th>description</th><th>default</th></tr> * <tr><td>className</td><td>nl.nn.adapterframework.errormessageformatters.XslErrorMessageFormatter</td><td> </td></tr> * <tr><td>{@link #setStyleSheet(String) styleSheet}</td><td>stylesheet to use for transformation</td><td> </td></tr> * <tr><td>{@link #setXpathExpression(String) xpathExpression}</td><td>xpathExpression to use for transformation</td><td> </td></tr> * </table> * * <p><table border="1"> * <tr><th>nested elements</th><th>description</th></tr> * <tr><td>{@link nl.nn.adapterframework.parameters.Parameter param}</td><td> </td></tr> * </table> * </p> * * Hint: use <code>xpathExression="/errorMessage/@message"</code> for a single compact string errormessage. * @author Johan Verrips IOS */ public class XslErrorMessageFormatter extends ErrorMessageFormatter { protected ParameterList paramList = null; private String styleSheet; private String xpathExpression; public String format( String message, Throwable t, INamedObject location, String originalMessage, String messageId, long receivedTime) { String result = super.format(message, t, location, originalMessage, messageId, receivedTime); if (StringUtils.isNotEmpty(getStyleSheet()) || StringUtils.isNotEmpty(getXpathExpression())) { try { Transformer errorTransformer; if (StringUtils.isNotEmpty(getStyleSheet())) { errorTransformer = XmlUtils.createTransformer(ClassUtils.getResourceURL(this, styleSheet)); } else { String xpath = getXpathExpression(); // if (StringUtils.isEmpty(xpath)) { // xpath="/errorMessage/@message"; // } errorTransformer = XmlUtils.createTransformer(XmlUtils.createXPathEvaluatorSource(xpath)); } ParameterList params = getParameterList(); if (params!=null) { try { params.configure(); } catch (ConfigurationException e) { log.error("exception while configuring parameters",e); } ParameterResolutionContext prc = new ParameterResolutionContext(message, new PipeLineSessionBase()); Map parametervalues = null; try { parametervalues = prc.getValueMap(params); } catch (ParameterException e) { log.error("got exception extracting parameters",e); } XmlUtils.setTransformerParameters(errorTransformer, parametervalues ); } result = XmlUtils.transformXml(errorTransformer, result); } catch (IOException e) { log.error(" cannot retrieve [" + styleSheet + "]", e); } catch (javax.xml.transform.TransformerConfigurationException te) { log.error("got error creating transformer from file [" + styleSheet + "]", te); } catch (Exception tfe) { log.error( "could not transform [" + result + "] using stylesheet [" + styleSheet + "]", tfe); } } else log.warn("no stylesheet defined for XslErrorMessageFormatter"); return result; } public void addParameter(Parameter p) { if (paramList == null) { paramList = new ParameterList(); } paramList.add(p); } public ParameterList getParameterList() { return paramList; } /** * URL to the stylesheet used to transform the output of the standard {@link ErrorMessageFormatter} */ public void setStyleSheet(String newStyleSheet) { styleSheet = newStyleSheet; } public String getStyleSheet() { return styleSheet; } public void setXpathExpression(String string) { xpathExpression = string; } public String getXpathExpression() { return xpathExpression; } }