001package org.hl7.fhir.dstu3.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.dstu3.model.CodeType;
039import org.hl7.fhir.dstu3.model.CodeableConcept;
040import org.hl7.fhir.dstu3.model.Coding;
041import org.hl7.fhir.dstu3.model.Enumeration;
042import org.hl7.fhir.dstu3.model.Resource;
043import org.hl7.fhir.dstu3.model.Type;
044import org.hl7.fhir.dstu3.utils.formats.Turtle;
045import org.hl7.fhir.dstu3.utils.formats.Turtle.Complex;
046import org.hl7.fhir.dstu3.utils.formats.Turtle.Section;
047import org.hl7.fhir.dstu3.utils.formats.Turtle.Subject;
048import org.hl7.fhir.exceptions.FHIRFormatError;
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        private String url;
071
072        @Override
073        public void compose(OutputStream stream, Resource resource) throws IOException {
074          Turtle ttl = new Turtle();
075                //      ttl.setFormat(FFormat);
076                ttl.prefix("fhir", "http://hl7.org/fhir/");
077                ttl.prefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
078                Section section = ttl.section("resource");
079                Subject subject;
080                if (url != null) 
081                        subject = section.triple("<"+url+">", "a", "fhir:"+resource.getResourceType().toString());
082                else
083                        subject = section.triple("[]", "a", "fhir:"+resource.getResourceType().toString());
084
085                composeResource(subject, resource);
086                try {
087                        ttl.commit(stream, false);
088                } catch (Exception e) {
089                        throw new IOException(e); 
090                }
091        }
092
093        @Override
094        public void compose(OutputStream stream, Type 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 composeXhtml(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}