001package org.hl7.fhir.r5.formats;
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;
027
028import org.hl7.fhir.exceptions.FHIRFormatError;
029import org.hl7.fhir.r5.model.Base;
030import org.hl7.fhir.r5.model.CodeType;
031import org.hl7.fhir.r5.model.CodeableConcept;
032import org.hl7.fhir.r5.model.Coding;
033import org.hl7.fhir.r5.model.Enumeration;
034import org.hl7.fhir.r5.model.Resource;
035import org.hl7.fhir.r5.model.DataType;
036import org.hl7.fhir.r5.model.Element;
037import org.hl7.fhir.r5.utils.formats.Turtle;
038import org.hl7.fhir.r5.utils.formats.Turtle.Complex;
039import org.hl7.fhir.r5.utils.formats.Turtle.Section;
040import org.hl7.fhir.r5.utils.formats.Turtle.Subject;
041import org.hl7.fhir.utilities.xhtml.XhtmlNode;
042
043public abstract class RdfParserBase extends ParserBase implements IParser  {
044
045        protected abstract void composeResource(Complex complex, Resource resource) throws IOException;
046
047        @Override
048        public ParserType getType() {
049                return ParserType.RDF_TURTLE;
050        }
051
052        @Override
053        public Resource parse(InputStream input) throws IOException, FHIRFormatError {
054                throw new Error("Parsing not implemented yet");
055        }
056
057  @Override
058  public DataType parseType(InputStream input, String knownType) throws IOException, FHIRFormatError {
059    throw new Error("Parsing not implemented yet");
060  }
061
062  @Override
063  public DataType parseAnyType(InputStream input, String knownType) throws IOException, FHIRFormatError {
064    throw new Error("Parsing not implemented yet");
065  }
066
067        private String url;
068
069        @Override
070        public void compose(OutputStream stream, Resource resource) throws IOException {
071          Turtle ttl = new Turtle();
072                //      ttl.setFormat(FFormat);
073                ttl.prefix("fhir", "http://hl7.org/fhir/");
074                ttl.prefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
075                Section section = ttl.section("resource");
076                Subject subject;
077                if (url != null) 
078                        subject = section.triple("<"+url+">", "a", "fhir:"+resource.getResourceType().toString());
079                else
080                        subject = section.triple("[]", "a", "fhir:"+resource.getResourceType().toString());
081
082                composeResource(subject, resource);
083                try {
084                        ttl.commit(stream, false);
085                } catch (Exception e) {
086                        throw new IOException(e); 
087                }
088        }
089
090  protected void composeBase(Complex t, String parentType, String name, Base element, int index) {
091  }
092
093        @Override
094        public void compose(OutputStream stream, DataType type, String rootName) throws IOException {
095                throw new Error("Not supported in RDF");  
096        }
097
098        protected String ttlLiteral(String value) {
099                return "\"" +Turtle.escape(value, true) + "\"";
100        }
101
102        protected void composeXhtmlNode(Complex t, String string, String string2, XhtmlNode div, int i) {
103        }
104
105        protected void decorateCode(Complex t, Enumeration<? extends Enum> value) {
106        }
107
108        protected void decorateCode(Complex t, CodeType value) {
109        }
110
111        protected void decorateCoding(Complex t, Coding element) {
112                if (!element.hasSystem())
113                        return;
114                if ("http://snomed.info/sct".equals(element.getSystem())) {
115                        t.prefix("sct", "http://snomed.info/sct/");
116                        t.predicate("a", "sct:"+element.getCode());
117                } else if ("http://snomed.info/sct".equals(element.getSystem())) {
118                        t.prefix("loinc", "http://loinc.org/rdf#");
119                        t.predicate("a", "loinc:"+element.getCode());
120                }  
121        }
122
123        protected void decorateCodeableConcept(Complex t, CodeableConcept element) {
124                for (Coding c : element.getCoding())
125                        decorateCoding(t, c);
126        }
127
128
129}