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.transform.transformations; 019 020import static org.fcrepo.kernel.api.RdfCollectors.toModel; 021 022import com.hp.hpl.jena.query.Query; 023import com.hp.hpl.jena.query.QueryExecution; 024import com.hp.hpl.jena.query.QueryExecutionFactory; 025import com.hp.hpl.jena.query.QueryFactory; 026import com.hp.hpl.jena.rdf.model.Model; 027 028import org.apache.commons.io.IOUtils; 029import org.fcrepo.kernel.api.RdfStream; 030import org.fcrepo.transform.Transformation; 031 032import java.io.IOException; 033import java.io.InputStream; 034import java.util.Objects; 035 036/** 037 * SPARQL Query-based transforms 038 * 039 * @author cbeer 040 */ 041public class SparqlQueryTransform implements Transformation<QueryExecution> { 042 043 private final InputStream query; 044 045 /** 046 * Construct a new SparqlQueryTransform from the data from 047 * the InputStream 048 * @param query the query 049 */ 050 public SparqlQueryTransform(final InputStream query) { 051 this.query = query; 052 } 053 054 @Override 055 public QueryExecution apply(final RdfStream rdfStream) { 056 057 try { 058 final Model model = rdfStream.collect(toModel()); 059 final Query sparqlQuery = 060 QueryFactory.create(IOUtils.toString(query)); 061 062 return QueryExecutionFactory.create(sparqlQuery, model); 063 } catch (final IOException e) { 064 throw new IllegalStateException(e); 065 } 066 } 067 068 @Override 069 public boolean equals(final Object other) { 070 return other instanceof SparqlQueryTransform && query.equals(((SparqlQueryTransform)other).query); 071 } 072 073 @Override 074 public int hashCode() { 075 return Objects.hashCode(query); 076 } 077}