001package ca.uhn.fhir.validation.schematron;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2022 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 ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.i18n.Msg;
025import ca.uhn.fhir.util.CoverageIgnore;
026import ca.uhn.fhir.validation.FhirValidator;
027import ca.uhn.fhir.validation.IValidatorModule;
028
029import java.lang.reflect.Constructor;
030
031public class SchematronProvider {
032
033
034        private static final String I18N_KEY_NO_PH_WARNING = FhirValidator.class.getName() + ".noPhWarningOnStartup";
035        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirValidator.class);
036        
037        @CoverageIgnore
038        public static boolean isSchematronAvailable(FhirContext theFhirContext) {
039                try {
040                        Class.forName("com.helger.schematron.ISchematronResource");
041                        return true;
042                } catch (ClassNotFoundException e) {
043                        ourLog.info(theFhirContext.getLocalizer().getMessage(I18N_KEY_NO_PH_WARNING));
044                        return false;
045                }
046        }
047        
048        @SuppressWarnings("unchecked")
049        @CoverageIgnore
050        public static Class<? extends IValidatorModule> getSchematronValidatorClass() {
051                try {
052                        return (Class<? extends IValidatorModule>) Class.forName("ca.uhn.fhir.validation.schematron.SchematronBaseValidator");
053                } catch (ClassNotFoundException e) {
054                        throw new IllegalStateException(Msg.code(1973) + "Cannot resolve schematron validator ", e);
055                }
056        }
057        
058        @CoverageIgnore
059        public static IValidatorModule getSchematronValidatorInstance(FhirContext myContext) {
060                try {
061                        Class<? extends IValidatorModule> cls = getSchematronValidatorClass();
062                        Constructor<? extends IValidatorModule> constructor = cls.getConstructor(FhirContext.class);
063                        return constructor.newInstance(myContext);
064                } catch (Exception e) {
065                        throw new IllegalStateException(Msg.code(1974) + "Cannot construct schematron validator ", e);
066                }
067        }
068}