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     */
017    package org.apache.camel.processor.validation;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    import javax.xml.transform.Result;
022    import javax.xml.validation.Schema;
023    
024    import org.xml.sax.SAXException;
025    import org.xml.sax.SAXParseException;
026    
027    import org.apache.camel.Exchange;
028    import org.apache.camel.ValidationException;
029    import org.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    
032    /**
033     * A default error handler which just stores all the errors so they can be reported or transformed.
034     *
035     * @version 
036     */
037    public class DefaultValidationErrorHandler implements ValidatorErrorHandler {
038        private static final Logger LOG = LoggerFactory.getLogger(DefaultValidationErrorHandler.class);
039        private List<SAXParseException> warnings = new ArrayList<SAXParseException>();
040        private List<SAXParseException> errors = new ArrayList<SAXParseException>();
041        private List<SAXParseException> fatalErrors = new ArrayList<SAXParseException>();
042    
043        public void warning(SAXParseException e) throws SAXException {
044            if (LOG.isDebugEnabled()) {
045                LOG.debug("Validation warning: " + e, e);
046            }
047            warnings.add(e);
048        }
049    
050        public void error(SAXParseException e) throws SAXException {
051            if (LOG.isDebugEnabled()) {
052                LOG.debug("Validation error: " + e, e);
053            }
054            errors.add(e);
055        }
056    
057        public void fatalError(SAXParseException e) throws SAXException {
058            if (LOG.isDebugEnabled()) {
059                LOG.debug("Validation fatalError: " + e, e);
060            }
061            fatalErrors.add(e);
062        }
063    
064        public void reset() {
065            warnings.clear();
066            errors.clear();
067            fatalErrors.clear();
068        }
069    
070        public boolean isValid() {
071            return errors.isEmpty() && fatalErrors.isEmpty();
072        }
073    
074        public void handleErrors(Exchange exchange, Schema schema, Result result) throws ValidationException {
075            if (!isValid()) {
076                throw new SchemaValidationException(exchange, schema, fatalErrors, errors, warnings);
077            }
078        }
079    
080        public void handleErrors(Exchange exchange, Object schema) throws ValidationException {
081            if (!isValid()) {
082                throw new SchemaValidationException(exchange, schema, fatalErrors, errors, warnings);
083            }
084        }
085    }