diff options
author | Vadim ProductEngine <vsavchuk@productengine.com> | 2011-09-09 20:09:01 +0300 |
---|---|---|
committer | Vadim ProductEngine <vsavchuk@productengine.com> | 2011-09-09 20:09:01 +0300 |
commit | a21a55482a076aa690ca947411f439dd14d59443 (patch) | |
tree | 35f368a17eb30e9d86157fed6ccb2f02af78c9d3 /indra | |
parent | 2c7e943cd2a0e08e7a8216fded332cac18fcc79c (diff) |
STORM-1577 WIP Added unit tests.
By the way, fixed minor parsing bugs.
Diffstat (limited to 'indra')
-rw-r--r-- | indra/newview/CMakeLists.txt | 7 | ||||
-rw-r--r-- | indra/newview/lltranslate.cpp | 295 | ||||
-rw-r--r-- | indra/newview/lltranslate.h | 77 |
3 files changed, 219 insertions, 160 deletions
diff --git a/indra/newview/CMakeLists.txt b/indra/newview/CMakeLists.txt index e7ca2a4294..635f425540 100644 --- a/indra/newview/CMakeLists.txt +++ b/indra/newview/CMakeLists.txt @@ -1985,12 +1985,19 @@ if (LL_TESTS) llmediadataclient.cpp lllogininstance.cpp llremoteparcelrequest.cpp + lltranslate.cpp llviewerhelputil.cpp llversioninfo.cpp llworldmap.cpp llworldmipmap.cpp ) + set_source_files_properties( + lltranslate.cpp + PROPERTIES + LL_TEST_ADDITIONAL_LIBRARIES "${JSONCPP_LIBRARIES}" + ) + ################################################## # DISABLING PRECOMPILED HEADERS USAGE FOR TESTS ################################################## diff --git a/indra/newview/lltranslate.cpp b/indra/newview/lltranslate.cpp index 895d8f78eb..a74b252c68 100644 --- a/indra/newview/lltranslate.cpp +++ b/indra/newview/lltranslate.cpp @@ -38,194 +38,171 @@ #include "reader.h" -class LLTranslationAPIHandler -{ -public: - virtual void getTranslateURL( - std::string &url, - const std::string &from_lang, - const std::string &to_lang, - const std::string &text) const = 0; - - virtual bool parseResponse( - int& status, - const std::string& body, - std::string& translation, - std::string& detected_lang, - std::string& err_msg) const = 0; - - virtual ~LLTranslationAPIHandler() {} - -protected: - static const int STATUS_OK = 200; -}; - -class LLGoogleHandler : public LLTranslationAPIHandler +// virtual +void LLGoogleTranslationHandler::getTranslateURL( + std::string &url, + const std::string &from_lang, + const std::string &to_lang, + const std::string &text) const { - LOG_CLASS(LLGoogleHandler); - -public: - /*virtual*/ void getTranslateURL( - std::string &url, - const std::string &from_lang, - const std::string &to_lang, - const std::string &text) const + url = std::string("https://www.googleapis.com/language/translate/v2?key=") + + getAPIKey() + "&q=" + LLURI::escape(text) + "&target=" + to_lang; + if (!from_lang.empty()) { - url = std::string("https://www.googleapis.com/language/translate/v2?key=") - + getAPIKey() + "&q=" + LLURI::escape(text) + "&target=" + to_lang; - if (!from_lang.empty()) - { - url += "&source=" + from_lang; - } + url += "&source=" + from_lang; } +} - /*virtual*/ bool parseResponse( - int& status, - const std::string& body, - std::string& translation, - std::string& detected_lang, - std::string& err_msg) const - { - Json::Value root; - Json::Reader reader; - - if (!reader.parse(body, root)) - { - err_msg = reader.getFormatedErrorMessages(); - return false; - } - - if (!root.isObject()) // empty response? should not happen - { - return false; - } - - if (status != STATUS_OK) - { - // Request failed. Extract error message from the response. - parseErrorResponse(root, status, err_msg); - return false; - } +// virtual +bool LLGoogleTranslationHandler::parseResponse( + int& status, + const std::string& body, + std::string& translation, + std::string& detected_lang, + std::string& err_msg) const +{ + Json::Value root; + Json::Reader reader; - // Request succeeded, extract translation from the response. - return parseTranslation(root, translation, detected_lang); + if (!reader.parse(body, root)) + { + err_msg = reader.getFormatedErrorMessages(); + return false; } -private: - static void parseErrorResponse( - const Json::Value& root, - int& status, - std::string& err_msg) + if (!root.isObject()) // empty response? should not happen { - const Json::Value& error = root.get("error", 0); - if (!error.isObject() || !error.isMember("message") || !error.isMember("code")) - { - return; - } + return false; + } - err_msg = error["message"].asString(); - status = error["code"].asInt(); + if (status != STATUS_OK) + { + // Request failed. Extract error message from the response. + parseErrorResponse(root, status, err_msg); + return false; } - static bool parseTranslation( - const Json::Value& root, - std::string& translation, - std::string& detected_lang) + // Request succeeded, extract translation from the response. + return parseTranslation(root, translation, detected_lang); +} + +// static +void LLGoogleTranslationHandler::parseErrorResponse( + const Json::Value& root, + int& status, + std::string& err_msg) +{ + const Json::Value& error = root.get("error", 0); + if (!error.isObject() || !error.isMember("message") || !error.isMember("code")) { - const Json::Value& data = root.get("data", 0); - if (!data.isObject() || !data.isMember("translations")) - { - return false; - } + return; + } - const Json::Value& translations = data["translations"]; - if (!translations.isArray() || translations.size() == 0) - { - return false; - } + err_msg = error["message"].asString(); + status = error["code"].asInt(); +} - const Json::Value& first = translations[0U]; - if (!first.isObject() || !first.isMember("translatedText")) - { - return false; - } +// static +bool LLGoogleTranslationHandler::parseTranslation( + const Json::Value& root, + std::string& translation, + std::string& detected_lang) +{ + const Json::Value& data = root.get("data", 0); + if (!data.isObject() || !data.isMember("translations")) + { + return false; + } - translation = first["translatedText"].asString(); - detected_lang = first.get("detectedSourceLanguage", "").asString(); - return true; + const Json::Value& translations = data["translations"]; + if (!translations.isArray() || translations.size() == 0) + { + return false; } - static std::string getAPIKey() + const Json::Value& first = translations[0U]; + if (!first.isObject() || !first.isMember("translatedText")) { - return gSavedSettings.getString("GoogleTranslateAPIKey"); + return false; } -}; -class LLBingHandler : public LLTranslationAPIHandler + translation = first["translatedText"].asString(); + detected_lang = first.get("detectedSourceLanguage", "").asString(); + return true; +} + +// static +std::string LLGoogleTranslationHandler::getAPIKey() +{ + return gSavedSettings.getString("GoogleTranslateAPIKey"); +} + +// virtual +void LLBingTranslarionHandler::getTranslateURL( + std::string &url, + const std::string &from_lang, + const std::string &to_lang, + const std::string &text) const { - LOG_CLASS(LLBingHandler); - -public: - /*virtual*/ void getTranslateURL( - std::string &url, - const std::string &from_lang, - const std::string &to_lang, - const std::string &text) const + url = std::string("http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=") + + getAPIKey() + "&text=" + LLURI::escape(text) + "&to=" + to_lang; + if (!from_lang.empty()) { - url = std::string("http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=") - + getAPIKey() + "&text=" + LLURI::escape(text) + "&to=" + to_lang; - if (!from_lang.empty()) - { - url += "&from=" + from_lang; - } + url += "&from=" + from_lang; } +} - /*virtual*/ bool parseResponse( - int& status, - const std::string& body, - std::string& translation, - std::string& detected_lang, - std::string& err_msg) const +// virtual +bool LLBingTranslarionHandler::parseResponse( + int& status, + const std::string& body, + std::string& translation, + std::string& detected_lang, + std::string& err_msg) const +{ + if (status != STATUS_OK) { - if (status != STATUS_OK) + static const std::string MSG_BEGIN_MARKER = "Message: "; + size_t begin = body.find(MSG_BEGIN_MARKER); + if (begin != std::string::npos) { - static const std::string MSG_BEGIN_MARKER = "Message: "; - size_t begin = body.find(MSG_BEGIN_MARKER); - if (begin != std::string::npos) - { - begin += MSG_BEGIN_MARKER.size(); - } - size_t end = body.find("</p>", begin); - err_msg = body.substr(begin, end-begin); - LLStringUtil::replaceString(err_msg, "
", ""); // strip CR - return false; + begin += MSG_BEGIN_MARKER.size(); } - - // Sample response: <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hola</string> - size_t begin = body.find(">"); - if (begin == std::string::npos || begin >= (body.size() - 1)) + else { - return false; + begin = 0; + err_msg.clear(); } - - size_t end = body.find("</string>", ++begin); - if (end == std::string::npos || end < begin) - { - return false; - } - - detected_lang = ""; // unsupported by this API - translation = body.substr(begin, end-begin); - LLStringUtil::replaceString(translation, "
", ""); // strip CR - return true; + size_t end = body.find("</p>", begin); + err_msg = body.substr(begin, end-begin); + LLStringUtil::replaceString(err_msg, "
", ""); // strip CR + return false; } -private: - static std::string getAPIKey() + // Sample response: <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hola</string> + size_t begin = body.find(">"); + if (begin == std::string::npos || begin >= (body.size() - 1)) { - return gSavedSettings.getString("BingTranslateAPIKey"); + begin = 0; } -}; + else + { + ++begin; + } + + size_t end = body.find("</string>", begin); + + detected_lang = ""; // unsupported by this API + translation = body.substr(begin, end-begin); + LLStringUtil::replaceString(translation, "
", ""); // strip CR + return true; +} + +// static +std::string LLBingTranslarionHandler::getAPIKey() +{ + return gSavedSettings.getString("BingTranslateAPIKey"); +} LLTranslate::TranslationReceiver::TranslationReceiver(const std::string& from_lang, const std::string& to_lang) : mFromLang(from_lang) @@ -248,6 +225,8 @@ void LLTranslate::TranslationReceiver::completedRaw( const std::string body = strstrm.str(); std::string translation, detected_lang, err_msg; int status = http_status; + LL_DEBUGS("Translate") << "HTTP status: " << status << " " << reason << LL_ENDL; + LL_DEBUGS("Translate") << "Response body: " << body << LL_ENDL; if (mHandler.parseResponse(status, body, translation, detected_lang, err_msg)) { // Fix up the response @@ -268,8 +247,6 @@ void LLTranslate::TranslationReceiver::completedRaw( } llwarns << "Translation request failed: " << err_msg << llendl; - LL_DEBUGS("Translate") << "HTTP status: " << status << " " << reason << LL_ENDL; - LL_DEBUGS("Translate") << "Error response body: " << body << LL_ENDL; handleFailure(status, err_msg); } } @@ -319,8 +296,8 @@ std::string LLTranslate::getTranslateLanguage() // static const LLTranslationAPIHandler& LLTranslate::getPreferredHandler() { - static LLGoogleHandler google; - static LLBingHandler bing; + static LLGoogleTranslationHandler google; + static LLBingTranslarionHandler bing; std::string service = gSavedSettings.getString("TranslationService"); if (service == "google") diff --git a/indra/newview/lltranslate.h b/indra/newview/lltranslate.h index 1dee792f7b..1bf6965fd4 100644 --- a/indra/newview/lltranslate.h +++ b/indra/newview/lltranslate.h @@ -30,7 +30,82 @@ #include "llhttpclient.h" #include "llbufferstream.h" -class LLTranslationAPIHandler; +namespace Json +{ + class Value; +} + +class LLTranslationAPIHandler +{ +public: + virtual void getTranslateURL( + std::string &url, + const std::string &from_lang, + const std::string &to_lang, + const std::string &text) const = 0; + + virtual bool parseResponse( + int& status, + const std::string& body, + std::string& translation, + std::string& detected_lang, + std::string& err_msg) const = 0; + + virtual ~LLTranslationAPIHandler() {} + +protected: + static const int STATUS_OK = 200; +}; + +class LLGoogleTranslationHandler : public LLTranslationAPIHandler +{ + LOG_CLASS(LLGoogleTranslationHandler); + +public: + /*virtual*/ void getTranslateURL( + std::string &url, + const std::string &from_lang, + const std::string &to_lang, + const std::string &text) const; + /*virtual*/ bool parseResponse( + int& status, + const std::string& body, + std::string& translation, + std::string& detected_lang, + std::string& err_msg) const; + +private: + static void parseErrorResponse( + const Json::Value& root, + int& status, + std::string& err_msg); + static bool parseTranslation( + const Json::Value& root, + std::string& translation, + std::string& detected_lang); + static std::string getAPIKey(); +}; + +class LLBingTranslarionHandler : public LLTranslationAPIHandler +{ + LOG_CLASS(LLBingTranslarionHandler); + +public: + /*virtual*/ void getTranslateURL( + std::string &url, + const std::string &from_lang, + const std::string &to_lang, + const std::string &text) const; + /*virtual*/ bool parseResponse( + int& status, + const std::string& body, + std::string& translation, + std::string& detected_lang, + std::string& err_msg) const; +private: + static std::string getAPIKey(); +}; + class LLTranslate { |