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 "ValidationContextImpl.java". Description: 010"A default implementation of ValidationContext." 011 012The Initial Developer of the Original Code is University Health Network. Copyright (C) 0132004. 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.impl; 027 028import java.io.Serializable; 029import java.util.ArrayList; 030import java.util.List; 031 032import ca.uhn.hl7v2.model.Primitive; 033import ca.uhn.hl7v2.validation.EncodingRule; 034import ca.uhn.hl7v2.validation.MessageRule; 035import ca.uhn.hl7v2.validation.PrimitiveTypeRule; 036import ca.uhn.hl7v2.validation.Rule; 037import ca.uhn.hl7v2.validation.ValidationContext; 038 039/** 040 * A default implementation of <code>ValidationContext</code>. 041 * 042 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a> 043 * @author Christian Ohr 044 */ 045@SuppressWarnings("serial") 046public class ValidationContextImpl implements ValidationContext, Serializable { 047 048 private List<RuleBinding<PrimitiveTypeRule>> myPrimitiveRuleBindings; 049 private List<RuleBinding<MessageRule>> myMessageRuleBindings; 050 private List<RuleBinding<EncodingRule>> myEncodingRuleBindings; 051 052 public ValidationContextImpl() { 053 myPrimitiveRuleBindings = new ArrayList<RuleBinding<PrimitiveTypeRule>>(); 054 myMessageRuleBindings = new ArrayList<RuleBinding<MessageRule>>(); 055 myEncodingRuleBindings = new ArrayList<RuleBinding<EncodingRule>>(); 056 } 057 058 /** 059 * @see ca.uhn.hl7v2.validation.ValidationContext#getDataTypeRules(java.lang.String, 060 * java.lang.String) 061 * @param theType ignored 062 */ 063 public PrimitiveTypeRule[] getPrimitiveRules(String theVersion, String theTypeName, Primitive theType) { 064 return getRules(myPrimitiveRuleBindings, theVersion, theTypeName, PrimitiveTypeRule.class); 065 } 066 067 /** 068 * @return a List of <code>RuleBinding</code>s for 069 * <code>PrimitiveTypeRule</code>s. 070 */ 071 public List<RuleBinding<PrimitiveTypeRule>> getPrimitiveRuleBindings() { 072 return myPrimitiveRuleBindings; 073 } 074 075 076 /** 077 * @see ca.uhn.hl7v2.validation.ValidationContext 078 * #getMessageRules(java.lang.String, java.lang.String, 079 * java.lang.String) 080 */ 081 public MessageRule[] getMessageRules(String theVersion, String theMessageType, String theTriggerEvent) { 082 return getRules(myMessageRuleBindings, theVersion, theMessageType + "^" + theTriggerEvent, MessageRule.class); 083 } 084 085 /** 086 * @return a List of <code>RuleBinding</code>s for <code>MessageRule</code>s. 087 */ 088 public List<RuleBinding<MessageRule>> getMessageRuleBindings() { 089 return myMessageRuleBindings; 090 } 091 092 /** 093 * @see ca.uhn.hl7v2.validation.ValidationContext#getEncodingRules(java.lang.String, 094 * java.lang.String) 095 */ 096 public EncodingRule[] getEncodingRules(String theVersion, String theEncoding) { 097 return getRules(myEncodingRuleBindings, theVersion, theEncoding, EncodingRule.class); 098 } 099 100 /** 101 * @return a List of <code>RuleBinding</code>s for <code>EncodingRule</code>s. 102 */ 103 public List<RuleBinding<EncodingRule>> getEncodingRuleBindings() { 104 return myEncodingRuleBindings; 105 } 106 107 private <T extends Rule> T[] getRules(List<RuleBinding<T>> bindings, String version, String scope, 108 Class<T> ruleClass) { 109 List<T> active = new ArrayList<T>(bindings.size()); 110 for (RuleBinding<T> binding : bindings) { 111 if (applies(binding, version, scope)) 112 active.add(binding.getRule()); 113 } 114 return toArray(active, ruleClass); 115 } 116 117 private boolean applies(RuleBinding<?> binding, String version, String scope) { 118 return (binding.getActive() && binding.appliesToVersion(version) && binding.appliesToScope(scope)); 119 } 120 121 @SuppressWarnings("unchecked") 122 private <T> T[] toArray(List<T> list, Class<T> c) { 123 return list.toArray((T[]) java.lang.reflect.Array.newInstance(c, list.size())); 124 } 125 126}