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 import org.apache.camel.Exchange;
028 import org.apache.camel.ValidationException;
029 import org.apache.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031
032 /**
033 * A default error handler which just stores all the errors so they can be reported or transformed.
034 *
035 * @version $Revision: 684301 $
036 */
037 public class DefaultValidationErrorHandler implements ValidatorErrorHandler {
038 private static final transient Log LOG = LogFactory.getLog(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("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("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("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, DOMResult 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 }