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 "AbstractValidationExceptionHandler.java". Description: 010"Abstract implementation of a ValidationExceptionHandler." 011 012The Initial Developer of the Original Code is University Health Network. Copyright (C) 0132012. 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 ca.uhn.hl7v2.HapiContext; 029import ca.uhn.hl7v2.HapiContextSupport; 030 031/** 032 * Abstract base class of a ValidationExceptionHandler that supports a validation subject. Concrete 033 * implementations should inherit from this class. 034 * 035 * @author Christian Ohr 036 */ 037public abstract class AbstractValidationExceptionHandler<R> extends HapiContextSupport implements 038 ValidationExceptionHandler<R> { 039 040 private Object subject; 041 042 /** 043 * @param context Hapi Context 044 */ 045 public AbstractValidationExceptionHandler(HapiContext context) { 046 super(context); 047 } 048 049 public void onExceptions(ValidationException... exceptions) { 050 for (ValidationException ve : exceptions) { 051 switch (ve.getSeverity()) { 052 case ERROR: 053 error(ve); 054 break; 055 case INFO: 056 info(ve); 057 break; 058 case WARNING: 059 warning(ve); 060 break; 061 } 062 } 063 } 064 065 /** 066 * Called on ValidationExceptions with error severity 067 * @param exception ValidationException 068 */ 069 public void error(final ValidationException exception) {} 070 071 /** 072 * Called on ValidationExceptions with warning severity 073 * @param exception ValidationException 074 */ 075 public void warning(final ValidationException exception) {} 076 077 /** 078 * Called on ValidationExceptions with info severity 079 * @param exception ValidationException 080 */ 081 public void info(final ValidationException exception) {} 082 083 /** 084 * Sets the object that is the target of validation. This is helpful 085 * to be called to give this handler e.g. access to the original 086 * message that has been validated. 087 * 088 * @param subject subject to be validated 089 */ 090 public void setValidationSubject(Object subject) { 091 this.subject = subject; 092 } 093 094 /** 095 * @return the validation subject 096 */ 097 public Object getValidationSubject() { 098 return subject; 099 } 100 101}