001package org.hl7.fhir.dstu3.elementmodel; 002 003/*- 004 * #%L 005 * org.hl7.fhir.dstu3 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; 027 028import org.hl7.fhir.dstu3.context.IWorkerContext; 029import org.hl7.fhir.dstu3.formats.IParser.OutputStyle; 030import org.hl7.fhir.dstu3.model.StructureDefinition; 031import org.hl7.fhir.exceptions.DefinitionException; 032import org.hl7.fhir.exceptions.FHIRException; 033import org.hl7.fhir.exceptions.FHIRFormatError; 034 035public class Manager { 036 037 public enum FhirFormat { XML, JSON, JSONLD, TURTLE, TEXT, VBAR; 038 039 public String getExtension() { 040 switch (this) { 041 case JSON: 042 return "json"; 043 case JSONLD: 044 return "ld.json"; 045 case TURTLE: 046 return "ttl"; 047 case XML: 048 return "xml"; 049 case TEXT: 050 return "txt"; 051 case VBAR: 052 return "hl7"; 053 } 054 return null; 055 } 056 } 057 058 public static Element parse(IWorkerContext context, InputStream source, FhirFormat inputFormat) throws FHIRFormatError, DefinitionException, IOException, FHIRException { 059 return makeParser(context, inputFormat).parse(source); 060 } 061 062 public static void compose(IWorkerContext context, Element e, OutputStream destination, FhirFormat outputFormat, OutputStyle style, String base) throws FHIRException, IOException { 063 makeParser(context, outputFormat).compose(e, destination, style, base); 064 } 065 066 public static ParserBase makeParser(IWorkerContext context, FhirFormat format) { 067 switch (format) { 068 case JSON : return new JsonParser(context); 069 case JSONLD : return new JsonLDParser(context); 070 case XML : return new XmlParser(context); 071 case TURTLE : return new TurtleParser(context); 072 case VBAR : return new VerticalBarParser(context); 073 case TEXT : throw new Error("Programming logic error: do not call makeParser for a text resource"); 074 } 075 return null; 076 } 077 078 public static Element build(IWorkerContext context, StructureDefinition sd) { 079 Property p = new Property(context, sd.getSnapshot().getElementFirstRep(), sd); 080 Element e = new Element(null, p); 081 return e; 082 } 083 084}