001package org.hl7.fhir.validation.profile; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033import java.util.ArrayList; 034import java.util.Hashtable; 035import java.util.List; 036 037import org.hl7.fhir.r5.context.IWorkerContext; 038import org.hl7.fhir.r5.model.ElementDefinition; 039import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionConstraintComponent; 040import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent; 041import org.hl7.fhir.r5.model.StructureDefinition; 042import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind; 043import org.hl7.fhir.r5.utils.FHIRPathEngine; 044import org.hl7.fhir.r5.utils.XVerExtensionManager; 045import org.hl7.fhir.utilities.Utilities; 046import org.hl7.fhir.utilities.validation.ValidationMessage; 047import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType; 048import org.hl7.fhir.validation.BaseValidator; 049 050public class ProfileValidator extends BaseValidator { 051 052 private boolean checkAggregation = false; 053 private boolean checkMustSupport = false; 054 private FHIRPathEngine fpe; 055 056 public ProfileValidator(IWorkerContext context, XVerExtensionManager xverManager) { 057 super(context, xverManager); 058 fpe = new FHIRPathEngine(context); 059 } 060 061 public boolean isCheckAggregation() { 062 return checkAggregation; 063 } 064 065 public boolean isCheckMustSupport() { 066 return checkMustSupport; 067 } 068 069 public void setCheckAggregation(boolean checkAggregation) { 070 this.checkAggregation = checkAggregation; 071 } 072 073 public void setCheckMustSupport(boolean checkMustSupport) { 074 this.checkMustSupport = checkMustSupport; 075 } 076 077 protected boolean rule(List<ValidationMessage> errors, IssueType type, String path, boolean b, String msg) { 078 String rn = path.contains(".") ? path.substring(0, path.indexOf(".")) : path; 079 return super.ruleHtml(errors, NO_RULE_DATE, type, path, b, msg, "<a href=\""+(rn.toLowerCase())+".html\">"+rn+"</a>: "+Utilities.escapeXml(msg)); 080 } 081 082 public List<ValidationMessage> validate(StructureDefinition profile, boolean forBuild) { 083 List<ValidationMessage> errors = new ArrayList<ValidationMessage>(); 084 085 // must have a FHIR version- GF#3160 086 String s = (profile.getKind() == StructureDefinitionKind.LOGICAL) ? "Logical Models" : "Profiles"; 087 warning(errors, NO_RULE_DATE, IssueType.BUSINESSRULE, profile.getUrl(), profile.hasFhirVersion(), s+" SHOULD state the FHIR Version on which they are based"); 088 warning(errors, NO_RULE_DATE, IssueType.BUSINESSRULE, profile.getUrl(), profile.hasVersion(), s+" SHOULD state their own version"); 089 090 // extensions must be defined 091 for (ElementDefinition ec : profile.getDifferential().getElement()) 092 checkExtensions(profile, errors, "differential", ec); 093 rule(errors, NO_RULE_DATE, IssueType.STRUCTURE, profile.getId(), profile.hasSnapshot(), "missing Snapshot at "+profile.getName()+"."+profile.getName()); 094 for (ElementDefinition ec : profile.getSnapshot().getElement()) 095 checkExtensions(profile, errors, "snapshot", ec); 096 097 if (rule(errors, NO_RULE_DATE, IssueType.STRUCTURE, profile.getId(), profile.hasSnapshot(), "A snapshot is required")) { 098 Hashtable<String, ElementDefinition> snapshotElements = new Hashtable<String, ElementDefinition>(); 099 for (ElementDefinition ed : profile.getSnapshot().getElement()) { 100 snapshotElements.put(ed.getId(), ed); 101 for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) { 102 if (forBuild) { 103 if (!inExemptList(inv.getKey())) { 104// if (rule(errors, NO_RULE_DATE, IssueType.BUSINESSRULE, profile.getId()+"::"+ed.getPath()+"::"+inv.getKey(), inv.hasExpression(), "The invariant has no FHIR Path expression ("+inv.getXpath()+")")) { 105// try { 106// fpe.check(null, profile.getType(), ed.getPath(), inv.getExpression()); // , inv.hasXpath() && inv.getXpath().startsWith("@value") 107// } catch (Exception e) { 108// // rule(errors, UNKNOWN_DATE_TIME, IssueType.STRUCTURE, profile.getId()+"::"+ed.getPath()+"::"+inv.getId(), false, e.getMessage()); 109// } 110// } 111 } 112 } 113 } 114 } 115 if (snapshotElements != null) { 116 for (ElementDefinition diffElement : profile.getDifferential().getElement()) { 117 if (diffElement == null) 118 throw new Error("Diff Element is null - this is not an expected thing"); 119 ElementDefinition snapElement = snapshotElements.get(diffElement.getId()); 120 if (snapElement!=null) { // Happens with profiles in the main build - should be able to fix once snapshot generation is fixed - Lloyd 121 warning(errors, NO_RULE_DATE, IssueType.BUSINESSRULE, diffElement.getId(), !checkMustSupport || snapElement.hasMustSupport(), "Elements included in the differential should declare mustSupport"); 122 if (checkAggregation) { 123 for (TypeRefComponent type : snapElement.getType()) { 124 if ("http://hl7.org/fhir/Reference".equals(type.getWorkingCode()) || "http://hl7.org/fhir/canonical".equals(type.getWorkingCode())) { 125 warning(errors, NO_RULE_DATE, IssueType.BUSINESSRULE, diffElement.getId(), type.hasAggregation(), "Elements with type Reference or canonical should declare aggregation"); 126 } 127 } 128 } 129 } 130 } 131 } 132 } 133 return errors; 134 } 135 136 // these are special cases 137 private boolean inExemptList(String key) { 138 return key.startsWith("txt-"); 139 } 140 141 private void checkExtensions(StructureDefinition profile, List<ValidationMessage> errors, String kind, ElementDefinition ec) { 142 if (!ec.getType().isEmpty() && "Extension".equals(ec.getType().get(0).getWorkingCode()) && ec.getType().get(0).hasProfile()) { 143 String url = ec.getType().get(0).getProfile().get(0).getValue(); 144 StructureDefinition defn = context.fetchResource(StructureDefinition.class, url); 145 if (defn == null) { 146 defn = getXverExt(profile, errors, url); 147 } 148 rule(errors, NO_RULE_DATE, IssueType.BUSINESSRULE, profile.getId(), defn != null, "Unable to find Extension '"+url+"' referenced at "+profile.getUrl()+" "+kind+" "+ec.getPath()+" ("+ec.getSliceName()+")"); 149 } 150 } 151 152 153}