Improve tools and usage of XML encoding, fixes #1982

This commit is contained in:
Marianne Gagnon 2015-02-14 18:30:23 -05:00
parent e7a9f8f95d
commit e6aea6e0b6
10 changed files with 39 additions and 32 deletions

View File

@ -66,7 +66,7 @@ Addon::Addon(const XMLNode &xml)
std::string designer;
xml.get("name", &name );
m_name = StringUtils::decodeFromHtmlEntities(name);
m_name = StringUtils::xmlDecode(name);
m_dir_name = StringUtils::toLowerCase(name);
xml.get("id", &m_dir_name );
m_id = createAddonId(m_dir_name);
@ -83,8 +83,8 @@ Addon::Addon(const XMLNode &xml)
xml.get("file", &m_zip_file );
xml.get("description", &description );
m_description = StringUtils::decodeFromHtmlEntities(description);
m_designer = StringUtils::decodeFromHtmlEntities(designer);
m_description = StringUtils::xmlDecode(description);
m_designer = StringUtils::xmlDecode(designer);
// resolve XML entities
//m_description = StringUtils::replace(m_description, "
", "\n");
@ -144,10 +144,10 @@ void Addon::writeXML(std::ofstream *out_stream)
// We write m_dir_name as 'id' to stay backwards compatible
(*out_stream) << " <" << m_type
<< " name=\""
<< StringUtils::encodeToHtmlEntities(m_name)
<< StringUtils::xmlEncode(m_name)
<< "\" id=\"" << m_dir_name
<< "\" designer=\""
<< StringUtils::encodeToHtmlEntities(m_designer)
<< StringUtils::xmlEncode(m_designer)
<< "\" status=\"" << m_status
<< "\" date=\"" << m_date
<< "\" installed=\""

View File

@ -91,9 +91,9 @@ MusicInformation::MusicInformation(const XMLNode *root,
// --------------------------
std::string s;
root->get("title", &s );
m_title = StringUtils::decodeFromHtmlEntities(s);
m_title = StringUtils::xmlDecode(s);
root->get("composer", &s );
m_composer = StringUtils::decodeFromHtmlEntities(s);
m_composer = StringUtils::xmlDecode(s);
root->get("file", &m_normal_filename);
root->get("gain", &m_gain );
root->get("tracks", &m_all_tracks );

View File

@ -214,7 +214,7 @@ void PlayerManager::load()
if(current)
{
stringw name;
current->get("player", &name);
current->getAndDecode("player", &name);
m_current_player = getPlayer(name);
}
@ -269,7 +269,7 @@ void PlayerManager::save()
if(m_current_player)
{
players_file << L" <current player=\""
<< m_current_player->getName() << L"\"/>\n";
<< StringUtils::xmlEncode(m_current_player->getName()) << L"\"/>\n";
}
// Save all non-guest players

View File

@ -78,7 +78,7 @@ PlayerProfile::PlayerProfile(const XMLNode* node)
m_achievements_status = NULL;
m_icon_filename = "";
node->get("name", &m_local_name );
node->getAndDecode("name", &m_local_name);
node->get("guest", &m_is_guest_account );
node->get("use-frequency", &m_use_frequency );
node->get("unique-id", &m_unique_id );
@ -196,7 +196,7 @@ const std::string PlayerProfile::getIconFilename() const
*/
void PlayerProfile::save(UTFWriter &out)
{
out << L" <player name=\"" << m_local_name
out << L" <player name=\"" << StringUtils::xmlEncode(m_local_name)
<< L"\" guest=\"" << m_is_guest_account
<< L"\" use-frequency=\"" << m_use_frequency << L"\"\n";

View File

@ -192,6 +192,17 @@ int XMLNode::get(const std::string &attribute, core::stringw *value) const
return 1;
} // get
// ----------------------------------------------------------------------------
int XMLNode::getAndDecode(const std::string &attribute, core::stringw *value) const
{
if (m_attributes.empty()) return 0;
std::map<std::string, core::stringw>::const_iterator o;
o = m_attributes.find(attribute);
if (o == m_attributes.end()) return 0;
std::string raw_value = core::stringc(o->second).c_str();
*value = StringUtils::xmlDecode(raw_value);
return 1;
} // get
// ----------------------------------------------------------------------------
int XMLNode::get(const std::string &attribute, core::vector2df *value) const
{
std::string s = "";

View File

@ -73,6 +73,7 @@ public:
unsigned int getNumNodes() const {return (unsigned int) m_nodes.size(); }
int get(const std::string &attribute, std::string *value) const;
int get(const std::string &attribute, core::stringw *value) const;
int getAndDecode(const std::string &attribute, core::stringw *value) const;
int get(const std::string &attribute, int32_t *value) const;
int get(const std::string &attribute, uint16_t *value) const;
int get(const std::string &attribute, uint32_t *value) const;

View File

@ -39,7 +39,7 @@ namespace Online
m_max_players = 0;
xml.get("name", &m_lower_case_name);
m_name = StringUtils::decodeFromHtmlEntities(m_lower_case_name);
m_name = StringUtils::xmlDecode(m_lower_case_name);
m_lower_case_name = StringUtils::toLowerCase(m_lower_case_name);
xml.get("id", &m_server_id);

View File

@ -480,7 +480,7 @@ void Track::loadTrackInfo()
std::string designer;
root->get("designer", &designer);
m_designer = StringUtils::decodeFromHtmlEntities(designer);
m_designer = StringUtils::xmlDecode(designer);
root->get("version", &m_version);
std::vector<std::string> filenames;

View File

@ -574,11 +574,11 @@ namespace StringUtils
}
// ------------------------------------------------------------------------
/** Converts ASCII text with HTML entities (e.g. &xE9;) to unicode strings
/** Converts ASCII text with XML entities (e.g. &x00;) to unicode strings
* \param input The input string which should be decoded.
* \return A irrlicht wide string with unicode characters.
*/
irr::core::stringw decodeFromHtmlEntities(const std::string& input)
irr::core::stringw xmlDecode(const std::string& input)
{
irr::core::stringw output;
std::string entity;
@ -659,35 +659,30 @@ namespace StringUtils
}
return output;
} // decodeFromHtmlEntities
} // xmlDecode
// ------------------------------------------------------------------------
/** Converts a unicode string to plain ASCII using html-like & codes.
/** Converts a unicode string to plain ASCII using XML entites (e.g. &x00;)
* \param s The input string which should be encoded.
* \return A std:;string with ASCII characters.
*/
std::string encodeToHtmlEntities(const irr::core::stringw &s)
std::string xmlEncode(const irr::core::stringw &s)
{
std::ostringstream output;
for(unsigned int i=0; i<s.size(); i++)
{
if(s[i]=='&')
output<<"&amp;";
else
if (s[i] >= 128 || s[i] == '&' || s[i] == '<' || s[i] == '>' || s[i] == '\"')
{
if(s[i]<128)
{
irr::c8 c=(char)(s[i]);
output<<c;
output << "&#x" << std::hex << std::uppercase << s[i] << ";";
}
else
{
output <<"&#x" << std::hex <<std::uppercase<< s[i]<<";";
}
irr::c8 c = (char)(s[i]);
output << c;
}
}
return output.str();
} // encodeToHtmlEntities
} // xmlEncode
// ------------------------------------------------------------------------
/** Converts a version string (in the form of 'X.Y.Za-rcU' into an

View File

@ -59,9 +59,9 @@ namespace StringUtils
std::vector<std::string> splitPath(const std::string& path);
std::string replace(const std::string& other, const std::string& from, const std::string& to);
irr::core::stringw decodeFromHtmlEntities(const std::string& input);
irr::core::stringw xmlDecode(const std::string& input);
std::string encodeToHtmlEntities(const irr::core::stringw &output);
std::string xmlEncode(const irr::core::stringw &output);
// ------------------------------------------------------------------------
template <class T>