Skip to content

Commit

Permalink
[JMT] Add 'this_and_childs_have_attributes' and 'this_and_childs_have…
Browse files Browse the repository at this point in the history
…_both_value_and_children'
  • Loading branch information
Juan Manzanero committed Mar 14, 2024
1 parent e8adcdf commit fdf54c0
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
58 changes: 56 additions & 2 deletions lion/io/Xml_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Xml_element
void set_name(const std::string& name) { _e->SetName(name.c_str()); }

//! Get value as string
std::string get_value() { return (_e->GetText() == nullptr) ? "" : _e->GetText(); }
std::string get_value(std::string&&) { return get_value(); }
std::string get_value() const { return (_e->GetText() == nullptr) ? "" : _e->GetText(); }
std::string get_value(std::string&&) const { return get_value(); }

//! Get value as double
//! @param[in] simply pass int() to overload this version
Expand Down Expand Up @@ -217,6 +217,15 @@ class Xml_element
throw lion_exception("[ERROR] delete_attribute -> attribute does not exist");
}


//! Check if this element and its childs have any attribute
bool this_and_childs_have_attributes() const;


//! Check if this element and its childs have value and children
bool this_and_childs_have_both_value_and_children() const;


private:
tinyxml2::XMLElement* _e;
};
Expand All @@ -229,12 +238,14 @@ inline void Xml_element::print(std::ostream& os) const
os << printer.CStr();
}


inline std::ostream& operator<<(std::ostream& os, const Xml_element& e)
{
e.print(os);
return os;
}


inline void Xml_element::copy_contents(Xml_element other)
{
tinyxml2::XMLElement* element = other._e->FirstChildElement();
Expand Down Expand Up @@ -464,6 +475,7 @@ void Xml_element::emplace_children_and_values_in_map(MapType &m)
}
}


template<typename T>
void Xml_element::emplace_back_children_and_values_in_vector_of_pairs(std::vector<std::pair<std::string, T> > &vp)
{
Expand Down Expand Up @@ -534,6 +546,7 @@ inline bool Xml_element::has_child(const std::string& name) const
return child.has_child(the_rest);
}


inline std::string Xml_element::get_attribute(const std::string& attribute, std::string&&) const
{
if ( has_attribute(attribute) )
Expand All @@ -547,4 +560,45 @@ inline std::string Xml_element::get_attribute(const std::string& attribute, std:
}
}


inline bool Xml_element::this_and_childs_have_attributes() const
{
// check if this element has attributes
if (_e -> FirstAttribute() != nullptr) {
return true;
}
else {

// check if children have attributes
for (const auto& child : get_children()) {
if (child.this_and_childs_have_attributes()) {
return true;
}
}

}

return false;
}


inline bool Xml_element::this_and_childs_have_both_value_and_children() const
{
if (!(get_value().empty()) && (get_children().size() > 0u)) {
return true;
}
else {

// check if children have attributes
for (const auto& child : get_children()) {
if (child.this_and_childs_have_both_value_and_children()) {
return true;
}
}

}

return false;
}

#endif
48 changes: 48 additions & 0 deletions test/io/xml_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,51 @@ TEST_F(Xml_test, deep_copy)
EXPECT_EQ(s_out.str(), s_out_ref.str());

}


TEST_F(Xml_test, this_and_child_have_attributes)
{
auto xmldef = "<test>"
"<abc>"
"<dfg>"
"<qwer aaaa=\"\"/>"
"</dfg>"
"</abc>"
"<qwe abc=\"auuu\">"
"</qwe>"
"</test>";

Xml_document doc;
doc.parse(xmldef);

ASSERT_TRUE(doc.get_root_element().this_and_childs_have_attributes());

doc.get_element("test/abc/dfg/qwer").delete_attribute("aaaa");
ASSERT_TRUE(doc.get_root_element().this_and_childs_have_attributes());

doc.get_element("test/qwe").delete_attribute("abc");
ASSERT_FALSE(doc.get_root_element().this_and_childs_have_attributes());
}


TEST_F(Xml_test, this_and_childs_have_both_value_and_children)
{

auto xmldef = "<test>"
"<abc>"
"<auu>"
" valueee "
" <childd/>"
"</auu>"
"</abc>"
"</test>";

Xml_document doc;
doc.parse(xmldef);

ASSERT_TRUE(doc.get_root_element().this_and_childs_have_both_value_and_children());

doc.get_element("test/abc/auu").set_value("");
ASSERT_FALSE(doc.get_root_element().this_and_childs_have_both_value_and_children());

}

0 comments on commit fdf54c0

Please sign in to comment.