001package ca.uhn.fhir.validation.schematron;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 University Health Network
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.validation.FhirValidator;
027import ca.uhn.fhir.validation.IValidatorModule;
028
029public class SchematronProvider {
030
031
032        private static final String I18N_KEY_NO_PHLOC_WARNING = FhirValidator.class.getName() + ".noPhlocWarningOnStartup";
033        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirValidator.class);
034        
035        public static boolean isScematronAvailable(FhirContext theFhirContext) {
036                try {
037                        Class.forName("com.phloc.schematron.ISchematronResource");
038                        return true;
039                } catch (ClassNotFoundException e) {
040                        ourLog.info(theFhirContext.getLocalizer().getMessage(I18N_KEY_NO_PHLOC_WARNING));
041                        return false;
042                }
043        }
044        
045        public static Class<? extends IValidatorModule> getSchematronValidatorClass() {
046                try {
047                        return (Class<? extends IValidatorModule>) Class.forName("ca.uhn.fhir.validation.schematron.SchematronBaseValidator");
048                } catch (ClassNotFoundException e) {
049                        throw new IllegalStateException("Cannot resolve schematron validator ", e);
050                }
051        }
052        
053        public static IValidatorModule getSchematronValidatorInstance(FhirContext myContext) {
054                try {
055                        Class<? extends IValidatorModule> cls = getSchematronValidatorClass();
056                        Constructor<? extends IValidatorModule> constructor = cls.getConstructor(FhirContext.class);
057                        return constructor.newInstance(myContext);
058                } catch (Exception e) {
059                        throw new IllegalStateException("Cannot construct schematron validator ", e);
060                }
061        }
062}