void PropertiesBinding::SetList(const ValueList& args, KValueRef result)
	{
		if (args.size() >= 2 && args.at(0)->IsString() && args.at(1)->IsList())
		{
			std::string property = args.at(0)->ToString();
			KListRef list = args.at(1)->ToList();

			std::string value = "";
			for (unsigned int i = 0; i < list->Size(); i++)
			{
				KValueRef arg = list->At(i);
				if (arg->IsString())
				{
					value += list->At(i)->ToString();
					if (i < list->Size() - 1)
					{
						value += ",";
					}
				}
				else
				{
					std::cerr << "skipping object: " << arg->GetType() << std::endl;
				}
			}
			config->setString(property, value);
			if (file_path.size() > 0)
			{
				config->save(file_path);
			}
		}
	}
Exemple #2
0
 static std::vector<std::string> ValueToURIList(KValueRef value)
 {
     std::vector<std::string> uriList;
     if (value->IsList())
     {
         KListRef list(value->ToList());
         for (unsigned int i = 0; i < list->Size(); i++)
         {
             KValueRef element(list->At(i));
             if (element->IsString())
                 uriList.push_back(element->ToString());
         }
     }
     else if (value->IsString())
     {
         StringTokenizer tokenizer(value->ToString(), "\n", 
             StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
         for (size_t i = 0; i < tokenizer.count(); i++)
         {
             uriList.push_back(tokenizer[i]);
         }
     }
     else
     {
         throw ValueException::FromString("URI List requires an Array or a newline-delimited String");
     }
     return uriList;
 }
 void EnvironmentBinding::Set(const char *name, KValueRef value)
 {
     if (value->IsString())
     {
         EnvironmentUtils::Set(name, value->ToString());
     }
 }
void Properties::SetList(const ValueList& args, KValueRef result)
{
    args.VerifyException("setList", "s l");

    std::string property = args.at(0)->ToString();
    KListRef list = args.at(1)->ToList();

    std::string value = "";
    for (unsigned int i = 0; i < list->Size(); i++)
    {
        KValueRef arg = list->At(i);
        if (arg->IsString())
        {
            value += list->At(i)->ToString();
            if (i < list->Size() - 1)
            {
                value += ",";
            }
        }
        else
        {
            logger->Warn("Skipping list entry %ui, not a string", i);
        }
    }
    config->setString(property, value);
    this->SaveConfig();
    
}
Exemple #5
0
std::string KObject::GetString(const char* name, std::string defaultValue)
{
    KValueRef prop = this->Get(name);
    if(prop->IsString())
    {
        return prop->ToString();
    }
    else
    {
        return defaultValue;
    }
}
	void AppBinding::GetStreamURL(const ValueList& args, KValueRef result)
	{
		SharedApplication app = this->host->GetApplication();
		std::string url(app->GetStreamURL("https"));

		for (size_t c = 0; c < args.size(); c++)
		{
			KValueRef arg = args.at(c);
			if (arg->IsString())
			{
				url.append("/");
				url.append(arg->ToString());
			}
		}
		result->SetString(url);
	}
    void KPHPArrayObject::AddKrollValueToPHPArray(KValueRef value, zval *phpArray)
    {
        if (value->IsNull() || value->IsUndefined())
        {
            add_next_index_null(phpArray);
        }
        else if (value->IsBool())
        {
            if (value->ToBool())
                add_next_index_bool(phpArray, 1);
            else
                add_next_index_bool(phpArray, 0);
        }
        else if (value->IsNumber())
        {
            /* No way to check whether the number is an
               integer or a double here. All Kroll numbers
               are doubles, so return a double. This could
               cause some PHP to function incorrectly if it's
               doing strict type checking. */
            add_next_index_double(phpArray, value->ToNumber());
        }
        else if (value->IsString())
        {
            add_next_index_stringl(phpArray, (char *) value->ToString(), strlen(value->ToString()), 1);
        }
        else if (value->IsObject())
        {
            /*TODO: Implement*/
        }
        else if (value->IsMethod())
        {
            /*TODO: Implement*/
        }
        else if (value->IsList())
        {
            zval *phpValue;
            AutoPtr<KPHPArrayObject> pl = value->ToList().cast<KPHPArrayObject>();
            if (!pl.isNull())
                phpValue = pl->ToPHP();
            else
                phpValue = PHPUtils::ToPHPValue(value);

            add_next_index_zval(phpArray, phpValue);
        }
    }
	void AppBinding::StdErr(const ValueList& args, KValueRef result)
	{
		for (size_t c = 0; c < args.size(); c++)
		{
			KValueRef arg = args.at(c);
			if (arg->IsString())
			{
				const char *s = arg->ToString();
				std::cerr << s;
			}
			else
			{
				SharedString ss = arg->DisplayString();
				std::cerr << *ss;
			}
		}
		std::cerr << std::endl;
	}
Exemple #9
0
 void APIBinding::_Print(const ValueList& args, KValueRef result)
 {
     for (size_t c=0; c < args.size(); c++)
     {
         KValueRef arg = args.at(c);
         if (arg->IsString())
         {
             const char *s = arg->ToString();
             std::cout << s;
         }
         else
         {
             SharedString ss = arg->DisplayString();
             std::cout << *ss;
         }
     }
     std::cout.flush();
 }
Exemple #10
0
 static BytesRef ValueToBytes(KValueRef value)
 {
     if (value->IsObject())
     {
         BytesRef bytes = value->ToObject().cast<Bytes>();
         if (bytes.isNull())
             bytes = new Bytes("", 0);
         return bytes;
     }
     else if (value->IsString())
     {
         const char* data = value->ToString();
         return new Bytes(data, strlen(data));
     }
     else
     {
         throw ValueException::FromString("Need a Bytes or a String");
     }
 }
Exemple #11
0
    //---------------- IMPLEMENTATION METHODS
    void APIBinding::Log(int severity, KValueRef value)
    {
        // optimize these calls since they're called a lot
        if (false == logger->IsEnabled((Logger::Level)severity))
        {
            return;
        }

        if (value->IsString())
        {
            string message = value->ToString();
            logger->Log((Logger::Level) severity, message);
        }
        else
        {
            SharedString message = value->DisplayString();
            logger->Log((Logger::Level) severity, *message);
        }
    }
Exemple #12
0
void KObject::GetStringList(const char *name, std::vector<std::string> &list)
{
    KValueRef prop = this->Get(name);
    if(!prop->IsUndefined() && prop->IsList())
    {
        KListRef values = prop->ToList();
        if (values->Size() > 0)
        {
            for (unsigned int c = 0; c < values->Size(); c++)
            {
                KValueRef v = values->At(c);
                if (v->IsString())
                {
                    const char *s = v->ToString();
                    list.push_back(s);
                }
            }
        }
    }
}
 void convert (Statement &select, KValueRef arg)
 {
     if (arg->IsString())
     {
         std::string *s = new std::string(arg->ToString()); 
         select , use(*s);
         strings.push_back(s);
     }
     else if (arg->IsInt())
     {
         int *i = new int(arg->ToInt());
         select , use(*i);
         ints.push_back(i);
     }
     else if (arg->IsDouble())
     {
         double *d = new double(arg->ToDouble());
         select , use(*d);
         doubles.push_back(d);
     }
     else if (arg->IsBool())
     {
         bool *b = new bool(arg->ToBool());
         select , use(*b);
         bools.push_back(b);
     }
     else if (arg->IsNull() || arg->IsUndefined())
     {
         // in this case, we bind a null string (dequoted)
         std::string *s = new std::string("null");
         select , use(s);
         strings.push_back(s);
     }
     else
     {
         throw ValueException::FromFormat("Unsupport type for argument: %s",
             arg->GetType().c_str());
     }
 } 
Exemple #14
0
 Logger::Level APIBinding::ValueToLevel(KValueRef v)
 {
     if (v->IsString())
     {
         string levelString = v->ToString();
         return Logger::GetLevel(levelString);
     }
     else if (v->IsObject())
     {
         SharedString ss = v->ToObject()->DisplayString();
         return Logger::GetLevel(*ss);
     }
     else if (v->IsNumber())
     {
         return (Logger::Level) v->ToInt();
     }
     else // return the appropriate default
     {
         string levelString = "";
         return Logger::GetLevel(levelString);
     }
 }
Exemple #15
0
 static void GetBytes(KValueRef value, std::vector<BytesRef>& blobs)
 {
     if (value->IsObject())
     {
         blobs.push_back(value->ToObject().cast<Bytes>());
     }
     else if (value->IsString())
     {
         blobs.push_back(new Bytes(value->ToString()));
     }
     else if (value->IsList())
     {
         KListRef list = value->ToList();
         for (size_t j = 0; j < list->Size(); j++)
         {
             GetBytes(list->At((int)j), blobs);
         }
     }
     else if (value->IsNumber())
     {
         blobs.push_back(new Bytes(value->ToInt()));
     }
 }
	bool HTTPClientBinding::BeginRequest(KValueRef sendData)
	{
		if (this->Get("connected")->ToBool())
		{
			return false;
		}

		// Reset internal variables for new request
		if (this->dirty)
		{
			this->Reset();
		}

		// Determine what data type we have to send
		if (sendData->IsObject())
		{
			KObjectRef dataObject = sendData->ToObject();
			KMethodRef nativePathMethod(dataObject->GetMethod("nativePath"));
			KMethodRef sizeMethod(dataObject->GetMethod("size"));

			if (nativePathMethod.isNull() || sizeMethod.isNull())
			{
				std::string err("Unsupported File-like object: did not have"
					"nativePath and size methods");
				logger->Error(err);
				throw ValueException::FromString(err);
			}

			const char* filename = nativePathMethod->Call()->ToString();
			if (!filename)
			{
				std::string err("Unsupported File-like object: nativePath method"
					"did not return a String");
				logger->Error(err);
				throw ValueException::FromString(err);
			}

			this->datastream = new std::ifstream(filename,
				std::ios::in | std::ios::binary);
			if (this->datastream->fail())
			{
				std::string err("Failed to open file: ");
				err.append(filename);
				logger->Error(err);
				throw ValueException::FromString(err);
			}

			this->contentLength = sizeMethod->Call()->ToInt();
		}
		else if (sendData->IsString())
		{
			std::string dataString(sendData->ToString());
			if (!dataString.empty())
			{
				this->datastream = new std::istringstream(dataString,
						std::ios::in | std::ios::binary);
				this->contentLength = dataString.length();
			}
		}
		else
		{
			// Sending no data
			this->datastream = 0;
		}

		this->dirty = true;
		this->Set("connected", Value::NewBool(true));

		if (this->async)
		{
			this->thread = new Poco::Thread();
			this->thread->start(*this);
		}
		else
		{
			this->ExecuteRequest();
		}

		return true;
	}
Exemple #17
0
	AutoPtr<PreprocessData> Script::Preprocess(const char *url, KObjectRef scope)
	{
		KObjectRef evaluator = this->FindEvaluatorWithMethod("canPreprocess", url);
		if (!evaluator.isNull())
		{
			KMethodRef preprocess = evaluator->GetMethod("preprocess");
			if (!preprocess.isNull())
			{
				ValueList args;
				args.push_back(Value::NewString(url));
				args.push_back(Value::NewObject(scope));
				
				KValueRef result = preprocess->Call(args);
				
				if (result->IsObject())
				{
					KObjectRef object = result->ToObject();
					AutoPtr<PreprocessData> data = new PreprocessData();
					if (object->HasProperty("data"))
					{
						KValueRef objectData = object->Get("data");
						if (objectData->IsObject())
						{
							BlobRef blobData = objectData->ToObject().cast<Blob>();
							if (!blobData.isNull())
							{
								data->data = blobData;
							}
						}
						else if (objectData->IsString())
						{
							data->data = new Blob(objectData->ToString(), strlen(objectData->ToString()));
						}
					}
					else
					{
						throw ValueException::FromString("Preprocessor didn't return any data");
					}
					if (object->HasProperty("mimeType"))
					{
						data->mimeType = object->Get("mimeType")->ToString();
					}
					else
					{
						throw ValueException::FromString("Preprocessor didn't return a mimeType");
					}
					
					return data;
				}
			}
			else
			{
				throw ValueException::FromFormat(
					"Error preprocessing: No \"preprocess\" method found on evaluator for url: \"%s\"", url);
			}
		}
		else
		{
			throw ValueException::FromFormat("Error preprocessing: No evaluator found for url: \"%s\"", url);
		}
		return 0;
	}