001package org.hl7.fhir.r4.formats;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.io.IOException;
035import java.io.InputStream;
036import java.io.OutputStream;
037
038import org.hl7.fhir.exceptions.FHIRFormatError;
039import org.hl7.fhir.r4.model.CodeType;
040import org.hl7.fhir.r4.model.CodeableConcept;
041import org.hl7.fhir.r4.model.Coding;
042import org.hl7.fhir.r4.model.Enumeration;
043import org.hl7.fhir.r4.model.Resource;
044import org.hl7.fhir.r4.model.Type;
045import org.hl7.fhir.r4.utils.formats.Turtle;
046import org.hl7.fhir.r4.utils.formats.Turtle.Complex;
047import org.hl7.fhir.r4.utils.formats.Turtle.Section;
048import org.hl7.fhir.r4.utils.formats.Turtle.Subject;
049import org.hl7.fhir.utilities.xhtml.XhtmlNode;
050
051public abstract class RdfParserBase extends ParserBase implements IParser  {
052
053        protected abstract void composeResource(Complex complex, Resource resource) throws IOException;
054
055        @Override
056        public ParserType getType() {
057                return ParserType.RDF_TURTLE;
058        }
059
060        @Override
061        public Resource parse(InputStream input) throws IOException, FHIRFormatError {
062                throw new Error("Parsing not implemented yet");
063        }
064
065  @Override
066  public Type parseType(InputStream input, String knownType) throws IOException, FHIRFormatError {
067    throw new Error("Parsing not implemented yet");
068  }
069
070  @Override
071  public Type parseAnyType(InputStream input, String knownType) throws IOException, FHIRFormatError {
072    throw new Error("Parsing not implemented yet");
073  }
074
075        private String url;
076
077        @Override
078        public void compose(OutputStream stream, Resource resource) throws IOException {
079          Turtle ttl = new Turtle();
080                //      ttl.setFormat(FFormat);
081                ttl.prefix("fhir", "http://hl7.org/fhir/");
082                ttl.prefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
083                Section section = ttl.section("resource");
084                Subject subject;
085                if (url != null) 
086                        subject = section.triple("<"+url+">", "a", "fhir:"+resource.getResourceType().toString());
087                else
088                        subject = section.triple("[]", "a", "fhir:"+resource.getResourceType().toString());
089
090                composeResource(subject, resource);
091                try {
092                        ttl.commit(stream, false);
093                } catch (Exception e) {
094                        throw new IOException(e); 
095                }
096        }
097
098        @Override
099        public void compose(OutputStream stream, Type type, String rootName) throws IOException {
100                throw new Error("Not supported in RDF");  
101        }
102
103        protected String ttlLiteral(String value) {
104                return "\"" +Turtle.escape(value, true) + "\"";
105        }
106
107        protected void composeXhtml(Complex t, String string, String string2, XhtmlNode div, int i) {
108        }
109
110        protected void decorateCode(Complex t, Enumeration<? extends Enum> value) {
111        }
112
113        protected void decorateCode(Complex t, CodeType value) {
114        }
115
116        protected void decorateCoding(Complex t, Coding element) {
117                if (!element.hasSystem())
118                        return;
119                if ("http://snomed.info/sct".equals(element.getSystem())) {
120                        t.prefix("sct", "http://snomed.info/sct/");
121                        t.predicate("a", "sct:"+element.getCode());
122                } else if ("http://snomed.info/sct".equals(element.getSystem())) {
123                        t.prefix("loinc", "http://loinc.org/rdf#");
124                        t.predicate("a", "loinc:"+element.getCode());
125                }  
126        }
127
128        protected void decorateCodeableConcept(Complex t, CodeableConcept element) {
129                for (Coding c : element.getCoding())
130                        decorateCoding(t, c);
131        }
132
133
134}