1 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 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 Resource rdfType = null ; 40 Set fetchHandlers = new HashSet() ; 41 42 public void init(Resource processor, Resource implmentation) 43 { 44 try { 45 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 String n1 = handler.getClass().getName() ; 57 for ( Iterator iter = fetchHandlers.iterator() ; iter.hasNext() ; ) 58 { 59 FetchHandler handler2 = (FetchHandler)iter.next() ; 60 String n2 = handler2.getClass().getName() ; 61 if ( n1.equals(n2) ) 62 log.warn(" Two handlersa of teh same class name: "+n1) ; 63 } 64 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 getInterfaceURI() { return JosekiVocab.queryOperationFetch ; } 81 82 public Model execQuery(ModelSource src, String 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 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 109 String 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 objURI = request.getParam("o") ; 118 if ( objURI == null ) 119 { 120 String 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 Set types = new HashSet() ; 156 StmtIterator sIter = resource.listProperties(RDF.type) ; 157 for ( ; sIter.hasNext() ;) 158 { 159 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 200 | Popular Tags |