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