View Javadoc

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 "ValidationContextFactory.java".  Description: 
10  "Source of ValidationContext" 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2004.  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.impl;
27  
28  import ca.uhn.hl7v2.HL7Exception;
29  import ca.uhn.hl7v2.util.ReflectionUtil;
30  import ca.uhn.hl7v2.validation.ValidationContext;
31  import ca.uhn.hl7v2.validation.ValidationException;
32  import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
33  import ca.uhn.hl7v2.validation.builder.support.DefaultValidationBuilder;
34  import ca.uhn.hl7v2.validation.builder.support.NoValidationBuilder;
35  
36  /**
37   * <p>
38   * The <code>ValidationContext</code> returned by <code>getContext()</code> is
39   * determined by the system property "ca.uhn.hl7v2.validation.context_class".
40   * This factory defines two inner classes that can be used: DefaultValidation
41   * and NoValidation. You can also create your own context, setting whatever
42   * rules you want in its constructor, and reference it instead (it must have a
43   * zero-arg constructor). If this property is not set, DefaultValidation is
44   * used.
45   * </p>
46   * 
47   * <p>
48   * Also note that the contexts provided here use
49   * <code>ValidationContextImpl</code>, so rule bindings can be added or removed
50   * programmatically from the starting set.
51   * </p>
52   * 
53   * @author Bryan Tripp
54   * @author Christian Ohr
55   * 
56   */
57  public class ValidationContextFactory {
58  
59      private static ValidationContext ourContext;
60  
61      public static final String CONTEXT_PROPERTY = "ca.uhn.hl7v2.validation.context_class";
62  
63      /**
64       * Returns a singleton <code>ValidationContext</code>, creating it if
65       * necessary.
66       * 
67       * @return <code>ValidationContext</code>
68       */
69      public synchronized static ValidationContext getContext() throws HL7Exception {
70          if (ourContext == null) {
71              String contextClassName = System.getProperty(CONTEXT_PROPERTY);
72              ourContext = contextClassName == null ? defaultValidation()
73                      : customValidation(contextClassName);
74          }
75          return ourContext;
76      }
77  
78      /**
79       * @return an instance of a non-validating context
80       */
81      @SuppressWarnings("unchecked")
82  	public static <T extends ValidationContext> T noValidation() {
83          return (T) new ValidationContextImpl(new NoValidationBuilder());
84      }
85  
86      /**
87       * @return an instance of a default validation context
88       */
89      @SuppressWarnings("unchecked")
90  	public static <T extends ValidationContext> T defaultValidation() {
91          return (T)new ValidationContextImpl(new DefaultValidationBuilder());
92      }
93  
94      /**
95       * @param ruleBuilderClassName class name of a {@link ValidationRuleBuilder}
96       *            subclass
97       * @return a validation rule builder instance
98       * @throws HL7Exception if builder cannot be built
99       */
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 }