001package org.hl7.fhir.validation;
002
003import org.hl7.fhir.r5.context.SimpleWorkerContext;
004import org.hl7.fhir.r5.elementmodel.Manager;
005import org.hl7.fhir.r5.elementmodel.SHCParser;
006import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat;
007import org.hl7.fhir.r5.elementmodel.SHCParser.JWT;
008import org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities;
009import org.hl7.fhir.utilities.TextFile;
010import org.hl7.fhir.utilities.Utilities;
011import org.hl7.fhir.utilities.json.JSONUtil;
012import org.hl7.fhir.utilities.json.JsonTrackingParser;
013
014import com.google.gson.JsonObject;
015
016import java.io.ByteArrayInputStream;
017import java.io.IOException;
018import java.nio.charset.StandardCharsets;
019
020public class ResourceChecker {
021
022//  protected static Manager.FhirFormat checkIsResource(SimpleWorkerContext context, boolean debug, String path) throws IOException {
023//    
024//    if (Utilities.existsInList(ext, "json"))
025//      return Manager.FhirFormat.JSON;
026//    if (Utilities.existsInList(ext, "map"))
027//      return Manager.FhirFormat.TEXT;
028//    if (Utilities.existsInList(ext, "txt"))
029//      return Manager.FhirFormat.TEXT;
030//    if (Utilities.existsInList(ext, "jwt", "jws"))
031//      return Manager.FhirFormat.SHC;
032//
033//    return checkIsResource(context, debug, TextFile.fileToBytes(path), path);
034//  }
035  public static Manager.FhirFormat checkIsResource(SimpleWorkerContext context, boolean debug, byte[] cnt, String filename, boolean guessFromExtension) {
036    System.out.println("   ..Detect format for " + filename);
037    if (cnt.length == 0) {
038      System.out.println("   " + filename+" is empty");
039      return null;
040    }
041    if (guessFromExtension) {
042      String ext = Utilities.getFileExtension(filename);
043      if (Utilities.existsInList(ext, "xml")) {
044        return FhirFormat.XML;            
045      }
046      if (Utilities.existsInList(ext, "ttl")) {
047        return FhirFormat.TURTLE;            
048      }
049      if (Utilities.existsInList(ext, "map")) {
050        return Manager.FhirFormat.TEXT;
051      }
052      if (Utilities.existsInList(ext, "jwt", "jws")) {
053        return Manager.FhirFormat.SHC;
054      }
055      if (Utilities.existsInList(ext, "json")) {
056        // no, we have to look inside, and decide.
057        try {
058          JsonObject json = JsonTrackingParser.parseJson(cnt);
059          if (json.has("verifiableCredential")) {
060            return FhirFormat.SHC;
061          }        
062        } catch (Exception e) {
063        }
064        return FhirFormat.JSON;            
065      }
066      if (Utilities.existsInList(ext, "txt")) {
067        try {
068          String src = TextFile.bytesToString(cnt);
069          if (src.startsWith("shc:/")) {
070            return FhirFormat.SHC;
071          }
072        } catch (Exception e) {
073        }
074        return Manager.FhirFormat.TEXT;
075      }
076    }
077
078    try {
079      Manager.parse(context, new ByteArrayInputStream(cnt), Manager.FhirFormat.JSON);
080      return Manager.FhirFormat.JSON;
081    } catch (Exception e) {
082      if (debug) {
083        System.out.println("Not JSON: " + e.getMessage());
084      }
085    }
086    try {
087      ValidatorUtils.parseXml(cnt);
088      return Manager.FhirFormat.XML;
089    } catch (Exception e) {
090      if (debug) {
091        System.out.println("Not XML: " + e.getMessage());
092      }
093    }
094    try {
095      Manager.parse(context, new ByteArrayInputStream(cnt), Manager.FhirFormat.TURTLE);
096      return Manager.FhirFormat.TURTLE;
097    } catch (Exception e) {
098      if (debug) {
099        System.out.println("Not Turtle: " + e.getMessage());
100      }
101    }
102    try {
103      String s = new String(cnt, StandardCharsets.UTF_8);
104      if (s.startsWith("shc:/")) 
105        s = SHCParser.decodeQRCode(s);
106      JWT jwt = new SHCParser(context).decodeJWT(s);
107      return Manager.FhirFormat.SHC;
108    } catch (Exception e) {
109      if (debug) {
110        System.out.println("Not a smart health card: " + e.getMessage());
111      }
112    }
113    try {
114      new StructureMapUtilities(context, null, null).parse(TextFile.bytesToString(cnt), null);
115      return Manager.FhirFormat.TEXT;
116    } catch (Exception e) {
117      if (debug) {
118        System.out.println("Not Text: " + e.getMessage());
119      }
120    }
121    if (debug)
122      System.out.println("     .. not a resource: " + filename);
123    return null;
124  }
125
126 
127}