
001package org.hl7.fhir.r5.utils.xver; 002 003import org.hl7.fhir.exceptions.FHIRException; 004import org.hl7.fhir.r5.context.IWorkerContext; 005import org.hl7.fhir.r5.model.ElementDefinition; 006import org.hl7.fhir.r5.model.StructureDefinition; 007import org.hl7.fhir.utilities.MarkedToMoveToAdjunctPackage; 008import org.hl7.fhir.utilities.VersionUtilities; 009import org.hl7.fhir.utilities.json.model.JsonObject; 010import org.hl7.fhir.utilities.json.parser.JsonParser; 011 012import java.io.IOException; 013import java.util.HashMap; 014import java.util.Map; 015 016@MarkedToMoveToAdjunctPackage 017public class XVerExtensionManagerNew extends XVerExtensionManager { 018 019 public static final String XVER_VERSION_RELEASE = "0.0.1-snapshot-2"; 020 private Map<String, JsonObject> lists = new HashMap<>(); 021 022 public XVerExtensionManagerNew(IWorkerContext context) { 023 super(context); 024 } 025 026 @Override 027 public XVerExtensionStatus status(String url) { 028 if (url.length() < 54) { 029 return XVerExtensionStatus.Invalid; 030 } 031 String v = url.substring(20, 23); 032 String targetVersion = VersionUtilities.getNameForVersion(v).toLowerCase(); 033 if (targetVersion.contains("?")) { 034 return XVerExtensionStatus.BadVersion; 035 } 036 037 String sourceVersion = VersionUtilities.getNameForVersion(context.getVersion()).toLowerCase(); 038 String pid = "hl7.fhir.uv.xver-"+targetVersion+"."+sourceVersion; 039 if (!context.hasPackage(pid, XVER_VERSION_RELEASE)) { 040 try { 041 context.getManager().loadPackage(pid+"#0.0.1-snapshot-2"); 042 } catch (IOException e) { 043 return XVerExtensionStatus.BadVersion; 044 } 045 } 046 StructureDefinition sd = context.fetchResource(StructureDefinition.class, url); 047 if (sd == null) { 048 // well, it's not an approved extension, but why? We're going to look in the old 049 // version stuff to see whether it's a valid element or not - it'll affect the return value. 050 if (isValidPath(v, url)) { 051 return XVerExtensionStatus.NotAllowed; 052 } else { 053 return XVerExtensionStatus.Unknown; 054 } 055 } else { 056 return XVerExtensionStatus.Valid; 057 } 058 } 059 060 private boolean isValidPath(String v, String url) { 061 if (!lists.containsKey(v)) { 062 if (context.hasBinaryKey("xver-paths-" + v + ".json")) { 063 try { 064 lists.put(v, JsonParser.parseObject(context.getBinaryForKey("xver-paths-" + v + ".json"))); 065 } catch (IOException e1) { 066 throw new FHIRException(e1); 067 } 068 069 url = url.replace("%5Bx%5D", "[x]"); 070 String e = url.substring(54); 071 JsonObject root = lists.get(v); 072 JsonObject path = root.getJsonObject(e); 073 if (path == null) { 074 path = root.getJsonObject(e + "[x]"); 075 } 076 if (path != null) { 077 return true; 078 } 079 } 080 } 081 return false; 082 } 083 084 085 @Override 086 public StructureDefinition getDefinition(String url) { 087 return context.fetchResource(StructureDefinition.class, url); 088 } 089} 090