KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > server > processors > QueryProcessorSPO


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package org.joseki.server.processors;
7
8 import org.apache.commons.logging.* ;
9
10 import org.joseki.util.Closure ;
11 import org.joseki.vocabulary.*;
12 import org.joseki.server.*;
13
14 import com.hp.hpl.jena.rdf.model.*;
15 import com.hp.hpl.jena.rdf.model.RDFException;
16
17 import java.util.* ;
18
19 /** Query processor that returns the whole model.
20  *
21  * @author Andy Seaborne
22  * @version $Id: QueryProcessorSPO.java,v 1.8 2004/04/30 14:13:11 andy_seaborne Exp $
23  */

24 public class QueryProcessorSPO extends QueryProcessorCom
25 {
26     static Log logger = LogFactory.getLog(QueryProcessorSPO.class.getName()) ;
27     
28     public QueryProcessorSPO()
29     {
30         super() ;
31     }
32
33     public String JavaDoc getInterfaceURI() { return JosekiVocab.queryOperationSPO ; }
34
35     public Model execQuery(ModelSource src, String JavaDoc queryString, Request request)
36         throws RDFException, QueryExecutionException
37     {
38         if (!(src instanceof ModelSourceJena))
39             throw new QueryExecutionException(
40                 ExecutionError.rcOperationNotSupported,
41                 "Wrong implementation - this Fetch processor works with Jena models");
42         Model model = ((ModelSourceJena)src).getModel() ;
43
44         String JavaDoc subjStr = request.getParam("s") ;
45         String JavaDoc predStr = request.getParam("p") ;
46         
47         String JavaDoc objStr = request.getParam("o") ;
48         String JavaDoc valStr = request.getParam("v") ;
49
50         if ( objStr != null && valStr != null )
51             throw new QueryExecutionException(ExecutionError.rcQueryExecutionFailure,
52                                               "Invalid request: both object URI and value specified");
53
54         Resource subj = null ;
55         if ( subjStr != null )
56              subj = model.createResource(subjStr) ;
57              
58         Property prop = null ;
59         if ( predStr != null )
60             prop = model.createProperty(predStr) ;
61             
62         RDFNode obj = null ;
63         if ( objStr != null )
64             obj = model.createResource(objStr) ;
65         if ( valStr != null )
66             // Datatypes to be done
67
obj = model.createLiteral(valStr) ;
68             
69         String JavaDoc closeOverBNodes$ = request.getParam("closure") ;
70         boolean closeOverBNodes = ( closeOverBNodes$ != null
71                                     && closeOverBNodes$.equalsIgnoreCase("true") );
72
73         logger.debug("Triples("+subj+", "+prop+", "+obj+")") ;
74             
75         Model resultModel = ModelFactory.createDefaultModel() ;
76         StmtIterator sIter = model.listStatements(subj, prop, obj) ;
77         
78         if ( closeOverBNodes )
79         {
80             for(; sIter.hasNext() ; )
81             {
82                 Statement stmt = sIter.nextStatement() ;
83                 Closure.closure(stmt, resultModel) ;
84             }
85         }
86         else
87         resultModel.add(sIter) ;
88             
89         resultModel.setNsPrefixes(model) ;
90         Map m = src.getPrefixes() ;
91         if ( m!= null )
92             resultModel.setNsPrefixes(src.getPrefixes()) ;
93         return resultModel ;
94     }
95
96     public Model execQuery(ModelSource aModel, Model queryModel, Request request)
97         throws RDFException, QueryExecutionException
98     {
99         throw new QueryExecutionException(ExecutionError.rcOperationNotSupported,
100                                      "Can't access a model with \"triples\" this way") ;
101     }
102
103
104 }
105
106 /*
107  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
108  * All rights reserved.
109  *
110  * Redistribution and use in source and binary forms, with or without
111  * modification, are permitted provided that the following conditions
112  * are met:
113  * 1. Redistributions of source code must retain the above copyright
114  * notice, this list of conditions and the following disclaimer.
115  * 2. Redistributions in binary form must reproduce the above copyright
116  * notice, this list of conditions and the following disclaimer in the
117  * documentation and/or other materials provided with the distribution.
118  * 3. The name of the author may not be used to endorse or promote products
119  * derived from this software without specific prior written permission.
120  *
121  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
122  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
123  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
124  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
125  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
126  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
127  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
128  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
129  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
130  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
131  */

132
Popular Tags