001package org.hl7.fhir.r4.elementmodel; 002 003import java.io.IOException; 004import java.io.InputStream; 005import java.io.OutputStream; 006 007import org.hl7.fhir.r4.context.IWorkerContext; 008import org.hl7.fhir.r4.formats.IParser.OutputStyle; 009import org.hl7.fhir.r4.model.StructureDefinition; 010import org.hl7.fhir.exceptions.DefinitionException; 011import org.hl7.fhir.exceptions.FHIRException; 012import org.hl7.fhir.exceptions.FHIRFormatError; 013 014public class Manager { 015 016 public enum FhirFormat { XML, JSON, TURTLE, TEXT, VBAR; 017 018 public String getExtension() { 019 switch (this) { 020 case JSON: 021 return "json"; 022 case TURTLE: 023 return "ttl"; 024 case XML: 025 return "xml"; 026 case TEXT: 027 return "txt"; 028 case VBAR: 029 return "hl7"; 030 } 031 return null; 032 } 033 } 034 035 public static Element parse(IWorkerContext context, InputStream source, FhirFormat inputFormat) throws FHIRFormatError, DefinitionException, IOException, FHIRException { 036 return makeParser(context, inputFormat).parse(source); 037 } 038 039 public static void compose(IWorkerContext context, Element e, OutputStream destination, FhirFormat outputFormat, OutputStyle style, String base) throws FHIRException, IOException { 040 makeParser(context, outputFormat).compose(e, destination, style, base); 041 } 042 043 public static ParserBase makeParser(IWorkerContext context, FhirFormat format) { 044 switch (format) { 045 case JSON : return new JsonParser(context); 046 case XML : return new XmlParser(context); 047 case TURTLE : return new TurtleParser(context); 048 case VBAR : return new VerticalBarParser(context); 049 case TEXT : throw new Error("Programming logic error: do not call makeParser for a text resource"); 050 } 051 return null; 052 } 053 054 public static Element build(IWorkerContext context, StructureDefinition sd) { 055 Property p = new Property(context, sd.getSnapshot().getElementFirstRep(), sd); 056 Element e = new Element(null, p); 057 return e; 058 } 059 060}