/*
* (c) Copyright 2006 Hewlett-Packard Development Company, LP
* All rights reserved.
* [See end of file]
*/
package squirrelrdf;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.query.engine1.QueryEngine;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.squirrelrdf.ldap.LdapQueryEngine;
import com.hp.hpl.squirrelrdf.querymap.exceptions.ConfigException;
import com.hp.hpl.squirrelrdf.rdb.DbMap;
import com.hp.hpl.squirrelrdf.rdb.SQLQueryEngine;
/**
* A very simple utility for command line querying
*
* @author pldms
*
*/
public class Query
{
public static void main(String[] args) throws IOException, ConfigException
{
if (args.length == 0)
{
usage();
System.exit(0);
}
Model config = FileManager.get().loadModel(args[0]);
String query;
if (args.length == 1 || !args[1].contains(" ")) // stdin or file name
{
InputStream in;
if (args.length == 1) in = System.in;
else in = new FileInputStream(args[1]);
query = readFromStream(in);
}
else query = args[1];
com.hp.hpl.jena.query.Query q = QueryFactory.create(query);
QueryEngine qe;
if (config.contains(null, RDF.type, DbMap.Map))
{
qe = new SQLQueryEngine(q, config);
}
else
{
qe = new LdapQueryEngine(q, config);
}
qe.setDataset(DatasetFactory.create());
ResultSet results = qe.execSelect();
ResultSetFormatter.out(System.out, results);
}
private static String readFromStream(InputStream in) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
{
builder.append(line);
}
reader.close();
return builder.toString();
}
private static void usage()
{
System.out.println("Usage: squirrelrdf.Query <config file> \"the query\"");
System.out.println("or squirrelrdf.Query <config file> [<query file>] (reads from stdin if no file)");
}
}
/*
* (c) Copyright 2006 Hewlett-Packard Development Company, LP
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/