~zorba-coders/zorba/xqxq-url-resolver

« back to all changes in this revision

Viewing changes to src/xqxq.xq.src/xqxq.cpp

  • Committer: Juan Zacarias
  • Date: 2012-09-14 18:32:07 UTC
  • Revision ID: juan457@gmail.com-20120914183207-nuh3jh0261guu619
included a way to add a uri mapper through prepare-main-module function

Show diffs side-by-side

added added

removed removed

Lines of Context:
231
231
    delete aStream;
232
232
  }  
233
233
  
 
234
  void 
 
235
    PrepareMainModuleFunction::XQXQURIMapper::mapURI(
 
236
     String aUri,
 
237
     EntityData const* aEntityData,
 
238
     std::vector<String>& oUris)
 
239
  {
 
240
    //Create entityData string to send to the new url resolver
 
241
    String lDataKind;
 
242
    switch (aEntityData->getKind())
 
243
    {
 
244
      case EntityData::SCHEMA:
 
245
        lDataKind = "schema";
 
246
        break;
 
247
      case EntityData::MODULE:
 
248
        lDataKind = "module";
 
249
        break;
 
250
      default:
 
251
        break;
 
252
    }
 
253
 
 
254
    //construct the arguments for the url resolver
 
255
    std::vector<ItemSequence_t> lArgs;
 
256
    ItemSequence_t lSeq1 = new SingletonItemSequence(XQXQModule::getItemFactory()->createString(aUri));
 
257
    ItemSequence_t lSeq2 = new SingletonItemSequence(XQXQModule::getItemFactory()->createString(lDataKind));
 
258
    lArgs.push_back(lSeq1);
 
259
    lArgs.push_back(lSeq2);
 
260
 
 
261
    //invoke the function using the arguments generated and the QName of the function
 
262
    ItemSequence_t lResult = theCtx->invoke(theFunction, lArgs);
 
263
 
 
264
    //Check if the result is an empty sequence by creating an Iterator, this is cheaper than serializing the result
 
265
    //and then checking if it was empty.
 
266
    Iterator_t lIter = lResult->getIterator();
 
267
    Item lItem;
 
268
    lIter->open();
 
269
    while (lIter->next(lItem))
 
270
    {
 
271
      std::cout << lItem.getStringValue() << std::endl;
 
272
      oUris.push_back(lItem.getStringValue());
 
273
    }
 
274
    lIter->close();
 
275
 
 
276
  }
 
277
      
 
278
 
234
279
  Resource*
235
280
    PrepareMainModuleFunction::XQXQURLResolver::resolveURL(
236
281
    const String& aUrl,
250
295
        break;
251
296
    }
252
297
 
 
298
    std::cout << aUrl << std::endl;
 
299
 
253
300
    //construct the arguments for the url resolver
254
301
    std::vector<ItemSequence_t> lArgs;
255
302
    ItemSequence_t lSeq1 = new SingletonItemSequence(XQXQModule::getItemFactory()->createString(aUrl));
284
331
    //return resource
285
332
    return StreamResource::create(new std::istringstream(lSerResult.str()), &streamReleaser);
286
333
 
287
 
    /*
288
 
    // we have only one module
289
 
       if (aEntityData->getKind() == EntityData::MODULE &&
290
 
         aUrl == "http://www.zorba-xquery.com/modules/xqxq/test") 
291
 
       {
292
 
         return StreamResource::create
293
 
           (new std::istringstream
294
 
             ("module namespace test = 'http://www.zorba-xquery.com/modules/xqxq/test'; "
295
 
              "declare function test:foo() { 'foo' };"), &streamReleaser);
296
 
       }
297
 
       else {
298
 
         return NULL;
299
 
       }
300
 
    */
301
334
  }
302
335
 
303
336
  zorba::ItemSequence_t
308
341
  {
309
342
    DynamicContext* lDynCtx = const_cast<DynamicContext*>(aDctx);
310
343
    StaticContext_t lSctxChild = aSctx->createChildContext();
 
344
    StaticContext_t lMapperSctx = aSctx->createChildContext();
311
345
   
312
346
    QueryMap* lQueryMap;
313
347
    if(!(lQueryMap = dynamic_cast<QueryMap*>(lDynCtx->getExternalFunctionParameter("xqxqQueryMap"))))
324
358
    
325
359
    StaticContext_t ltempSctx = lZorba->createStaticContext();
326
360
    XQXQURLResolver* lResolver = NULL;
 
361
    XQXQURIMapper* lMapper = NULL;
 
362
 
 
363
    if ( aArgs.size() > 2 )
 
364
    {
 
365
      Item lMapperQName = getItemArgument(aArgs, 2);
 
366
      if (!lMapperQName.isNull())
 
367
      {
 
368
        if (lMapperSctx->containsFunction(lMapperQName.getNamespace(), lMapperQName.getLocalName(), 2))
 
369
          {
 
370
            lMapper = new XQXQURIMapper(lMapperQName, lSctxChild);      
 
371
            ltempSctx->registerURIMapper(lMapper);
 
372
          }
 
373
      }
 
374
    }
327
375
 
328
376
    if ( aArgs.size() > 1 )
329
377
    {
330
 
      Item lFunctionQname = getItemArgument(aArgs, 1);
331
 
      
332
 
      if (lSctxChild->containsFunction(lFunctionQname.getNamespace(), lFunctionQname.getLocalName(), 2))
 
378
      Item lResolverQName = getItemArgument(aArgs, 1);
 
379
      if (!lResolverQName.isNull())
 
380
      {
 
381
        if (lSctxChild->containsFunction(lResolverQName.getNamespace(), lResolverQName.getLocalName(), 2))
333
382
        {
334
 
          lResolver = new XQXQURLResolver(lFunctionQname, lSctxChild);      
 
383
          lResolver = new XQXQURLResolver(lResolverQName, lSctxChild);      
335
384
          ltempSctx->registerURLResolver(lResolver);
336
385
        }
 
386
      }
337
387
 
338
388
    }
339
389
 
367
417
    
368
418
    if (lResolver)
369
419
      delete lResolver;
 
420
    if (lMapper)
 
421
      delete lMapper;
370
422
 
371
423
    return ItemSequence_t(new SingletonItemSequence(XQXQModule::getItemFactory()->createAnyURI(lUUID)));
372
424
  }