-
-
Save mattytrentini/657605 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//-------------------------------------------------------------------------------------------------- | |
// hpp | |
#ifndef xml_helpers | |
#define xml_helpers | |
#include <xmlpp/libxml++.h> | |
#include "boost/tr1/memory.hpp" | |
namespace util | |
{ | |
class Element | |
{ | |
public: | |
explicit Element(const std::string name); | |
Element & attrib(const std::string &name, const std::string &value); | |
Element & text(const std::string &label); | |
Element & add_children(const Element& elem); | |
Element & add_children(const Element& elem, const Element& elem4); | |
Element & add_children(const Element& elem, const Element& elem2, const Element& elem3); | |
operator std::tr1::shared_ptr<xmlpp::Document>(); | |
private: | |
std::tr1::shared_ptr<xmlpp::Document> document; | |
}; | |
}// namespace util | |
#endif | |
//-------------------------------------------------------------------------------------------------- | |
// cpp | |
//#include "helpers.hpp" | |
Elem::Elem(const std::string name) : | |
document(new xmlpp::Document()) | |
{ | |
document->create_root_node(name); | |
} | |
Elem & Elem::attrib(const std::string &name, const std::string &value) | |
{ | |
document->get_root_node()->set_attribute(name, value); | |
return *this; | |
} | |
Elem & Elem::insert_siblings(const Elem& elem) | |
{ | |
document->get_root_node()->import_node(elem.document->get_root_node()); | |
return *this; | |
} | |
Elem & Elem::insert_siblings(const Elem& elem, const Elem& elem2) | |
{ | |
insert_siblings(elem); | |
return insert_siblings(elem2); | |
} | |
Elem & Elem::insert_siblings(const Elem& elem, const Elem& elem2, const Elem& elem3) | |
{ | |
insert_siblings(elem, elem2); | |
return insert_siblings(elem3); | |
} | |
Elem & Elem::text(const std::string &label) | |
{ | |
document->get_root_node()->add_child_text(label); | |
return *this; | |
} | |
Elem::operator xmlpp::Element*() | |
{ | |
return document->get_root_node(); | |
} | |
Elem::operator std::tr1::shared_ptr<xmlpp::Document>() | |
{ | |
return document; | |
} | |
Elem create_element(const std::string name) | |
{ | |
return Elem(name); | |
} | |
//-------------------------------------------------------------------------------------------------- | |
#include <vector> | |
using std::string; | |
using std::vector; | |
class Event | |
{ | |
public: | |
std::string get_source() const { return ""; } | |
std::string get_description() const { return ""; } | |
}; | |
class Event_severity | |
{ | |
public: | |
bool operator!=(const Event_severity&) { return true; } | |
}; | |
class EventLogger | |
{ | |
public: | |
vector<std::string> get_active_events() { return vector<std::string>(); } | |
Event_severity get_event_severity(std::string) { return Event_severity(); } | |
std::string get_event_description(std::string) { return ""; } | |
vector<Event> get_active_event_data(std::string) { return vector<Event>(); } | |
}; | |
std::string Event_severity_to_string(const Event_severity&) { return ""; } | |
EventLogger* event_logger; | |
//-------------------------------------------------------------------------------------------------- | |
boost::shared_ptr <xmlpp::Document> test( int limit_response, const string& filter_id, const Event_severity& filter_severity ) | |
{ | |
Elem active_events_node = create_element("active_events"); //G> boost::shared_ptr<xmlpp::Document> document_ptr(new xmlpp::Document); | |
//G> xmlpp::Element* root_node = document_ptr->create_root_node(""); | |
//G> xmlpp::Element* active_events_node = root_node->add_child( "active_events" ); | |
vector<std::string> active_events = event_logger->get_active_events( ); | |
for( vector<std::string>::iterator it; it != active_events.end(); ++it ) | |
{ | |
// Check filter event id match | |
if( !filter_id.empty() && | |
filter_id != *it ) | |
{ | |
continue; | |
} | |
// We'll get all the information we need about this event | |
Event_severity event_severity = event_logger->get_event_severity( *it ); | |
string event_description = event_logger->get_event_description( *it ); | |
vector<Event> active_event_sources = event_logger->get_active_event_data( *it ); | |
// Check if event severity is a match // TODO TODO TODO what if no filter present? use boost optional here? | |
if( event_severity != filter_severity ) | |
{ | |
continue; | |
} | |
Elem event_node = create_element("event").attrib("id", *it); //G> xmlpp::Element* event_node = active_events_node->add_child( "event" ); | |
//G> event_node->set_attribute( "id", *it ); | |
// Add the severity | |
event_node.insert_siblings(create_element("severity").text(Event_severity_to_string( event_severity ))); //G> xmlpp::Element* severity_node = event_node->add_child( "severity" ); | |
//G> severity_node->add_child_text( Event_severity_to_string( event_severity ) ); | |
// Add the description | |
event_node.insert_siblings(create_element("description").text(event_description)); //G> xmlpp::Element* description_node = event_node->add_child( "description" ); | |
//G> description_node->add_child_text( event_description ); | |
// Add source information | |
Elem sources_node = create_element("sources"); //G> xmlpp::Element* sources_node = event_node->add_child( "sources" ); | |
for( vector<Event>::const_iterator event_it = active_event_sources.begin(); event_it != active_event_sources.end(); ++event_it) | |
{ | |
Elem source_node = create_element("source").attrib("id", event_it->get_source()); //G> xmlpp::Element* source_node = sources_node->add_child( "source" ); | |
//G> source_node->set_attribute( "id", event_it->get_source() ); | |
source_node.insert_siblings(create_element("timestamp").text("...")); //G> xmlpp::Element* timestamp_node = source_node->add_child( "timestamp" ); | |
//TODO figure out how to convert this to a string | |
//timestamp_node->add_child_text( boost::time::to_iso_string( event_it->get_timestamp() ) ); | |
source_node.insert_siblings(create_element("description").text(event_it->get_description())); //G> xmlpp::Element* event_detail_node = source_node->add_child( "description" ); | |
//G> event_detail_node->add_child_text( event_it->get_description() ); | |
sources_node.insert_siblings(source_node); //G> | |
} | |
event_node.insert_siblings(sources_node); //G> | |
active_events_node.insert_siblings(event_node); //G> | |
} | |
return active_events_node; //G> return document_ptr; | |
} | |
int main() | |
{ | |
return 0; | |
} | |
//-------------------------------------------------------------------------------------------------- | |
//namespace helper | |
//{ | |
// class Element | |
// { | |
// public: | |
// | |
// explicit Element(const std::string& root_name = "") : | |
// m_doc(new xmlpp::Document) | |
// { | |
// m_doc->create_root_node(root_name); | |
// } | |
// | |
// std::string str() const | |
// { | |
// return m_doc->write_to_string_formatted(); | |
// } | |
// | |
// Element& operator=(const std::string& text) | |
// { | |
// m_doc->get_root_node()->add_child_text(text); | |
// return *this; | |
// } | |
// | |
// Element& operator<<(const Element& right) | |
// { | |
// xmlpp::Element* right_root = right.m_doc->get_root_node(); | |
// if (!right_root->get_name().empty()) | |
// { | |
// m_doc->get_root_node()->import_node(right_root); | |
// } | |
// else | |
// { | |
// // if the root node doesn't have a name, we import its children | |
// const xmlpp::Node::NodeList children = right_root->get_children(); | |
// for (xmlpp::Node::NodeList::const_iterator iter = children.begin(); iter != children.end(); ++iter) | |
// { | |
// m_doc->get_root_node()->import_node(*iter); | |
// } | |
// } | |
// return *this; | |
// } | |
// | |
// private: | |
// | |
// boost::shared_ptr<xmlpp::Document> m_doc; | |
// }; | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment