001package org.hl7.fhir.r4.conformance;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r4
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import java.io.File;
025import java.io.FileOutputStream;
026import java.io.IOException;
027import java.io.OutputStreamWriter;
028
029import org.hl7.fhir.exceptions.FHIRException;
030import org.hl7.fhir.r4.context.IWorkerContext;
031import org.hl7.fhir.r4.model.StructureDefinition;
032import org.hl7.fhir.utilities.Utilities;
033
034public class ConstraintJavaGenerator {
035
036  private IWorkerContext context; // for doing expansions
037  private String version; // for getting includes correct
038  private String folder; //dest dir where the profile will be generated into
039  private String packageName;
040  
041  public ConstraintJavaGenerator(IWorkerContext context, String version, String folder, String packageName) {
042    super();
043    this.context = context;
044    this.version = version;
045    this.folder = folder;
046    this.packageName = packageName;
047  }
048
049  public String generate(StructureDefinition sd) throws FHIRException, IOException {
050    String name = sd.hasName() ? Utilities.titleize(sd.getName().replace(".", "").replace("-", "").replace("\"", "")).replace(" ", "") : "";
051    if (!Utilities.nmtokenize(name).equals(name)) {
052      System.out.println("Cannot generate Java code for profile "+sd.getUrl()+" because the name \""+name+"\" is not a valid Java class name");
053      return null;
054    }
055    File destFile = new File(Utilities.path(folder, name+".java"));
056    OutputStreamWriter dest = new OutputStreamWriter(new FileOutputStream(destFile), "UTF-8");
057    
058    dest.write("package "+packageName+";\r\n");
059    dest.write("\r\n");
060    dest.write("import org.hl7.fhir.r4.model.ProfilingWrapper;\r\n");
061    dest.write("\r\n");
062    dest.write("public class "+name+" {\r\n");
063    dest.write("\r\n");
064    
065    dest.write("}\r\n");
066    dest.flush();
067    dest.close();
068    return destFile.getAbsolutePath();
069  }
070  
071}