Ejemplo n.º 1
0
int main(int argc, char** argv)
{
    string queryString(argc > 1 ? argv[1] : "//node[@rel='su' and string(@begin) = 4]");

	DynamicContext *ctx = s_xqilla.createContext(XQilla::XPATH2);
    ctx->setXPath1CompatibilityMode(true);

    XQQuery *query = s_xqilla.parse(X(queryString.c_str()), ctx);

    cout << query->getQueryPlan() << endl;

    ASTNode *root = query->getQueryBody();

    std::tr1::shared_ptr<QueryScope> rootScope(new QueryScope());
    rootScope->setNodeName("[document root]");

    ElementMap elements;

    std::string dtdData = readFile("alpino_ds.dtd");
    SimpleDTD alpinoDtd(dtdData);

    inspect(root, rootScope, alpinoDtd);

    cout << "Done." << endl;

	return 0;
}
Ejemplo n.º 2
0
XQQuery *XQQuery::parseModule(const XMLCh *ns, const XMLCh *at, const LocationInfo *location) const
{
  InputSource* srcToUse = 0;
  if(m_context->getDocumentCache()->getXMLEntityResolver()){
    XMLResourceIdentifier resourceIdentifier(XMLResourceIdentifier::UnKnown, at, ns,
                                             XMLUni::fgZeroLenString, m_context->getBaseURI());
    srcToUse = m_context->getDocumentCache()->getXMLEntityResolver()->resolveEntity(&resourceIdentifier);
  }

  if(srcToUse==0) {
    try {
      XMLURL urlTmp(m_context->getBaseURI(), at);
      if (urlTmp.isRelative()) {
        throw MalformedURLException(__FILE__, __LINE__, XMLExcepts::URL_NoProtocolPresent);
      }
      srcToUse = new URLInputSource(urlTmp);
    }
    catch(const MalformedURLException&) {
      // It's not a URL, so let's assume it's a local file name.
      const XMLCh* baseUri=m_context->getBaseURI();
      if(baseUri && baseUri[0]) {
        XMLCh* tmpBuf = XMLPlatformUtils::weavePaths(baseUri, at);
        srcToUse = new LocalFileInputSource(tmpBuf);
        XMLPlatformUtils::fgMemoryManager->deallocate(tmpBuf);
      }
      else {
        srcToUse = new LocalFileInputSource(at);
      }
    }
  }
  Janitor<InputSource> janIS(srcToUse);

  XQQuery *module = m_moduleCache->getByURI(srcToUse->getSystemId());
  if(module) return module;

  AutoDelete<DynamicContext> ctxGuard(m_context->createModuleContext());
  DynamicContext *moduleCtx = ctxGuard.get();

  moduleCtx->setBaseURI(srcToUse->getSystemId());
  moduleCtx->setXMLEntityResolver(m_context->getXMLEntityResolver());

  AutoDelete<XQQuery> moduleGuard(new (XMLPlatformUtils::fgMemoryManager)
                                  XQQuery(ctxGuard.adopt(), true, m_moduleCache, XMLPlatformUtils::fgMemoryManager));
  module = moduleGuard;
  module->setFile(srcToUse->getSystemId());
  module->setModuleTargetNamespace(ns);

  // Put the unparsed module in the cache, to resolve loops correctly
  m_moduleCache->put(moduleGuard.adopt());

  XQilla::parse(*srcToUse, moduleCtx, XQilla::NO_STATIC_RESOLUTION, XMLPlatformUtils::fgMemoryManager, module);

  return module;
}
Ejemplo n.º 3
0
InstanceMap* 
  JdbcModule::getCreateInstanceMap(const zorba::DynamicContext* aDynamincContext, String mapName) {

  InstanceMap* result;
  DynamicContext* lDctx = const_cast<DynamicContext*>(aDynamincContext);
  if (!(result = dynamic_cast<InstanceMap*>(lDctx->getExternalFunctionParameter(mapName))))
  {
    result = new InstanceMap();
    result->id = mapName;
    lDctx->addExternalFunctionParameter(mapName, result);
  }
  return result;
}
Ejemplo n.º 4
0
void* query_stress_test_1(void *param)
{
  argv* var = (argv*)param;

  std::ostringstream os;
  os << var->index;

  XQuery_t aQuery = var->lZorba->compileQuery(".//chapter[@id=" + os.str() + "]");

  DynamicContext* lCtx = aQuery->getDynamicContext();

  lCtx->setContextItem(var->lItem);

  os.clear();
  os << aQuery << std::endl;

  var->lItem = NULL;
  aQuery->close();

  return (void*)0;
}
Ejemplo n.º 5
0
ASTNode *DelayedFuncFactory::createInstance(const VectorOfASTNodes &args, XPath2MemoryManager* memMgr) const
{
  if(function_ == 0) {
    DynamicContext *context = (DynamicContext*)query_->getStaticContext();

    if(body_ == 0) {
      const_cast<const XMLCh *&>(body_) = context->getMemoryManager()->getPooledString(body8_);
    }

    XQLexer lexer(memMgr, _LANG_FUNCDECL_, query_->getFile(), line_, column_, body_);
    XQParserArgs args(&lexer, query_);
    XQParser::yyparse(&args);

    const_cast<XQUserFunction*&>(function_) = args._function;
    function_->setDelayed(true);

    query_->addFunction(function_);
    function_->staticResolutionStage1(context);

    AutoDelete<Optimizer> optimizer(XQilla::createOptimizer(context, XQilla::NO_OPTIMIZATION));
    optimizer->startOptimize(function_);
  }
  return function_->createInstance(args, memMgr);
}
Ejemplo n.º 6
0
void XQQuery::importModule(const XMLCh* szUri, VectorOfStrings* locations, StaticContext* context, const LocationInfo *location)
{
  for(ImportedModules::iterator modIt = m_importedModules.begin();
      modIt != m_importedModules.end(); ++modIt) {
    if(XPath2Utils::equals((*modIt)->getModuleTargetNamespace(),szUri)) {
      XMLBuffer buf(1023,context->getMemoryManager());
      buf.set(X("Module for namespace '"));
      buf.append(szUri);
      buf.append(X("' has already been imported [err:XQST0047]"));
      XQThrow3(StaticErrorException, X("XQQuery::ImportModule"), buf.getRawBuffer(), location);
    }
  }
  if(locations==NULL)
    locations=context->resolveModuleURI(szUri);
  if(locations==NULL || locations->empty()) {
    XMLBuffer buf(1023,context->getMemoryManager());
    buf.set(X("Cannot locate module for namespace "));
    buf.append(szUri);
    buf.append(X(" without the 'at <location>' keyword [err:XQST0059]"));
    XQThrow3(StaticErrorException,X("XQQuery::ImportModule"), buf.getRawBuffer(), location);
  }

  bool bFound=false;
  for(VectorOfStrings::iterator it=locations->begin();it!=locations->end();++it) {
    InputSource* srcToUse = 0;
    if (context->getDocumentCache()->getXMLEntityResolver()){
      XMLResourceIdentifier resourceIdentifier(XMLResourceIdentifier::UnKnown,
                                               *it, szUri, XMLUni::fgZeroLenString, 
                                               context->getBaseURI());
      srcToUse = context->getDocumentCache()->getXMLEntityResolver()->resolveEntity(&resourceIdentifier);
    }

    if(srcToUse==0) {
      try {
        XMLURL urlTmp(context->getBaseURI(), *it);
        if (urlTmp.isRelative()) {
          throw MalformedURLException(__FILE__, __LINE__, XMLExcepts::URL_NoProtocolPresent);
        }
        srcToUse = new URLInputSource(urlTmp);
      }
      catch(const MalformedURLException&) {
        // It's not a URL, so let's assume it's a local file name.
        const XMLCh* baseUri=context->getBaseURI();
        if(baseUri && baseUri[0]) {
          XMLCh* tmpBuf = XMLPlatformUtils::weavePaths(baseUri, *it);
          srcToUse = new LocalFileInputSource(tmpBuf);
          XMLPlatformUtils::fgMemoryManager->deallocate(tmpBuf);
        }
        else {
          srcToUse = new LocalFileInputSource(*it);
        }
      }
    }
    Janitor<InputSource> janIS(srcToUse);

    AutoDelete<DynamicContext> ctxGuard(context->createModuleContext());
    DynamicContext *moduleCtx = ctxGuard.get();

    moduleCtx->setBaseURI(srcToUse->getSystemId());
    LoopDetector loopDetector(context->getXMLEntityResolver(), szUri, location);
    moduleCtx->setXMLEntityResolver(&loopDetector);

    AutoDelete<XQQuery> pParsedQuery(XQilla::parse(*srcToUse, ctxGuard.adopt(), XQilla::NO_OPTIMIZATION));

    if(!pParsedQuery->getIsLibraryModule()) {
      XMLBuffer buf(1023,context->getMemoryManager());
      buf.set(X("The module at "));
      buf.append(srcToUse->getSystemId());
      buf.append(X(" is not a module"));
      XQThrow3(StaticErrorException, X("XQQuery::ImportModule"), buf.getRawBuffer(), location);
    }
    if(!XERCES_CPP_NAMESPACE::XMLString::equals(szUri,pParsedQuery->getModuleTargetNamespace())) {
      XMLBuffer buf(1023,context->getMemoryManager());
      buf.set(X("The module at "));
      buf.append(srcToUse->getSystemId());
      buf.append(X(" specifies a different namespace [err:XQST0059]"));
      XQThrow3(StaticErrorException, X("XQQuery::ImportModule"), buf.getRawBuffer(), location);
    }
    // now move the variable declarations and the function definitions into my context
    for(UserFunctions::iterator itFn = pParsedQuery->m_userDefFns.begin(); itFn != pParsedQuery->m_userDefFns.end(); ++itFn) {
      (*itFn)->setModuleDocumentCache(const_cast<DocumentCache*>(moduleCtx->getDocumentCache()));
      if((*itFn)->isTemplate()) {
        context->addTemplate(*itFn);
      }
      else if((*itFn)->getName()) {
        context->addCustomFunction(*itFn);
      }
    }
    for(GlobalVariables::iterator itVar = pParsedQuery->m_userDefVars.begin(); itVar != pParsedQuery->m_userDefVars.end(); ++itVar) {
      for(ImportedModules::const_iterator modIt = m_importedModules.begin();
          modIt != m_importedModules.end(); ++modIt) {
        for(GlobalVariables::const_iterator varIt = (*modIt)->m_userDefVars.begin();
            varIt != (*modIt)->m_userDefVars.end(); ++varIt) {
          if(XPath2Utils::equals((*varIt)->getVariableURI(), (*itVar)->getVariableURI()) &&
             XPath2Utils::equals((*varIt)->getVariableLocalName(), (*itVar)->getVariableLocalName())) {
            XMLBuffer buf(1023,context->getMemoryManager());
            buf.set(X("An imported variable {"));
            buf.append((*itVar)->getVariableURI());
            buf.append(X("}"));
            buf.append((*itVar)->getVariableLocalName());
            buf.append(X(" conflicts with an already defined global variable [err:XQST0049]."));
            XQThrow3(StaticErrorException, X("XQQuery::ImportModule"), buf.getRawBuffer(), *varIt);
          }
        }
      }
    }

    moduleCtx->setXMLEntityResolver(context->getXMLEntityResolver());
    m_importedModules.push_back(pParsedQuery.adopt());

    bFound=true;
  }
  if(!bFound)
  {
    XMLBuffer buf(1023,context->getMemoryManager());
    buf.set(X("Cannot locate the module for namespace \""));
    buf.append(szUri);
    buf.append(X("\" [err:XQST0059]"));
    XQThrow3(StaticErrorException,X("XQQuery::ImportModule"), buf.getRawBuffer(), location);
  }
}