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 025 import org.apache.commons.logging.Log; 026 import org.apache.commons.logging.LogFactory; 027 import org.xml.sax.SAXException; 028 import org.xml.sax.SAXParseException; 029 030 import org.apache.camel.Exchange; 031 import org.apache.camel.ValidationException; 032 033 /** 034 * A default error handler which just stores all the errors so they can be reported or transformed. 035 * 036 * @version $Revision: $ 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 } 067 068 public boolean isValid() { 069 return errors.isEmpty() && fatalErrors.isEmpty(); 070 } 071 072 public void handleErrors(Exchange exchange, Schema schema, DOMResult result) throws ValidationException { 073 if (!isValid()) { 074 throw new SchemaValidationException(exchange, schema, fatalErrors, errors, warnings); 075 } 076 } 077 078 public void handleErrors(Exchange exchange, Object schema) throws ValidationException { 079 if (!isValid()) { 080 throw new SchemaValidationException(exchange, schema, fatalErrors, errors, warnings); 081 } 082 } 083 }