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.List;
020
021import org.xml.sax.SAXParseException;
022
023import org.apache.camel.Exchange;
024import org.apache.camel.ValidationException;
025
026/**
027 * A Schema validation exception occurred
028 * 
029 * @version 
030 */
031public class SchemaValidationException extends ValidationException {
032    private static final long serialVersionUID = 2656907296674888684L;
033
034    private final Object schema;
035    private final List<SAXParseException> fatalErrors;
036    private final List<SAXParseException> errors;
037    private final List<SAXParseException> warnings;
038
039    public SchemaValidationException(Exchange exchange, Object schema, List<SAXParseException> fatalErrors,
040                                     List<SAXParseException> errors, List<SAXParseException> warnings) {
041        super(exchange, message(schema, fatalErrors, errors, warnings));
042        this.schema = schema;
043        this.fatalErrors = fatalErrors;
044        this.errors = errors;
045        this.warnings = warnings;
046    }
047
048    /**
049     * Returns the schema that failed
050     */
051    public Object getSchema() {
052        return schema;
053    }
054
055    /**
056     * Returns the validation errors
057     */
058    public List<SAXParseException> getErrors() {
059        return errors;
060    }
061
062    /**
063     * Returns the fatal validation errors
064     */
065    public List<SAXParseException> getFatalErrors() {
066        return fatalErrors;
067    }
068
069    /**
070     * Returns the validation warnings
071     */
072    public List<SAXParseException> getWarnings() {
073        return warnings;
074    }
075
076    protected static String message(Object schema, List<SAXParseException> fatalErrors,
077                                    List<SAXParseException> errors, List<SAXParseException> warnings) {
078        StringBuilder buffer = new StringBuilder("Validation failed for: ")
079            .append(schema)
080            .append("\n");
081
082        if (!fatalErrors.isEmpty()) {
083            buffer.append("fatal errors: [")
084                .append("\n");
085            appendDetails(buffer, fatalErrors);
086            buffer.append("]")
087                .append("\n");
088        }
089
090        if (!errors.isEmpty()) {
091            buffer.append("errors: [")
092                .append("\n");
093            appendDetails(buffer, errors);
094            buffer.append("]");
095        }
096
097        return buffer.toString();
098    }
099
100    private static void appendDetails(StringBuilder buffer, List<SAXParseException> saxParseExceptions) {
101        for (SAXParseException e : saxParseExceptions) {
102            buffer.append(e.getClass().getName()).append(": ");
103            buffer.append(e.getMessage()).append(", ");
104            buffer.append("Line : ").append(e.getLineNumber()).append(", ");
105            buffer.append("Column : ").append(e.getColumnNumber()).append("\n");
106        }
107    }
108}