001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.builder.xml;
018
019import javax.xml.transform.ErrorListener;
020import javax.xml.transform.TransformerException;
021
022import org.xml.sax.ErrorHandler;
023import org.xml.sax.SAXException;
024import org.xml.sax.SAXParseException;
025
026import org.apache.camel.Exchange;
027
028/**
029 * {@link ErrorHandler} and {@link ErrorListener} which will ignore warnings,
030 * and throws error and fatal as exception, which ensures those can be caught by Camel and dealt-with.
031 * <p/>
032 * Also any warning, error or fatal error is stored on the {@link Exchange} as a property with the keys
033 * <tt>CamelXsltWarning</tt>, <tt>CamelXsltError</tt>, and <tt>CamelXsltFatalError</tt> which
034 * allows end users to access those information form the exchange.
035 */
036public class DefaultTransformErrorHandler implements ErrorHandler, ErrorListener {
037
038    private final Exchange exchange;
039
040    public DefaultTransformErrorHandler(Exchange exchange) {
041        this.exchange = exchange;
042    }
043
044    public void error(SAXParseException exception) throws SAXException {
045        exchange.setProperty(Exchange.XSLT_ERROR, exception);
046        throw exception;
047    }
048
049    public void fatalError(SAXParseException exception) throws SAXException {
050        exchange.setProperty(Exchange.XSLT_FATAL_ERROR, exception);
051        throw exception;
052    }
053
054    public void warning(SAXParseException exception) throws SAXException {
055        exchange.setProperty(Exchange.XSLT_WARNING, exception);
056    }
057
058    public void error(TransformerException exception) throws TransformerException {
059        exchange.setProperty(Exchange.XSLT_ERROR, exception);
060        throw exception;
061    }
062
063    public void fatalError(TransformerException exception) throws TransformerException {
064        exchange.setProperty(Exchange.XSLT_FATAL_ERROR, exception);
065        throw exception;
066    }
067
068    public void warning(TransformerException exception) throws TransformerException {
069        exchange.setProperty(Exchange.XSLT_WARNING, exception);
070    }
071
072}