001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.jena; 019 020import org.apache.jena.atlas.io.AWriter; 021import org.apache.jena.atlas.io.IO; 022import org.apache.jena.atlas.lib.CharSpace; 023import org.apache.jena.riot.RDFFormat; 024import org.apache.jena.riot.RDFWriterRegistry; 025import org.apache.jena.riot.WriterGraphRIOTFactory; 026import org.apache.jena.riot.system.StreamRDFWriter; 027import org.apache.jena.riot.system.StreamRDFWriterFactory; 028import org.slf4j.Logger; 029import org.springframework.stereotype.Component; 030 031import java.util.Objects; 032 033import static org.slf4j.LoggerFactory.getLogger; 034 035/** 036 * @author awoods 037 * @since 2017/01/03 038 */ 039@Component 040public class RdfWriterHelper { 041 042 private static final Logger LOGGER = getLogger(RdfWriterHelper.class); 043 044 private RdfWriterHelper() { 045 // prevent construction 046 } 047 048 private static StreamRDFWriterFactory streamWriterFactoryBlocks = 049 (output, format) -> new FedoraWriterStreamRDFBlocks(output); 050 051 private static StreamRDFWriterFactory streamWriterFactoryFlat = 052 (output, format) -> new FedoraWriterStreamRDFFlat(output); 053 054 private static StreamRDFWriterFactory streamWriterFactoryTriplesQuads = 055 (output, format) -> { 056 final AWriter w = IO.wrapUTF8(output); 057 return new FedoraWriterStreamRDFPlain(w, CharSpace.UTF8); // N-Quads and N-Triples. 058 }; 059 060 private static StreamRDFWriterFactory streamWriterFactoryTriplesQuadsAscii = 061 (output, format) -> { 062 final AWriter w = IO.wrapUTF8(output); 063 return new FedoraWriterStreamRDFPlain(w, CharSpace.ASCII); // N-Quads and N-Triples. 064 }; 065 066 private static WriterGraphRIOTFactory wgfactory = (serialization) -> 067 { 068 if ( Objects.equals(RDFFormat.TURTLE_PRETTY, serialization) ) { 069 return new FedoraTurtleWriter(); 070 } 071 if ( Objects.equals(RDFFormat.TURTLE_BLOCKS, serialization) ) { 072 return new FedoraTurtleWriterBlocks(); 073 } 074 if ( Objects.equals(RDFFormat.TURTLE_FLAT, serialization) ) { 075 return new FedoraTurtleWriterFlat(); 076 } 077 if ( Objects.equals(RDFFormat.RDFXML_PLAIN, serialization) ) { 078 return new FedoraRDFXMLPlainWriter(); 079 } 080 081 return null ; 082 }; 083 084 085 static { 086 LOGGER.info("Loading JENA 3.1.1 Patched RDF Output Writers"); 087 StreamRDFWriter.register(RDFFormat.TURTLE_BLOCKS, streamWriterFactoryBlocks); 088 StreamRDFWriter.register(RDFFormat.TURTLE_FLAT, streamWriterFactoryFlat); 089 090 StreamRDFWriter.register(RDFFormat.NTRIPLES, streamWriterFactoryTriplesQuads); 091 StreamRDFWriter.register(RDFFormat.NTRIPLES_UTF8, streamWriterFactoryTriplesQuads); 092 StreamRDFWriter.register(RDFFormat.NTRIPLES_ASCII, streamWriterFactoryTriplesQuadsAscii); 093 094 RDFWriterRegistry.register(RDFFormat.TURTLE_PRETTY, wgfactory) ; 095 RDFWriterRegistry.register(RDFFormat.TURTLE_BLOCKS, wgfactory) ; 096 RDFWriterRegistry.register(RDFFormat.TURTLE_FLAT, wgfactory) ; 097 098 RDFWriterRegistry.register(RDFFormat.RDFXML_PLAIN, wgfactory) ; 099 } 100}