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 "ValidationContextFactory.java". Description: 010"Source 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 ca.uhn.hl7v2.HL7Exception; 029import ca.uhn.hl7v2.util.ReflectionUtil; 030import ca.uhn.hl7v2.validation.ValidationContext; 031import ca.uhn.hl7v2.validation.ValidationException; 032import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder; 033import ca.uhn.hl7v2.validation.builder.support.DefaultValidationBuilder; 034import ca.uhn.hl7v2.validation.builder.support.NoValidationBuilder; 035 036/** 037 * <p> 038 * The <code>ValidationContext</code> returned by <code>getContext()</code> is 039 * determined by the system property "ca.uhn.hl7v2.validation.context_class". 040 * This factory defines two inner classes that can be used: DefaultValidation 041 * and NoValidation. You can also create your own context, setting whatever 042 * rules you want in its constructor, and reference it instead (it must have a 043 * zero-arg constructor). If this property is not set, DefaultValidation is 044 * used. 045 * </p> 046 * 047 * <p> 048 * Also note that the contexts provided here use 049 * <code>ValidationContextImpl</code>, so rule bindings can be added or removed 050 * programmatically from the starting set. 051 * </p> 052 * 053 * @author Bryan Tripp 054 * @author Christian Ohr 055 * 056 */ 057public class ValidationContextFactory { 058 059 private static ValidationContext ourContext; 060 061 public static final String CONTEXT_PROPERTY = "ca.uhn.hl7v2.validation.context_class"; 062 063 /** 064 * Returns a singleton <code>ValidationContext</code>, creating it if 065 * necessary. 066 * 067 * @return <code>ValidationContext</code> 068 */ 069 public synchronized static ValidationContext getContext() throws HL7Exception { 070 if (ourContext == null) { 071 String contextClassName = System.getProperty(CONTEXT_PROPERTY); 072 ourContext = contextClassName == null ? defaultValidation() 073 : customValidation(contextClassName); 074 } 075 return ourContext; 076 } 077 078 /** 079 * @return an instance of a non-validating context 080 */ 081 @SuppressWarnings("unchecked") 082 public static <T extends ValidationContext> T noValidation() { 083 return (T) new ValidationContextImpl(new NoValidationBuilder()); 084 } 085 086 /** 087 * @return an instance of a default validation context 088 */ 089 @SuppressWarnings("unchecked") 090 public static <T extends ValidationContext> T defaultValidation() { 091 return (T)new ValidationContextImpl(new DefaultValidationBuilder()); 092 } 093 094 /** 095 * @param ruleBuilderClassName class name of a {@link ValidationRuleBuilder} 096 * subclass 097 * @return a validation rule builder instance 098 * @throws HL7Exception if builder cannot be built 099 */ 100 @SuppressWarnings("unchecked") 101 public static ValidationRuleBuilder customBuilder(String ruleBuilderClassName) 102 throws HL7Exception { 103 Class<? extends ValidationRuleBuilder> c; 104 try { 105 c = (Class<? extends ValidationRuleBuilder>) Class.forName(ruleBuilderClassName); 106 } catch (ClassNotFoundException e) { 107 throw new HL7Exception(e); 108 } 109 return ReflectionUtil.instantiate(c); 110 } 111 112 public static ValidationContext fromBuilder(String ruleBuilderClassName) throws HL7Exception { 113 return new ValidationContextImpl(customBuilder(ruleBuilderClassName)); 114 } 115 116 public static ValidationContext fromBuilder(ValidationRuleBuilder builder) { 117 return new ValidationContextImpl(builder); 118 } 119 120 /** 121 * @param contextClassName class name of a {@link ValidationContext} 122 * subclass 123 * @return instance of the ValidationContext 124 * @throws HL7Exception if context cannot be obtained 125 */ 126 @SuppressWarnings("unchecked") 127 public static ValidationContext customValidation(String contextClassName) throws HL7Exception { 128 Class<? extends ValidationContext> c; 129 try { 130 c = (Class<? extends ValidationContext>) Class.forName(contextClassName); 131 } catch (Exception e) { 132 throw new HL7Exception(e); 133 } 134 return ReflectionUtil.instantiate(c); 135 136 } 137 138}