001package org.hl7.fhir.r4.conformance;
002
003import java.io.File;
004import java.io.FileOutputStream;
005import java.io.IOException;
006import java.io.OutputStreamWriter;
007
008import org.hl7.fhir.exceptions.FHIRException;
009import org.hl7.fhir.r4.context.IWorkerContext;
010import org.hl7.fhir.r4.model.StructureDefinition;
011import org.hl7.fhir.utilities.Utilities;
012
013public class ConstraintJavaGenerator {
014
015  private IWorkerContext context; // for doing expansions
016  private String version; // for getting includes correct
017  private String folder; //dest dir where the profile will be generated into
018  private String packageName;
019  
020  public ConstraintJavaGenerator(IWorkerContext context, String version, String folder, String packageName) {
021    super();
022    this.context = context;
023    this.version = version;
024    this.folder = folder;
025    this.packageName = packageName;
026  }
027
028  public String generate(StructureDefinition sd) throws FHIRException, IOException {
029    String name = sd.hasName() ? Utilities.titleize(sd.getName().replace(".", "").replace("-", "").replace("\"", "")).replace(" ", "") : "";
030    if (!Utilities.nmtokenize(name).equals(name)) {
031      System.out.println("Cannot generate Java code for profile "+sd.getUrl()+" because the name \""+name+"\" is not a valid Java class name");
032      return null;
033    }
034    File destFile = new File(Utilities.path(folder, name+".java"));
035    OutputStreamWriter dest = new OutputStreamWriter(new FileOutputStream(destFile), "UTF-8");
036    
037    dest.write("package "+packageName+";\r\n");
038    dest.write("\r\n");
039    dest.write("import org.hl7.fhir.r4.model.ProfilingWrapper;\r\n");
040    dest.write("\r\n");
041    dest.write("public class "+name+" {\r\n");
042    dest.write("\r\n");
043    
044    dest.write("}\r\n");
045    dest.flush();
046    dest.close();
047    return destFile.getAbsolutePath();
048  }
049  
050}