001package org.hl7.fhir.r5.openapi;
002
003import org.hl7.fhir.utilities.MarkedToMoveToAdjunctPackage;
004
005/*
006  Copyright (c) 2011+, HL7, Inc.
007  All rights reserved.
008  
009  Redistribution and use in source and binary forms, with or without modification, 
010  are permitted provided that the following conditions are met:
011    
012   * Redistributions of source code must retain the above copyright notice, this 
013     list of conditions and the following disclaimer.
014   * Redistributions in binary form must reproduce the above copyright notice, 
015     this list of conditions and the following disclaimer in the documentation 
016     and/or other materials provided with the distribution.
017   * Neither the name of HL7 nor the names of its contributors may be used to 
018     endorse or promote products derived from this software without specific 
019     prior written permission.
020  
021  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
022  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
023  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
024  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
025  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
026  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
027  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
028  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
029  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
030  POSSIBILITY OF SUCH DAMAGE.
031  
032 */
033
034
035
036import com.google.gson.JsonArray;
037import com.google.gson.JsonElement;
038import com.google.gson.JsonObject;
039
040@MarkedToMoveToAdjunctPackage
041public class BaseWriter {
042
043  protected JsonObject object;
044
045  public BaseWriter(JsonObject object) {
046    super();
047    this.object = object;
048  }
049
050  protected JsonObject ensureObject(String name) {
051    JsonObject child = object.getAsJsonObject(name);
052    if (child == null) {
053      child = new JsonObject();
054      object.add(name, child);
055    }
056    return child;
057  }
058  
059  protected JsonObject ensureArrayObject(String arrayName, String propertyName, String value) {
060    JsonArray arr = forceArray(arrayName);
061    for (JsonElement e : arr) {
062      String s = e.getAsJsonObject().get(propertyName).getAsString();
063      if (value.equals(s))
064        return e.getAsJsonObject();
065    }
066    JsonObject e = new JsonObject();
067    arr.add(e);
068    e.addProperty(propertyName, value);
069    return e;
070  }
071
072  protected JsonArray forceArray(String arrayName) {
073    JsonArray arr = object.getAsJsonArray(arrayName);
074    if (arr == null) {
075      arr = new JsonArray();
076      object.add(arrayName, arr);
077    }
078    return arr;
079  }
080  
081  protected JsonObject forceArrayObject(String arrayName) {
082    JsonArray arr = object.getAsJsonArray(arrayName);
083    if (arr == null) {
084      arr = new JsonArray();
085      object.add(arrayName, arr);
086    }
087    JsonObject obj = new JsonObject();
088    arr.add(obj);
089    return obj;
090  }
091  
092
093  protected JsonObject ensureMapObject(String mapName, String value) {
094    JsonObject map = object.getAsJsonObject(mapName);
095    if (map == null) {
096      map = new JsonObject();
097      object.add(mapName, map);
098    }
099    if (map.has(value))
100      return map.getAsJsonObject(value);
101    JsonObject e = new JsonObject();
102    map.add(value, e);
103    return e;
104  }
105  
106
107  protected JsonObject ensureMapObject(String value) {
108    if (object.has(value))
109      return object.getAsJsonObject(value);
110    JsonObject e = new JsonObject();
111    object.add(value, e);
112    return e;
113  }
114  
115}