001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.util.xml;
018
019import org.apache.commons.logging.Log;
020import org.xml.sax.ErrorHandler;
021import org.xml.sax.SAXException;
022import org.xml.sax.SAXParseException;
023
024/**
025 * Simple {@code org.xml.sax.ErrorHandler} implementation:
026 * logs warnings using the given Commons Logging logger instance,
027 * and rethrows errors to discontinue the XML transformation.
028 *
029 * @author Juergen Hoeller
030 * @since 1.2
031 */
032public class SimpleSaxErrorHandler implements ErrorHandler {
033
034        private final Log logger;
035
036
037        /**
038         * Create a new SimpleSaxErrorHandler for the given
039         * Commons Logging logger instance.
040         */
041        public SimpleSaxErrorHandler(Log logger) {
042                this.logger = logger;
043        }
044
045
046        @Override
047        public void warning(SAXParseException ex) throws SAXException {
048                logger.warn("Ignored XML validation warning", ex);
049        }
050
051        @Override
052        public void error(SAXParseException ex) throws SAXException {
053                throw ex;
054        }
055
056        @Override
057        public void fatalError(SAXParseException ex) throws SAXException {
058                throw ex;
059        }
060
061}