001/*
002 * Copyright 2016 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.camel.ldpath;
017
018import static java.util.Collections.singletonList;
019
020import java.io.InputStream;
021import java.io.InputStreamReader;
022import java.util.Collection;
023import java.util.List;
024import java.util.Map;
025
026import com.github.jsonldjava.sesame.SesameJSONLDParserFactory;
027import org.apache.marmotta.ldpath.LDPath;
028import org.apache.marmotta.ldpath.backend.linkeddata.LDCacheBackend;
029import org.apache.marmotta.ldpath.exception.LDPathParseException;
030import org.openrdf.model.Value;
031import org.openrdf.model.impl.URIImpl;
032import org.openrdf.query.resultio.BooleanQueryResultParserRegistry;
033import org.openrdf.query.resultio.TupleQueryResultParserRegistry;
034import org.openrdf.query.resultio.sparqlxml.SPARQLBooleanXMLParserFactory;
035import org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLParserFactory;
036import org.openrdf.rio.RDFParserRegistry;
037import org.openrdf.rio.n3.N3ParserFactory;
038import org.openrdf.rio.ntriples.NTriplesParserFactory;
039import org.openrdf.rio.rdfjson.RDFJSONParserFactory;
040import org.openrdf.rio.rdfxml.RDFXMLParserFactory;
041import org.openrdf.rio.trig.TriGParserFactory;
042import org.openrdf.rio.turtle.TurtleParserFactory;
043import org.semarglproject.sesame.rdf.rdfa.SesameRDFaParserFactory;
044
045/**
046 * A convenience factory for creating an LDPath object with an LDCacheBackend.
047 * @author acoburn
048 * @since Aug 5, 2016
049 */
050public class LDPathWrapper {
051
052    private final LDPath<Value> ldpath;
053
054    /**
055     * Create an LDPathWrapper Object
056     * @param backend the linkeddata backend
057     */
058    public LDPathWrapper(final LDCacheBackend backend) {
059
060        // Register the Sesame RDF Parsers manually
061        // TODO: use the OSGi service registry as described in:
062        // http://blog.osgi.org/2013/02/javautilserviceloader-in-osgi.html
063        RDFParserRegistry.getInstance().add(new RDFXMLParserFactory());
064        RDFParserRegistry.getInstance().add(new NTriplesParserFactory());
065        RDFParserRegistry.getInstance().add(new TurtleParserFactory());
066        RDFParserRegistry.getInstance().add(new N3ParserFactory());
067        RDFParserRegistry.getInstance().add(new SesameJSONLDParserFactory());
068        RDFParserRegistry.getInstance().add(new RDFJSONParserFactory());
069        RDFParserRegistry.getInstance().add(new SesameRDFaParserFactory());
070        RDFParserRegistry.getInstance().add(new TriGParserFactory());
071        BooleanQueryResultParserRegistry.getInstance().add(new SPARQLBooleanXMLParserFactory());
072        TupleQueryResultParserRegistry.getInstance().add(new SPARQLResultsXMLParserFactory());
073
074        ldpath = new LDPath<Value>(backend);
075    }
076
077    /**
078     * Execute an LDPath query
079     * @param uri the URI to query
080     * @param program the LDPath program
081     * @return a result object wrapped in a List
082     * @throws LDPathParseException if the LDPath program was malformed
083     */
084    public List<Map<String, Collection<?>>> programQuery(final String uri, final InputStream program)
085            throws LDPathParseException {
086        return singletonList(ldpath.programQuery(new URIImpl(uri), new InputStreamReader(program)));
087    }
088}