performance related changes

This commit is contained in:
Grahame Grieve 2023-05-07 11:04:04 -05:00
parent e9d10baba2
commit ae13abfc2a
16 changed files with 110 additions and 77 deletions

View File

@ -98,7 +98,7 @@ public class JavaExtensionsFactoryGenerator extends JavaBaseGenerator {
ElementDefinition edRoot = sd.getSnapshot().getElementFirstRep();
boolean repeats = !edRoot.getMax().equals("1");
String verb = repeats ? "add" : "set";
ElementDefinition edValue = sd.getSnapshot().getElementByPath("Extension.value[x]");
ElementDefinition edValue = sd.getSnapshot().getElementByPath("Extension.value[x]", false);
List<TypeTuple> types = analyseTypes(edValue);
if (types.size() > 5) {
src.append(" public static Extension make"+name+"(DataType value) {\r\n");

View File

@ -338,7 +338,7 @@ public class DataRenderer extends Renderer {
StructureDefinition sd = getContext().getWorker().fetchTypeDefinition(t);
if (sd == null)
return false;
if (Utilities.existsInList(t, VersionUtilities.getCanonicalResourceNames(getContext().getWorker().getVersion()))) {
if (VersionUtilities.getCanonicalResourceNames(getContext().getWorker().getVersion()).contains(t)) {
return true;
}
if (Utilities.existsInList(t,

View File

@ -3969,12 +3969,12 @@ public class ProfileUtilities extends TranslatingUtilities {
if (!isExtensionDefinition(sd)) {
return false;
}
ElementDefinition value = sd.getSnapshot().getElementByPath("Extension.value");
ElementDefinition value = sd.getSnapshot().getElementByPath("Extension.value", true);
return value != null && !value.isProhibited();
}
public static boolean isModifierExtension(StructureDefinition sd) {
ElementDefinition defn = sd.getSnapshot().getElementByPath("Extension");
ElementDefinition defn = sd.getSnapshot().getElementByPath("Extension", true);
return defn.getIsModifier();
}

View File

@ -1457,7 +1457,7 @@ public class Element extends Base {
if (property.getStructure().hasExtension(ToolingExtensions.EXT_RESOURCE_IMPLEMENTS)) {
StructureDefinition sd = property.getContext().fetchResource(StructureDefinition.class, ExtensionsUtils.getExtensionString(property.getStructure(), ToolingExtensions.EXT_RESOURCE_IMPLEMENTS));
if (sd != null) {
ElementDefinition ed = sd.getSnapshot().getElementByPath(property.getDefinition().getPath().replace(property.getStructure().getType(), sd.getType()));
ElementDefinition ed = sd.getSnapshot().getElementByPath(property.getDefinition().getPath().replace(property.getStructure().getType(), sd.getType()), true);
if (ed != null) {
return ed.getBase().getPath();
}

View File

@ -1233,12 +1233,15 @@ public class StructureDefinition extends CanonicalResource {
// added from java-adornments.txt:
public ElementDefinition getElementByPath(String path) {
public ElementDefinition getElementByPath(String path, boolean autoChoice) {
if (autoChoice && path.endsWith("[x]")) {
path = path.substring(0, path.length()-3);
}
if (path == null) {
return null;
}
for (ElementDefinition ed : getElement()) {
if (path.equals(ed.getPath()) || (path+"[x]").equals(ed.getPath())) {
if (path.equals(ed.getPath()) || (autoChoice && (path+"[x]").equals(ed.getPath()))) {
return ed;
}
}

View File

@ -129,7 +129,7 @@ public abstract class PEDefinition {
type= type.substring(0, type.indexOf("."));
}
StructureDefinition sd = builder.getContext().fetchTypeDefinition(type);
return sd.getSnapshot().getElementByPath(definition.getBase().getPath());
return sd.getSnapshot().getElementByPath(definition.getBase().getPath(), true);
}
/**

View File

@ -21,8 +21,8 @@ public class PEDefinitionExtension extends PEDefinition {
super(builder, name, profile, definition, ppath);
this.sliceDefinition = sliceDefinition;
this.extension= extension;
eed = extension.getSnapshot().getElementByPath("Extension.extension");
ved = extension.getSnapshot().getElementByPath("Extension.value[x]");
eed = extension.getSnapshot().getElementByPath("Extension.extension", true);
ved = extension.getSnapshot().getElementByPath("Extension.value[x]", true);
}
@Override

View File

@ -375,7 +375,7 @@ public class DataRenderer extends Renderer implements CodeResolver {
StructureDefinition sd = getContext().getWorker().fetchTypeDefinition(t);
if (sd == null)
return false;
if (Utilities.existsInList(t, VersionUtilities.getCanonicalResourceNames(getContext().getWorker().getVersion()))) {
if (VersionUtilities.getCanonicalResourceNames(getContext().getWorker().getVersion()).contains(t)) {
return true;
}
if (Utilities.existsInList(t,

View File

@ -32,6 +32,7 @@ package org.hl7.fhir.r5.terminologies.validation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
@ -471,10 +472,12 @@ public class ValueSetValidator {
return res;
}
private static final HashSet<String> SERVER_SIDE_LIST = new HashSet<>(Arrays.asList("http://fdasis.nlm.nih.gov", "http://hl7.org/fhir/sid/ndc", "http://loinc.org", "http://snomed.info/sct", "http://unitsofmeasure.org",
"http://unstats.un.org/unsd/methods/m49/m49.htm", "http://varnomen.hgvs.org", "http://www.nlm.nih.gov/research/umls/rxnorm", "https://www.usps.com/",
"urn:ietf:bcp:13","urn:ietf:bcp:47","urn:ietf:rfc:3986", "urn:iso:std:iso:3166","urn:iso:std:iso:4217", "urn:oid:1.2.36.1.2001.1005.17"));
private boolean preferServerSide(String system) {
if (Utilities.existsInList(system, "http://fdasis.nlm.nih.gov", "http://hl7.org/fhir/sid/ndc", "http://loinc.org", "http://snomed.info/sct", "http://unitsofmeasure.org",
"http://unstats.un.org/unsd/methods/m49/m49.htm", "http://varnomen.hgvs.org", "http://www.nlm.nih.gov/research/umls/rxnorm", "https://www.usps.com/",
"urn:ietf:bcp:13","urn:ietf:bcp:47","urn:ietf:rfc:3986", "urn:iso:std:iso:3166","urn:iso:std:iso:4217", "urn:oid:1.2.36.1.2001.1005.17")) {
if (SERVER_SIDE_LIST.contains(system)) {
return true;
}

View File

@ -3,7 +3,9 @@ package org.hl7.fhir.r5.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class BuildExtensions extends ToolingExtensions {
@ -51,19 +53,24 @@ public class BuildExtensions extends ToolingExtensions {
public static final String EXT_BINDING_NAME = "http://hl7.org/fhir/StructureDefinition/elementdefinition-bindingName";
public static List<String> allConsts() {
List<String> list = new ArrayList<>();
for (Field field : BuildExtensions.class.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
try {
list.add(field.get(field.getType()).toString());
} catch (Exception e) {
private static Set<String> cachedConsts;
public static Set<String> allConsts() {
if (cachedConsts == null) {
Set<String> list = new HashSet<>();
for (Field field : BuildExtensions.class.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
try {
list.add(field.get(field.getType()).toString());
} catch (Exception e) {
}
}
}
list.addAll(ToolingExtensions.allConsts());
cachedConsts = list;
}
list.addAll(ToolingExtensions.allConsts());
return list;
return cachedConsts;
}

View File

@ -68,6 +68,8 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import org.apache.commons.lang3.StringUtils;
import org.fhir.ucum.Utilities;
@ -236,8 +238,6 @@ public class ToolingExtensions {
public static final String EXT_MAPPING_TGTTYPE = "http://hl7.org/fhir/tools/StructureDefinition/conceptmap-target-type";
public static final String EXT_MAPPING_TGTCARD = "http://hl7.org/fhir/tools/StructureDefinition/conceptmap-target-cardinality";
public static final String WEB_EXTENSION_STYLE = "http://build.fhir.org/ig/FHIR/fhir-tools-ig/format-extensions.html#extension-related-extensions";
public static final String EXT_IGDEP_COMMENT = "http://hl7.org/fhir/tools/StructureDefinition/implementationguide-dependency-comment";
public static final String EXT_XPATH_CONSTRAINT = "http://hl7.org/fhir/4.0/StructureDefinition/extension-ElementDefinition.constraint.xpath";
@ -1029,20 +1029,23 @@ public class ToolingExtensions {
return false;
}
public static List<String> allConsts() {
List<String> list = new ArrayList<>();
for (Field field : ToolingExtensions.class.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
try {
list.add(field.get(field.getType()).toString());
} catch (Exception e) {
private static Set<String> cachedConsts;
public static Set<String> allConsts() {
if (cachedConsts == null) {
Set<String> list = new HashSet<>();
for (Field field : ToolingExtensions.class.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
try {
list.add(field.get(field.getType()).toString());
} catch (Exception e) {
}
}
}
cachedConsts = list;
}
return list;
return cachedConsts;
}
public static boolean hasExtensions(ElementDefinition d, String... urls) {

View File

@ -2,6 +2,8 @@ package org.hl7.fhir.utilities;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.exceptions.FHIRException;
@ -393,8 +395,10 @@ public class VersionUtilities {
return null;
}
public static List<String> getCanonicalResourceNames(String version) {
ArrayList<String> res = new ArrayList<String>();
public static Set<String> getCanonicalResourceNames(String version) {
Set<String> res = new HashSet<String>();
if (isR2Ver(version) || isR2BVer(version)) {
res.add("ValueSet");
res.add("ConceptMap");

View File

@ -1031,7 +1031,7 @@ public class BaseValidator implements IValidationContextResourceLoader {
if (!Utilities.isAbsoluteUrl(ref)) {
String[] p = ref.split("\\/");
List<Element> ml = new ArrayList<>();
if (p.length >= 2 && Utilities.existsInList(p[0], context.getResourceNames()) && Utilities.isValidId(p[1])) {
if (p.length >= 2 && context.getResourceNamesAsSet().contains(p[0]) && Utilities.isValidId(p[1])) {
for (int i = 0; i < entries.size(); i++) {
Element we = entries.get(i);
Element r = we.getNamedChild(RESOURCE);

View File

@ -42,6 +42,7 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
@ -260,6 +261,29 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
private static final boolean STACK_TRACE = false;
private static final boolean DEBUG_ELEMENT = false;
private static final HashSet<String> NO_TX_SYSTEM_EXEMPT = new HashSet<>(Arrays.asList("http://loinc.org", "http://unitsofmeasure.org", "http://hl7.org/fhir/sid/icd-9-cm", "http://snomed.info/sct", "http://www.nlm.nih.gov/research/umls/rxnorm"));
private static final HashSet<String> NO_HTTPS_LIST = new HashSet<>(Arrays.asList("https://loinc.org", "https://unitsofmeasure.org", "https://snomed.info/sct", "https://www.nlm.nih.gov/research/umls/rxnorm"));
private static final HashSet<String> EXTENSION_CONTEXT_LIST = new HashSet<>(Arrays.asList("ElementDefinition.example.value", "ElementDefinition.pattern", "ElementDefinition.fixed"));
private static final HashSet<String> ID_EXEMPT_LIST = new HashSet<>(Arrays.asList("id", "base64Binary", "markdown"));
private static final HashSet<String> HTML_ELEMENTS = new HashSet<>(Arrays.asList(
"p", "br", "div", "h1", "h2", "h3", "h4", "h5", "h6", "a", "span", "b", "em", "i", "strong",
"small", "big", "tt", "small", "dfn", "q", "var", "abbr", "acronym", "cite", "blockquote", "hr", "address", "bdo", "kbd", "q", "sub", "sup",
"ul", "ol", "li", "dl", "dt", "dd", "pre", "table", "caption", "colgroup", "col", "thead", "tr", "tfoot", "tbody", "th", "td",
"code", "samp", "img", "map", "area"));
private static final HashSet<String> HTML_ATTRIBUTES = new HashSet<>(Arrays.asList(
"title", "style", "class", "id", "lang", "xml:lang", "dir", "accesskey", "tabindex",
// tables
"span", "width", "align", "valign", "char", "charoff", "abbr", "axis", "headers", "scope", "rowspan", "colspan"));
private static final HashSet<String> HTML_COMBO_LIST = new HashSet<>(Arrays.asList(
"a.href", "a.name", "img.src", "img.border", "div.xmlns", "blockquote.cite", "q.cite",
"a.charset", "a.type", "a.name", "a.href", "a.hreflang", "a.rel", "a.rev", "a.shape", "a.coords", "img.src",
"img.alt", "img.longdesc", "img.height", "img.width", "img.usemap", "img.ismap", "map.name", "area.shape",
"area.coords", "area.href", "area.nohref", "area.alt", "table.summary", "table.width", "table.border",
"table.frame", "table.rules", "table.cellspacing", "table.cellpadding", "pre.space", "td.nowrap"));
private static final HashSet<String> HTML_BLOCK_LIST = new HashSet<>(Arrays.asList("div", "blockquote", "table", "ol", "ul", "p"));
private static final HashSet<String> RESOURCE_X_POINTS = new HashSet<>(Arrays.asList("Bundle.entry.resource", "Bundle.entry.response.outcome", "DomainResource.contained", "Parameters.parameter.resource", "Parameters.parameter.part.resource"));
private class ValidatorHostServices implements IEvaluationContext {
@Override
@ -1024,7 +1048,7 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
}
return false;
}
} else if (context.isNoTerminologyServer() && Utilities.existsInList(system, "http://loinc.org", "http://unitsofmeasure.org", "http://hl7.org/fhir/sid/icd-9-cm", "http://snomed.info/sct", "http://www.nlm.nih.gov/research/umls/rxnorm")) {
} else if (context.isNoTerminologyServer() && NO_TX_SYSTEM_EXEMPT.contains(system)) {
return true; // no checks in this case
} else if (startsWithButIsNot(system, "http://snomed.info/sct", "http://loinc.org", "http://unitsofmeasure.org", "http://www.nlm.nih.gov/research/umls/rxnorm")) {
rule(errors, NO_RULE_DATE, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_SYSTEM_INVALID, system);
@ -1039,7 +1063,7 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
if (system.startsWith("https:") && system.length() > 7) {
String ns = "http:"+system.substring(6);
CodeSystem cs = getCodeSystem(ns);
if (cs != null || Utilities.existsInList(system, "https://loinc.org", "https://unitsofmeasure.org", "https://snomed.info/sct", "https://www.nlm.nih.gov/research/umls/rxnorm")) {
if (cs != null || NO_HTTPS_LIST.contains(system)) {
rule(errors, NO_RULE_DATE, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_SYSTEM_HTTPS, system);
done = true;
}
@ -1868,7 +1892,7 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
}
} else if (SpecialExtensions.isKnownExtension(url)) {
ex = SpecialExtensions.getDefinition(url);
} else if (Utilities.existsInList(url, BuildExtensions.allConsts())) {
} else if (BuildExtensions.allConsts().contains(url)) {
// nothing
} else if (rule(errors, NO_RULE_DATE, IssueType.STRUCTURE, element.line(), element.col(), path, allowUnknownExtension(url), I18nConstants.EXTENSION_EXT_UNKNOWN_NOTHERE, url)) {
hint(errors, NO_RULE_DATE, IssueType.STRUCTURE, element.line(), element.col(), path, isKnownExtension(url), I18nConstants.EXTENSION_EXT_UNKNOWN, url);
@ -1964,7 +1988,7 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
for (String s : stack.getLogicalPaths()) {
String p = stripIndexes(s);
// all extensions are always allowed in ElementDefinition.example.value, and in fixed and pattern values. TODO: determine the logical paths from the path stated in the element definition....
if (Utilities.existsInList(p, "ElementDefinition.example.value", "ElementDefinition.pattern", "ElementDefinition.fixed")) {
if (EXTENSION_CONTEXT_LIST.contains(p)) {
return true;
}
plist.add(p);
@ -2549,9 +2573,8 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
if (context.hasPattern()) {
ok = checkFixedValue(errors, path, e, context.getPattern(), profile.getVersionedUrl(), context.getSliceName(), null, true) && ok;
}
if (ok && !Utilities.existsInList(e.fhirType(), "id", "base64Binary", "markdown")) { // ids get checked elsewhere
if (ok && !ID_EXEMPT_LIST.contains(e.fhirType())) { // ids get checked elsewhere
String regext = FHIRPathExpressionFixer.fixRegex(getRegexFromType(e.fhirType()));
if (regext != null) {
try {
@ -2757,7 +2780,7 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
if (!"url".equals(p[1])) {
return false;
}
return Utilities.existsInList(p[0], VersionUtilities.getCanonicalResourceNames(context.getVersion()));
return VersionUtilities.getCanonicalResourceNames(context.getVersion()).contains((p[0]));
}
private boolean containsHtmlTags(String cnt) {
@ -2852,30 +2875,16 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
rule(errors, NO_RULE_DATE, IssueType.INVALID, e.line(), e.col(), path, !node.getContent().startsWith("DOCTYPE"), I18nConstants.XHTML_XHTML_DOCTYPE_ILLEGAL);
}
if (node.getNodeType() == NodeType.Element) {
rule(errors, NO_RULE_DATE, IssueType.INVALID, e.line(), e.col(), path, Utilities.existsInList(node.getName(),
"p", "br", "div", "h1", "h2", "h3", "h4", "h5", "h6", "a", "span", "b", "em", "i", "strong",
"small", "big", "tt", "small", "dfn", "q", "var", "abbr", "acronym", "cite", "blockquote", "hr", "address", "bdo", "kbd", "q", "sub", "sup",
"ul", "ol", "li", "dl", "dt", "dd", "pre", "table", "caption", "colgroup", "col", "thead", "tr", "tfoot", "tbody", "th", "td",
"code", "samp", "img", "map", "area"), I18nConstants.XHTML_XHTML_ELEMENT_ILLEGAL, node.getName());
rule(errors, NO_RULE_DATE, IssueType.INVALID, e.line(), e.col(), path, HTML_ELEMENTS.contains(node.getName()), I18nConstants.XHTML_XHTML_ELEMENT_ILLEGAL, node.getName());
for (String an : node.getAttributes().keySet()) {
boolean bok = an.startsWith("xmlns") || Utilities.existsInList(an,
"title", "style", "class", ID, "lang", "xml:lang", "dir", "accesskey", "tabindex",
// tables
"span", "width", "align", "valign", "char", "charoff", "abbr", "axis", "headers", "scope", "rowspan", "colspan") ||
Utilities.existsInList(node.getName() + "." + an, "a.href", "a.name", "img.src", "img.border", "div.xmlns", "blockquote.cite", "q.cite",
"a.charset", "a.type", "a.name", "a.href", "a.hreflang", "a.rel", "a.rev", "a.shape", "a.coords", "img.src",
"img.alt", "img.longdesc", "img.height", "img.width", "img.usemap", "img.ismap", "map.name", "area.shape",
"area.coords", "area.href", "area.nohref", "area.alt", "table.summary", "table.width", "table.border",
"table.frame", "table.rules", "table.cellspacing", "table.cellpadding", "pre.space", "td.nowrap"
);
boolean bok = an.startsWith("xmlns") || HTML_ATTRIBUTES.contains(an) || HTML_COMBO_LIST.contains(node.getName() + "." + an);
if (!bok) {
rule(errors, NO_RULE_DATE, IssueType.INVALID, e.line(), e.col(), path, false, I18nConstants.XHTML_XHTML_ATTRIBUTE_ILLEGAL, an, node.getName());
}
}
ok = rule(errors, NO_RULE_DATE, IssueType.INVALID, e.line(), e.col(), path, !(inPara && Utilities.existsInList(node.getName(), "div", "blockquote", "table", "ol", "ul", "p")) , I18nConstants.XHTML_XHTML_ELEMENT_ILLEGAL_IN_PARA, node.getName()) && ok;
ok = rule(errors, NO_RULE_DATE, IssueType.INVALID, e.line(), e.col(), path, !(inPara && HTML_BLOCK_LIST.contains(node.getName())) , I18nConstants.XHTML_XHTML_ELEMENT_ILLEGAL_IN_PARA, node.getName()) && ok;
ok = checkInnerNames(errors, e, path, node.getChildNodes(), inPara || "p".equals(node.getName())) && ok;
}
@ -3291,7 +3300,7 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
refType = "bundled";
}
}
boolean conditional = ref.contains("?") && Utilities.existsInList(ref.substring(0, ref.indexOf("?")), context.getResourceNames());
boolean conditional = ref.contains("?") && context.getResourceNamesAsSet().contains(ref.substring(0, ref.indexOf("?")));
ReferenceValidationPolicy pol;
if (refType.equals("contained") || refType.equals("bundled")) {
pol = ReferenceValidationPolicy.CHECK_VALID;
@ -5685,7 +5694,7 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
}
private boolean isResourceAndTypes(ElementDefinition ed) {
if (!Utilities.existsInList(ed.getBase().getPath(), "Bundle.entry.resource", "Bundle.entry.response.outcome", "DomainResource.contained", "Parameters.parameter.resource", "Parameters.parameter.part.resource")) {
if (!RESOURCE_X_POINTS.contains(ed.getBase().getPath())) {
return false;
}
for (TypeRefComponent tr : ed.getType()) {

View File

@ -1060,7 +1060,7 @@ public class StructureMapValidator extends BaseValidator {
if (sdt == null) {
throw new Error("Unable to resolve "+url);
} else {
ElementDefinition t2 = sdt.getSnapshot().getElementByPath(path);
ElementDefinition t2 = sdt.getSnapshot().getElementByPath(path, true);
if (t2 == null) {
throw new Error("Unable to resolve "+path+" in "+url);
} else {