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