001package ca.uhn.fhir.validation.schematron;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import java.lang.reflect.Constructor;
024
025import ca.uhn.fhir.context.FhirContext;
026import ca.uhn.fhir.util.CoverageIgnore;
027import ca.uhn.fhir.validation.FhirValidator;
028import ca.uhn.fhir.validation.IValidatorModule;
029
030public class SchematronProvider {
031
032
033        private static final String I18N_KEY_NO_PH_WARNING = FhirValidator.class.getName() + ".noPhWarningOnStartup";
034        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirValidator.class);
035        
036        @CoverageIgnore
037        public static boolean isSchematronAvailable(FhirContext theFhirContext) {
038                try {
039                        Class.forName("com.helger.schematron.ISchematronResource");
040                        return true;
041                } catch (ClassNotFoundException e) {
042                        ourLog.info(theFhirContext.getLocalizer().getMessage(I18N_KEY_NO_PH_WARNING));
043                        return false;
044                }
045        }
046        
047        @SuppressWarnings("unchecked")
048        @CoverageIgnore
049        public static Class<? extends IValidatorModule> getSchematronValidatorClass() {
050                try {
051                        return (Class<? extends IValidatorModule>) Class.forName("ca.uhn.fhir.validation.schematron.SchematronBaseValidator");
052                } catch (ClassNotFoundException e) {
053                        throw new IllegalStateException("Cannot resolve schematron validator ", e);
054                }
055        }
056        
057        @CoverageIgnore
058        public static IValidatorModule getSchematronValidatorInstance(FhirContext myContext) {
059                try {
060                        Class<? extends IValidatorModule> cls = getSchematronValidatorClass();
061                        Constructor<? extends IValidatorModule> constructor = cls.getConstructor(FhirContext.class);
062                        return constructor.newInstance(myContext);
063                } catch (Exception e) {
064                        throw new IllegalStateException("Cannot construct schematron validator ", e);
065                }
066        }
067}