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 "ReportingValidationExceptionHandler.java". Description: 10 "ValidationExceptionHandler that logs exceptions" 11 12 The Initial Developer of the Original Code is University Health Network. Copyright (C) 13 2002. 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 org.slf4j.Logger; 29 import org.slf4j.LoggerFactory; 30 31 import ca.uhn.hl7v2.HL7Exception; 32 import ca.uhn.hl7v2.HapiContext; 33 34 /** 35 * ValidationExceptionHandler that logs all {@link ValidationException}s and 36 * optionally throws the first one wrapped in a {@link HL7Exception}. 37 * <p> 38 * The mimics the behavior of the {@link MessageValidator} of previous releases. 39 * 40 * @author Christian Ohr 41 * 42 */ 43 public class ReportingValidationExceptionHandler implements ValidationExceptionHandler<Boolean>, 44 ValidationExceptionHandlerFactory<Boolean> { 45 46 private static final Logger LOG = LoggerFactory 47 .getLogger(ReportingValidationExceptionHandler.class); 48 private final boolean throwFirstException; 49 private ValidationException firstException; 50 51 /** 52 * @param throwFirstException true if first exception shall be thrown 53 */ 54 public ReportingValidationExceptionHandler(boolean throwFirstException) { 55 this.throwFirstException = throwFirstException; 56 } 57 58 /** 59 * Logs all exceptions 60 * 61 * @see ca.uhn.hl7v2.validation.CollectingValidationExceptionHandler#onExceptions(ca.uhn.hl7v2.validation.ValidationException[]) 62 */ 63 public void onExceptions(ValidationException... exceptions) { 64 if (firstException == null) 65 firstException = exceptions[0]; 66 for (ValidationException ve : exceptions) { 67 switch (ve.getSeverity()) { 68 case ERROR : LOG.error("Invalid message", ve); break; 69 case WARNING: LOG.warn("Message with warnings", ve); break; 70 case INFO: LOG.info("Message with comments", ve); break; 71 } 72 } 73 } 74 75 /** 76 * If the validation failed, throws Exception or returns <code>false</code>, 77 * depending on {link {@link #throwFirstException}. 78 * 79 * @throws HL7Exception if validation has failed and 80 * {@link #throwFirstException} is true 81 * @see ca.uhn.hl7v2.validation.DefaultValidationExceptionHandler#result() 82 */ 83 public Boolean result() throws HL7Exception { 84 if (hasFailed() && throwFirstException) 85 throw new HL7Exception(firstException.getMessage(), firstException); 86 return !hasFailed(); 87 } 88 89 public boolean hasFailed() { 90 return firstException != null; 91 } 92 93 public ValidationExceptionHandler<Boolean> getNewInstance(HapiContext context) { 94 return new ReportingValidationExceptionHandler(throwFirstException); 95 } 96 97 /** 98 * Does nothing 99 */ 100 public void setValidationSubject(Object subject) { 101 } 102 103 104 105 }