001package org.hl7.fhir.utilities; 002 003import java.util.ArrayList; 004import java.util.List; 005 006import org.hl7.fhir.exceptions.FHIRException; 007import org.hl7.fhir.utilities.cache.NpmPackage; 008 009/* 010 Copyright (c) 2011+, HL7, Inc. 011 All rights reserved. 012 013 Redistribution and use in source and binary forms, with or without modification, 014 are permitted provided that the following conditions are met: 015 016 * Redistributions of source code must retain the above copyright notice, this 017 list of conditions and the following disclaimer. 018 * Redistributions in binary form must reproduce the above copyright notice, 019 this list of conditions and the following disclaimer in the documentation 020 and/or other materials provided with the distribution. 021 * Neither the name of HL7 nor the names of its contributors may be used to 022 endorse or promote products derived from this software without specific 023 prior written permission. 024 025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 026 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 027 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 028 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 029 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 030 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 031 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 032 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 033 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 034 POSSIBILITY OF SUCH DAMAGE. 035 036 */ 037 038 039public class VersionUtilities { 040 041 042 public static class VersionURLInfo { 043 private String version; 044 private String url; 045 public VersionURLInfo(String version, String url) { 046 super(); 047 this.version = version; 048 this.url = url; 049 } 050 public String getVersion() { 051 return version; 052 } 053 public String getUrl() { 054 return url; 055 } 056 } 057 058 public static final String CURRENT_VERSION = "4.4"; 059 public static final String CURRENT_FULL_VERSION = "4.4.0"; 060 061 public static String packageForVersion(String v) { 062 if (isR2Ver(v)) { 063 return "hl7.fhir.r2.core"; 064 } 065 if (isR2BVer(v)) { 066 return "hl7.fhir.r2b.core"; 067 } 068 if (isR3Ver(v)) { 069 return "hl7.fhir.r3.core"; 070 } 071 if (isR4Ver(v)) { 072 return "hl7.fhir.r4.core"; 073 } 074 if ("current".equals(v)) { 075 return "hl7.fhir.r5.core"; 076 } 077 if (v != null && v.startsWith(CURRENT_VERSION)) { 078 return "hl7.fhir.r5.core"; 079 } 080 return null; 081 } 082 083 public static String getCurrentVersion(String v) { 084 if (isR2Ver(v)) { 085 return "1.0.2"; 086 } 087 if (isR2BVer(v)) { 088 return "1.4.0"; 089 } 090 if (isR3Ver(v)) { 091 return "3.0.2"; 092 } 093 if (isR4Ver(v)) { 094 return "4.0.1"; 095 } 096 if (v != null && v.startsWith(CURRENT_VERSION)) { 097 return "current"; 098 } 099 return v; 100 } 101 102 public static String getCurrentPackageVersion(String v) { 103 if (isR2Ver(v)) { 104 return "1.0"; 105 } 106 if (isR2BVer(v)) { 107 return "1.4"; 108 } 109 if (isR3Ver(v)) { 110 return "3.0"; 111 } 112 if (isR4Ver(v)) { 113 return "4.0"; 114 } 115 if (v != null && v.startsWith(CURRENT_VERSION)) { 116 return "current"; 117 } 118 return v; 119 } 120 121 public static boolean isSupportedVersion(String version) { 122 return Utilities.existsInList(version, "1.0.2", "1.4.0", "3.0.2", "4.0.1", CURRENT_FULL_VERSION); 123 } 124 125 public static String listSupportedVersions() { 126 return "1.0.2, 1.4.0, 3.0.2, 4.0.1, "+CURRENT_FULL_VERSION; 127 } 128 129 public static boolean isR5Ver(String ver) { 130 return ver != null && ver.startsWith(CURRENT_VERSION); 131 } 132 133 public static boolean isR4Ver(String ver) { 134 return ver != null && ver.startsWith("4.0"); 135 } 136 137 public static boolean isR3Ver(String ver) { 138 return ver != null && ver.startsWith("3.0"); 139 } 140 141 public static boolean isR2BVer(String ver) { 142 return ver != null && ver.startsWith("1.4"); 143 } 144 145 public static boolean isR2Ver(String ver) { 146 return ver != null && ver.startsWith("1.0"); 147 } 148 149 public static boolean versionsCompatible(String v1, String v2) { 150 String mm1 = getMajMin(v1); 151 String mm2 = getMajMin(v2); 152 if (mm1 == null || mm2 == null) { 153 return false; 154 } else { 155 return mm1.equals(mm2); 156 } 157 } 158 159 public static boolean isCorePackage(String s) { 160 if (s.contains("#")) { 161 s = s.substring(0, s.indexOf("#")); 162 } 163 return Utilities.existsInList(s, "hl7.fhir.core","hl7.fhir.r2.core", "hl7.fhir.r2b.core", "hl7.fhir.r3.core", "hl7.fhir.r4.core"); 164 } 165 166 public static String getMajMin(String version) { 167 if (version == null) 168 return null; 169 170 if (Utilities.charCount(version, '.') == 1) { 171 String[] p = version.split("\\."); 172 return p[0]+"."+p[1]; 173 } else if (Utilities.charCount(version, '.') == 2) { 174 String[] p = version.split("\\."); 175 return p[0]+"."+p[1]; 176 } else { 177 return null; 178 } 179 } 180 181 public static boolean isSemVer(String version) { 182 if (Utilities.charCount(version, '.') != 2) { 183 return false; 184 } 185 String[] p = version.split("\\."); 186 return Utilities.isInteger(p[0]) && Utilities.isInteger(p[1]) && Utilities.isInteger(p[2]); 187 } 188 189 /** 190 * return true if the current vresion equals test, or later 191 * 192 * so if a feature is defined in 4.0, if (VersionUtilities.isThisOrLater("4.0", version))... 193 * 194 * @param test 195 * @param current 196 * @return 197 */ 198 public static boolean isThisOrLater(String test, String current) { 199 String t = getMajMin(test); 200 String c = getMajMin(current); 201 boolean ok = c.compareTo(t) >= 0; 202 return ok; 203 } 204 205 public static String incMajorVersion(String v) { 206 assert isSemVer(v); 207 int[] parts = splitParts(v); 208 return Integer.toString(parts[0]+1)+".0.0"; 209 } 210 211 public static String incMinorVersion(String v) { 212 assert isSemVer(v); 213 int[] parts = splitParts(v); 214 return Integer.toString(parts[0])+"."+Integer.toString(parts[1]+1)+".0"; 215 } 216 217 public static String incPatchVersion(String v) { 218 assert isSemVer(v); 219 int[] parts = splitParts(v); 220 return Integer.toString(parts[0])+"."+Integer.toString(parts[1])+"."+Integer.toString(parts[2]+1); 221 } 222 223 private static int[] splitParts(String v) { 224 String[] p = v.split("\\."); 225 int[] i = new int[] {Integer.parseInt(p[0]),Integer.parseInt(p[1]),Integer.parseInt(p[2])}; 226 return i; 227 } 228 229 public static String versionFromCode(String version) { 230 if ("r2".equals(version)) { 231 return "1.0.2"; 232 } 233 if ("r2b".equals(version)) { 234 return "1.4.0"; 235 } 236 if ("r3".equals(version)) { 237 return "3.0.2"; 238 } 239 if ("r4".equals(version)) { 240 return "4.0.1"; 241 } 242 if ("r5".equals(version)) { 243 return CURRENT_FULL_VERSION; 244 } 245 throw new FHIRException("Unknown version "+version); 246 } 247 248 public static VersionURLInfo parseVersionUrl(String url) { 249 if (url.length() < 24) { 250 return null; 251 } 252 String v = url.substring(20, 24); 253 if (v.endsWith("/")) { 254 v = v.substring(0, v.length()-1); 255 if (Utilities.existsInList(v, "1.0", "1.4", "3.0", "4.0", "5.0", CURRENT_VERSION)) { 256 return new VersionURLInfo(v, "http://hl7.org/fhir/"+url.substring(24)); 257 } 258 } 259 return null; 260 } 261 262 public static List<String> getCanonicalResourceNames(String version) { 263 ArrayList<String> res = new ArrayList<String>(); 264 if (isR2Ver(version) || isR2BVer(version)) { 265 res.add("ValueSet"); 266 res.add("ConceptMap"); 267 res.add("NamingSystem"); 268 res.add("StructureDefinition"); 269 res.add("DataElement"); 270 res.add("Conformance"); 271 res.add("OperationDefinition"); 272 res.add("SearchParameter"); 273 res.add("ImplementationGuide"); 274 res.add("TestScript"); 275 } 276 if (isR3Ver(version)) { 277 res.add("CapabilityStatement"); 278 res.add("StructureDefinition"); 279 res.add("ImplementationGuide"); 280 res.add("SearchParameter"); 281 res.add("MessageDefinition"); 282 res.add("OperationDefinition"); 283 res.add("CompartmentDefinition"); 284 res.add("StructureMap"); 285 res.add("GraphDefinition"); 286 res.add("DataElement"); 287 res.add("CodeSystem"); 288 res.add("ValueSet"); 289 res.add("ConceptMap"); 290 res.add("ExpansionProfile"); 291 res.add("Questionnaire"); 292 res.add("ActivityDefinition"); 293 res.add("ServiceDefinition"); 294 res.add("PlanDefinition"); 295 res.add("Measure"); 296 res.add("TestScript"); 297 298 } 299 if (isR4Ver(version)) { 300 301 res.add("ActivityDefinition"); 302 res.add("CapabilityStatement"); 303 res.add("ChargeItemDefinition"); 304 res.add("CodeSystem"); 305 res.add("CompartmentDefinition"); 306 res.add("ConceptMap"); 307 res.add("EffectEvidenceSynthesis"); 308 res.add("EventDefinition"); 309 res.add("Evidence"); 310 res.add("EvidenceVariable"); 311 res.add("ExampleScenario"); 312 res.add("GraphDefinition"); 313 res.add("ImplementationGuide"); 314 res.add("Library"); 315 res.add("Measure"); 316 res.add("MessageDefinition"); 317 res.add("NamingSystem"); 318 res.add("OperationDefinition"); 319 res.add("PlanDefinition"); 320 res.add("Questionnaire"); 321 res.add("ResearchDefinition"); 322 res.add("ResearchElementDefinition"); 323 res.add("RiskEvidenceSynthesis"); 324 res.add("SearchParameter"); 325 res.add("StructureDefinition"); 326 res.add("StructureMap"); 327 res.add("TerminologyCapabilities"); 328 res.add("TestScript"); 329 res.add("ValueSet"); 330 } 331 332 if (isR5Ver(version)) { 333 334 res.add("ActivityDefinition"); 335 res.add("CapabilityStatement"); 336 res.add("CapabilityStatement2"); 337 res.add("ChargeItemDefinition"); 338 res.add("Citation"); 339 res.add("CodeSystem"); 340 res.add("CompartmentDefinition"); 341 res.add("ConceptMap"); 342 res.add("ConditionDefinition"); 343 res.add("EventDefinition"); 344 res.add("Evidence"); 345 res.add("EvidenceReport"); 346 res.add("EvidenceVariable"); 347 res.add("ExampleScenario"); 348 res.add("GraphDefinition"); 349 res.add("ImplementationGuide"); 350 res.add("Library"); 351 res.add("Measure"); 352 res.add("MessageDefinition"); 353 res.add("NamingSystem"); 354 res.add("OperationDefinition"); 355 res.add("PlanDefinition"); 356 res.add("Questionnaire"); 357 res.add("SearchParameter"); 358 res.add("StructureDefinition"); 359 res.add("StructureMap"); 360 res.add("TerminologyCapabilities"); 361 res.add("TestScript"); 362 res.add("ValueSet"); 363 364 } 365 return res; 366 } 367 368}