org.carewebframework.fhir.validator.FhirValidate.java Source code

Java tutorial

Introduction

Here is the source code for org.carewebframework.fhir.validator.FhirValidate.java

Source

/**
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 * If a copy of the MPL was not distributed with this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */
package org.carewebframework.fhir.validator;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.base.resource.BaseOperationOutcome;
import ca.uhn.fhir.model.base.resource.BaseOperationOutcome.BaseIssue;
import ca.uhn.fhir.model.primitive.UriDt;
import ca.uhn.fhir.rest.client.GenericClient;
import ca.uhn.fhir.rest.server.EncodingEnum;
import ca.uhn.fhir.validation.FhirValidator;
import ca.uhn.fhir.validation.ValidationResult;

import org.apache.commons.lang3.StringUtils;

/**
 * Fhir Resource Validator
 */
public class FhirValidate {

    public static void main(String[] args) throws Exception {
        if (args.length != 4) {
            usage();
            System.exit(1);
        }

        try {
            FhirVersionEnum fhirVersion = FhirVersionEnum.valueOf(args[0].toUpperCase());
            EncodingEnum encoding = EncodingEnum.valueOf(args[1].toUpperCase());
            System.out.printf("\nUsing FHIR version %s, encoding %s\n", fhirVersion.name(), encoding.name());
            FhirContext ctx = new FhirContext(fhirVersion);
            FhirValidator validator = ctx.newValidator();
            validator.setValidateAgainstStandardSchema(true);
            validator.setValidateAgainstStandardSchematron(true);
            GenericClient client = (GenericClient) ctx.newRestfulGenericClient(fixURI(args[2]));
            client.setEncoding(encoding);
            UriDt id = new UriDt(fixURI(args[3]));
            ValidationResult result = null;
            int count = 1;

            if (isResource(id)) {
                System.out.print("Validating resource: " + id.getValue() + "...  ");
                result = validator.validateWithResult(client.read(id));
            } else {
                System.out.print("Validating bundle:   " + id.getValue() + "...  ");
                Bundle bundle = client.search(id);
                count = bundle.getTotalResults().getValue();
                result = validator.validateWithResult(bundle);
            }

            System.out.printf("(%d) ", count);

            if (!result.isSuccessful()) {
                BaseOperationOutcome outcome = result.getOperationOutcome();
                System.out.println("failed:");

                for (BaseIssue issue : outcome.getIssue()) {
                    System.out.println(issue.getDetailsElement().getValue());
                }

                System.exit(2);
            }

            System.out.println("succeeded.");
            System.exit(0);
        } catch (Exception e) {
            System.out.println("failed:");
            System.out.println(e.getMessage());
            System.exit(3);
        }
    }

    private static boolean isResource(UriDt id) {
        return StringUtils.countMatches(id.getValue(), "/") % 2 == 1;
    }

    private static String fixURI(String uri) {
        if (uri.contains("?")) {
            uri = uri.split("\\?")[0];
        }

        while (uri.endsWith("/")) {
            uri = uri.substring(0, uri.length() - 1);
        }

        return uri;
    }

    private static void usage() {
        StringBuilder sb = new StringBuilder();

        for (FhirVersionEnum version : FhirVersionEnum.values()) {
            if (version.isPresentOnClasspath()) {
                sb.append(sb.length() == 0 ? "" : " | ").append(version.name());
            }
        }

        System.out.println("\nUsage:\n");
        System.out.println("  validate fhir-version encoding-type service-root resource-path");
        System.out.println("\nWhere:\n");
        System.out.println("  fhir-version  = " + sb.toString());
        System.out.println("  encoding-type = XML | JSON");
        System.out.println("  service-base  = root URL of FHIR service endpoint.");
        System.out.println("  resource-path = path of request resource.\n");
    }
}