001/** 002The contents of this file are subject to the Mozilla Public License Version 1.1 003(the "License"); you may not use this file except in compliance with the License. 004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 005Software distributed under the License is distributed on an "AS IS" basis, 006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 007specific language governing rights and limitations under the License. 008 009The Original Code is "CollectingValidationExceptionHandler.java". Description: 010"ValidationExceptionHandler that collects exceptions" 011 012The Initial Developer of the Original Code is University Health Network. Copyright (C) 0132002. All Rights Reserved. 014 015Contributor(s): ______________________________________. 016 017Alternatively, the contents of this file may be used under the terms of the 018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 019applicable instead of those above. If you wish to allow use of your version of this 020file only under the terms of the GPL and not to allow others to use your version 021of this file under the MPL, indicate your decision by deleting the provisions above 022and replace them with the notice and other provisions required by the GPL License. 023If you do not delete the provisions above, a recipient may use your version of 024this file under either the MPL or the GPL. 025 */ 026package ca.uhn.hl7v2.validation; 027 028import java.util.ArrayList; 029import java.util.Collections; 030import java.util.List; 031 032import ca.uhn.hl7v2.HapiContext; 033import ca.uhn.hl7v2.Severity; 034 035/** 036 * ValidationExceptionHandler that collects all {@link ValidationException}s in a list. 037 * Subclasses can then derive a overall {@link #result() result} from this list. 038 * 039 * @author Christian Ohr 040 * 041 */ 042public abstract class CollectingValidationExceptionHandler<R> extends AbstractValidationExceptionHandler<R> { 043 044 private List<ValidationException> exceptions = new ArrayList<ValidationException>(); 045 private Severity minimumSeverityToCollect = Severity.ERROR; 046 047 /** 048 * @param context Hapi context 049 */ 050 public CollectingValidationExceptionHandler(HapiContext context) { 051 super(context); 052 } 053 054 @Override 055 public void error(ValidationException exception) { 056 if (isSeverityAtLeast(Severity.ERROR)) exceptions.add(exception); 057 } 058 059 @Override 060 public void info(ValidationException exception) { 061 if (isSeverityAtLeast(Severity.INFO)) exceptions.add(exception); 062 } 063 064 @Override 065 public void warning(ValidationException exception) { 066 if (isSeverityAtLeast(Severity.WARNING)) exceptions.add(exception); 067 } 068 069 private boolean isSeverityAtLeast(Severity s) { 070 return s.compareTo(getMinimumSeverityToCollect()) >= 0; 071 } 072 073 /** 074 * @return unmodifiable list of collected exceptions. If none have occurred, the list is empty. 075 */ 076 public List<ValidationException> getExceptions() { 077 return Collections.unmodifiableList(exceptions); 078 } 079 080 /** 081 * Set the minimum severity to be added to the list of exceptions. Default is ERROR. 082 * @param minimumSeverityToCollect the minimum severity to be added to the list of exceptions 083 */ 084 public void setMinimumSeverityToCollect(Severity minimumSeverityToCollect) { 085 this.minimumSeverityToCollect = minimumSeverityToCollect; 086 } 087 088 /** 089 * @return the minimum severity to be added to the list of exceptions. Default is ERROR. 090 */ 091 public Severity getMinimumSeverityToCollect() { 092 return minimumSeverityToCollect; 093 } 094 095 /** 096 * @see ca.uhn.hl7v2.validation.ValidationExceptionHandler#hasFailed() 097 */ 098 public boolean hasFailed() { 099 for (ValidationException ve : exceptions) { 100 if (ve.getSeverity() == Severity.ERROR) return true; 101 } 102 return false; 103 } 104 105 106 107}