001package org.hl7.fhir.utilities.graphql; 002 003/*- 004 * #%L 005 * org.hl7.fhir.utilities 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.util.ArrayList; 025import java.util.List; 026 027import com.google.gson.JsonArray; 028import com.google.gson.JsonElement; 029import com.google.gson.JsonObject; 030import com.google.gson.JsonPrimitive; 031 032public class Argument { 033 public enum ArgumentListStatus {NOT_SPECIFIED, SINGLETON, REPEATING} 034 String name; 035 private List<Value> values = new ArrayList<Value>(); 036 ArgumentListStatus listStatus; 037 public Argument() { 038 super(); 039 } 040 public Argument(String name, Value value) { 041 super(); 042 this.name = name; 043 this.values.add(value); 044 } 045 public Argument(String name, JsonElement json) throws EGraphQLException { 046 super(); 047 this.name = name; 048 valuesFromNode(json); 049 } 050 public String getName() { 051 return name; 052 } 053 public void setName(String name) { 054 this.name = name; 055 } 056 public ArgumentListStatus getListStatus() { 057 return listStatus; 058 } 059 public void setListStatus(ArgumentListStatus listStatus) { 060 this.listStatus = listStatus; 061 } 062 public List<Value> getValues() { 063 return values; 064 } 065 public void addValue(Value value){ 066 values.add(value); 067 } 068 069 public boolean hasValue(String value) { 070 for (Value v : values ) 071 if (v.isValue(value)) 072 return true; 073 return false; 074 } 075 076 public void valuesFromNode(JsonElement json) throws EGraphQLException { 077 if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isString()) 078 values.add(new StringValue(((JsonPrimitive)json).getAsString())); 079 else if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isNumber()) 080 values.add(new NumberValue(((JsonPrimitive)json).getAsString())); 081 else if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isBoolean()) 082 values.add(new NameValue(((JsonPrimitive)json).getAsBoolean())); 083 else if (json instanceof JsonObject) 084 values.add(new ObjectValue((JsonObject) json)); 085 else if (json instanceof JsonArray) { 086 for (JsonElement v : (JsonArray) json) 087 valuesFromNode(v); 088 } else 089 throw new EGraphQLException("Unexpected JSON type for \""+name+"\": "+json.getClass().getName()); 090 } 091 092 public void write(StringBuilder b, int indent) throws EGraphQLException, EGraphEngine { 093 b.append("\""); 094 for (char ch : name.toCharArray()) { 095 if (ch == '"') b.append("\""); 096 else if (ch == '\\') b.append("\\"); 097 else if (ch == '\r') b.append("\\r"); 098 else if (ch == '\n') b.append("\\n"); 099 else if (ch == '\t') b.append("\\t"); 100 else if (ch < 32) 101 b.append("\\u"+Integer.toHexString(ch)); 102 else 103 b.append(ch); 104 } 105 b.append("\":"); 106 if (listStatus == ArgumentListStatus.REPEATING) { 107 b.append("["); 108 boolean first = true; 109 for (Value v : values) { 110 if (first) first = false; else b.append(","); 111 v.write(b, indent); 112 } 113 b.append("]"); 114 } else { 115 if (values.size() > 1) 116 throw new EGraphQLException("Internal error: non list \""+name+"\" has "+Integer.toString(values.size())+" values"); 117 if (values.size() == 0) 118 b.append("null"); 119 else 120 values.get(0).write(b, indent); 121 } 122 } 123}