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