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