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