Search in sources :

Example 1 with NamedElement

use of org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement in project org.hl7.fhir.core by hapifhir.

the class JsonParser method parse.

@Override
public List<NamedElement> parse(InputStream stream) throws IOException, FHIRException {
    // if we're parsing at this point, then we're going to use the custom parser
    List<NamedElement> res = new ArrayList<>();
    map = new IdentityHashMap<JsonElement, LocationData>();
    String source = TextFile.streamToString(stream);
    if (policy == ValidationPolicy.EVERYTHING) {
        JsonObject obj = null;
        try {
            obj = JsonTrackingParser.parse(source, map, false, allowComments);
        } catch (Exception e) {
            logError(-1, -1, context.formatMessage(I18nConstants.DOCUMENT), IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_JSON_, e.getMessage()), IssueSeverity.FATAL);
            return null;
        }
        assert (map.containsKey(obj));
        Element e = parse(obj);
        if (e != null) {
            res.add(new NamedElement(null, e));
        }
    } else {
        // (JsonObject) new com.google.gson.JsonParser().parse(source);
        JsonObject obj = JsonTrackingParser.parse(source, null);
        // assert (map.containsKey(obj));
        Element e = parse(obj);
        if (e != null) {
            res.add(new NamedElement(null, e));
        }
    }
    return res;
}
Also used : LocationData(org.hl7.fhir.utilities.json.JsonTrackingParser.LocationData) JsonElement(com.google.gson.JsonElement) JsonElement(com.google.gson.JsonElement) SpecialElement(org.hl7.fhir.r4b.elementmodel.Element.SpecialElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 2 with NamedElement

use of org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement in project org.hl7.fhir.core by hapifhir.

the class SHCParser method parse.

public List<NamedElement> parse(InputStream stream) throws IOException, FHIRFormatError, DefinitionException, FHIRException {
    List<NamedElement> res = new ArrayList<>();
    String src = TextFile.streamToString(stream).trim();
    List<String> list = new ArrayList<>();
    String pfx = null;
    if (src.startsWith("{")) {
        JsonObject json = JsonTrackingParser.parseJson(src);
        if (checkProperty(json, "$", "verifiableCredential", true, "Array")) {
            pfx = "verifiableCredential";
            JsonArray arr = json.getAsJsonArray("verifiableCredential");
            int i = 0;
            for (JsonElement e : arr) {
                if (!(e instanceof JsonPrimitive)) {
                    logError(line(e), col(e), "$.verifiableCredential[" + i + "]", IssueType.STRUCTURE, "Wrong Property verifiableCredential in JSON Payload. Expected : String but found " + JSONUtil.type(e), IssueSeverity.ERROR);
                } else {
                    list.add(e.getAsString());
                }
                i++;
            }
        } else {
            return res;
        }
    } else {
        list.add(src);
    }
    int c = 0;
    for (String ssrc : list) {
        String prefix = pfx == null ? "" : pfx + "[" + Integer.toString(c) + "].";
        c++;
        JWT jwt = null;
        try {
            jwt = decodeJWT(ssrc);
        } catch (Exception e) {
            logError(1, 1, prefix + "JWT", IssueType.INVALID, "Unable to decode JWT token", IssueSeverity.ERROR);
            return res;
        }
        map = jwt.map;
        checkNamedProperties(jwt.getPayload(), prefix + "payload", "iss", "nbf", "vc");
        checkProperty(jwt.getPayload(), prefix + "payload", "iss", true, "String");
        logError(1, 1, prefix + "JWT", IssueType.INFORMATIONAL, "The FHIR Validator does not check the JWT signature " + "(see https://demo-portals.smarthealth.cards/VerifierPortal.html or https://github.com/smart-on-fhir/health-cards-dev-tools) (Issuer = '" + jwt.getPayload().get("iss").getAsString() + "')", IssueSeverity.INFORMATION);
        checkProperty(jwt.getPayload(), prefix + "payload", "nbf", true, "Number");
        JsonObject vc = jwt.getPayload().getAsJsonObject("vc");
        if (vc == null) {
            logError(1, 1, "JWT", IssueType.STRUCTURE, "Unable to find property 'vc' in the payload", IssueSeverity.ERROR);
            return res;
        }
        String path = prefix + "payload.vc";
        checkNamedProperties(vc, path, "type", "credentialSubject");
        if (!checkProperty(vc, path, "type", true, "Array")) {
            return res;
        }
        JsonArray type = vc.getAsJsonArray("type");
        int i = 0;
        for (JsonElement e : type) {
            if (!(e instanceof JsonPrimitive)) {
                logError(line(e), col(e), path + ".type[" + i + "]", IssueType.STRUCTURE, "Wrong Property Type in JSON Payload. Expected : String but found " + JSONUtil.type(e), IssueSeverity.ERROR);
            } else {
                types.add(e.getAsString());
            }
            i++;
        }
        if (!types.contains("https://smarthealth.cards#health-card")) {
            logError(line(vc), col(vc), path, IssueType.STRUCTURE, "Card does not claim to be of type https://smarthealth.cards#health-card, cannot validate", IssueSeverity.ERROR);
            return res;
        }
        if (!checkProperty(vc, path, "credentialSubject", true, "Object")) {
            return res;
        }
        JsonObject cs = vc.getAsJsonObject("credentialSubject");
        path = path + ".credentialSubject";
        if (!checkProperty(cs, path, "fhirVersion", true, "String")) {
            return res;
        }
        JsonElement fv = cs.get("fhirVersion");
        if (!VersionUtilities.versionsCompatible(context.getVersion(), fv.getAsString())) {
            logError(line(fv), col(fv), path + ".fhirVersion", IssueType.STRUCTURE, "Card claims to be of version " + fv.getAsString() + ", cannot be validated against version " + context.getVersion(), IssueSeverity.ERROR);
            return res;
        }
        if (!checkProperty(cs, path, "fhirBundle", true, "Object")) {
            return res;
        }
        // ok. all checks passed, we can now validate the bundle
        Element e = jsonParser.parse(cs.getAsJsonObject("fhirBundle"), map);
        if (e != null) {
            res.add(new NamedElement(path, e));
        }
    }
    return res;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement)

Example 3 with NamedElement

use of org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement in project org.hl7.fhir.core by hapifhir.

the class TurtleParser method parse.

@Override
public List<NamedElement> parse(InputStream input) throws IOException, FHIRException {
    List<NamedElement> res = new ArrayList<>();
    Turtle src = new Turtle();
    if (policy == ValidationPolicy.EVERYTHING) {
        try {
            src.parse(TextFile.streamToString(input));
        } catch (Exception e) {
            logError(-1, -1, "(document)", IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_TURTLE_, e.getMessage()), IssueSeverity.FATAL);
            return null;
        }
        Element e = parse(src);
        if (e != null) {
            res.add(new NamedElement(null, e));
        }
    } else {
        src.parse(TextFile.streamToString(input));
        Element e = parse(src);
        if (e != null) {
            res.add(new NamedElement(null, e));
        }
    }
    return res;
}
Also used : Turtle(org.hl7.fhir.utilities.turtle.Turtle) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) ArrayList(java.util.ArrayList) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 4 with NamedElement

use of org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement in project org.hl7.fhir.core by hapifhir.

the class JsonParser method parse.

@Override
public List<NamedElement> parse(InputStream stream) throws IOException, FHIRException {
    // if we're parsing at this point, then we're going to use the custom parser
    List<NamedElement> res = new ArrayList<>();
    map = new IdentityHashMap<JsonElement, LocationData>();
    String source = TextFile.streamToString(stream);
    if (policy == ValidationPolicy.EVERYTHING) {
        JsonObject obj = null;
        try {
            obj = JsonTrackingParser.parse(source, map, false, allowComments);
        } catch (Exception e) {
            logError(-1, -1, context.formatMessage(I18nConstants.DOCUMENT), IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_JSON_, e.getMessage()), IssueSeverity.FATAL);
            return null;
        }
        assert (map.containsKey(obj));
        Element e = parse(obj);
        if (e != null) {
            res.add(new NamedElement(null, e));
        }
    } else {
        // (JsonObject) new com.google.gson.JsonParser().parse(source);
        JsonObject obj = JsonTrackingParser.parse(source, null);
        // assert (map.containsKey(obj));
        Element e = parse(obj);
        if (e != null) {
            res.add(new NamedElement(null, e));
        }
    }
    return res;
}
Also used : LocationData(org.hl7.fhir.utilities.json.JsonTrackingParser.LocationData) JsonElement(com.google.gson.JsonElement) JsonElement(com.google.gson.JsonElement) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 5 with NamedElement

use of org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement in project org.hl7.fhir.core by hapifhir.

the class InstanceValidator method validate.

@Override
public org.hl7.fhir.r5.elementmodel.Element validate(Object appContext, List<ValidationMessage> errors, InputStream stream, FhirFormat format, List<StructureDefinition> profiles) throws FHIRException {
    ParserBase parser = Manager.makeParser(context, format);
    if (parser instanceof XmlParser)
        ((XmlParser) parser).setAllowXsiLocation(allowXsiLocation);
    parser.setupValidation(ValidationPolicy.EVERYTHING, errors);
    long t = System.nanoTime();
    List<NamedElement> list = null;
    try {
        list = parser.parse(stream);
    } catch (IOException e1) {
        throw new FHIRException(e1);
    }
    timeTracker.load(t);
    if (list != null && !list.isEmpty()) {
        String url = parser.getImpliedProfile();
        if (url != null) {
            StructureDefinition sd = context.fetchResource(StructureDefinition.class, url);
            if (sd == null) {
                rule(errors, IssueType.NOTFOUND, "Payload", false, "Implied profile " + url + " not known to validator");
            } else {
                profiles.add(sd);
            }
        }
        for (NamedElement ne : list) {
            validate(appContext, errors, ne.getName(), ne.getElement(), profiles);
        }
    }
    // todo: this is broken, but fixing it really complicates things elsewhere, so we do this for now
    return (list == null || list.isEmpty()) ? null : list.get(0).getElement();
}
Also used : XmlParser(org.hl7.fhir.r5.elementmodel.XmlParser) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ParserBase(org.hl7.fhir.r5.elementmodel.ParserBase) IOException(java.io.IOException) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)11 ArrayList (java.util.ArrayList)10 IOException (java.io.IOException)9 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)5 JsonElement (com.google.gson.JsonElement)4 JsonObject (com.google.gson.JsonObject)4 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)4 NamedElement (org.hl7.fhir.r4b.elementmodel.ParserBase.NamedElement)4 SpecialElement (org.hl7.fhir.r4b.elementmodel.Element.SpecialElement)3 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)3 JsonArray (com.google.gson.JsonArray)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataFormatException (java.util.zip.DataFormatException)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 SAXParser (javax.xml.parsers.SAXParser)2 SAXParserFactory (javax.xml.parsers.SAXParserFactory)2