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
022 import javax.xml.transform.dom.DOMResult;
023 import javax.xml.validation.Schema;
024 import org.xml.sax.SAXException;
025 import org.xml.sax.SAXParseException;
026
027
028 import org.apache.camel.Exchange;
029 import org.apache.camel.ValidationException;
030 import org.apache.commons.logging.Log;
031 import org.apache.commons.logging.LogFactory;
032
033 /**
034 * A default error handler which just stores all the errors so they can be reported or transformed.
035 *
036 * @version $Revision: 640438 $
037 */
038 public class DefaultValidationErrorHandler implements ValidatorErrorHandler {
039 private static final transient Log LOG = LogFactory.getLog(DefaultValidationErrorHandler.class);
040 private List<SAXParseException> warnings = new ArrayList<SAXParseException>();
041 private List<SAXParseException> errors = new ArrayList<SAXParseException>();
042 private List<SAXParseException> fatalErrors = new ArrayList<SAXParseException>();
043
044 public void warning(SAXParseException e) throws SAXException {
045 if (LOG.isDebugEnabled()) {
046 LOG.debug("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("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("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, DOMResult 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 }