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.validation.ValidationContext; 029import ca.uhn.hl7v2.validation.ValidationException; 030 031/** 032 * <p>Source of <code>ValidationContext</code>. </p> 033 * 034 * <p>The <code>ValidationContext</code> returned by <code>getContext()</code> 035 * is determined by the system property "ca.uhn.hl7v2.validation.context_class". 036 * This factory defines two inner classes that can be used: DefaultValidation 037 * and NoValidation. You can also create your own context, setting whatever rules 038 * you want in its constructor, and reference it instead (it must have a zero-arg 039 * constructor). If this property is not set, DefaultValidation is used. </p> 040 * 041 * <p>Also note that the contexts provided here extend <code>ValidationContextImpl</code>, 042 * so rule bindings can be added or removed programmatically from the starting set. </p> 043 * 044 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a> 045 * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:40 $ by $Author: jamesagnew $ 046 */ 047public class ValidationContextFactory { 048 049 private static ValidationContext ourContext; 050 051 public static final String CONTEXT_PROPERTY = "ca.uhn.hl7v2.validation.context_class"; 052 053 private static final ValidationContext NO_VALIDATION = new NoValidation(); 054 private static final ValidationContext DEFAULT_VALIDATION = new DefaultValidation(); 055 056 /** 057 * Returns a singleton <code>ValidationContext</code>, creating it if necessary. 058 * 059 * @return <code>ValidationContext</code> 060 */ 061 public synchronized static ValidationContext getContext() throws ValidationException { 062 if (ourContext == null) { 063 String contextClassName = System.getProperty(CONTEXT_PROPERTY); 064 ourContext = contextClassName == null ? 065 defaultValidation() : 066 customValidation(contextClassName); 067 } 068 return ourContext; 069 } 070 071 public static ValidationContext noValidation() { 072 return NO_VALIDATION; 073 } 074 075 public static ValidationContext defaultValidation() { 076 return DEFAULT_VALIDATION; 077 } 078 079 @SuppressWarnings("unchecked") 080 public static ValidationContext customValidation(String contextClassName) throws ValidationException { 081 try { 082 Class<? extends ValidationContext> c = (Class<? extends ValidationContext>) Class.forName(contextClassName); 083 return c.newInstance(); 084 } catch (Exception e) { 085 throw new ValidationException(e); 086 } 087 } 088 089}