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