001package org.hl7.fhir.common.hapi.validation.support;
002
003import ca.uhn.fhir.context.ConfigurationException;
004import ca.uhn.fhir.context.FhirContext;
005import ca.uhn.fhir.context.FhirVersionEnum;
006import ca.uhn.fhir.context.support.ConceptValidationOptions;
007import ca.uhn.fhir.context.support.IValidationSupport;
008import ca.uhn.fhir.context.support.ValidationSupportContext;
009import ca.uhn.fhir.i18n.Msg;
010import ca.uhn.fhir.util.ClasspathUtil;
011import ca.uhn.hapi.converters.canonical.VersionCanonicalizer;
012import com.fasterxml.jackson.core.JsonProcessingException;
013import com.fasterxml.jackson.databind.ObjectMapper;
014import com.fasterxml.jackson.databind.node.ArrayNode;
015import com.fasterxml.jackson.databind.node.ObjectNode;
016import org.apache.commons.lang3.StringUtils;
017import org.apache.commons.lang3.Validate;
018import org.fhir.ucum.UcumEssenceService;
019import org.fhir.ucum.UcumException;
020import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_30_40;
021import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_40_50;
022import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_43_50;
023import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40;
024import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50;
025import org.hl7.fhir.convertors.factory.VersionConvertorFactory_43_50;
026import org.hl7.fhir.dstu2.model.ValueSet;
027import org.hl7.fhir.instance.model.api.IBaseResource;
028import org.hl7.fhir.r4.model.CodeSystem;
029import org.hl7.fhir.r5.model.Resource;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033import javax.annotation.Nonnull;
034import javax.annotation.Nullable;
035import java.io.InputStream;
036import java.util.Collections;
037import java.util.HashMap;
038import java.util.Map;
039import java.util.Optional;
040
041import static org.apache.commons.lang3.StringUtils.defaultString;
042import static org.apache.commons.lang3.StringUtils.isBlank;
043import static org.apache.commons.lang3.StringUtils.isNotBlank;
044
045/**
046 * This {@link IValidationSupport validation support module} can be used to validate codes against common
047 * CodeSystems that are commonly used, but are not distriuted with the FHIR specification for various reasons
048 * (size, complexity, etc.).
049 * <p>
050 * See <a href="https://hapifhir.io/hapi-fhir/docs/validation/validation_support_modules.html#CommonCodeSystemsTerminologyService">CommonCodeSystemsTerminologyService</a> in the HAPI FHIR documentation
051 * for details about what is and isn't covered by this class.
052 * </p>
053 */
054@SuppressWarnings("EnhancedSwitchMigration")
055public class CommonCodeSystemsTerminologyService implements IValidationSupport {
056        public static final String LANGUAGES_VALUESET_URL = "http://hl7.org/fhir/ValueSet/languages";
057        public static final String LANGUAGES_CODESYSTEM_URL = "urn:ietf:bcp:47";
058        public static final String MIMETYPES_VALUESET_URL = "http://hl7.org/fhir/ValueSet/mimetypes";
059        public static final String MIMETYPES_CODESYSTEM_URL = "urn:ietf:bcp:13";
060        public static final String CURRENCIES_CODESYSTEM_URL = "urn:iso:std:iso:4217";
061        public static final String CURRENCIES_VALUESET_URL = "http://hl7.org/fhir/ValueSet/currencies";
062        public static final String COUNTRIES_CODESYSTEM_URL = "urn:iso:std:iso:3166";
063        public static final String UCUM_CODESYSTEM_URL = "http://unitsofmeasure.org";
064        public static final String UCUM_VALUESET_URL = "http://hl7.org/fhir/ValueSet/ucum-units";
065        public static final String ALL_LANGUAGES_VALUESET_URL = "http://hl7.org/fhir/ValueSet/all-languages";
066        private static final String USPS_CODESYSTEM_URL = "https://www.usps.com/";
067        private static final String USPS_VALUESET_URL = "http://hl7.org/fhir/us/core/ValueSet/us-core-usps-state";
068        private static final Logger ourLog = LoggerFactory.getLogger(CommonCodeSystemsTerminologyService.class);
069        private static final Map<String, String> USPS_CODES = Collections.unmodifiableMap(buildUspsCodes());
070        private static final Map<String, String> ISO_4217_CODES = Collections.unmodifiableMap(buildIso4217Codes());
071        private static final Map<String, String> ISO_3166_CODES = Collections.unmodifiableMap(buildIso3166Codes());
072        private final FhirContext myFhirContext;
073        private final VersionCanonicalizer myVersionCanonicalizer;
074        private volatile org.hl7.fhir.r5.model.ValueSet myLanguagesVs;
075        private volatile Map<String, String> myLanguagesLanugageMap;
076        private volatile Map<String, String> myLanguagesRegionMap;
077
078        /**
079         * Constructor
080         */
081        public CommonCodeSystemsTerminologyService(FhirContext theFhirContext) {
082                Validate.notNull(theFhirContext);
083
084                myFhirContext = theFhirContext;
085                myVersionCanonicalizer = new VersionCanonicalizer(theFhirContext);
086        }
087
088        @Override
089        public CodeValidationResult validateCodeInValueSet(ValidationSupportContext theValidationSupportContext, ConceptValidationOptions theOptions, String theCodeSystem, String theCode, String theDisplay, @Nonnull IBaseResource theValueSet) {
090                String url = getValueSetUrl(getFhirContext(), theValueSet);
091                return validateCode(theValidationSupportContext, theOptions, theCodeSystem, theCode, theDisplay, url);
092        }
093
094        @Override
095        public CodeValidationResult validateCode(@Nonnull ValidationSupportContext theValidationSupportContext, @Nonnull ConceptValidationOptions theOptions, String theCodeSystem, String theCode, String theDisplay, String theValueSetUrl) {
096                /* **************************************************************************************
097                 * NOTE: Update validation_support_modules.html if any of the support in this module
098                 * changes in any way!
099                 * **************************************************************************************/
100
101                Map<String, String> handlerMap = null;
102                String expectSystem = null;
103                switch (defaultString(theValueSetUrl)) {
104                        case USPS_VALUESET_URL:
105                                handlerMap = USPS_CODES;
106                                expectSystem = USPS_CODESYSTEM_URL;
107                                break;
108
109                        case CURRENCIES_VALUESET_URL:
110                                handlerMap = ISO_4217_CODES;
111                                expectSystem = CURRENCIES_CODESYSTEM_URL;
112                                break;
113
114                        case LANGUAGES_VALUESET_URL:
115                                if (!LANGUAGES_CODESYSTEM_URL.equals(theCodeSystem) && !(theCodeSystem == null && theOptions.isInferSystem())) {
116                                        return new CodeValidationResult()
117                                                .setSeverity(IssueSeverity.ERROR)
118                                                .setMessage("Inappropriate CodeSystem URL \"" + theCodeSystem + "\" for ValueSet: " + theValueSetUrl);
119                                }
120
121                                IBaseResource languagesVs = myLanguagesVs;
122                                if (languagesVs == null) {
123                                        languagesVs = theValidationSupportContext.getRootValidationSupport().fetchValueSet("http://hl7.org/fhir/ValueSet/languages");
124                                        myLanguagesVs = myVersionCanonicalizer.valueSetToValidatorCanonical(languagesVs);
125                                }
126                                Optional<org.hl7.fhir.r5.model.ValueSet.ConceptReferenceComponent> match = myLanguagesVs
127                                        .getCompose()
128                                        .getInclude()
129                                        .stream()
130                                        .flatMap(t -> t.getConcept().stream())
131                                        .filter(t -> theCode.equals(t.getCode()))
132                                        .findFirst();
133                                if (match.isPresent()) {
134                                        return new CodeValidationResult()
135                                                .setCode(theCode)
136                                                .setDisplay(match.get().getDisplay());
137                                } else {
138                                        return new CodeValidationResult()
139                                                .setSeverity(IssueSeverity.ERROR)
140                                                .setMessage("Code \"" + theCode + "\" is not in valueset: " + theValueSetUrl);
141                                }
142
143                        case ALL_LANGUAGES_VALUESET_URL:
144                                if (!LANGUAGES_CODESYSTEM_URL.equals(theCodeSystem) && !(theCodeSystem == null && theOptions.isInferSystem())) {
145                                        return new CodeValidationResult()
146                                                .setSeverity(IssueSeverity.ERROR)
147                                                .setMessage("Inappropriate CodeSystem URL \"" + theCodeSystem + "\" for ValueSet: " + theValueSetUrl);
148                                }
149
150                                LookupCodeResult outcome = lookupLanguageCode(theCode);
151                                if (outcome.isFound()) {
152                                        return new CodeValidationResult()
153                                                .setCode(theCode)
154                                                .setDisplay(outcome.getCodeDisplay());
155                                } else {
156                                        return new CodeValidationResult()
157                                                .setSeverity(IssueSeverity.ERROR)
158                                                .setMessage("Code \"" + theCode + "\" is not in valueset: " + theValueSetUrl);
159                                }
160
161                        case MIMETYPES_VALUESET_URL:
162                                // This is a pretty naive implementation - Should be enhanced in future
163                                return new CodeValidationResult()
164                                        .setCode(theCode)
165                                        .setDisplay(theDisplay);
166
167                        case UCUM_VALUESET_URL: {
168                                String system = theCodeSystem;
169                                if (system == null && theOptions.isInferSystem()) {
170                                        system = UCUM_CODESYSTEM_URL;
171                                }
172                                CodeValidationResult validationResult = validateLookupCode(theValidationSupportContext, theCode, system);
173                                if (validationResult != null) {
174                                        return validationResult;
175                                }
176                        }
177                }
178
179                if (handlerMap != null) {
180                        String display = handlerMap.get(theCode);
181                        if (display != null) {
182                                if (expectSystem.equals(theCodeSystem) || theOptions.isInferSystem()) {
183                                        return new CodeValidationResult()
184                                                .setCode(theCode)
185                                                .setDisplay(display);
186                                }
187                        }
188
189                        return new CodeValidationResult()
190                                .setSeverity(IssueSeverity.ERROR)
191                                .setMessage("Code \"" + theCode + "\" is not in system: " + USPS_CODESYSTEM_URL);
192                }
193
194                if (isBlank(theValueSetUrl)) {
195                        return validateLookupCode(theValidationSupportContext, theCode, theCodeSystem);
196                }
197
198                return null;
199        }
200
201        @Nullable
202        public CodeValidationResult validateLookupCode(ValidationSupportContext theValidationSupportContext, String theCode, String theSystem) {
203                LookupCodeResult lookupResult = lookupCode(theValidationSupportContext, theSystem, theCode);
204                CodeValidationResult validationResult = null;
205                if (lookupResult != null) {
206                        if (lookupResult.isFound()) {
207                                validationResult = new CodeValidationResult()
208                                        .setCode(lookupResult.getSearchedForCode())
209                                        .setDisplay(lookupResult.getCodeDisplay());
210                        }
211                }
212
213                return validationResult;
214        }
215
216
217        @Override
218        public LookupCodeResult lookupCode(ValidationSupportContext theValidationSupportContext, String theSystem, String theCode, String theDisplayLanguage) {
219                Map<String, String> map;
220                switch (theSystem) {
221                        case LANGUAGES_CODESYSTEM_URL:
222                                return lookupLanguageCode(theCode);
223                        case UCUM_CODESYSTEM_URL:
224                                return lookupUcumCode(theCode);
225                        case MIMETYPES_CODESYSTEM_URL:
226                                return lookupMimetypeCode(theCode);
227                        case COUNTRIES_CODESYSTEM_URL:
228                                map = ISO_3166_CODES;
229                                break;
230                        case CURRENCIES_CODESYSTEM_URL:
231                                map = ISO_4217_CODES;
232                                break;
233                        case USPS_CODESYSTEM_URL:
234                                map = USPS_CODES;
235                                break;
236                        default:
237                                return null;
238                }
239
240                String display = map.get(theCode);
241                if (isNotBlank(display)) {
242                        LookupCodeResult retVal = new LookupCodeResult();
243                        retVal.setSearchedForCode(theCode);
244                        retVal.setSearchedForSystem(theSystem);
245                        retVal.setFound(true);
246                        retVal.setCodeDisplay(display);
247                        return retVal;
248                }
249
250                // If we get here it means we know the codesystem but the code was bad
251                LookupCodeResult retVal = new LookupCodeResult();
252                retVal.setSearchedForCode(theCode);
253                retVal.setSearchedForSystem(theSystem);
254                retVal.setFound(false);
255                return retVal;
256
257        }
258
259        private LookupCodeResult lookupLanguageCode(String theCode) {
260                if (myLanguagesLanugageMap == null || myLanguagesRegionMap == null) {
261                        initializeBcp47LanguageMap();
262                }
263
264                int langRegionSeparatorIndex = StringUtils.indexOfAny(theCode, '-', '_');
265                boolean hasRegionAndCodeSegments = langRegionSeparatorIndex > 0;
266                String language;
267                String region;
268
269                if (hasRegionAndCodeSegments) {
270                        // we look for languages in lowercase only
271                        // this will allow case insensitivity for language portion of code
272                        language = myLanguagesLanugageMap.get(theCode.substring(0, langRegionSeparatorIndex).toLowerCase());
273                        region = myLanguagesRegionMap.get(theCode.substring(langRegionSeparatorIndex + 1).toUpperCase());
274
275                        if (language == null || region == null) {
276                                //In case the user provides both a language and a region, they must both be valid for the lookup to succeed.
277                                ourLog.warn("Couldn't find a valid bcp47 language-region combination from code: {}", theCode);
278                                return buildNotFoundLookupCodeResult(theCode);
279                        } else {
280                                return buildLookupResultForLanguageAndRegion(theCode, language, region);
281                        }
282                } else {
283                        //In case user has only provided a language, we build the lookup from only that.
284                        //NB: we only use the lowercase version of the language
285                        language = myLanguagesLanugageMap.get(theCode.toLowerCase());
286                        if (language == null) {
287                                ourLog.warn("Couldn't find a valid bcp47 language from code: {}", theCode);
288                                return buildNotFoundLookupCodeResult(theCode);
289                        } else {
290                                return buildLookupResultForLanguage(theCode, language);
291                        }
292                }
293        }
294
295        private LookupCodeResult buildLookupResultForLanguageAndRegion(@Nonnull String theOriginalCode, @Nonnull String theLanguage, @Nonnull String theRegion) {
296                LookupCodeResult lookupCodeResult = buildNotFoundLookupCodeResult(theOriginalCode);
297                lookupCodeResult.setCodeDisplay(theLanguage + " " + theRegion);
298                lookupCodeResult.setFound(true);
299                return lookupCodeResult;
300        }
301
302        private LookupCodeResult buildLookupResultForLanguage(@Nonnull String theOriginalCode, @Nonnull String theLanguage) {
303                LookupCodeResult lookupCodeResult = buildNotFoundLookupCodeResult(theOriginalCode);
304                lookupCodeResult.setCodeDisplay(theLanguage);
305                lookupCodeResult.setFound(true);
306                return lookupCodeResult;
307        }
308
309        private LookupCodeResult buildNotFoundLookupCodeResult(@Nonnull String theOriginalCode) {
310                LookupCodeResult lookupCodeResult = new LookupCodeResult();
311                lookupCodeResult.setFound(false);
312                lookupCodeResult.setSearchedForSystem(LANGUAGES_CODESYSTEM_URL);
313                lookupCodeResult.setSearchedForCode(theOriginalCode);
314                return lookupCodeResult;
315        }
316
317        private void initializeBcp47LanguageMap() {
318                Map<String, String> regionsMap;
319                Map<String, String> languagesMap;
320                ourLog.info("Loading BCP47 Language Registry");
321
322                String input = ClasspathUtil.loadResource("org/hl7/fhir/common/hapi/validation/support/registry.json");
323                ArrayNode map;
324                try {
325                        map = (ArrayNode) new ObjectMapper().readTree(input);
326                } catch (JsonProcessingException e) {
327                        throw new ConfigurationException(Msg.code(694) + e);
328                }
329
330                languagesMap = new HashMap<>();
331                regionsMap = new HashMap<>();
332
333                for (int i = 0; i < map.size(); i++) {
334                        ObjectNode next = (ObjectNode) map.get(i);
335                        String type = next.get("Type").asText();
336                        if ("language".equals(type)) {
337                                populateSubTagMap(languagesMap, next);
338                        }
339                        if ("region".equals(type)) {
340                                populateSubTagMap(regionsMap, next);
341                        }
342                }
343
344                ourLog.info("Have {} languages and {} regions", languagesMap.size(), regionsMap.size());
345
346                myLanguagesLanugageMap = languagesMap;
347                myLanguagesRegionMap = regionsMap;
348        }
349
350        private void populateSubTagMap(Map<String, String> theLanguagesMap, ObjectNode theNext) {
351                String language = theNext.get("Subtag").asText();
352                ArrayNode descriptions = (ArrayNode) theNext.get("Description");
353                String description = null;
354                if (descriptions.size() > 0) {
355                        description = descriptions.get(0).asText();
356                }
357                theLanguagesMap.put(language, description);
358        }
359
360        @Nonnull
361        private LookupCodeResult lookupMimetypeCode(String theCode) {
362                // This is a pretty naive implementation - Should be enhanced in future
363                LookupCodeResult mimeRetVal = new LookupCodeResult();
364                mimeRetVal.setSearchedForCode(theCode);
365                mimeRetVal.setSearchedForSystem(MIMETYPES_CODESYSTEM_URL);
366                mimeRetVal.setFound(true);
367                return mimeRetVal;
368        }
369
370        @Nonnull
371        private LookupCodeResult lookupUcumCode(String theCode) {
372                InputStream input = ClasspathUtil.loadResourceAsStream("/ucum-essence.xml");
373                String outcome = null;
374                try {
375                        UcumEssenceService svc = new UcumEssenceService(input);
376                        outcome = svc.analyse(theCode);
377                } catch (UcumException e) {
378                        ourLog.warn("Failed parse UCUM code: {}", theCode, e);
379                } finally {
380                        ClasspathUtil.close(input);
381                }
382                LookupCodeResult retVal = new LookupCodeResult();
383                retVal.setSearchedForCode(theCode);
384                retVal.setSearchedForSystem(UCUM_CODESYSTEM_URL);
385                if (outcome != null) {
386                        retVal.setFound(true);
387                        retVal.setCodeDisplay(outcome);
388                }
389                return retVal;
390        }
391
392        @Override
393        public IBaseResource fetchCodeSystem(String theSystem) {
394
395                Map<String, String> map;
396                switch (defaultString(theSystem)) {
397                        case COUNTRIES_CODESYSTEM_URL:
398                                map = ISO_3166_CODES;
399                                break;
400                        case CURRENCIES_CODESYSTEM_URL:
401                                map = ISO_4217_CODES;
402                                break;
403                        default:
404                                return null;
405                }
406
407                CodeSystem retVal = new CodeSystem();
408                retVal.setContent(CodeSystem.CodeSystemContentMode.COMPLETE);
409                retVal.setUrl(theSystem);
410                for (Map.Entry<String, String> nextEntry : map.entrySet()) {
411                        retVal.addConcept().setCode(nextEntry.getKey()).setDisplay(nextEntry.getValue());
412                }
413
414                IBaseResource normalized = null;
415                switch (getFhirContext().getVersion().getVersion()) {
416                        case DSTU2:
417                        case DSTU2_HL7ORG:
418                        case DSTU2_1:
419                                return null;
420                        case DSTU3:
421                                normalized = VersionConvertorFactory_30_40.convertResource(retVal, new BaseAdvisor_30_40(false));
422                                break;
423                        case R4:
424                                normalized = retVal;
425                                break;
426                        case R4B:
427                                Resource normalized50 = VersionConvertorFactory_40_50.convertResource(retVal, new BaseAdvisor_40_50(false));
428                                normalized = VersionConvertorFactory_43_50.convertResource(normalized50, new BaseAdvisor_43_50());
429                                break;
430                        case R5:
431                                normalized = VersionConvertorFactory_40_50.convertResource(retVal, new BaseAdvisor_40_50(false));
432                                break;
433                }
434
435                Validate.notNull(normalized);
436
437                return normalized;
438        }
439
440        @Override
441        public boolean isCodeSystemSupported(ValidationSupportContext theValidationSupportContext, String theSystem) {
442
443                switch (theSystem) {
444                        case COUNTRIES_CODESYSTEM_URL:
445                        case UCUM_CODESYSTEM_URL:
446                        case MIMETYPES_CODESYSTEM_URL:
447                        case USPS_CODESYSTEM_URL:
448                        case LANGUAGES_CODESYSTEM_URL:
449                                return true;
450                }
451
452                return false;
453        }
454
455        @Override
456        public boolean isValueSetSupported(ValidationSupportContext theValidationSupportContext, String theValueSetUrl) {
457
458                switch (theValueSetUrl) {
459                        case CURRENCIES_VALUESET_URL:
460                        case LANGUAGES_VALUESET_URL:
461                        case ALL_LANGUAGES_VALUESET_URL:
462                        case MIMETYPES_VALUESET_URL:
463                        case UCUM_VALUESET_URL:
464                        case USPS_VALUESET_URL:
465                                return true;
466                }
467
468                return false;
469        }
470
471        @Override
472        public FhirContext getFhirContext() {
473                return myFhirContext;
474        }
475
476        public static String getValueSetUrl(FhirContext theFhirContext, @Nonnull IBaseResource theValueSet) {
477                String url;
478                FhirVersionEnum structureFhirVersionEnum = getFhirVersionEnum(theFhirContext, theValueSet);
479                switch (structureFhirVersionEnum) {
480                        case DSTU2: {
481                                url = ((ca.uhn.fhir.model.dstu2.resource.ValueSet) theValueSet).getUrl();
482                                break;
483                        }
484                        case DSTU2_HL7ORG: {
485                                url = ((ValueSet) theValueSet).getUrl();
486                                break;
487                        }
488                        case DSTU3: {
489                                url = ((org.hl7.fhir.dstu3.model.ValueSet) theValueSet).getUrl();
490                                break;
491                        }
492                        case R4: {
493                                url = ((org.hl7.fhir.r4.model.ValueSet) theValueSet).getUrl();
494                                break;
495                        }
496                        case R4B: {
497                                url = ((org.hl7.fhir.r4b.model.ValueSet) theValueSet).getUrl();
498                                break;
499                        }
500                        case R5: {
501                                url = ((org.hl7.fhir.r5.model.ValueSet) theValueSet).getUrl();
502                                break;
503                        }
504                        case DSTU2_1:
505                        default:
506                                throw new IllegalArgumentException(Msg.code(695) + "Can not handle version: " + structureFhirVersionEnum);
507                }
508                return url;
509        }
510
511        public static String getCodeSystemUrl(@Nonnull FhirContext theFhirContext, @Nonnull IBaseResource theCodeSystem) {
512                String url;
513                FhirVersionEnum structureFhirVersionEnum = getFhirVersionEnum(theFhirContext, theCodeSystem);
514                switch (structureFhirVersionEnum) {
515                        case R4: {
516                                url = ((org.hl7.fhir.r4.model.CodeSystem) theCodeSystem).getUrl();
517                                break;
518                        }
519                        case R4B: {
520                                url = ((org.hl7.fhir.r4b.model.CodeSystem) theCodeSystem).getUrl();
521                                break;
522                        }
523                        case R5: {
524                                url = ((org.hl7.fhir.r5.model.CodeSystem) theCodeSystem).getUrl();
525                                break;
526                        }
527                        case DSTU3:
528                        default:
529                                throw new IllegalArgumentException(Msg.code(696) + "Can not handle version: " + structureFhirVersionEnum);
530                }
531                return url;
532        }
533
534        public static String getValueSetVersion(@Nonnull FhirContext theFhirContext, @Nonnull IBaseResource theValueSet) {
535                String version;
536                switch (getFhirVersionEnum(theFhirContext, theValueSet)) {
537                        case DSTU3: {
538                                version = ((org.hl7.fhir.dstu3.model.ValueSet) theValueSet).getVersion();
539                                break;
540                        }
541                        case R4: {
542                                version = ((org.hl7.fhir.r4.model.ValueSet) theValueSet).getVersion();
543                                break;
544                        }
545                        case R4B: {
546                                version = ((org.hl7.fhir.r4b.model.ValueSet) theValueSet).getVersion();
547                                break;
548                        }
549                        case R5: {
550                                version = ((org.hl7.fhir.r5.model.ValueSet) theValueSet).getVersion();
551                                break;
552                        }
553                        case DSTU2:
554                        case DSTU2_HL7ORG:
555                        case DSTU2_1:
556                        default:
557                                version = null;
558                }
559                return version;
560        }
561
562        /**
563         * N.B.:  We are keeping this as a shim due to the upgrade we did to core 5.6.97+
564         */
565        public static FhirVersionEnum getFhirVersionEnum(@Nonnull FhirContext theFhirContext, @Nonnull IBaseResource theResource) {
566                FhirVersionEnum structureFhirVersionEnum = theResource.getStructureFhirVersionEnum();
567                // TODO: Address this when core lib version is bumped
568                if (theResource.getStructureFhirVersionEnum() == FhirVersionEnum.R5 && theFhirContext.getVersion().getVersion() == FhirVersionEnum.R4B) {
569                        if (!(theResource instanceof org.hl7.fhir.r5.model.Resource)) {
570                                structureFhirVersionEnum = FhirVersionEnum.R4B;
571                        }
572                }
573                return structureFhirVersionEnum;
574        }
575
576        private static HashMap<String, String> buildUspsCodes() {
577                HashMap<String, String> uspsCodes = new HashMap<>();
578                uspsCodes.put("AK", "Alaska");
579                uspsCodes.put("AL", "Alabama");
580                uspsCodes.put("AR", "Arkansas");
581                uspsCodes.put("AS", "American Samoa");
582                uspsCodes.put("AZ", "Arizona");
583                uspsCodes.put("CA", "California");
584                uspsCodes.put("CO", "Colorado");
585                uspsCodes.put("CT", "Connecticut");
586                uspsCodes.put("DC", "District of Columbia");
587                uspsCodes.put("DE", "Delaware");
588                uspsCodes.put("FL", "Florida");
589                uspsCodes.put("FM", "Federated States of Micronesia");
590                uspsCodes.put("GA", "Georgia");
591                uspsCodes.put("GU", "Guam");
592                uspsCodes.put("HI", "Hawaii");
593                uspsCodes.put("IA", "Iowa");
594                uspsCodes.put("ID", "Idaho");
595                uspsCodes.put("IL", "Illinois");
596                uspsCodes.put("IN", "Indiana");
597                uspsCodes.put("KS", "Kansas");
598                uspsCodes.put("KY", "Kentucky");
599                uspsCodes.put("LA", "Louisiana");
600                uspsCodes.put("MA", "Massachusetts");
601                uspsCodes.put("MD", "Maryland");
602                uspsCodes.put("ME", "Maine");
603                uspsCodes.put("MH", "Marshall Islands");
604                uspsCodes.put("MI", "Michigan");
605                uspsCodes.put("MN", "Minnesota");
606                uspsCodes.put("MO", "Missouri");
607                uspsCodes.put("MP", "Northern Mariana Islands");
608                uspsCodes.put("MS", "Mississippi");
609                uspsCodes.put("MT", "Montana");
610                uspsCodes.put("NC", "North Carolina");
611                uspsCodes.put("ND", "North Dakota");
612                uspsCodes.put("NE", "Nebraska");
613                uspsCodes.put("NH", "New Hampshire");
614                uspsCodes.put("NJ", "New Jersey");
615                uspsCodes.put("NM", "New Mexico");
616                uspsCodes.put("NV", "Nevada");
617                uspsCodes.put("NY", "New York");
618                uspsCodes.put("OH", "Ohio");
619                uspsCodes.put("OK", "Oklahoma");
620                uspsCodes.put("OR", "Oregon");
621                uspsCodes.put("PA", "Pennsylvania");
622                uspsCodes.put("PR", "Puerto Rico");
623                uspsCodes.put("PW", "Palau");
624                uspsCodes.put("RI", "Rhode Island");
625                uspsCodes.put("SC", "South Carolina");
626                uspsCodes.put("SD", "South Dakota");
627                uspsCodes.put("TN", "Tennessee");
628                uspsCodes.put("TX", "Texas");
629                uspsCodes.put("UM", "U.S. Minor Outlying Islands");
630                uspsCodes.put("UT", "Utah");
631                uspsCodes.put("VA", "Virginia");
632                uspsCodes.put("VI", "Virgin Islands of the U.S.");
633                uspsCodes.put("VT", "Vermont");
634                uspsCodes.put("WA", "Washington");
635                uspsCodes.put("WI", "Wisconsin");
636                uspsCodes.put("WV", "West Virginia");
637                uspsCodes.put("WY", "Wyoming");
638                return uspsCodes;
639        }
640
641        private static HashMap<String, String> buildIso4217Codes() {
642                HashMap<String, String> iso4217Codes = new HashMap<>();
643                iso4217Codes.put("AED", "United Arab Emirates dirham");
644                iso4217Codes.put("AFN", "Afghan afghani");
645                iso4217Codes.put("ALL", "Albanian lek");
646                iso4217Codes.put("AMD", "Armenian dram");
647                iso4217Codes.put("ANG", "Netherlands Antillean guilder");
648                iso4217Codes.put("AOA", "Angolan kwanza");
649                iso4217Codes.put("ARS", "Argentine peso");
650                iso4217Codes.put("AUD", "Australian dollar");
651                iso4217Codes.put("AWG", "Aruban florin");
652                iso4217Codes.put("AZN", "Azerbaijani manat");
653                iso4217Codes.put("BAM", "Bosnia and Herzegovina convertible mark");
654                iso4217Codes.put("BBD", "Barbados dollar");
655                iso4217Codes.put("BDT", "Bangladeshi taka");
656                iso4217Codes.put("BGN", "Bulgarian lev");
657                iso4217Codes.put("BHD", "Bahraini dinar");
658                iso4217Codes.put("BIF", "Burundian franc");
659                iso4217Codes.put("BMD", "Bermudian dollar");
660                iso4217Codes.put("BND", "Brunei dollar");
661                iso4217Codes.put("BOB", "Boliviano");
662                iso4217Codes.put("BOV", "Bolivian Mvdol (funds code)");
663                iso4217Codes.put("BRL", "Brazilian real");
664                iso4217Codes.put("BSD", "Bahamian dollar");
665                iso4217Codes.put("BTN", "Bhutanese ngultrum");
666                iso4217Codes.put("BWP", "Botswana pula");
667                iso4217Codes.put("BYN", "Belarusian ruble");
668                iso4217Codes.put("BZD", "Belize dollar");
669                iso4217Codes.put("CAD", "Canadian dollar");
670                iso4217Codes.put("CDF", "Congolese franc");
671                iso4217Codes.put("CHE", "WIR Euro (complementary currency)");
672                iso4217Codes.put("CHF", "Swiss franc");
673                iso4217Codes.put("CHW", "WIR Franc (complementary currency)");
674                iso4217Codes.put("CLF", "Unidad de Fomento (funds code)");
675                iso4217Codes.put("CLP", "Chilean peso");
676                iso4217Codes.put("CNY", "Renminbi (Chinese) yuan[8]");
677                iso4217Codes.put("COP", "Colombian peso");
678                iso4217Codes.put("COU", "Unidad de Valor Real (UVR) (funds code)[9]");
679                iso4217Codes.put("CRC", "Costa Rican colon");
680                iso4217Codes.put("CUC", "Cuban convertible peso");
681                iso4217Codes.put("CUP", "Cuban peso");
682                iso4217Codes.put("CVE", "Cape Verde escudo");
683                iso4217Codes.put("CZK", "Czech koruna");
684                iso4217Codes.put("DJF", "Djiboutian franc");
685                iso4217Codes.put("DKK", "Danish krone");
686                iso4217Codes.put("DOP", "Dominican peso");
687                iso4217Codes.put("DZD", "Algerian dinar");
688                iso4217Codes.put("EGP", "Egyptian pound");
689                iso4217Codes.put("ERN", "Eritrean nakfa");
690                iso4217Codes.put("ETB", "Ethiopian birr");
691                iso4217Codes.put("EUR", "Euro");
692                iso4217Codes.put("FJD", "Fiji dollar");
693                iso4217Codes.put("FKP", "Falkland Islands pound");
694                iso4217Codes.put("GBP", "Pound sterling");
695                iso4217Codes.put("GEL", "Georgian lari");
696                iso4217Codes.put("GGP", "Guernsey Pound");
697                iso4217Codes.put("GHS", "Ghanaian cedi");
698                iso4217Codes.put("GIP", "Gibraltar pound");
699                iso4217Codes.put("GMD", "Gambian dalasi");
700                iso4217Codes.put("GNF", "Guinean franc");
701                iso4217Codes.put("GTQ", "Guatemalan quetzal");
702                iso4217Codes.put("GYD", "Guyanese dollar");
703                iso4217Codes.put("HKD", "Hong Kong dollar");
704                iso4217Codes.put("HNL", "Honduran lempira");
705                iso4217Codes.put("HRK", "Croatian kuna");
706                iso4217Codes.put("HTG", "Haitian gourde");
707                iso4217Codes.put("HUF", "Hungarian forint");
708                iso4217Codes.put("IDR", "Indonesian rupiah");
709                iso4217Codes.put("ILS", "Israeli new shekel");
710                iso4217Codes.put("IMP", "Isle of Man Pound");
711                iso4217Codes.put("INR", "Indian rupee");
712                iso4217Codes.put("IQD", "Iraqi dinar");
713                iso4217Codes.put("IRR", "Iranian rial");
714                iso4217Codes.put("ISK", "Icelandic króna");
715                iso4217Codes.put("JEP", "Jersey Pound");
716                iso4217Codes.put("JMD", "Jamaican dollar");
717                iso4217Codes.put("JOD", "Jordanian dinar");
718                iso4217Codes.put("JPY", "Japanese yen");
719                iso4217Codes.put("KES", "Kenyan shilling");
720                iso4217Codes.put("KGS", "Kyrgyzstani som");
721                iso4217Codes.put("KHR", "Cambodian riel");
722                iso4217Codes.put("KMF", "Comoro franc");
723                iso4217Codes.put("KPW", "North Korean won");
724                iso4217Codes.put("KRW", "South Korean won");
725                iso4217Codes.put("KWD", "Kuwaiti dinar");
726                iso4217Codes.put("KYD", "Cayman Islands dollar");
727                iso4217Codes.put("KZT", "Kazakhstani tenge");
728                iso4217Codes.put("LAK", "Lao kip");
729                iso4217Codes.put("LBP", "Lebanese pound");
730                iso4217Codes.put("LKR", "Sri Lankan rupee");
731                iso4217Codes.put("LRD", "Liberian dollar");
732                iso4217Codes.put("LSL", "Lesotho loti");
733                iso4217Codes.put("LYD", "Libyan dinar");
734                iso4217Codes.put("MAD", "Moroccan dirham");
735                iso4217Codes.put("MDL", "Moldovan leu");
736                iso4217Codes.put("MGA", "Malagasy ariary");
737                iso4217Codes.put("MKD", "Macedonian denar");
738                iso4217Codes.put("MMK", "Myanmar kyat");
739                iso4217Codes.put("MNT", "Mongolian tögrög");
740                iso4217Codes.put("MOP", "Macanese pataca");
741                iso4217Codes.put("MRU", "Mauritanian ouguiya");
742                iso4217Codes.put("MUR", "Mauritian rupee");
743                iso4217Codes.put("MVR", "Maldivian rufiyaa");
744                iso4217Codes.put("MWK", "Malawian kwacha");
745                iso4217Codes.put("MXN", "Mexican peso");
746                iso4217Codes.put("MXV", "Mexican Unidad de Inversion (UDI) (funds code)");
747                iso4217Codes.put("MYR", "Malaysian ringgit");
748                iso4217Codes.put("MZN", "Mozambican metical");
749                iso4217Codes.put("NAD", "Namibian dollar");
750                iso4217Codes.put("NGN", "Nigerian naira");
751                iso4217Codes.put("NIO", "Nicaraguan córdoba");
752                iso4217Codes.put("NOK", "Norwegian krone");
753                iso4217Codes.put("NPR", "Nepalese rupee");
754                iso4217Codes.put("NZD", "New Zealand dollar");
755                iso4217Codes.put("OMR", "Omani rial");
756                iso4217Codes.put("PAB", "Panamanian balboa");
757                iso4217Codes.put("PEN", "Peruvian Sol");
758                iso4217Codes.put("PGK", "Papua New Guinean kina");
759                iso4217Codes.put("PHP", "Philippine piso[13]");
760                iso4217Codes.put("PKR", "Pakistani rupee");
761                iso4217Codes.put("PLN", "Polish złoty");
762                iso4217Codes.put("PYG", "Paraguayan guaraní");
763                iso4217Codes.put("QAR", "Qatari riyal");
764                iso4217Codes.put("RON", "Romanian leu");
765                iso4217Codes.put("RSD", "Serbian dinar");
766                iso4217Codes.put("RUB", "Russian ruble");
767                iso4217Codes.put("RWF", "Rwandan franc");
768                iso4217Codes.put("SAR", "Saudi riyal");
769                iso4217Codes.put("SBD", "Solomon Islands dollar");
770                iso4217Codes.put("SCR", "Seychelles rupee");
771                iso4217Codes.put("SDG", "Sudanese pound");
772                iso4217Codes.put("SEK", "Swedish krona/kronor");
773                iso4217Codes.put("SGD", "Singapore dollar");
774                iso4217Codes.put("SHP", "Saint Helena pound");
775                iso4217Codes.put("SLL", "Sierra Leonean leone");
776                iso4217Codes.put("SOS", "Somali shilling");
777                iso4217Codes.put("SRD", "Surinamese dollar");
778                iso4217Codes.put("SSP", "South Sudanese pound");
779                iso4217Codes.put("STN", "São Tomé and Príncipe dobra");
780                iso4217Codes.put("SVC", "Salvadoran colón");
781                iso4217Codes.put("SYP", "Syrian pound");
782                iso4217Codes.put("SZL", "Swazi lilangeni");
783                iso4217Codes.put("THB", "Thai baht");
784                iso4217Codes.put("TJS", "Tajikistani somoni");
785                iso4217Codes.put("TMT", "Turkmenistan manat");
786                iso4217Codes.put("TND", "Tunisian dinar");
787                iso4217Codes.put("TOP", "Tongan paʻanga");
788                iso4217Codes.put("TRY", "Turkish lira");
789                iso4217Codes.put("TTD", "Trinidad and Tobago dollar");
790                iso4217Codes.put("TVD", "Tuvalu Dollar");
791                iso4217Codes.put("TWD", "New Taiwan dollar");
792                iso4217Codes.put("TZS", "Tanzanian shilling");
793                iso4217Codes.put("UAH", "Ukrainian hryvnia");
794                iso4217Codes.put("UGX", "Ugandan shilling");
795                iso4217Codes.put("USD", "United States dollar");
796                iso4217Codes.put("USN", "United States dollar (next day) (funds code)");
797                iso4217Codes.put("UYI", "Uruguay Peso en Unidades Indexadas (URUIURUI) (funds code)");
798                iso4217Codes.put("UYU", "Uruguayan peso");
799                iso4217Codes.put("UZS", "Uzbekistan som");
800                iso4217Codes.put("VEF", "Venezuelan bolívar");
801                iso4217Codes.put("VND", "Vietnamese đồng");
802                iso4217Codes.put("VUV", "Vanuatu vatu");
803                iso4217Codes.put("WST", "Samoan tala");
804                iso4217Codes.put("XAF", "CFA franc BEAC");
805                iso4217Codes.put("XAG", "Silver (one troy ounce)");
806                iso4217Codes.put("XAU", "Gold (one troy ounce)");
807                iso4217Codes.put("XBA", "European Composite Unit (EURCO) (bond market unit)");
808                iso4217Codes.put("XBB", "European Monetary Unit (E.M.U.-6) (bond market unit)");
809                iso4217Codes.put("XBC", "European Unit of Account 9 (E.U.A.-9) (bond market unit)");
810                iso4217Codes.put("XBD", "European Unit of Account 17 (E.U.A.-17) (bond market unit)");
811                iso4217Codes.put("XCD", "East Caribbean dollar");
812                iso4217Codes.put("XDR", "Special drawing rights");
813                iso4217Codes.put("XOF", "CFA franc BCEAO");
814                iso4217Codes.put("XPD", "Palladium (one troy ounce)");
815                iso4217Codes.put("XPF", "CFP franc (franc Pacifique)");
816                iso4217Codes.put("XPT", "Platinum (one troy ounce)");
817                iso4217Codes.put("XSU", "SUCRE");
818                iso4217Codes.put("XTS", "Code reserved for testing purposes");
819                iso4217Codes.put("XUA", "ADB Unit of Account");
820                iso4217Codes.put("XXX", "No currency");
821                iso4217Codes.put("YER", "Yemeni rial");
822                iso4217Codes.put("ZAR", "South African rand");
823                iso4217Codes.put("ZMW", "Zambian kwacha");
824                iso4217Codes.put("ZWL", "Zimbabwean dollar A/10");
825                return iso4217Codes;
826        }
827
828
829        private static HashMap<String, String> buildIso3166Codes() {
830                HashMap<String, String> codes = new HashMap<>();
831
832                // 2 letter codes
833                codes.put("AF", "Afghanistan");
834                codes.put("AX", "Åland Islands");
835                codes.put("AL", "Albania");
836                codes.put("DZ", "Algeria");
837                codes.put("AS", "American Samoa");
838                codes.put("AD", "Andorra");
839                codes.put("AO", "Angola");
840                codes.put("AI", "Anguilla");
841                codes.put("AQ", "Antarctica");
842                codes.put("AG", "Antigua & Barbuda");
843                codes.put("AR", "Argentina");
844                codes.put("AM", "Armenia");
845                codes.put("AW", "Aruba");
846                codes.put("AU", "Australia");
847                codes.put("AT", "Austria");
848                codes.put("AZ", "Azerbaijan");
849                codes.put("BS", "Bahamas");
850                codes.put("BH", "Bahrain");
851                codes.put("BD", "Bangladesh");
852                codes.put("BB", "Barbados");
853                codes.put("BY", "Belarus");
854                codes.put("BE", "Belgium");
855                codes.put("BZ", "Belize");
856                codes.put("BJ", "Benin");
857                codes.put("BM", "Bermuda");
858                codes.put("BT", "Bhutan");
859                codes.put("BO", "Bolivia");
860                codes.put("BA", "Bosnia & Herzegovina");
861                codes.put("BW", "Botswana");
862                codes.put("BV", "Bouvet Island");
863                codes.put("BR", "Brazil");
864                codes.put("IO", "British Indian Ocean Territory");
865                codes.put("VG", "British Virgin Islands");
866                codes.put("BN", "Brunei");
867                codes.put("BG", "Bulgaria");
868                codes.put("BF", "Burkina Faso");
869                codes.put("BI", "Burundi");
870                codes.put("KH", "Cambodia");
871                codes.put("CM", "Cameroon");
872                codes.put("CA", "Canada");
873                codes.put("CV", "Cape Verde");
874                codes.put("BQ", "Caribbean Netherlands");
875                codes.put("KY", "Cayman Islands");
876                codes.put("CF", "Central African Republic");
877                codes.put("TD", "Chad");
878                codes.put("CL", "Chile");
879                codes.put("CN", "China");
880                codes.put("CX", "Christmas Island");
881                codes.put("CC", "Cocos (Keeling) Islands");
882                codes.put("CO", "Colombia");
883                codes.put("KM", "Comoros");
884                codes.put("CG", "Congo - Brazzaville");
885                codes.put("CD", "Congo - Kinshasa");
886                codes.put("CK", "Cook Islands");
887                codes.put("CR", "Costa Rica");
888                codes.put("CI", "Côte d’Ivoire");
889                codes.put("HR", "Croatia");
890                codes.put("CU", "Cuba");
891                codes.put("CW", "Curaçao");
892                codes.put("CY", "Cyprus");
893                codes.put("CZ", "Czechia");
894                codes.put("DK", "Denmark");
895                codes.put("DJ", "Djibouti");
896                codes.put("DM", "Dominica");
897                codes.put("DO", "Dominican Republic");
898                codes.put("EC", "Ecuador");
899                codes.put("EG", "Egypt");
900                codes.put("SV", "El Salvador");
901                codes.put("GQ", "Equatorial Guinea");
902                codes.put("ER", "Eritrea");
903                codes.put("EE", "Estonia");
904                codes.put("SZ", "Eswatini");
905                codes.put("ET", "Ethiopia");
906                codes.put("FK", "Falkland Islands");
907                codes.put("FO", "Faroe Islands");
908                codes.put("FJ", "Fiji");
909                codes.put("FI", "Finland");
910                codes.put("FR", "France");
911                codes.put("GF", "French Guiana");
912                codes.put("PF", "French Polynesia");
913                codes.put("TF", "French Southern Territories");
914                codes.put("GA", "Gabon");
915                codes.put("GM", "Gambia");
916                codes.put("GE", "Georgia");
917                codes.put("DE", "Germany");
918                codes.put("GH", "Ghana");
919                codes.put("GI", "Gibraltar");
920                codes.put("GR", "Greece");
921                codes.put("GL", "Greenland");
922                codes.put("GD", "Grenada");
923                codes.put("GP", "Guadeloupe");
924                codes.put("GU", "Guam");
925                codes.put("GT", "Guatemala");
926                codes.put("GG", "Guernsey");
927                codes.put("GN", "Guinea");
928                codes.put("GW", "Guinea-Bissau");
929                codes.put("GY", "Guyana");
930                codes.put("HT", "Haiti");
931                codes.put("HM", "Heard & McDonald Islands");
932                codes.put("HN", "Honduras");
933                codes.put("HK", "Hong Kong SAR China");
934                codes.put("HU", "Hungary");
935                codes.put("IS", "Iceland");
936                codes.put("IN", "India");
937                codes.put("ID", "Indonesia");
938                codes.put("IR", "Iran");
939                codes.put("IQ", "Iraq");
940                codes.put("IE", "Ireland");
941                codes.put("IM", "Isle of Man");
942                codes.put("IL", "Israel");
943                codes.put("IT", "Italy");
944                codes.put("JM", "Jamaica");
945                codes.put("JP", "Japan");
946                codes.put("JE", "Jersey");
947                codes.put("JO", "Jordan");
948                codes.put("KZ", "Kazakhstan");
949                codes.put("KE", "Kenya");
950                codes.put("KI", "Kiribati");
951                codes.put("KW", "Kuwait");
952                codes.put("KG", "Kyrgyzstan");
953                codes.put("LA", "Laos");
954                codes.put("LV", "Latvia");
955                codes.put("LB", "Lebanon");
956                codes.put("LS", "Lesotho");
957                codes.put("LR", "Liberia");
958                codes.put("LY", "Libya");
959                codes.put("LI", "Liechtenstein");
960                codes.put("LT", "Lithuania");
961                codes.put("LU", "Luxembourg");
962                codes.put("MO", "Macao SAR China");
963                codes.put("MG", "Madagascar");
964                codes.put("MW", "Malawi");
965                codes.put("MY", "Malaysia");
966                codes.put("MV", "Maldives");
967                codes.put("ML", "Mali");
968                codes.put("MT", "Malta");
969                codes.put("MH", "Marshall Islands");
970                codes.put("MQ", "Martinique");
971                codes.put("MR", "Mauritania");
972                codes.put("MU", "Mauritius");
973                codes.put("YT", "Mayotte");
974                codes.put("MX", "Mexico");
975                codes.put("FM", "Micronesia");
976                codes.put("MD", "Moldova");
977                codes.put("MC", "Monaco");
978                codes.put("MN", "Mongolia");
979                codes.put("ME", "Montenegro");
980                codes.put("MS", "Montserrat");
981                codes.put("MA", "Morocco");
982                codes.put("MZ", "Mozambique");
983                codes.put("MM", "Myanmar (Burma)");
984                codes.put("NA", "Namibia");
985                codes.put("NR", "Nauru");
986                codes.put("NP", "Nepal");
987                codes.put("NL", "Netherlands");
988                codes.put("NC", "New Caledonia");
989                codes.put("NZ", "New Zealand");
990                codes.put("NI", "Nicaragua");
991                codes.put("NE", "Niger");
992                codes.put("NG", "Nigeria");
993                codes.put("NU", "Niue");
994                codes.put("NF", "Norfolk Island");
995                codes.put("KP", "North Korea");
996                codes.put("MK", "North Macedonia");
997                codes.put("MP", "Northern Mariana Islands");
998                codes.put("NO", "Norway");
999                codes.put("OM", "Oman");
1000                codes.put("PK", "Pakistan");
1001                codes.put("PW", "Palau");
1002                codes.put("PS", "Palestinian Territories");
1003                codes.put("PA", "Panama");
1004                codes.put("PG", "Papua New Guinea");
1005                codes.put("PY", "Paraguay");
1006                codes.put("PE", "Peru");
1007                codes.put("PH", "Philippines");
1008                codes.put("PN", "Pitcairn Islands");
1009                codes.put("PL", "Poland");
1010                codes.put("PT", "Portugal");
1011                codes.put("PR", "Puerto Rico");
1012                codes.put("QA", "Qatar");
1013                codes.put("RE", "Réunion");
1014                codes.put("RO", "Romania");
1015                codes.put("RU", "Russia");
1016                codes.put("RW", "Rwanda");
1017                codes.put("WS", "Samoa");
1018                codes.put("SM", "San Marino");
1019                codes.put("ST", "São Tomé & Príncipe");
1020                codes.put("SA", "Saudi Arabia");
1021                codes.put("SN", "Senegal");
1022                codes.put("RS", "Serbia");
1023                codes.put("SC", "Seychelles");
1024                codes.put("SL", "Sierra Leone");
1025                codes.put("SG", "Singapore");
1026                codes.put("SX", "Sint Maarten");
1027                codes.put("SK", "Slovakia");
1028                codes.put("SI", "Slovenia");
1029                codes.put("SB", "Solomon Islands");
1030                codes.put("SO", "Somalia");
1031                codes.put("ZA", "South Africa");
1032                codes.put("GS", "South Georgia & South Sandwich Islands");
1033                codes.put("KR", "South Korea");
1034                codes.put("SS", "South Sudan");
1035                codes.put("ES", "Spain");
1036                codes.put("LK", "Sri Lanka");
1037                codes.put("BL", "St. Barthélemy");
1038                codes.put("SH", "St. Helena");
1039                codes.put("KN", "St. Kitts & Nevis");
1040                codes.put("LC", "St. Lucia");
1041                codes.put("MF", "St. Martin");
1042                codes.put("PM", "St. Pierre & Miquelon");
1043                codes.put("VC", "St. Vincent & Grenadines");
1044                codes.put("SD", "Sudan");
1045                codes.put("SR", "Suriname");
1046                codes.put("SJ", "Svalbard & Jan Mayen");
1047                codes.put("SE", "Sweden");
1048                codes.put("CH", "Switzerland");
1049                codes.put("SY", "Syria");
1050                codes.put("TW", "Taiwan");
1051                codes.put("TJ", "Tajikistan");
1052                codes.put("TZ", "Tanzania");
1053                codes.put("TH", "Thailand");
1054                codes.put("TL", "Timor-Leste");
1055                codes.put("TG", "Togo");
1056                codes.put("TK", "Tokelau");
1057                codes.put("TO", "Tonga");
1058                codes.put("TT", "Trinidad & Tobago");
1059                codes.put("TN", "Tunisia");
1060                codes.put("TR", "Turkey");
1061                codes.put("TM", "Turkmenistan");
1062                codes.put("TC", "Turks & Caicos Islands");
1063                codes.put("TV", "Tuvalu");
1064                codes.put("UM", "U.S. Outlying Islands");
1065                codes.put("VI", "U.S. Virgin Islands");
1066                codes.put("UG", "Uganda");
1067                codes.put("UA", "Ukraine");
1068                codes.put("AE", "United Arab Emirates");
1069                codes.put("GB", "United Kingdom");
1070                codes.put("US", "United States");
1071                codes.put("UY", "Uruguay");
1072                codes.put("UZ", "Uzbekistan");
1073                codes.put("VU", "Vanuatu");
1074                codes.put("VA", "Vatican City");
1075                codes.put("VE", "Venezuela");
1076                codes.put("VN", "Vietnam");
1077                codes.put("WF", "Wallis & Futuna");
1078                codes.put("EH", "Western Sahara");
1079                codes.put("YE", "Yemen");
1080                codes.put("ZM", "Zambia");
1081                codes.put("ZW", "Zimbabwe");
1082
1083                // 3 letter codes
1084                codes.put("ABW", "Aruba");
1085                codes.put("AFG", "Afghanistan");
1086                codes.put("AGO", "Angola");
1087                codes.put("AIA", "Anguilla");
1088                codes.put("ALA", "Åland Islands");
1089                codes.put("ALB", "Albania");
1090                codes.put("AND", "Andorra");
1091                codes.put("ARE", "United Arab Emirates");
1092                codes.put("ARG", "Argentina");
1093                codes.put("ARM", "Armenia");
1094                codes.put("ASM", "American Samoa");
1095                codes.put("ATA", "Antarctica");
1096                codes.put("ATF", "French Southern Territories");
1097                codes.put("ATG", "Antigua and Barbuda");
1098                codes.put("AUS", "Australia");
1099                codes.put("AUT", "Austria");
1100                codes.put("AZE", "Azerbaijan");
1101                codes.put("BDI", "Burundi");
1102                codes.put("BEL", "Belgium");
1103                codes.put("BEN", "Benin");
1104                codes.put("BES", "Bonaire, Sint Eustatius and Saba");
1105                codes.put("BFA", "Burkina Faso");
1106                codes.put("BGD", "Bangladesh");
1107                codes.put("BGR", "Bulgaria");
1108                codes.put("BHR", "Bahrain");
1109                codes.put("BHS", "Bahamas");
1110                codes.put("BIH", "Bosnia and Herzegovina");
1111                codes.put("BLM", "Saint Barthélemy");
1112                codes.put("BLR", "Belarus");
1113                codes.put("BLZ", "Belize");
1114                codes.put("BMU", "Bermuda");
1115                codes.put("BOL", "Bolivia, Plurinational State of");
1116                codes.put("BRA", "Brazil");
1117                codes.put("BRB", "Barbados");
1118                codes.put("BRN", "Brunei Darussalam");
1119                codes.put("BTN", "Bhutan");
1120                codes.put("BVT", "Bouvet Island");
1121                codes.put("BWA", "Botswana");
1122                codes.put("CAF", "Central African Republic");
1123                codes.put("CAN", "Canada");
1124                codes.put("CCK", "Cocos (Keeling) Islands");
1125                codes.put("CHE", "Switzerland");
1126                codes.put("CHL", "Chile");
1127                codes.put("CHN", "China");
1128                codes.put("CIV", "Côte d'Ivoire");
1129                codes.put("CMR", "Cameroon");
1130                codes.put("COD", "Congo, the Democratic Republic of the");
1131                codes.put("COG", "Congo");
1132                codes.put("COK", "Cook Islands");
1133                codes.put("COL", "Colombia");
1134                codes.put("COM", "Comoros");
1135                codes.put("CPV", "Cabo Verde");
1136                codes.put("CRI", "Costa Rica");
1137                codes.put("CUB", "Cuba");
1138                codes.put("CUW", "Curaçao");
1139                codes.put("CXR", "Christmas Island");
1140                codes.put("CYM", "Cayman Islands");
1141                codes.put("CYP", "Cyprus");
1142                codes.put("CZE", "Czechia");
1143                codes.put("DEU", "Germany");
1144                codes.put("DJI", "Djibouti");
1145                codes.put("DMA", "Dominica");
1146                codes.put("DNK", "Denmark");
1147                codes.put("DOM", "Dominican Republic");
1148                codes.put("DZA", "Algeria");
1149                codes.put("ECU", "Ecuador");
1150                codes.put("EGY", "Egypt");
1151                codes.put("ERI", "Eritrea");
1152                codes.put("ESH", "Western Sahara");
1153                codes.put("ESP", "Spain");
1154                codes.put("EST", "Estonia");
1155                codes.put("ETH", "Ethiopia");
1156                codes.put("FIN", "Finland");
1157                codes.put("FJI", "Fiji");
1158                codes.put("FLK", "Falkland Islands (Malvinas)");
1159                codes.put("FRA", "France");
1160                codes.put("FRO", "Faroe Islands");
1161                codes.put("FSM", "Micronesia, Federated States of");
1162                codes.put("GAB", "Gabon");
1163                codes.put("GBR", "United Kingdom");
1164                codes.put("GEO", "Georgia");
1165                codes.put("GGY", "Guernsey");
1166                codes.put("GHA", "Ghana");
1167                codes.put("GIB", "Gibraltar");
1168                codes.put("GIN", "Guinea");
1169                codes.put("GLP", "Guadeloupe");
1170                codes.put("GMB", "Gambia");
1171                codes.put("GNB", "Guinea-Bissau");
1172                codes.put("GNQ", "Equatorial Guinea");
1173                codes.put("GRC", "Greece");
1174                codes.put("GRD", "Grenada");
1175                codes.put("GRL", "Greenland");
1176                codes.put("GTM", "Guatemala");
1177                codes.put("GUF", "French Guiana");
1178                codes.put("GUM", "Guam");
1179                codes.put("GUY", "Guyana");
1180                codes.put("HKG", "Hong Kong");
1181                codes.put("HMD", "Heard Island and McDonald Islands");
1182                codes.put("HND", "Honduras");
1183                codes.put("HRV", "Croatia");
1184                codes.put("HTI", "Haiti");
1185                codes.put("HUN", "Hungary");
1186                codes.put("IDN", "Indonesia");
1187                codes.put("IMN", "Isle of Man");
1188                codes.put("IND", "India");
1189                codes.put("IOT", "British Indian Ocean Territory");
1190                codes.put("IRL", "Ireland");
1191                codes.put("IRN", "Iran, Islamic Republic of");
1192                codes.put("IRQ", "Iraq");
1193                codes.put("ISL", "Iceland");
1194                codes.put("ISR", "Israel");
1195                codes.put("ITA", "Italy");
1196                codes.put("JAM", "Jamaica");
1197                codes.put("JEY", "Jersey");
1198                codes.put("JOR", "Jordan");
1199                codes.put("JPN", "Japan");
1200                codes.put("KAZ", "Kazakhstan");
1201                codes.put("KEN", "Kenya");
1202                codes.put("KGZ", "Kyrgyzstan");
1203                codes.put("KHM", "Cambodia");
1204                codes.put("KIR", "Kiribati");
1205                codes.put("KNA", "Saint Kitts and Nevis");
1206                codes.put("KOR", "Korea, Republic of");
1207                codes.put("KWT", "Kuwait");
1208                codes.put("LAO", "Lao People's Democratic Republic");
1209                codes.put("LBN", "Lebanon");
1210                codes.put("LBR", "Liberia");
1211                codes.put("LBY", "Libya");
1212                codes.put("LCA", "Saint Lucia");
1213                codes.put("LIE", "Liechtenstein");
1214                codes.put("LKA", "Sri Lanka");
1215                codes.put("LSO", "Lesotho");
1216                codes.put("LTU", "Lithuania");
1217                codes.put("LUX", "Luxembourg");
1218                codes.put("LVA", "Latvia");
1219                codes.put("MAC", "Macao");
1220                codes.put("MAF", "Saint Martin (French part)");
1221                codes.put("MAR", "Morocco");
1222                codes.put("MCO", "Monaco");
1223                codes.put("MDA", "Moldova, Republic of");
1224                codes.put("MDG", "Madagascar");
1225                codes.put("MDV", "Maldives");
1226                codes.put("MEX", "Mexico");
1227                codes.put("MHL", "Marshall Islands");
1228                codes.put("MKD", "Macedonia, the former Yugoslav Republic of");
1229                codes.put("MLI", "Mali");
1230                codes.put("MLT", "Malta");
1231                codes.put("MMR", "Myanmar");
1232                codes.put("MNE", "Montenegro");
1233                codes.put("MNG", "Mongolia");
1234                codes.put("MNP", "Northern Mariana Islands");
1235                codes.put("MOZ", "Mozambique");
1236                codes.put("MRT", "Mauritania");
1237                codes.put("MSR", "Montserrat");
1238                codes.put("MTQ", "Martinique");
1239                codes.put("MUS", "Mauritius");
1240                codes.put("MWI", "Malawi");
1241                codes.put("MYS", "Malaysia");
1242                codes.put("MYT", "Mayotte");
1243                codes.put("NAM", "Namibia");
1244                codes.put("NCL", "New Caledonia");
1245                codes.put("NER", "Niger");
1246                codes.put("NFK", "Norfolk Island");
1247                codes.put("NGA", "Nigeria");
1248                codes.put("NIC", "Nicaragua");
1249                codes.put("NIU", "Niue");
1250                codes.put("NLD", "Netherlands");
1251                codes.put("NOR", "Norway");
1252                codes.put("NPL", "Nepal");
1253                codes.put("NRU", "Nauru");
1254                codes.put("NZL", "New Zealand");
1255                codes.put("OMN", "Oman");
1256                codes.put("PAK", "Pakistan");
1257                codes.put("PAN", "Panama");
1258                codes.put("PCN", "Pitcairn");
1259                codes.put("PER", "Peru");
1260                codes.put("PHL", "Philippines");
1261                codes.put("PLW", "Palau");
1262                codes.put("PNG", "Papua New Guinea");
1263                codes.put("POL", "Poland");
1264                codes.put("PRI", "Puerto Rico");
1265                codes.put("PRK", "Korea, Democratic People's Republic of");
1266                codes.put("PRT", "Portugal");
1267                codes.put("PRY", "Paraguay");
1268                codes.put("PSE", "Palestine, State of");
1269                codes.put("PYF", "French Polynesia");
1270                codes.put("QAT", "Qatar");
1271                codes.put("REU", "Réunion");
1272                codes.put("ROU", "Romania");
1273                codes.put("RUS", "Russian Federation");
1274                codes.put("RWA", "Rwanda");
1275                codes.put("SAU", "Saudi Arabia");
1276                codes.put("SDN", "Sudan");
1277                codes.put("SEN", "Senegal");
1278                codes.put("SGP", "Singapore");
1279                codes.put("SGS", "South Georgia and the South Sandwich Islands");
1280                codes.put("SHN", "Saint Helena, Ascension and Tristan da Cunha");
1281                codes.put("SJM", "Svalbard and Jan Mayen");
1282                codes.put("SLB", "Solomon Islands");
1283                codes.put("SLE", "Sierra Leone");
1284                codes.put("SLV", "El Salvador");
1285                codes.put("SMR", "San Marino");
1286                codes.put("SOM", "Somalia");
1287                codes.put("SPM", "Saint Pierre and Miquelon");
1288                codes.put("SRB", "Serbia");
1289                codes.put("SSD", "South Sudan");
1290                codes.put("STP", "Sao Tome and Principe");
1291                codes.put("SUR", "Suriname");
1292                codes.put("SVK", "Slovakia");
1293                codes.put("SVN", "Slovenia");
1294                codes.put("SWE", "Sweden");
1295                codes.put("SWZ", "Swaziland");
1296                codes.put("SXM", "Sint Maarten (Dutch part)");
1297                codes.put("SYC", "Seychelles");
1298                codes.put("SYR", "Syrian Arab Republic");
1299                codes.put("TCA", "Turks and Caicos Islands");
1300                codes.put("TCD", "Chad");
1301                codes.put("TGO", "Togo");
1302                codes.put("THA", "Thailand");
1303                codes.put("TJK", "Tajikistan");
1304                codes.put("TKL", "Tokelau");
1305                codes.put("TKM", "Turkmenistan");
1306                codes.put("TLS", "Timor-Leste");
1307                codes.put("TON", "Tonga");
1308                codes.put("TTO", "Trinidad and Tobago");
1309                codes.put("TUN", "Tunisia");
1310                codes.put("TUR", "Turkey");
1311                codes.put("TUV", "Tuvalu");
1312                codes.put("TWN", "Taiwan, Province of China");
1313                codes.put("TZA", "Tanzania, United Republic of");
1314                codes.put("UGA", "Uganda");
1315                codes.put("UKR", "Ukraine");
1316                codes.put("UMI", "United States Minor Outlying Islands");
1317                codes.put("URY", "Uruguay");
1318                codes.put("USA", "United States of America");
1319                codes.put("UZB", "Uzbekistan");
1320                codes.put("VAT", "Holy See");
1321                codes.put("VCT", "Saint Vincent and the Grenadines");
1322                codes.put("VEN", "Venezuela, Bolivarian Republic of");
1323                codes.put("VGB", "Virgin Islands, British");
1324                codes.put("VIR", "Virgin Islands, U.S.");
1325                codes.put("VNM", "Viet Nam");
1326                codes.put("VUT", "Vanuatu");
1327                codes.put("WLF", "Wallis and Futuna");
1328                codes.put("WSM", "Samoa");
1329                codes.put("YEM", "Yemen");
1330                codes.put("ZAF", "South Africa");
1331                codes.put("ZMB", "Zambia");
1332                codes.put("ZWE", "Zimbabwe");
1333                return codes;
1334        }
1335
1336}