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.builder.ValidationRuleBuilder;
32  import ca.uhn.hl7v2.validation.builder.support.DefaultValidationBuilder;
33  import ca.uhn.hl7v2.validation.builder.support.NoValidationBuilder;
34  
35  /**
36   * <p>
37   * The <code>ValidationContext</code> returned by <code>getContext()</code> is
38   * determined by the system property "ca.uhn.hl7v2.validation.context_class".
39   * This factory defines two inner classes that can be used: DefaultValidation
40   * and NoValidation. You can also create your own context, setting whatever
41   * rules you want in its constructor, and reference it instead (it must have a
42   * zero-arg constructor). If this property is not set, DefaultValidation is
43   * used.
44   * </p>
45   * 
46   * <p>
47   * Also note that the contexts provided here use
48   * <code>ValidationContextImpl</code>, so rule bindings can be added or removed
49   * programmatically from the starting set.
50   * </p>
51   * 
52   * @author Bryan Tripp
53   * @author Christian Ohr
54   * 
55   */
56  public class ValidationContextFactory {
57  
58      private static ValidationContext ourContext;
59  
60      public static final String CONTEXT_PROPERTY = "ca.uhn.hl7v2.validation.context_class";
61  
62      /**
63       * Returns a singleton <code>ValidationContext</code>, creating it if
64       * necessary.
65       * 
66       * @return <code>ValidationContext</code>
67       */
68      public synchronized static ValidationContext getContext() throws HL7Exception {
69          if (ourContext == null) {
70              String contextClassName = System.getProperty(CONTEXT_PROPERTY);
71              ourContext = contextClassName == null ? defaultValidation()
72                      : customValidation(contextClassName);
73          }
74          return ourContext;
75      }
76  
77      /**
78       * @return an instance of a non-validating context
79       */
80      @SuppressWarnings("unchecked")
81  	public static ValidationContext noValidation() {
82          return new ValidationContextImpl(new NoValidationBuilder());
83      }
84  
85      /**
86       * @return an instance of a default validation context
87       */
88      @SuppressWarnings("unchecked")
89  	public static ValidationContext defaultValidation() {
90          return new ValidationContextImpl(new DefaultValidationBuilder());
91      }
92  
93      /**
94       * @param ruleBuilderClassName class name of a {@link ValidationRuleBuilder}
95       *            subclass
96       * @return a validation rule builder instance
97       * @throws HL7Exception if builder cannot be built
98       */
99      @SuppressWarnings("unchecked")
100     public static ValidationRuleBuilder customBuilder(String ruleBuilderClassName)
101             throws HL7Exception {
102         Class<? extends ValidationRuleBuilder> c;
103         try {
104             c = (Class<? extends ValidationRuleBuilder>) Class.forName(ruleBuilderClassName);
105         } catch (ClassNotFoundException e) {
106             throw new HL7Exception(e);
107         }
108         return ReflectionUtil.instantiate(c);
109     }
110 
111     public static ValidationContext fromBuilder(String ruleBuilderClassName) throws HL7Exception {
112         return new ValidationContextImpl(customBuilder(ruleBuilderClassName));
113     }
114 
115     public static ValidationContext fromBuilder(ValidationRuleBuilder builder) {
116         return new ValidationContextImpl(builder);
117     }
118 
119     /**
120      * @param contextClassName class name of a {@link ValidationContext}
121      *            subclass
122      * @return instance of the ValidationContext
123      * @throws HL7Exception if context cannot be obtained
124      */
125     @SuppressWarnings("unchecked")
126     public static ValidationContext customValidation(String contextClassName) throws HL7Exception {
127         Class<? extends ValidationContext> c;
128         try {
129             c = (Class<? extends ValidationContext>) Class.forName(contextClassName);
130         } catch (Exception e) {
131             throw new HL7Exception(e);
132         }
133         return ReflectionUtil.instantiate(c);
134 
135     }
136 
137 }