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.camel.ldpath; 019 020import static java.util.Collections.singletonList; 021 022import java.io.InputStream; 023import java.io.InputStreamReader; 024import java.util.Collection; 025import java.util.List; 026import java.util.Map; 027 028import com.github.jsonldjava.sesame.SesameJSONLDParserFactory; 029import org.apache.marmotta.ldpath.LDPath; 030import org.apache.marmotta.ldpath.backend.linkeddata.LDCacheBackend; 031import org.apache.marmotta.ldpath.exception.LDPathParseException; 032import org.openrdf.model.Value; 033import org.openrdf.model.impl.URIImpl; 034import org.openrdf.query.resultio.BooleanQueryResultParserRegistry; 035import org.openrdf.query.resultio.TupleQueryResultParserRegistry; 036import org.openrdf.query.resultio.sparqlxml.SPARQLBooleanXMLParserFactory; 037import org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLParserFactory; 038import org.openrdf.rio.RDFParserRegistry; 039import org.openrdf.rio.n3.N3ParserFactory; 040import org.openrdf.rio.ntriples.NTriplesParserFactory; 041import org.openrdf.rio.rdfjson.RDFJSONParserFactory; 042import org.openrdf.rio.rdfxml.RDFXMLParserFactory; 043import org.openrdf.rio.trig.TriGParserFactory; 044import org.openrdf.rio.turtle.TurtleParserFactory; 045import org.semarglproject.sesame.rdf.rdfa.SesameRDFaParserFactory; 046 047/** 048 * A convenience factory for creating an LDPath object with an LDCacheBackend. 049 * @author acoburn 050 * @since Aug 5, 2016 051 */ 052public class LDPathWrapper { 053 054 private final LDPath<Value> ldpath; 055 056 /** 057 * Create an LDPathWrapper Object 058 * @param backend the linkeddata backend 059 */ 060 public LDPathWrapper(final LDCacheBackend backend) { 061 062 // Register the Sesame RDF Parsers manually 063 // TODO: use the OSGi service registry as described in: 064 // http://blog.osgi.org/2013/02/javautilserviceloader-in-osgi.html 065 RDFParserRegistry.getInstance().add(new RDFXMLParserFactory()); 066 RDFParserRegistry.getInstance().add(new NTriplesParserFactory()); 067 RDFParserRegistry.getInstance().add(new TurtleParserFactory()); 068 RDFParserRegistry.getInstance().add(new N3ParserFactory()); 069 RDFParserRegistry.getInstance().add(new SesameJSONLDParserFactory()); 070 RDFParserRegistry.getInstance().add(new RDFJSONParserFactory()); 071 RDFParserRegistry.getInstance().add(new SesameRDFaParserFactory()); 072 RDFParserRegistry.getInstance().add(new TriGParserFactory()); 073 BooleanQueryResultParserRegistry.getInstance().add(new SPARQLBooleanXMLParserFactory()); 074 TupleQueryResultParserRegistry.getInstance().add(new SPARQLResultsXMLParserFactory()); 075 076 ldpath = new LDPath<Value>(backend); 077 } 078 079 /** 080 * Execute an LDPath query 081 * @param uri the URI to query 082 * @param program the LDPath program 083 * @return a result object wrapped in a List 084 * @throws LDPathParseException if the LDPath program was malformed 085 */ 086 public List<Map<String, Collection<?>>> programQuery(final String uri, final InputStream program) 087 throws LDPathParseException { 088 return singletonList(ldpath.programQuery(new URIImpl(uri), new InputStreamReader(program))); 089 } 090}