diff --git a/include/ObjectXML.h b/include/ObjectXML.h index 051dc619c7..fe22774312 100644 --- a/include/ObjectXML.h +++ b/include/ObjectXML.h @@ -78,6 +78,15 @@ public: */ void xpath(int& value, const char * xpath_expr, const int& def); + /** + * Gets and sets a xpath attribute, if the attribute is not found a default + * is used + * @param value to set + * @param xpath_expr of the xml element + * @param def default value if the element is not found + */ + void xpath(time_t& value, const char * xpath_expr, const time_t& def); + /** * Gets the value of an element from an xml string * @param value the value of the element diff --git a/src/host/Host.cc b/src/host/Host.cc index f856bfc092..eff18143e0 100644 --- a/src/host/Host.cc +++ b/src/host/Host.cc @@ -224,25 +224,23 @@ string& Host::to_xml(string& xml) const int Host::from_xml(const string& xml) { vector content; + int int_state; // Initialize the internal XML object update_from_str(xml); - oid = atoi(((*this)["/HOST/ID"] )[0].c_str() ); - hostname = ((*this)["/HOST/NAME"])[0]; - state = static_cast( atoi(((*this)["/HOST/STATE"])[0].c_str()) ); + xpath(oid, "/HOST/ID", -1); + xpath(hostname, "/HOST/NAME", "not_found"); + xpath(int_state, "/HOST/STATE", 0); + state = static_cast( int_state ); -// TODO: create an ObjectXML method to allow this syntax: -// im_mad_name = xpath("/HOST/IM_MAD", "im_default"); + xpath(im_mad_name, "/HOST/IM_MAD", "not_found"); + xpath(vmm_mad_name, "/HOST/VM_MAD", "not_found"); + xpath(tm_mad_name, "/HOST/TM_MAD", "not_found"); - im_mad_name = ((*this)["/HOST/IM_MAD"])[0]; - vmm_mad_name = ((*this)["/HOST/VM_MAD"])[0]; - tm_mad_name = ((*this)["/HOST/TM_MAD"])[0]; + xpath(last_monitored, "/HOST/LAST_MON_TIME", 0); - - last_monitored = static_cast( atoi(((*this)["/HOST/LAST_MON_TIME"] )[0].c_str() ) ); - - cluster = ((*this)["/HOST/CLUSTER"])[0]; + xpath(cluster, "/HOST/CLUSTER", "not_found"); ObjectXML::get_nodes("/HOST/HOST_SHARE", content); host_share.from_xml_node( content[0] ); diff --git a/src/host/HostShare.cc b/src/host/HostShare.cc index a656c74761..964e60d02f 100644 --- a/src/host/HostShare.cc +++ b/src/host/HostShare.cc @@ -92,23 +92,23 @@ int HostShare::from_xml_node(const xmlNodePtr node) // Initialize the internal XML object ObjectXML::update_from_node(node); - disk_usage = atoi(((*this)["/HOST_SHARE/DISK_USAGE"] )[0].c_str() ); - mem_usage = atoi(((*this)["/HOST_SHARE/MEM_USAGE"] )[0].c_str() ); - cpu_usage = atoi(((*this)["/HOST_SHARE/CPU_USAGE"] )[0].c_str() ); + xpath(disk_usage, "/HOST_SHARE/DISK_USAGE", -1); + xpath(mem_usage, "/HOST_SHARE/MEM_USAGE", -1); + xpath(cpu_usage, "/HOST_SHARE/CPU_USAGE", -1); - max_disk = atoi(((*this)["/HOST_SHARE/MAX_DISK"] )[0].c_str() ); - max_mem = atoi(((*this)["/HOST_SHARE/MAX_MEM"] )[0].c_str() ); - max_cpu = atoi(((*this)["/HOST_SHARE/MAX_CPU"] )[0].c_str() ); + xpath(max_disk, "/HOST_SHARE/MAX_DISK", -1); + xpath(max_mem , "/HOST_SHARE/MAX_MEM", -1); + xpath(max_cpu , "/HOST_SHARE/MAX_CPU", -1); - free_disk = atoi(((*this)["/HOST_SHARE/FREE_DISK"] )[0].c_str() ); - free_mem = atoi(((*this)["/HOST_SHARE/FREE_MEM"] )[0].c_str() ); - free_cpu = atoi(((*this)["/HOST_SHARE/FREE_CPU"] )[0].c_str() ); + xpath(free_disk, "/HOST_SHARE/FREE_DISK", -1); + xpath(free_mem , "/HOST_SHARE/FREE_MEM", -1); + xpath(free_cpu , "/HOST_SHARE/FREE_CPU", -1); - used_disk = atoi(((*this)["/HOST_SHARE/USED_DISK"] )[0].c_str() ); - used_mem = atoi(((*this)["/HOST_SHARE/USED_MEM"] )[0].c_str() ); - used_cpu = atoi(((*this)["/HOST_SHARE/USED_CPU"] )[0].c_str() ); + xpath(used_disk, "/HOST_SHARE/USED_DISK", -1); + xpath(used_mem , "/HOST_SHARE/USED_MEM", -1); + xpath(used_cpu , "/HOST_SHARE/USED_CPU", -1); - running_vms = atoi(((*this)["/HOST_SHARE/RUNNING_VMS"] )[0].c_str() ); + xpath(running_vms,"/HOST_SHARE/RUNNING_VMS",-1); return 0; } diff --git a/src/xml/ObjectXML.cc b/src/xml/ObjectXML.cc index f08973b6bc..92c0431e5d 100644 --- a/src/xml/ObjectXML.cc +++ b/src/xml/ObjectXML.cc @@ -179,6 +179,18 @@ void ObjectXML::xpath(int& value, const char * xpath_expr, const int& def) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +void ObjectXML::xpath(time_t& value, const char * xpath_expr, const time_t& def) +{ + int int_val; + int int_def = static_cast(def); + + xpath(int_val, xpath_expr, int_def); + value = static_cast(int_val); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int ObjectXML::xpath_value(string& value,const char *doc,const char *the_xpath) { int rc = 0;