001package org.hl7.fhir.utilities.graphql;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import com.google.gson.JsonArray;
007import com.google.gson.JsonElement;
008import com.google.gson.JsonObject;
009import com.google.gson.JsonPrimitive;
010
011public class Argument {
012  public enum ArgumentListStatus {NOT_SPECIFIED, SINGLETON, REPEATING}
013  String name;
014  private List<Value> values = new ArrayList<Value>();
015  ArgumentListStatus listStatus;
016  public Argument() {
017    super();
018  }
019  public Argument(String name, Value value) {
020    super();
021    this.name = name;
022    this.values.add(value);
023  }
024  public Argument(String name, JsonElement json) throws EGraphQLException {
025    super();
026    this.name = name;
027    valuesFromNode(json);
028  }
029  public String getName() {
030    return name;
031  }
032  public void setName(String name) {
033    this.name = name;
034  }
035  public ArgumentListStatus getListStatus() {
036    return listStatus;
037  }
038  public void setListStatus(ArgumentListStatus listStatus) {
039    this.listStatus = listStatus;
040  }
041  public List<Value> getValues() {
042    return values;
043  }
044  public void addValue(Value value){ 
045    values.add(value);
046  }
047
048  public boolean hasValue(String value) {
049    for (Value v : values ) 
050      if (v.isValue(value)) 
051        return true;
052    return false;
053  }
054
055  public void valuesFromNode(JsonElement json) throws EGraphQLException {
056    if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isString())
057      values.add(new StringValue(((JsonPrimitive)json).getAsString()));
058    else if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isNumber())
059      values.add(new NumberValue(((JsonPrimitive)json).getAsString()));
060    else if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isBoolean())
061      values.add(new NameValue(((JsonPrimitive)json).getAsBoolean()));
062    else if (json instanceof JsonObject)
063      values.add(new ObjectValue((JsonObject) json));
064    else if (json instanceof JsonArray) {
065      for (JsonElement v : (JsonArray) json)
066        valuesFromNode(v);
067    } else
068      throw new EGraphQLException("Unexpected JSON type for \""+name+"\": "+json.getClass().getName());
069  }
070
071  public void write(StringBuilder b, int indent) throws EGraphQLException, EGraphEngine {
072    b.append("\"");
073    for (char ch : name.toCharArray()) {
074      if (ch == '"') b.append("\"");
075      else if (ch == '\\') b.append("\\");
076      else if (ch == '\r') b.append("\\r");
077      else if (ch == '\n') b.append("\\n");
078      else if (ch == '\t') b.append("\\t");
079      else if (ch < 32)
080        b.append("\\u"+Integer.toHexString(ch));
081      else
082        b.append(ch);
083    }
084    b.append("\":");
085    if (listStatus == ArgumentListStatus.REPEATING) {
086      b.append("[");
087      boolean first = true;
088      for (Value v : values) {
089        if (first) first = false; else b.append(",");
090        v.write(b, indent);
091      }
092      b.append("]");
093    } else {
094      if (values.size() > 1)
095        throw new EGraphQLException("Internal error: non list \""+name+"\" has "+Integer.toString(values.size())+" values");
096      if (values.size() == 0)
097        b.append("null");
098      else
099        values.get(0).write(b, indent);
100    }
101  }
102}