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