1 /** 2 The contents of this file are subject to the Mozilla Public License Version 1.1 3 (the "License"); you may not use this file except in compliance with the License. 4 You may obtain a copy of the License at http://www.mozilla.org/MPL/ 5 Software distributed under the License is distributed on an "AS IS" basis, 6 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 7 specific language governing rights and limitations under the License. 8 9 The Original Code is "AbstractValidationExceptionHandler.java". Description: 10 "Abstract implementation of a ValidationExceptionHandler." 11 12 The Initial Developer of the Original Code is University Health Network. Copyright (C) 13 2012. All Rights Reserved. 14 15 Contributor(s): ______________________________________. 16 17 Alternatively, the contents of this file may be used under the terms of the 18 GNU General Public License (the "GPL"), in which case the provisions of the GPL are 19 applicable instead of those above. If you wish to allow use of your version of this 20 file only under the terms of the GPL and not to allow others to use your version 21 of this file under the MPL, indicate your decision by deleting the provisions above 22 and replace them with the notice and other provisions required by the GPL License. 23 If you do not delete the provisions above, a recipient may use your version of 24 this file under either the MPL or the GPL. 25 */ 26 package ca.uhn.hl7v2.validation; 27 28 import ca.uhn.hl7v2.HapiContext; 29 import ca.uhn.hl7v2.HapiContextSupport; 30 31 /** 32 * Abstract base class of a ValidationExceptionHandler that supports a validation subject. Concrete 33 * implementations should inherit from this class. 34 * 35 * @author Christian Ohr 36 */ 37 public abstract class AbstractValidationExceptionHandler<R> extends HapiContextSupport implements 38 ValidationExceptionHandler<R> { 39 40 private Object subject; 41 42 /** 43 * @param context Hapi Context 44 */ 45 public AbstractValidationExceptionHandler(HapiContext context) { 46 super(context); 47 } 48 49 public void onExceptions(ValidationException... exceptions) { 50 for (ValidationException ve : exceptions) { 51 switch (ve.getSeverity()) { 52 case ERROR: 53 error(ve); 54 break; 55 case INFO: 56 info(ve); 57 break; 58 case WARNING: 59 warning(ve); 60 break; 61 } 62 } 63 } 64 65 /** 66 * Called on ValidationExceptions with error severity 67 * @param exception ValidationException 68 */ 69 public void error(final ValidationException exception) {} 70 71 /** 72 * Called on ValidationExceptions with warning severity 73 * @param exception ValidationException 74 */ 75 public void warning(final ValidationException exception) {} 76 77 /** 78 * Called on ValidationExceptions with info severity 79 * @param exception ValidationException 80 */ 81 public void info(final ValidationException exception) {} 82 83 /** 84 * Sets the object that is the target of validation. This is helpful 85 * to be called to give this handler e.g. access to the original 86 * message that has been validated. 87 * 88 * @param subject subject to be validated 89 */ 90 public void setValidationSubject(Object subject) { 91 this.subject = subject; 92 } 93 94 /** 95 * @return the validation subject 96 */ 97 public Object getValidationSubject() { 98 return subject; 99 } 100 101 }