001package org.hl7.fhir.r5.utils.structuremap;
002
003import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
004import org.hl7.fhir.utilities.StringPair;
005import org.hl7.fhir.utilities.xhtml.XhtmlNode;
006
007import java.util.ArrayList;
008import java.util.HashMap;
009import java.util.List;
010import java.util.Map;
011
012public class TargetWriter {
013  private Map<String, String> newResources = new HashMap<String, String>();
014  private List<StringPair> assignments = new ArrayList<StringPair>();
015  private List<StringPair> keyProps = new ArrayList<StringPair>();
016  private CommaSeparatedStringBuilder txt = new CommaSeparatedStringBuilder();
017
018  public void newResource(String var, String name) {
019    newResources.put(var, name);
020    txt.append("new " + name);
021  }
022
023  public void valueAssignment(String context, String desc) {
024    assignments.add(new StringPair(context, desc));
025    txt.append(desc);
026  }
027
028  public void keyAssignment(String context, String desc) {
029    keyProps.add(new StringPair(context, desc));
030    txt.append(desc);
031  }
032
033  public void commit(XhtmlNode xt) {
034    if (newResources.size() == 1 && assignments.size() == 1 && newResources.containsKey(assignments.get(0).getName()) && keyProps.size() == 1 && newResources.containsKey(keyProps.get(0).getName())) {
035      xt.addText("new " + assignments.get(0).getValue() + " (" + keyProps.get(0).getValue().substring(keyProps.get(0).getValue().indexOf(".") + 1) + ")");
036    } else if (newResources.size() == 1 && assignments.size() == 1 && newResources.containsKey(assignments.get(0).getName()) && keyProps.size() == 0) {
037      xt.addText("new " + assignments.get(0).getValue());
038    } else {
039      xt.addText(txt.toString());
040    }
041  }
042}