[bookmarks] Added CDATA escaping for special characters in kml

This commit is contained in:
Alex Zolotarev 2012-11-30 17:04:03 -08:00 committed by Alex Zolotarev
parent 2beef410f8
commit 8bd758a698
2 changed files with 42 additions and 7 deletions

View file

@ -351,19 +351,38 @@ char const * kmlFooter =
"</kml>\n";
}
namespace
{
// According to kml/xml spec, we need to escape special symbols inside CDATA
inline bool ShouldUseCDATA(string const & s)
{
return s.find_first_of("<&") != string::npos;
}
}
void BookmarkCategory::SaveToKML(ostream & s)
{
s << kmlHeader;
s << " <name>" << GetName() <<"</name>\n";
// Use CDATA if we have special symbols in the name
if (ShouldUseCDATA(GetName()))
s << " <name><![CDATA[" << GetName() << "]]></name>\n";
else
s << " <name>" << GetName() << "</name>\n";
s << " <visibility>" << (IsVisible() ? "1" : "0") <<"</visibility>\n";
for (size_t i = 0; i < m_bookmarks.size(); ++i)
{
Bookmark const * bm = m_bookmarks[i];
s << " <Placemark>\n"
<< " <name>" << bm->GetName() << "</name>\n"
<< " <styleUrl>#" << bm->GetType() << "</styleUrl>\n"
s << " <Placemark>\n";
// Use CDATA if we have special symbols in the name
if (ShouldUseCDATA(bm->GetName()))
s << " <name><![CDATA[" << bm->GetName() << "]]></name>\n";
else
s << " <name>" << bm->GetName() << "</name>\n";
s << " <styleUrl>#" << bm->GetType() << "</styleUrl>\n"
<< " <Point>\n"
<< " <coordinates>" << PointToString(bm->GetOrg()) << "</coordinates>\n"
<< " </Point>\n";

View file

@ -88,7 +88,15 @@ char const * kmlString =
"<description><![CDATA[]]></description>"
"<styleUrl>#placemark-blue</styleUrl>"
"<Point>"
"<coordinates>27.566765,53.900047,0.000000</coordinates>"
"<coordinates>27.566765,53.900047,0</coordinates>"
"</Point>"
"</Placemark>"
"<Placemark>"
"<name><![CDATA[<MWM & Sons>]]></name>"
"<description><![CDATA[Amps & <brackets>]]></description>"
"<styleUrl>#placemark-green</styleUrl>"
"<Point>"
"<coordinates>27.551532,53.89306</coordinates>"
"</Point>"
"</Placemark>"
"</Document>"
@ -96,7 +104,7 @@ char const * kmlString =
void CheckBookmarks(BookmarkCategory const & cat)
{
TEST_EQUAL(cat.GetBookmarksCount(), 3, ());
TEST_EQUAL(cat.GetBookmarksCount(), 4, ());
Bookmark const * bm = cat.GetBookmark(0);
TEST_EQUAL(bm->GetName(), "Nebraska", ());
@ -107,10 +115,18 @@ char const * kmlString =
TEST_EQUAL(bm->GetType(), "placemark-pink", ());
bm = cat.GetBookmark(2);
m2::PointD const org = bm->GetOrg();
m2::PointD org = bm->GetOrg();
TEST_ALMOST_EQUAL(MercatorBounds::XToLon(org.x), 27.566765, ());
TEST_ALMOST_EQUAL(MercatorBounds::YToLat(org.y), 53.900047, ());
TEST_EQUAL(bm->GetName(), "From: Минск, Минская область, Беларусь", ());
TEST_EQUAL(bm->GetType(), "placemark-blue", ());
bm = cat.GetBookmark(3);
org = bm->GetOrg();
TEST_ALMOST_EQUAL(MercatorBounds::XToLon(org.x), 27.551532, ());
TEST_ALMOST_EQUAL(MercatorBounds::YToLat(org.y), 53.89306, ());
TEST_EQUAL(bm->GetName(), "<MWM & Sons>", ());
}
}