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.builder.ValidationRuleBuilder;
032import ca.uhn.hl7v2.validation.builder.support.DefaultValidationBuilder;
033import ca.uhn.hl7v2.validation.builder.support.NoValidationBuilder;
034
035/**
036 * <p>
037 * The <code>ValidationContext</code> returned by <code>getContext()</code> is
038 * determined by the system property "ca.uhn.hl7v2.validation.context_class".
039 * This factory defines two inner classes that can be used: DefaultValidation
040 * and NoValidation. You can also create your own context, setting whatever
041 * rules you want in its constructor, and reference it instead (it must have a
042 * zero-arg constructor). If this property is not set, DefaultValidation is
043 * used.
044 * </p>
045 * 
046 * <p>
047 * Also note that the contexts provided here use
048 * <code>ValidationContextImpl</code>, so rule bindings can be added or removed
049 * programmatically from the starting set.
050 * </p>
051 * 
052 * @author Bryan Tripp
053 * @author Christian Ohr
054 * 
055 */
056public class ValidationContextFactory {
057
058    private static ValidationContext ourContext;
059
060    public static final String CONTEXT_PROPERTY = "ca.uhn.hl7v2.validation.context_class";
061
062    /**
063     * Returns a singleton <code>ValidationContext</code>, creating it if
064     * necessary.
065     * 
066     * @return <code>ValidationContext</code>
067     */
068    public synchronized static ValidationContext getContext() throws HL7Exception {
069        if (ourContext == null) {
070            String contextClassName = System.getProperty(CONTEXT_PROPERTY);
071            ourContext = contextClassName == null ? defaultValidation()
072                    : customValidation(contextClassName);
073        }
074        return ourContext;
075    }
076
077    /**
078     * @return an instance of a non-validating context
079     */
080    @SuppressWarnings("unchecked")
081        public static ValidationContext noValidation() {
082        return new ValidationContextImpl(new NoValidationBuilder());
083    }
084
085    /**
086     * @return an instance of a default validation context
087     */
088    @SuppressWarnings("unchecked")
089        public static ValidationContext defaultValidation() {
090        return new ValidationContextImpl(new DefaultValidationBuilder());
091    }
092
093    /**
094     * @param ruleBuilderClassName class name of a {@link ValidationRuleBuilder}
095     *            subclass
096     * @return a validation rule builder instance
097     * @throws HL7Exception if builder cannot be built
098     */
099    @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}