001package ca.uhn.fhir.util.rdf; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2019 University Health Network 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 023import org.apache.commons.io.input.ReaderInputStream; 024import org.apache.commons.io.output.WriterOutputStream; 025import org.apache.jena.graph.Triple; 026import org.apache.jena.rdf.model.Model; 027import org.apache.jena.rdf.model.ModelFactory; 028import org.apache.jena.riot.Lang; 029import org.apache.jena.riot.system.StreamRDF; 030import org.apache.jena.riot.system.StreamRDFWriter; 031 032import java.io.*; 033import java.nio.charset.Charset; 034import java.util.*; 035 036public class RDFUtil { 037 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(RDFUtil.class); 038 private static final Map<String, Integer> VALID_ENTITY_NAMES; 039 040 static { 041 HashMap<String, Integer> validEntityNames = new HashMap<>(1448); 042 VALID_ENTITY_NAMES = Collections.unmodifiableMap(validEntityNames); 043 } 044 045 public static StreamRDF createRDFWriter(final Writer writer, final Lang lang) { 046 WriterOutputStream wos = new WriterOutputStream(writer, Charset.defaultCharset()); 047 return StreamRDFWriter.getWriterStream(wos, lang); 048 } 049 050 public static StreamRDF createRDFReader(final Reader reader, final Lang lang) { 051 ReaderInputStream ris = new ReaderInputStream(reader, Charset.defaultCharset()); 052 return StreamRDFWriter.getWriterStream(null, lang); 053 } 054 055 public static Triple triple(String tripleAsTurtle) { 056 Model m = ModelFactory.createDefaultModel(); 057 m.read(new StringReader(tripleAsTurtle), "urn:x-base:", "TURTLE"); 058 return m.listStatements().next().asTriple(); 059 } 060 061}