001/* 002 * The contents of this file are subject to the license and copyright 003 * detailed in the LICENSE and NOTICE files at the root of the source 004 * tree. 005 */ 006package org.fcrepo.camel.ldpath; 007 008import static java.util.Collections.singletonList; 009 010import java.io.InputStream; 011import java.io.InputStreamReader; 012import java.util.Collection; 013import java.util.List; 014import java.util.Map; 015import java.util.Set; 016 017import com.github.jsonldjava.sesame.SesameJSONLDParserFactory; 018import org.apache.marmotta.ldpath.LDPath; 019import org.apache.marmotta.ldpath.api.functions.SelectorFunction; 020import org.apache.marmotta.ldpath.backend.linkeddata.LDCacheBackend; 021import org.apache.marmotta.ldpath.exception.LDPathParseException; 022import org.openrdf.model.Value; 023import org.openrdf.model.impl.URIImpl; 024import org.openrdf.query.resultio.BooleanQueryResultParserRegistry; 025import org.openrdf.query.resultio.TupleQueryResultParserRegistry; 026import org.openrdf.query.resultio.sparqlxml.SPARQLBooleanXMLParserFactory; 027import org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLParserFactory; 028import org.openrdf.rio.RDFParserRegistry; 029import org.openrdf.rio.n3.N3ParserFactory; 030import org.openrdf.rio.ntriples.NTriplesParserFactory; 031import org.openrdf.rio.rdfjson.RDFJSONParserFactory; 032import org.openrdf.rio.rdfxml.RDFXMLParserFactory; 033import org.openrdf.rio.trig.TriGParserFactory; 034import org.openrdf.rio.turtle.TurtleParserFactory; 035import org.semarglproject.sesame.rdf.rdfa.SesameRDFaParserFactory; 036 037/** 038 * A convenience factory for creating an LDPath object with an LDCacheBackend. 039 * @author acoburn 040 * @since Aug 5, 2016 041 */ 042public class LDPathWrapper { 043 044 private final LDPath<Value> ldpath; 045 046 /** 047 * Create an LDPathWrapper and register a set of selector functions. 048 * 049 * @param backend the linkeddata backend 050 * @param functions selector functions 051 */ 052 @SuppressWarnings({ "unchecked", "rawtypes" }) 053 public LDPathWrapper(final LDCacheBackend backend, final Set<SelectorFunction> functions) { 054 this(backend); 055 for (SelectorFunction<Value> function : functions) { 056 ldpath.registerFunction(function); 057 } 058 } 059 060 /** 061 * Create an LDPathWrapper Object 062 * @param backend the linkeddata backend 063 */ 064 public LDPathWrapper(final LDCacheBackend backend) { 065 066 // Register the Sesame RDF Parsers manually 067 RDFParserRegistry.getInstance().add(new RDFXMLParserFactory()); 068 RDFParserRegistry.getInstance().add(new NTriplesParserFactory()); 069 RDFParserRegistry.getInstance().add(new TurtleParserFactory()); 070 RDFParserRegistry.getInstance().add(new N3ParserFactory()); 071 RDFParserRegistry.getInstance().add(new SesameJSONLDParserFactory()); 072 RDFParserRegistry.getInstance().add(new RDFJSONParserFactory()); 073 RDFParserRegistry.getInstance().add(new SesameRDFaParserFactory()); 074 RDFParserRegistry.getInstance().add(new TriGParserFactory()); 075 BooleanQueryResultParserRegistry.getInstance().add(new SPARQLBooleanXMLParserFactory()); 076 TupleQueryResultParserRegistry.getInstance().add(new SPARQLResultsXMLParserFactory()); 077 078 ldpath = new LDPath<Value>(backend); 079 } 080 081 /** 082 * Execute an LDPath query 083 * @param uri the URI to query 084 * @param program the LDPath program 085 * @return a result object wrapped in a List 086 * @throws LDPathParseException if the LDPath program was malformed 087 */ 088 public List<Map<String, Collection<?>>> programQuery(final String uri, final InputStream program) 089 throws LDPathParseException { 090 return singletonList(ldpath.programQuery(new URIImpl(uri), new InputStreamReader(program))); 091 } 092}