KickJava   Java API By Example, From Geeks To Geeks.

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


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.joseki.server.*;
9 import org.apache.commons.logging.* ;
10 import java.util.* ;
11
12 import com.hp.hpl.jena.rdf.model.*;
13 import com.hp.hpl.jena.shared.* ;
14 import com.hp.hpl.jena.vocabulary.* ;
15 import org.joseki.server.processors.fetch.* ;
16 import org.joseki.vocabulary.*;
17 import org.joseki.server.module.*;
18 import org.joseki.util.PrintUtils;
19
20 /** Query processor that returns the data object referenced.
21  * This subsystem finds a node in the graph (by directly being given its URI)
22  * and uses some server side rules that calculate the RDF model to return.
23  * For example, the rule may be based on the type of the object and/or on a bNode
24  * closure over the properties with this node as subject.
25  *
26  * @author Andy Seaborne
27  * @version $Id: QueryProcessorFetch.java,v 1.10 2004/04/30 12:35:55 andy_seaborne Exp $
28  */

29 public class QueryProcessorFetch extends QueryProcessorCom
30 {
31     Log log = LogFactory.getLog(QueryProcessorFetch.class.getName() ) ;
32     
33     public QueryProcessorFetch()
34     {
35         super() ;
36     }
37
38     // This is the type of object this
39
Resource rdfType = null ;
40     Set fetchHandlers = new HashSet() ;
41
42     public void init(Resource processor, Resource implmentation)
43     {
44         try {
45             // Pull in further modules per-attached model fetches possible.
46
StmtIterator sIter = processor.listProperties(JosekiModule.module) ;
47             for ( ; sIter.hasNext() ; )
48             {
49                 Statement stmt = sIter.nextStatement() ;
50                 Resource fetchBinding = stmt.getResource() ;
51                 Loader loader = new Loader() ;
52                 FetchHandler handler = (FetchHandler)loader.loadAndInstantiate(fetchBinding, FetchHandler.class) ;
53                 log.info(" Fetch: "+handler.getClass().getName()) ;
54                 
55                 // Sanity check for fetch handlers of the same name.
56
String JavaDoc n1 = handler.getClass().getName() ;
57                 for ( Iterator iter = fetchHandlers.iterator() ; iter.hasNext() ; )
58                 {
59                     FetchHandler handler2 = (FetchHandler)iter.next() ;
60                     String JavaDoc n2 = handler2.getClass().getName() ;
61                     if ( n1.equals(n2) )
62                         log.warn(" Two handlersa of teh same class name: "+n1) ;
63                 }
64                 // End sanity.
65
fetchHandlers.add(handler) ;
66             }
67         
68         } catch (JenaException ex)
69         {
70             log.warn("Exception loading fetch handlers: "+ex) ;
71             return ;
72         }
73         
74         if ( implmentation.hasProperty(RDFS.label))
75             log.info("rdfs:label(binding): "+implmentation.getProperty(RDFS.label).getString()) ;
76         if ( processor.hasProperty(RDFS.label))
77             log.info("rdfs:label(processor): "+processor.getProperty(RDFS.label).getString()) ;
78     }
79
80     public String JavaDoc getInterfaceURI() { return JosekiVocab.queryOperationFetch ; }
81
82     public Model execQuery(ModelSource src, String JavaDoc queryString, Request request)
83         throws RDFException, QueryExecutionException
84     {
85         if (!(src instanceof ModelSourceJena))
86             throw new QueryExecutionException(
87                 ExecutionError.rcOperationNotSupported,
88                 "Wrong implementation - this Fetch processor works with Jena models");
89         Model model = ((ModelSourceJena)src).getModel() ;
90         
91         Model resultModel = ModelFactory.createDefaultModel() ;
92         resultModel.setNsPrefixes(model) ;
93         Map m = src.getPrefixes() ;
94         if ( m!= null )
95             resultModel.setNsPrefixes(src.getPrefixes()) ;
96         
97         String JavaDoc uri = request.getParam("r") ;
98
99         if (uri != null )
100         {
101             Resource r = model.createResource(uri) ;
102             log.debug("Fetch: "+uri) ;
103             doOneResource(r, resultModel) ;
104             return resultModel ;
105         }
106
107         //Not the "r=" form
108

109         // Is this the predicat/value form?
110
String JavaDoc pURI = request.getParam("p") ;
111         if ( pURI == null )
112             throw new QueryExecutionException(
113                     ExecutionError.rcArgumentError,
114                     "Fetch must have either resource URI parameter or identifying predicate");
115         Property p = model.createProperty(pURI) ;
116         RDFNode o = null ;
117         String JavaDoc objURI = request.getParam("o") ;
118         if ( objURI == null )
119         {
120             String JavaDoc objVal = request.getParam("v") ;
121             o = model.createLiteral(objVal) ;
122             log.debug("Fetch: p="+pURI+" v="+o) ;
123         }
124         else
125         {
126             log.debug("Fetch: p="+pURI+" o="+objURI) ;
127             o = model.createResource(objURI) ;
128         }
129         
130         StmtIterator sIter = model.listStatements(null, p, o) ;
131         if ( ! sIter.hasNext() )
132             return resultModel;
133         
134         for ( ; sIter.hasNext() ; )
135         {
136             Resource r = sIter.nextStatement().getSubject() ;
137             doOneResource(r, resultModel) ;
138         }
139         sIter.close() ;
140         return resultModel ;
141     }
142
143     public Model execQuery(ModelSource aModel, Model queryModel, Request request)
144         throws RDFException, QueryExecutionException
145     {
146         throw new QueryExecutionException(ExecutionError.rcOperationNotSupported,
147                                      "Can't 'fetch' this way") ;
148     }
149
150     private void doOneResource(Resource resource, Model resultModel)
151     {
152         log.debug("Fetch: "+PrintUtils.fmt(resource)) ;
153         // Types are so significant, we get them all once and
154
// pass them to the fetch handlers.
155
Set types = new HashSet() ;
156         StmtIterator sIter = resource.listProperties(RDF.type) ;
157         for ( ; sIter.hasNext() ;)
158         {
159             // Find handler.
160
Resource type = sIter.nextStatement().getResource() ;
161             types.add(type) ;
162         }
163         
164         for ( Iterator iter = fetchHandlers.iterator() ; iter.hasNext() ; )
165         {
166             FetchHandler fetch = (FetchHandler)iter.next() ;
167             if ( fetch.handles(resource, types) )
168                 fetch.fetch(resource, types, resultModel) ;
169         }
170     }
171     
172 }
173
174 /*
175  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
176  * All rights reserved.
177  *
178  * Redistribution and use in source and binary forms, with or without
179  * modification, are permitted provided that the following conditions
180  * are met:
181  * 1. Redistributions of source code must retain the above copyright
182  * notice, this list of conditions and the following disclaimer.
183  * 2. Redistributions in binary form must reproduce the above copyright
184  * notice, this list of conditions and the following disclaimer in the
185  * documentation and/or other materials provided with the distribution.
186  * 3. The name of the author may not be used to endorse or promote products
187  * derived from this software without specific prior written permission.
188  *
189  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
190  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
191  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
193  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
194  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
195  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
196  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
197  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
198  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199  */

200
Popular Tags