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