001package org.hl7.fhir.convertors.misc;
002
003import org.hl7.fhir.r5.formats.XmlParser;
004import org.hl7.fhir.r5.model.ListResource;
005import org.hl7.fhir.r5.model.Reference;
006import org.hl7.fhir.utilities.json.model.JsonObject;
007import org.hl7.fhir.utilities.json.parser.JsonParser;
008
009import java.io.File;
010import java.io.FileInputStream;
011import java.io.IOException;
012
013public class R5ExampleFinder {
014
015  public static void main(String... args) throws IOException {
016    new R5ExampleFinder().execute(new File("/Users/grahamegrieve/work/r5/source"));
017  }
018
019  private void execute(File file) throws IOException {
020    JsonObject json = new JsonObject();
021    exec(json, file);
022    JsonParser.compose(json, new File("/Users/grahamegrieve/temp/r5-examples.json"), true);
023  }
024
025  private void exec(JsonObject json, File file) {
026    for (File f : file.listFiles()) {
027      if (f.isDirectory()) {
028        exec(json, f);
029      } else if (f.getName().startsWith("list-")) {
030        try {
031          ListResource r = (ListResource) new XmlParser().parse(new FileInputStream(f));
032          for (ListResource.ListResourceEntryComponent entry : r.getEntry()) {
033            Reference item = entry.getItem();
034            if (item.hasReference() && item.hasDisplay() && entry.hasExtension("http://hl7.org/fhir/build/StructureDefinition/title")) {
035              JsonObject e = new JsonObject();
036              json.add(item.getReference(), e);
037              e.add("name", item.getReference());
038              e.add("path", entry.getExtensionString("http://hl7.org/fhir/build/StructureDefinition/title"));
039            }
040          }
041        } catch (Exception e) {
042          // hothing
043        }
044      }
045    }
046  }
047}