From 329fde434a836aba90fc15dd11ea7bf136c210af Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Thu, 5 Feb 2015 09:23:19 +0100 Subject: [PATCH] Add GXmlElement::has_attribute() method and correct tscalc flag handling in GModel. --- ChangeLog | 3 ++- NEWS | 5 +++-- include/GXmlElement.hpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ pyext/GXmlElement.i | 3 ++- src/model/GModelSky.cpp | 14 +++++++------- src/xml/GXmlElement.cpp | 29 ++++++++++++++++++++++++++++- 6 files changed, 111 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 32e1eb8..2897739 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,4 @@ -2015-02-04 Juergen Knoedlseder +2015-02-05 Juergen Knoedlseder * Version 1.0.0 released ======================== @@ -12,6 +12,7 @@ Do not dump unqueried parameters (#1349) Add GBilinear class Add WCS projections (MOL) + Add GXmlElement::has_attribute() method 2015-01-07 Juergen Knoedlseder diff --git a/NEWS b/NEWS index dbf0201..de4b0ed 100644 --- a/NEWS +++ b/NEWS @@ -2,7 +2,7 @@ New Features and Important Changes in GammaLib 1.0.0 Author(s) : Juergen Knoedlseder -4 February 2015 +5 February 2015 1. Introduction @@ -93,7 +93,8 @@ bilinear interpolations within GammaLib. 14. XML module -------------- -None +The method GXmlElement::has_attribute() has been added to check whether an +XML element contains a given attribute. 15. VO module diff --git a/include/GXmlElement.hpp b/include/GXmlElement.hpp index 6311a78..4f63304 100644 --- a/include/GXmlElement.hpp +++ b/include/GXmlElement.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GXmlElement.hpp - XML element node class definition * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2014 by Juergen Knoedlseder * + * copyright (C) 2010-2015 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -61,15 +61,16 @@ public: virtual void clear(void); virtual GXmlElement* clone(void) const; virtual std::string classname(void) const; - const std::string& name(void) const { return m_name; } - void name(const std::string& name) { m_name=name; } + const std::string& name(void) const; + void name(const std::string& name); std::string attribute(const std::string& name) const; void attribute(const std::string& name, const std::string& value); + bool has_attribute(const std::string& name) const; void remove_attribute(const std::string& name); - GXmlNode* parent(void) const { return m_parent; } - void parent(GXmlNode* node) { m_parent = node; } + GXmlNode* parent(void) const; + void parent(GXmlNode* parent); virtual void write(GUrl& url, const int& indent = 0) const; - virtual NodeType type(void) const { return NT_ELEMENT; } + virtual NodeType type(void) const; virtual std::string print(const GChatter& chatter = NORMAL, const int& indent = 0) const; @@ -100,4 +101,66 @@ std::string GXmlElement::classname(void) const return ("GXmlElement"); } + +/***********************************************************************//** + * @brief Return XML element name + * + * @return XML element name. + ***************************************************************************/ +inline +const std::string& GXmlElement::name(void) const +{ + return (m_name); +} + + +/***********************************************************************//** + * @brief Set XML element name + * + * @param[in] name XML element name. + ***************************************************************************/ +inline +void GXmlElement::name(const std::string& name) +{ + m_name = name; + return; +} + + +/***********************************************************************//** + * @brief Return parent of XML element + * + * @return Parent of XML element. + ***************************************************************************/ +inline +GXmlNode* GXmlElement::parent(void) const +{ + return (m_parent); +} + + +/***********************************************************************//** + * @brief Set parent of XML element + * + * @param[in] node Parent of XML element. + ***************************************************************************/ +inline +void GXmlElement::parent(GXmlNode* parent) +{ + m_parent = parent; + return; +} + + +/***********************************************************************//** + * @brief Return XML node type + * + * @return XML node type (NT_ELEMENT). + ***************************************************************************/ +inline +GXmlNode::NodeType GXmlElement::type(void) const +{ + return (NT_ELEMENT); +} + #endif /* GXMLELEMENT_HPP */ diff --git a/pyext/GXmlElement.i b/pyext/GXmlElement.i index 413d82b..125628d 100644 --- a/pyext/GXmlElement.i +++ b/pyext/GXmlElement.i @@ -50,9 +50,10 @@ public: void name(const std::string& name); std::string attribute(const std::string& name) const; void attribute(const std::string& name, const std::string& value); + bool has_attribute(const std::string& name) const; void remove_attribute(const std::string& name); GXmlNode* parent(void) const; - void parent(GXmlNode* node); + void parent(GXmlNode* parent); virtual void write(GUrl& url, const int& indent = 0) const; virtual NodeType type(void) const; }; diff --git a/src/model/GModelSky.cpp b/src/model/GModelSky.cpp index 7a4e04f..cb5cb64 100644 --- a/src/model/GModelSky.cpp +++ b/src/model/GModelSky.cpp @@ -666,16 +666,16 @@ void GModelSky::read(const GXmlElement& xml) name(xml.attribute("name")); // Set model TS - std::string ts = xml.attribute("ts"); - if (ts.length() > 0) { + if (xml.has_attribute("ts")) { + std::string ts = xml.attribute("ts"); this->ts(gammalib::todouble(ts)); } - // Check flag if TS value should be computed - bool tscalc = (xml.attribute("tscalc") == "1") ? true : false; - - // Set flag if TS value should be computed - this->tscalc(tscalc); + // Set TS computation flag + if (xml.has_attribute("tscalc")) { + bool tscalc = (xml.attribute("tscalc") == "1") ? true : false; + this->tscalc(tscalc); + } // Set instruments instruments(xml.attribute("instrument")); diff --git a/src/xml/GXmlElement.cpp b/src/xml/GXmlElement.cpp index c781b8b..7e8d151 100644 --- a/src/xml/GXmlElement.cpp +++ b/src/xml/GXmlElement.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GXmlElement.cpp - XML element node class implementation * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2013 by Juergen Knoedlseder * + * copyright (C) 2010-2015 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -264,6 +264,33 @@ void GXmlElement::attribute(const std::string& name, const std::string& value) /***********************************************************************//** + * @brief Check if element has a given attribute + * + * @param[in] name Attribute name. + * @return True if attribute exists, false otherwise. + * + * Checks whether the element contains an attribute with @p name. If the + * attribute was found true is returned, false otherwise. + ***************************************************************************/ +bool GXmlElement::has_attribute(const std::string& name) const +{ + // Initialise found flag + bool found = false; + + // Search attribute value in list of attributes + for (int i = 0; i < m_attr.size(); ++i) { + if (m_attr[i]->name() == name) { + found = true; + break; + } + } + + // Return found flag + return found; +} + + +/***********************************************************************//** * @brief Remove attribute from element * * @param[in] name Attribute name. -- libgit2 0.21.4