View Javadoc
1   package ca.uhn.fhir.validation.schematron;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.lang.reflect.Constructor;
24  
25  import ca.uhn.fhir.context.FhirContext;
26  import ca.uhn.fhir.util.CoverageIgnore;
27  import ca.uhn.fhir.validation.FhirValidator;
28  import ca.uhn.fhir.validation.IValidatorModule;
29  
30  public class SchematronProvider {
31  
32  
33  	private static final String I18N_KEY_NO_PH_WARNING = FhirValidator.class.getName() + ".noPhWarningOnStartup";
34  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirValidator.class);
35  	
36  	@CoverageIgnore
37  	public static boolean isSchematronAvailable(FhirContext theFhirContext) {
38  		try {
39  			Class.forName("com.helger.schematron.ISchematronResource");
40  			return true;
41  		} catch (ClassNotFoundException e) {
42  			ourLog.info(theFhirContext.getLocalizer().getMessage(I18N_KEY_NO_PH_WARNING));
43  			return false;
44  		}
45  	}
46  	
47  	@SuppressWarnings("unchecked")
48  	@CoverageIgnore
49  	public static Class<? extends IValidatorModule> getSchematronValidatorClass() {
50  		try {
51  			return (Class<? extends IValidatorModule>) Class.forName("ca.uhn.fhir.validation.schematron.SchematronBaseValidator");
52  		} catch (ClassNotFoundException e) {
53  			throw new IllegalStateException("Cannot resolve schematron validator ", e);
54  		}
55  	}
56  	
57  	@CoverageIgnore
58  	public static IValidatorModule getSchematronValidatorInstance(FhirContext myContext) {
59  		try {
60  			Class<? extends IValidatorModule> cls = getSchematronValidatorClass();
61  			Constructor<? extends IValidatorModule> constructor = cls.getConstructor(FhirContext.class);
62  			return constructor.newInstance(myContext);
63  		} catch (Exception e) {
64  			throw new IllegalStateException("Cannot construct schematron validator ", e);
65  		}
66  	}
67  }