001 package ca.uhn.hl7v2.validation.app; 002 003 import ca.uhn.hl7v2.model.*; 004 import ca.uhn.hl7v2.HL7Exception; 005 import ca.uhn.hl7v2.validation.ValidationException; 006 import ca.uhn.hl7v2.validation.impl.ConformanceProfileRule; 007 008 import ca.uhn.log.*; 009 010 /** 011 * <p>Tests messages against conformance profiles. A constant profile 012 * can be used, or each message can be tested against the profiles 013 * that it declares.</p> 014 * <p>Note that profiles are referenced by ID, and that the ca.uhn.hl7v2.conf.store 015 * package is used to get the actual profiles. </p> 016 * @author Bryan Tripp 017 */ 018 public class ProfileTestApplication extends TestApplication { 019 020 private static final HapiLog log = HapiLogFactory.getHapiLog(ProfileTestApplication.class); 021 private String profileID; 022 private ConformanceProfileRule rule; 023 024 /** 025 * Creates a new instance of ProfileTestApplication that tests using profiles 026 * declared in each message (if any) 027 */ 028 public ProfileTestApplication() { 029 rule = new ConformanceProfileRule(); 030 } 031 032 /** 033 * Creates a new instance of ProfileTestApplication. 034 * @param profileID the ID of the profile against which to test messages; 035 * null means use profiles declared in the message (if any) 036 */ 037 public ProfileTestApplication(String profileID) { 038 this.profileID = profileID; 039 rule = new ConformanceProfileRule(profileID); 040 } 041 042 /** 043 * Returns true if this Application wishes to accept the message. By returning 044 * true, this Application declares itself the recipient of the message, accepts 045 * responsibility for it, and must be able to respond appropriately to the sending system. 046 */ 047 public boolean canProcess(Message in) { 048 return true; 049 } 050 051 /** 052 * Tests the message against a profile or profiles. A constant profile 053 * is used if one was provided to the constructor, otherwise any profiles 054 * declared in the message are used. 055 */ 056 public HL7Exception[] test(Message in) throws HL7Exception { 057 ValidationException[] errors = rule.test(in); 058 059 HL7Exception[] result = new HL7Exception[errors.length]; 060 for (int i = 0; i < errors.length; i++) { 061 Throwable t = errors[i].getCause(); 062 if ((t != null) && (t instanceof HL7Exception)) { 063 result[i] = (HL7Exception) t; 064 } else { 065 result[i] = new HL7Exception(errors[i]); 066 } 067 } 068 069 return result; 070 } 071 072 public String getProfileID() { 073 return this.profileID; 074 } 075 }