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.utils.FHIRPathEngine; 043import org.hl7.fhir.r5.utils.XVerExtensionManager; 044import org.hl7.fhir.utilities.Utilities; 045import org.hl7.fhir.utilities.i18n.I18nConstants; 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 055 public ProfileValidator(IWorkerContext context, XVerExtensionManager xverManager) { 056 super(context, xverManager); 057 } 058 059 public boolean isCheckAggregation() { 060 return checkAggregation; 061 } 062 063 public boolean isCheckMustSupport() { 064 return checkMustSupport; 065 } 066 067 public void setCheckAggregation(boolean checkAggregation) { 068 this.checkAggregation = checkAggregation; 069 } 070 071 public void setCheckMustSupport(boolean checkMustSupport) { 072 this.checkMustSupport = checkMustSupport; 073 } 074 075 protected boolean rule(List<ValidationMessage> errors, IssueType type, String path, boolean b, String msg) { 076 String rn = path.contains(".") ? path.substring(0, path.indexOf(".")) : path; 077 return super.ruleHtml(errors, type, path, b, msg, "<a href=\""+(rn.toLowerCase())+".html\">"+rn+"</a>: "+Utilities.escapeXml(msg)); 078 } 079 080 public List<ValidationMessage> validate(StructureDefinition profile, boolean forBuild) { 081 List<ValidationMessage> errors = new ArrayList<ValidationMessage>(); 082 083 // must have a FHIR version- GF#3160 084 warning(errors, IssueType.BUSINESSRULE, profile.getUrl(), profile.hasFhirVersion(), "Profiles SHOULD state the FHIR Version on which they are based"); 085 warning(errors, IssueType.BUSINESSRULE, profile.getUrl(), profile.hasVersion(), "Profiles SHOULD state their own version"); 086 087 // extensions must be defined 088 for (ElementDefinition ec : profile.getDifferential().getElement()) 089 checkExtensions(profile, errors, "differential", ec); 090 rule(errors, IssueType.STRUCTURE, profile.getId(), profile.hasSnapshot(), "missing Snapshot at "+profile.getName()+"."+profile.getName()); 091 for (ElementDefinition ec : profile.getSnapshot().getElement()) 092 checkExtensions(profile, errors, "snapshot", ec); 093 094 if (rule(errors, IssueType.STRUCTURE, profile.getId(), profile.hasSnapshot(), "A snapshot is required")) { 095 Hashtable<String, ElementDefinition> snapshotElements = new Hashtable<String, ElementDefinition>(); 096 for (ElementDefinition ed : profile.getSnapshot().getElement()) { 097 snapshotElements.put(ed.getId(), ed); 098 checkExtensions(profile, errors, "snapshot", ed); 099 for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) { 100 if (forBuild) { 101 if (!inExemptList(inv.getKey())) { 102 if (rule(errors, IssueType.BUSINESSRULE, profile.getId()+"::"+ed.getPath()+"::"+inv.getKey(), inv.hasExpression(), "The invariant has no FHIR Path expression ("+inv.getXpath()+")")) { 103 try { 104 new FHIRPathEngine(context).check(null, profile.getType(), ed.getPath(), inv.getExpression()); // , inv.hasXpath() && inv.getXpath().startsWith("@value") 105 } catch (Exception e) { 106// rule(errors, IssueType.STRUCTURE, profile.getId()+"::"+ed.getPath()+"::"+inv.getId(), exprExt != null, e.getMessage()); 107 } 108 } 109 } 110 } 111 } 112 } 113 if (snapshotElements != null) { 114 for (ElementDefinition diffElement : profile.getDifferential().getElement()) { 115 if (diffElement == null) 116 throw new Error("Diff Element is null - this is not an expected thing"); 117 ElementDefinition snapElement = snapshotElements.get(diffElement.getId()); 118 if (snapElement!=null) { // Happens with profiles in the main build - should be able to fix once snapshot generation is fixed - Lloyd 119 warning(errors, IssueType.BUSINESSRULE, diffElement.getId(), !checkMustSupport || snapElement.hasMustSupport(), "Elements included in the differential should declare mustSupport"); 120 if (checkAggregation) { 121 for (TypeRefComponent type : snapElement.getType()) { 122 if ("http://hl7.org/fhir/Reference".equals(type.getWorkingCode()) || "http://hl7.org/fhir/canonical".equals(type.getWorkingCode())) { 123 warning(errors, IssueType.BUSINESSRULE, diffElement.getId(), type.hasAggregation(), "Elements with type Reference or canonical should declare aggregation"); 124 } 125 } 126 } 127 } 128 } 129 } 130 } 131 return errors; 132 } 133 134 // these are special cases 135 private boolean inExemptList(String key) { 136 return key.startsWith("txt-"); 137 } 138 139 private void checkExtensions(StructureDefinition profile, List<ValidationMessage> errors, String kind, ElementDefinition ec) { 140 if (!ec.getType().isEmpty() && "Extension".equals(ec.getType().get(0).getWorkingCode()) && ec.getType().get(0).hasProfile()) { 141 String url = ec.getType().get(0).getProfile().get(0).getValue(); 142 StructureDefinition defn = context.fetchResource(StructureDefinition.class, url); 143 if (defn == null) { 144 defn = getXverExt(profile, errors, url); 145 } 146 rule(errors, IssueType.BUSINESSRULE, profile.getId(), defn != null, "Unable to find Extension '"+url+"' referenced at "+profile.getUrl()+" "+kind+" "+ec.getPath()+" ("+ec.getSliceName()+")"); 147 } 148 } 149 150 151}