001package org.hl7.fhir.r5.openapi; 002 003/*- 004 * #%L 005 * org.hl7.fhir.r5 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.IOException; 025import java.io.InputStream; 026import java.io.OutputStream; 027import java.io.OutputStreamWriter; 028 029import org.hl7.fhir.utilities.TextFile; 030 031import com.google.gson.Gson; 032import com.google.gson.GsonBuilder; 033import com.google.gson.JsonObject; 034import com.google.gson.JsonParser; 035import com.google.gson.JsonSyntaxException; 036 037public class Writer extends BaseWriter { 038 039 private OutputStream stream; 040 041 public Writer(OutputStream stream) { 042 super( new JsonObject()); 043 this.stream = stream; 044 object.addProperty("openapi", "3.0.2"); 045 } 046 047 public Writer(OutputStream stream, InputStream template) throws JsonSyntaxException, IOException { 048 super(parse(template)); 049 this.stream = stream; 050 object.addProperty("openapi", "3.0.2"); 051 } 052 053 private static JsonObject parse(InputStream template) throws JsonSyntaxException, IOException { 054 JsonParser parser = new com.google.gson.JsonParser(); 055 return parser.parse(TextFile.streamToString(template)).getAsJsonObject(); 056 } 057 058 public void commit() throws IOException { 059 Gson gson = new GsonBuilder().setPrettyPrinting().create(); 060 String json = gson.toJson(object); 061 OutputStreamWriter sw = new OutputStreamWriter(stream, "UTF-8"); 062 sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter 063 sw.write(json); 064 sw.flush(); 065 sw.close(); 066 } 067 068 public InfoWriter info() { 069 return new InfoWriter(ensureObject("info")); 070 } 071 072 public PathItemWriter path(String path) { 073 return new PathItemWriter(ensureMapObject("paths", path)); 074 } 075 076 public Writer pathRef(String path, String url) { 077 ensureMapObject("paths", path).addProperty("$ref", url); 078 return this; 079 } 080 081 082 public ServerWriter server(String url) { 083 return new ServerWriter(ensureArrayObject("servers", "url", url)); 084 } 085 086 087 public TagWriter tag(String name) { 088 return new TagWriter(ensureArrayObject("tags", "name", name)); 089 } 090 091 public ExternalDocsWriter externalDocs() { 092 return new ExternalDocsWriter(ensureObject("externalDocs")); 093 } 094 095 096 public ComponentsWriter components() { 097 return new ComponentsWriter(ensureObject("components")); 098 } 099 100}