1 5 6 package org.joseki.server.processors; 7 8 9 import org.joseki.util.* ; 10 import org.joseki.vocabulary.*; 11 import org.joseki.server.* ; 12 13 import com.hp.hpl.jena.rdf.model.* ; 14 15 import com.hp.hpl.jena.rdql.* ; 16 17 import org.apache.commons.logging.* ; 18 import java.util.* ; 19 20 30 31 public class QueryProcessorRDQL extends QueryProcessorCom implements QueryProcessor 32 { 33 static Log logger = LogFactory.getLog(QueryProcessorRDQL.class.getName()) ; 34 35 public QueryProcessorRDQL() 36 { 37 super() ; 38 } 39 40 public String getInterfaceURI() { return JosekiVocab.queryOperationRDQL ; } 41 42 public Model execQuery(ModelSource src, String queryString, Request request) 43 throws RDFException, QueryExecutionException 44 { 45 if (!(src instanceof ModelSourceJena)) 46 throw new QueryExecutionException( 47 ExecutionError.rcOperationNotSupported, 48 "Wrong implementation - this RDQL processor works with Jena models"); 49 Model model = ((ModelSourceJena)src).getModel() ; 50 QueryResults results = null ; 51 52 try { 53 if ( queryString == null || queryString.equals("") ) 54 { 55 logger.debug("Query: No query string provided") ; 56 } 57 else 58 { 59 String tmp = queryString ; 60 tmp = tmp.replace('\n', ' ') ; 61 tmp = tmp.replace('\r', ' ') ; 62 logger.debug("Query: "+tmp) ; 63 } 64 65 if (queryString == null) 66 throw new QueryExecutionException( 67 ExecutionError.rcQueryExecutionFailure, 68 "No query supplied"); 69 70 Query query = null ; 71 try { 72 query = new Query(queryString) ; 73 } catch (Throwable thrown) 74 { 75 logger.info("Query parse error: request failed") ; 76 throw new QueryExecutionException(ExecutionError.rcQueryParseFailure, "Parse error") ; 77 } 78 79 query.setSource(model) ; 80 81 QueryExecution qe = new QueryEngine(query) ; 83 84 results = qe.exec(); 85 String format = request.getParam("format"); 86 String closure$ = request.getParam("closure"); 88 boolean closure = (closure$ != null && closure$.equalsIgnoreCase("true")); 89 90 Model resultModel = ModelFactory.createDefaultModel(); 91 92 if (closure) 95 { 96 doClosure(query, results, resultModel); 97 return resultModel; 98 } 99 100 if (format == null) 101 { 102 for (; results.hasNext();) 103 { 104 ResultBinding rb = (ResultBinding)results.next(); 105 rb.mergeTriples(resultModel); 106 } 107 resultModel.setNsPrefixes(model); 108 Map m = src.getPrefixes(); 109 if (m != null) 110 resultModel.setNsPrefixes(src.getPrefixes()); 111 112 return resultModel; 113 } 114 115 if (format.equalsIgnoreCase("BV") || format.equals("http://jena.hpl.hp.com/2003/07/query/BV")) 116 { 117 QueryResultsFormatter qfmt = new QueryResultsFormatter(results); 118 qfmt.asRDF(resultModel); 119 qfmt.close() ; 120 resultModel.setNsPrefix("rs", "http://jena.hpl.hp.com/2003/03/result-set#"); 121 return resultModel; 122 } 123 124 throw new QueryExecutionException( 125 ExecutionError.rcOperationNotSupported, 126 "Unknown results format requested: " + format); 127 } 128 catch (QueryException qEx) 129 { 130 logger.info("Query execution error: "+qEx) ; 131 throw new QueryExecutionException(ExecutionError.rcQueryExecutionFailure, null) ; 132 } 133 finally 134 { 135 if ( results != null ) 136 results.close() ; 137 } 138 } 139 140 public Model execQuery(ModelSource aModel, Model queryModel, Request request) 141 throws RDFException, QueryExecutionException 142 { 143 Model resultModel = null ; 144 NodeIterator nIter = queryModel.listObjectsOfProperty(JosekiVocab.queryScript) ; 145 for ( ; nIter.hasNext() ; ) 146 { 147 RDFNode n = nIter.nextNode() ; 148 if ( n instanceof Literal ) 149 { 150 String queryString = ((Literal)n).getString() ; 151 Model m = execQuery(aModel, queryString, request) ; 152 if ( resultModel == null ) 153 resultModel = m ; 154 else 155 resultModel.add(m) ; 156 } 157 } 158 return resultModel ; 159 } 160 161 private void doClosure(Query query, QueryResults results, Model resultModel) 162 { 163 for (; results.hasNext() ; ) 164 { 165 ResultBinding rb = (ResultBinding)results.next() ; 166 for ( Iterator iter = query.getResultVars().iterator() ; iter.hasNext() ; ) 167 { 168 String var = (String )iter.next() ; 169 Object x = rb.get(var) ; 170 if ( ! ( x instanceof Resource ) ) 171 { 172 logger.warn("Non-resource in query closure: "+x) ; 173 continue ; 174 } 175 Closure.closure((Resource)x, false, resultModel) ; 176 } 177 } 178 } 179 180 } 181 182 208 | Popular Tags |