コード例 #1
0
zstring zstring::replace(zstring const& src, zstring const& dst) const {
    zstring result(m_encoding);
    if (length() < src.length()) {
        return zstring(*this);
    }
    if (src.length() == 0) {
        return zstring(*this);
    }
    bool found = false;
    for (unsigned i = 0; i < length(); ++i) {
        bool eq = !found && i + src.length() <= length();
        for (unsigned j = 0; eq && j < src.length(); ++j) {
            eq = m_buffer[i+j] == src[j];
        }
        if (eq) {
            result.m_buffer.append(dst.m_buffer);
            found = true;
            i += src.length() - 1;
        }
        else {
            result.m_buffer.push_back(m_buffer[i]);
        }
    }
    return result;
}
コード例 #2
0
Item ItemFactoryImpl::createQName(
    const String& aNamespace,
    const String& aLocalname)
{
  zstring const &lNamespace = Unmarshaller::getInternalString( aNamespace );
  zstring const &lLocalname = Unmarshaller::getInternalString( aLocalname );

  if (!GenericCast::instance()->castableToNCName(lLocalname.c_str()))
  {
    RAISE_ERROR_NO_LOC(err::FORG0001,
    ERROR_PARAMS(ZED(FORG0001_LocalNotNCName_2), lLocalname));
  }

  store::Item_t lItem;
  theItemFactory->createQName(lItem, lNamespace, zstring(), lLocalname);
  return &*lItem;
}
コード例 #3
0
Item
ItemFactoryImpl::createQName(const String& aQNameString)
{
  const zstring& lQNameString = Unmarshaller::getInternalString( aQNameString );
  store::Item_t lItem;

  size_t lOpen  = lQNameString.find("{");
  size_t lClose = lQNameString.find("}");

  if (lOpen == 0 && lClose != std::string::npos)
  {
    zstring const &lNamespace = lQNameString.substr(1, lClose - 1);
    zstring const &lLocalname = lQNameString.substr(lClose+1);
    theItemFactory->createQName(lItem, lNamespace, zstring(), lLocalname);

    if (!GenericCast::instance()->castableToNCName(lLocalname.c_str()))
    {
      RAISE_ERROR_NO_LOC(err::FORG0001,
      ERROR_PARAMS(ZED(FORG0001_LocalNotNCName_2), lLocalname));
    }
  }
  return &*lItem;
}
コード例 #4
0
/*******************************************************************************
  14.9.1.1 fn-zorba-xml:canonicalize
********************************************************************************/
bool FnZorbaCanonicalizeIterator::nextImpl(store::Item_t& result, PlanState& planState) const
{
  zstring lDocString;
  xmlDocPtr lDoc;
  xmlChar* lResult;
  std::istream* lInstream = NULL;
  char buf[1024];
  store::Item_t tempItem;

  FnZorbaCanonicalizeIteratorState* state;
  DEFAULT_STACK_INIT(FnZorbaCanonicalizeIteratorState, state, planState);
  // Read the XML string
  // if the XML string is a streamable string it will have to be materialized
  // since the libxml2 xmlReadMemory functions can't work with streamable strings
  consumeNext(result, theChildren[0].getp(), planState);

  // read options
  if (theChildren.size() == 2)
  {
    consumeNext(tempItem, theChildren[1].getp(), planState);
    zorba::processOptions(tempItem, state->theProperties, theSctx, loc);
  }

  try
  {
   if (result->isStreamable())
    {
      lInstream = &result->getStream();
      while (lInstream->good())
      {
        lInstream->read(buf, 1024);
        lDocString.append(buf, static_cast<zstring::size_type>(lInstream->gcount()));
      }
    }
    else
    {
      result->getStringValue2(lDocString);  
    }
   int lOptions = XML_PARSE_NOERROR | state->theProperties.toLibXmlOptions();
   lDoc = xmlReadMemory(lDocString.c_str(), lDocString.size(), "input.xml", NULL, lOptions);
    if (!lDoc)
    {
      zstring lErrorMsg;
      lErrorMsg = "\"" + lDocString + "\"";
      throw XQUERY_EXCEPTION(err::FOCZ0001, ERROR_PARAMS("x:canonicalize()", lErrorMsg ), ERROR_LOC(loc));
    }

    xmlC14NDocDumpMemory(lDoc, NULL, 2/*XML_C14N_1_1*/, NULL, 1, &lResult);
    lDocString = zstring((char*)lResult);    
    xmlFree(lResult);
    xmlFreeDoc(lDoc);
  }
  catch ( std::exception const& )
  {
      zstring lErrorMsg;
      lErrorMsg = "\"" + lDocString + "\"";
      throw XQUERY_EXCEPTION(err::FOCZ0001, ERROR_PARAMS("x:canonicalize()", lErrorMsg ), ERROR_LOC(loc));
  }
  STACK_PUSH(GENV_ITEMFACTORY->createString(result, lDocString), state);
  STACK_END(state);
}
コード例 #5
0
void dynamic_context::set_environment_variables()
{
  if (!theEnvironmentVariables)
    theEnvironmentVariables = new EnvVarMap();

#if defined (WIN32)
    LPTCH envVarsCH = GetEnvironmentStrings();
    LPTSTR envVarsSTR = (LPTSTR) envVarsCH;

    while (*envVarsSTR)
    {
      int size = lstrlen(envVarsSTR);

      char * envVar = new char[size+1];

      WideCharToMultiByte( CP_ACP,
                           WC_NO_BEST_FIT_CHARS|WC_COMPOSITECHECK|WC_DEFAULTCHAR,
                           envVarsSTR,
                           size+1,
                           envVar,
                           size+1,
                           NULL,
                           NULL);

      zstring envVarZS(envVar);

      int eqPos = envVarZS.find_first_of("=");

      if (eqPos > 0)
      {
        zstring varname(envVarZS.substr(0, eqPos));
        zstring varvalue(envVarZS.substr(eqPos+1, size));

        if (!varname.empty() || !varvalue.empty())
          theEnvironmentVariables->insert(std::pair<zstring, zstring>(varname,varvalue));
      }

      delete envVar;
      envVarsSTR += lstrlen(envVarsSTR) + 1;
    }

    FreeEnvironmentStrings(envVarsCH);
#else
    const char* invalid_char;
    for (char **env = environ; *env; ++env)
    {
      zstring envVarZS(*env);

      if ((invalid_char = utf8::validate(envVarZS.c_str())) != NULL)
        throw XQUERY_EXCEPTION(err::FOCH0001,
          ERROR_PARAMS(zstring("#x") +
          BUILD_STRING(std::uppercase << std::hex
            << (static_cast<unsigned int>(*invalid_char)&0xFF))));

      if ((invalid_char = utf8::validate(envVarZS.c_str())) != NULL)
      {
        throw XQUERY_EXCEPTION(err::FOCH0001,
        ERROR_PARAMS(zstring("#x") +
        BUILD_STRING(std::uppercase << std::hex
                     << (static_cast<unsigned int>(*invalid_char) & 0xFF)) ));
      }

      int size = envVarZS.size();

      int eqPos = envVarZS.find_first_of("=");

      if (eqPos > 0)
      {
        zstring varname(envVarZS.substr(0, eqPos));
        zstring varvalue(envVarZS.substr(eqPos+1, size));

        if (!varname.empty() || !varvalue.empty())
          theEnvironmentVariables->insert(std::pair<zstring, zstring>(varname,varvalue));
      }
    }

#endif

}
コード例 #6
0
bool SingleDynamicFnCallIterator::nextImpl(
    store::Item_t& result,
    PlanState& planState) const
{
  store::Item_t item;
  store::Item_t targetItem;
  FunctionItem* fnItem;
  store::Item_t selectorItem1;
  store::Item_t selectorItem2;
  store::Item_t selectorItem3;

  SingleDynamicFnCallIteratorState* state;

  DEFAULT_STACK_INIT(SingleDynamicFnCallIteratorState, state, planState);

  // first child must return exactly one item which is a function item
  // otherwise XPTY0004 is raised
  if (!consumeNext(targetItem, theChildren[0], planState) || targetItem == NULL)
  {
    RAISE_ERROR(err::XPTY0004, loc, 
    ERROR_PARAMS(ZED(XPTY0004_NoTypePromote_23),
                 "empty-sequence()",
                 GENV_TYPESYSTEM.ANY_FUNCTION_TYPE_ONE->toSchemaString()));
  }

  if (targetItem->isFunction())
  {
    if (consumeNext(item, theChildren[0], planState))
    {
      RAISE_ERROR(err::XPTY0004, loc, 
      ERROR_PARAMS(ZED(XPTY0004_NoMultiSeqTypePromotion_2),
                   GENV_TYPESYSTEM.ANY_FUNCTION_TYPE_ONE->toSchemaString()));
    }

    fnItem = static_cast<FunctionItem*>(targetItem.getp());

    if (theChildren.size() - 1 != fnItem->getArity())
    {
      RAISE_ERROR(err::XPTY0004, loc,
      ERROR_PARAMS("dynamic function invoked with incorrect number of arguments"));
    }

    if (theIsPartialApply)
    {
      for (csize i = 1, pos = 0; i < theChildren.size(); ++i)
      {
        if (dynamic_cast<ArgumentPlaceholderIterator*>(theChildren[i].getp()) == NULL)
        {
          // The argument needs to be materialized only for local vars and only
          // if the function item is returned and used outside of the current
          // function. It might be impossible to determine if the partially
          // applied function item will be used outside of the current function,
          // so it is quite probable that it always needs to be materialized.          
          std::vector<store::Item_t> argValues;
          store::Item_t tempItem;

          while (consumeNext(tempItem, theChildren[i], planState))
            argValues.push_back(tempItem);

          store::TempSeq_t argSeq = GENV_STORE.createTempSeq(argValues);
          store::Iterator_t argSeqIter = argSeq->getIterator();
          PlanIter_t value = new PlanStateIteratorWrapper(argSeqIter);
                    
          fnItem->setArgumentValue(pos, value);
        }
        else
        {
          ++pos;
        }
      }

      result = fnItem;
      STACK_PUSH(true, state);
    }
    else
    {
      state->thePlan = fnItem->getImplementation(theChildren, planState.theCompilerCB);
      
      // must be opened after vars and params are set
      state->thePlan->open(planState, state->theUDFStateOffset);
      state->theIsOpen = true;

      while (consumeNext(result, state->thePlan, planState))
      {
        STACK_PUSH(true, state);
      }

      // Need to close here early in case the plan is completely consumed.
      // Otherwise, the plan would still be opened if destroyed from the
      // state's destructor.
      state->thePlan->close(planState);
      state->theIsOpen = false;
    } // if (!theIsPartialApply)
  } // if (targetItem->isFunction())

  else if (targetItem->isJSONItem())
  {
    if (theChildren.size() > 2)
    {
      RAISE_ERROR_NO_PARAMS(jerr::JNTY0018, loc);
    }
    else if (theChildren.size() == 2)
    {
      if (consumeNext(selectorItem1, theChildren[1], planState))
      {
        if (consumeNext(item, theChildren[1], planState))
        {
          RAISE_ERROR(err::XPTY0004, loc,
          ERROR_PARAMS(ZED(NoSeqCastToTypeWithQuantOneOrQuestion)));
        }

        // Atomize the selector item
        switch (selectorItem1->getKind())
        {
        case store::Item::ATOMIC:
        {
          selectorItem2.transfer(selectorItem1);
          break;
        }
        case store::Item::NODE:
        {
          store::Iterator_t iter;
          selectorItem1->getTypedValue(selectorItem2, iter);

          if (iter != NULL && iter->next(selectorItem2))
          {
            if (iter->next(item))
            {
              RAISE_ERROR(err::XPTY0004, loc,
              ERROR_PARAMS(ZED(NoSeqCastToTypeWithQuantOneOrQuestion)));
            }
          }

          break;
        }
        case store::Item::OBJECT:
        {
          RAISE_ERROR(jerr::JNTY0004, loc, ERROR_PARAMS("object"));
        }
        case store::Item::ARRAY:
        {
          RAISE_ERROR(jerr::JNTY0004, loc, ERROR_PARAMS("array"));
        }
        case store::Item::FUNCTION:
        {
          store::Item_t fnName = selectorItem1->getFunctionName();
          RAISE_ERROR(err::FOTY0013, loc, 
          ERROR_PARAMS(fnName.getp() ?
                       result->getFunctionName()->getStringValue() :
                       zstring("???")));
        }
        default:
        {
          ZORBA_ASSERT(false);
        }
        }
        
        if (selectorItem2 != NULL)
        {
          if (targetItem->isObject())
          {
            GenericCast::castToBuiltinAtomic(selectorItem3,
                                             selectorItem2,
                                             store::XS_STRING,
                                             NULL,
                                             loc);
        
            result = targetItem->getObjectValue(selectorItem3);
          }
          else
          {
            GenericCast::castToBuiltinAtomic(selectorItem3,
                                             selectorItem2,
                                             store::XS_INTEGER,
                                             NULL,
                                             loc);

            result = targetItem->getArrayValue(selectorItem3->getIntegerValue());
          }

          STACK_PUSH(result != NULL, state);
        }
      } // there is a selector item
    } // 1 argument 
    else // no arguments
    {
      if (targetItem->isObject())
        state->theIterator = targetItem->getObjectKeys();
      else
        state->theIterator = targetItem->getArrayValues();

      state->theIterator->open();

      while (state->theIterator->next(result))
      {
        STACK_PUSH(true, state);
      }

      state->theIterator->close();
    }
  }
#if 0
  else if (theSctx->language_kind() == StaticContextConsts::language_kind_xquery)
  {
    xqtref_t type = theSctx->get_typemanager()->create_value_type(targetItem);

    RAISE_ERROR(err::XPTY0004, loc, 
    ERROR_PARAMS(ZED(XPTY0004_NoTypePromote_23),
                 type->toSchemaString(),
                 GENV_TYPESYSTEM.ANY_FUNCTION_TYPE_ONE->toSchemaString()));
  }
#endif

  STACK_END(state);
};