All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.camel.processor.validation.SchemaValidationException Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.processor.validation;

import java.util.List;

import org.xml.sax.SAXParseException;

import org.apache.camel.Exchange;
import org.apache.camel.ValidationException;

/**
 * A Schema validation exception occurred
 * 
 * @version 
 */
public class SchemaValidationException extends ValidationException {
    private static final long serialVersionUID = 2656907296674888684L;

    private final Object schema;
    private final List fatalErrors;
    private final List errors;
    private final List warnings;

    public SchemaValidationException(Exchange exchange, Object schema, List fatalErrors,
                                     List errors, List warnings) {
        super(exchange, message(schema, fatalErrors, errors, warnings));
        this.schema = schema;
        this.fatalErrors = fatalErrors;
        this.errors = errors;
        this.warnings = warnings;
    }

    /**
     * Returns the schema that failed
     */
    public Object getSchema() {
        return schema;
    }

    /**
     * Returns the validation errors
     */
    public List getErrors() {
        return errors;
    }

    /**
     * Returns the fatal validation errors
     */
    public List getFatalErrors() {
        return fatalErrors;
    }

    /**
     * Returns the validation warnings
     */
    public List getWarnings() {
        return warnings;
    }

    protected static String message(Object schema, List fatalErrors,
                                    List errors, List warnings) {
        StringBuilder buffer = new StringBuilder("Validation failed for: ")
            .append(schema)
            .append("\n");

        if (!fatalErrors.isEmpty()) {
            buffer.append("fatal errors: [")
                .append("\n");
            appendDetails(buffer, fatalErrors);
            buffer.append("]")
                .append("\n");
        }

        if (!errors.isEmpty()) {
            buffer.append("errors: [")
                .append("\n");
            appendDetails(buffer, errors);
            buffer.append("]");
        }

        return buffer.toString();
    }

    private static void appendDetails(StringBuilder buffer, List saxParseExceptions) {
        for (SAXParseException e : saxParseExceptions) {
            buffer.append(e.getClass().getName()).append(": ");
            buffer.append(e.getMessage()).append(", ");
            buffer.append("Line : ").append(e.getLineNumber()).append(", ");
            buffer.append("Column : ").append(e.getColumnNumber()).append("\n");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy