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