GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GXmlElement Class Reference

XML element node class. More...

#include <GXmlElement.hpp>

Inheritance diagram for GXmlElement:
GXmlNode GContainer GBase

Public Member Functions

 GXmlElement (void)
 Void constructor. More...
 
 GXmlElement (const GXmlElement &node)
 Copy constructor. More...
 
 GXmlElement (const std::string &segment)
 Segment constructor. More...
 
 GXmlElement (const std::string &name, const int &value)
 Integer value constructor. More...
 
 GXmlElement (const std::string &name, const double &value)
 Floating point value constructor. More...
 
 GXmlElement (const std::string &name, const std::string &value)
 String value constructor. More...
 
virtual ~GXmlElement (void)
 Destructor. More...
 
GXmlElementoperator= (const GXmlElement &node)
 Assignment operator. More...
 
virtual void clear (void)
 Clear XML element. More...
 
virtual GXmlElementclone (void) const
 Clone XML element. More...
 
virtual std::string classname (void) const
 Return class name. More...
 
const std::string & name (void) const
 Return XML element name. More...
 
void name (const std::string &name)
 Set XML element name. More...
 
std::string value (void) const
 Return string value. More...
 
std::string string (void) const
 Return string value. More...
 
double real (void) const
 Return floating point value. More...
 
int integer (void) const
 Return integer value. More...
 
int attributes (void) const
 Return number of attributes. More...
 
const GXmlAttributeattribute (const int &index) const
 Return attribute. More...
 
std::string attribute (const std::string &name) const
 Return attribute value. More...
 
void attribute (const std::string &name, const std::string &value)
 Set attribute value. More...
 
bool has_attribute (const std::string &name) const
 Check if element has a given attribute. More...
 
void remove_attribute (const std::string &name)
 Remove attribute from element. More...
 
virtual void write (GUrl &url, const int &indent=0) const
 Write element into URL. More...
 
virtual NodeType type (void) const
 Return XML node type. More...
 
virtual std::string print (const GChatter &chatter=NORMAL, const int &indent=0) const
 Print XML element. More...
 
- Public Member Functions inherited from GXmlNode
 GXmlNode (void)
 Void constructor. More...
 
 GXmlNode (const GXmlNode &node)
 Copy constructor. More...
 
virtual ~GXmlNode (void)
 Destructor. More...
 
GXmlNodeoperator= (const GXmlNode &node)
 Assignment operator. More...
 
GXmlNodeoperator[] (const int &index)
 Return pointer to XML child node. More...
 
const GXmlNodeoperator[] (const int &index) const
 Return pointer to XML child node (const version) More...
 
virtual int size (void) const
 Return number of child nodes. More...
 
virtual bool is_empty (void) const
 Signals if node has no child nodes. More...
 
virtual GXmlNodeset (const int &index, const GXmlNode &node)
 Set XML child node. More...
 
virtual GXmlNodeappend (const GXmlNode &node)
 Append XML child node. More...
 
virtual GXmlElementappend (const std::string &segment)
 Append XML element child node. More...
 
virtual GXmlNodeinsert (const int &index, const GXmlNode &node)
 Insert XML child node. More...
 
virtual void remove (const int &index)
 Remove XML child node. More...
 
virtual void reserve (const int &num)
 Reserve space for child nodes. More...
 
virtual void extend (const GXmlNode &node)
 Append all XML child nodes from another XML node. More...
 
GXmlNodeparent (void) const
 Return parent XML node. More...
 
void parent (GXmlNode *parent)
 Set parent of XML node. More...
 
GFilename filename (void) const
 Return filename of XML file. More...
 
virtual int elements (void) const
 Return number of GXMLElement children of node. More...
 
virtual int elements (const std::string &name) const
 Return number of GXMLElement children with a given name. More...
 
virtual GXmlElementelement (const int &index)
 Return pointer to GXMLElement child. More...
 
virtual const GXmlElementelement (const int &index) const
 Return pointer to GXMLElement child (const variant) More...
 
virtual GXmlElementelement (const std::string &name)
 Return pointer on child walking down a hierarchy of tags. More...
 
virtual const GXmlElementelement (const std::string &name) const
 Return pointer on child walking down a hierarchy of tags (const version) More...
 
virtual GXmlElementelement (const std::string &name, const int &index)
 Return pointer on GXMLElement child of a given name. More...
 
virtual const GXmlElementelement (const std::string &name, const int &index) const
 Return pointer on GXMLElement child of a given name (const variant) More...
 
virtual std::string print (const GChatter &chatter=NORMAL) const
 Print XML node in string. More...
 
- Public Member Functions inherited from GContainer
virtual ~GContainer (void)
 Destructor. More...
 
- Public Member Functions inherited from GBase
virtual ~GBase (void)
 Destructor. More...
 

Protected Member Functions

void init_members (void)
 Initialise class members. More...
 
void copy_members (const GXmlElement &node)
 Copy class members. More...
 
void free_members (void)
 Delete class members. More...
 
void parse_start (const std::string &segment)
 Parse element start segment string. More...
 
void parse_stop (const std::string &segment)
 Parse element stop segment string. More...
 
void parse_attribute (size_t *pos, const std::string &segment)
 Parse element attribute. More...
 
- Protected Member Functions inherited from GXmlNode
void init_members (void)
 Initialise class members. More...
 
void copy_members (const GXmlNode &node)
 Copy class members. More...
 
void free_members (void)
 Delete class members. More...
 
int extract_index (std::string &tag) const
 Extract index from tag. More...
 

Protected Attributes

std::string m_name
 Element name. More...
 
std::vector< GXmlAttribute * > m_attr
 Attributes. More...
 
- Protected Attributes inherited from GXmlNode
GXmlNodem_parent
 Pointer on parent node. More...
 
std::vector< GXmlNode * > m_nodes
 Pointer to child nodes. More...
 

Friends

class GXml
 

Additional Inherited Members

- Public Types inherited from GXmlNode
enum  NodeType {
  NT_DOCUMENT, NT_ELEMENT, NT_COMMENT, NT_UNKNOWN,
  NT_TEXT, NT_DECLARATION, NT_PI, NT_TYPECOUNT
}
 

Detailed Description

XML element node class.

This class implements an XML element with it's associated attributes.

Definition at line 48 of file GXmlElement.hpp.

Constructor & Destructor Documentation

GXmlElement::GXmlElement ( void  )

Void constructor.

Definition at line 65 of file GXmlElement.cpp.

References init_members().

Referenced by clone().

GXmlElement::GXmlElement ( const GXmlElement node)

Copy constructor.

Parameters
[in]nodeXML element.

Definition at line 80 of file GXmlElement.cpp.

References copy_members(), and init_members().

GXmlElement::GXmlElement ( const std::string &  segment)
explicit

Segment constructor.

Parameters
[in]segmentXML segment.

Constructs a XML element from a text segment. The text segment is parsed and the element name and attributes are extracted using the parse_start() method.

Definition at line 102 of file GXmlElement.cpp.

References init_members(), and parse_start().

GXmlElement::GXmlElement ( const std::string &  name,
const int &  value 
)

Integer value constructor.

Parameters
[in]nameElement name.
[in]valueInteger value.

Constructs a XML value element of the form

  <name>value</name>

Definition at line 125 of file GXmlElement.cpp.

References GXmlNode::append(), init_members(), m_name, name(), and gammalib::str().

GXmlElement::GXmlElement ( const std::string &  name,
const double &  value 
)

Floating point value constructor.

Parameters
[in]nameElement name.
[in]valueFloating point value.

Constructs a XML value element of the form

  <name>value</name>

Definition at line 152 of file GXmlElement.cpp.

References GXmlNode::append(), init_members(), m_name, name(), and gammalib::str().

GXmlElement::GXmlElement ( const std::string &  name,
const std::string &  value 
)

String value constructor.

Parameters
[in]nameElement name.
[in]valueString value.

Constructs a XML value element of the form

  <name>value</name>

Definition at line 179 of file GXmlElement.cpp.

References GXmlNode::append(), init_members(), m_name, and name().

GXmlElement::~GXmlElement ( void  )
virtual

Destructor.

Definition at line 199 of file GXmlElement.cpp.

References free_members().

Member Function Documentation

const GXmlAttribute * GXmlElement::attribute ( const int &  index) const

Return attribute.

Parameters
[in]indexAttribute index [0,...,attributes()[.
Returns
Pointer to attribute.
Exceptions
GException::out_of_rangeIndex out of range.

Definition at line 328 of file GXmlElement.cpp.

References attributes(), G_ATTRIBUTE, and m_attr.

Referenced by GCTAModelSpatialRegistry::alloc(), GModelSpectralRegistry::alloc(), GModelTemporalRegistry::alloc(), GModelSpatialRegistry::alloc(), GVOTable::field_from_fits_column(), GModelAssociations::get_association_xml(), GModelAssociation::get_property_xml(), GXml::process_markup(), GLATObservation::read(), GCOMModelDRM::read(), GModelAssociation::read(), GModelSpatialComposite::read(), GCTAModelSpatialMultiplicative::read(), GCTARoi::read(), GSPIObservation::read(), GModelSpectralComposite::read(), GModelSpectralMultiplicative::read(), GCTAPointing::read(), GCTAModelRadialPolynom::read(), GModelSpatialDiffuseMap::read(), GTimeReference::read(), GModelTemporalLightCurve::read(), GCTAModelSpatialLookup::read(), GSPIModelDataSpace::read(), GModelSpectralTable::read(), GModelSpatialDiffuseCube::read(), GModelSpectralFunc::read(), GCTAResponseIrf::read(), GCTAObservation::read(), GGti::read(), GCTAResponseCube::read(), GModelPar::read(), GModelTemporalPhaseCurve::read(), GCOMObservation::read(), GCTAOnOffObservation::read(), GEbounds::read(), GObservations::read(), GModels::read(), GModel::read_attributes(), GModel::read_scales(), GCTAModelSkyCube::read_xml_spatial(), GDaemon::update_countries_data(), GDaemon::update_daily(), GDaemon::update_versions_data(), GLATObservation::write(), GCTAModelSpatialGaussSpectrum::write(), GMWLObservation::write(), GCOMModelDRM::write(), GCTAModelIrfBackground::write(), GModelAssociation::write(), GModelSpatialComposite::write(), GCOMModelDRBPhibarNodes::write(), GCTAModelSpatialMultiplicative::write(), GCOMModelDRBPhibarBins::write(), GCTAModelAeffBackground::write(), GCTARoi::write(), GSPIObservation::write(), GCTAModelCubeBackground::write(), GModelSpectralComposite::write(), GModelSpectralMultiplicative::write(), GCTAModelRadialAcceptance::write(), GCTAPointing::write(), GModelSpatialDiffuseMap::write(), GTimeReference::write(), GCTAModelBackground::write(), GModelTemporalLightCurve::write(), GCTAModelSpatialLookup::write(), GSPIModelDataSpace::write(), GCTAModelSkyCube::write(), GModelSpectralTable::write(), GModelSpatialDiffuseCube::write(), GModelSpectralFunc::write(), GCTAResponseIrf::write(), GTestSuites::write(), GCTAObservation::write(), GGti::write(), GCTAResponseCube::write(), GModelPar::write(), GModelTemporalPhaseCurve::write(), GCOMObservation::write(), GCTAOnOffObservation::write(), GEbounds::write(), GObservations::write(), GModelSky::write(), GModel::write_attributes(), GModel::write_scales(), GCTAModelSkyCube::write_xml_spatial(), gammalib::xml_check_type(), gammalib::xml_get_attr(), gammalib::xml_get_par(), gammalib::xml_has_par(), gammalib::xml_need_par(), and GCTAModelRadialAcceptance::xml_radial().

std::string GXmlElement::attribute ( const std::string &  name) const

Return attribute value.

Parameters
[in]nameAttribute name.
Returns
String containing attribute value.

Returns the value of the attribute name. If the requested attribute was not found an empty string is returned.

Definition at line 352 of file GXmlElement.cpp.

References m_attr, name(), and value().

void GXmlElement::attribute ( const std::string &  name,
const std::string &  value 
)

Set attribute value.

Parameters
[in]nameAttribute name.
[in]valueAttribute value.

Sets an attribute of the element. If the attribute name exists the value is modified. If the attribute does not yet exist it is created and added to the list of attributes.

Note that this logical assures that only one attribute with a given name will exist in the element.

Definition at line 383 of file GXmlElement.cpp.

References m_attr, name(), GXmlAttribute::name(), and GXmlAttribute::value().

int GXmlElement::attributes ( void  ) const
inline

Return number of attributes.

Returns
Number of attributes.

Definition at line 194 of file GXmlElement.hpp.

References m_attr.

Referenced by attribute().

std::string GXmlElement::classname ( void  ) const
inlinevirtual

Return class name.

Returns
String containing the class name ("GXmlElement").

Implements GXmlNode.

Definition at line 109 of file GXmlElement.hpp.

void GXmlElement::clear ( void  )
virtual

Clear XML element.

Resets the XML element to a clean initial state.

Implements GXmlNode.

Definition at line 256 of file GXmlElement.cpp.

References free_members(), GXmlNode::free_members(), init_members(), and GXmlNode::init_members().

GXmlElement * GXmlElement::clone ( void  ) const
virtual

Clone XML element.

Returns
Pointer to deep copy of XML element.

Implements GXmlNode.

Definition at line 276 of file GXmlElement.cpp.

References GXmlElement().

void GXmlElement::copy_members ( const GXmlElement node)
protected

Copy class members.

Parameters
[in]nodeXML element.

This method copies all class members. XML attributes are cloned.

Definition at line 610 of file GXmlElement.cpp.

References m_attr, and m_name.

Referenced by GXmlElement(), and operator=().

void GXmlElement::free_members ( void  )
protected

Delete class members.

As container classes that hold pointers need to handle themselves the proper deallocation of memory, we loop here over all pointers and make sure that we deallocate the associated nodes.

Definition at line 633 of file GXmlElement.cpp.

References m_attr.

Referenced by clear(), operator=(), and ~GXmlElement().

bool GXmlElement::has_attribute ( const std::string &  name) const

Check if element has a given attribute.

Parameters
[in]nameAttribute name.
Returns
True if attribute exists, false otherwise.

Checks whether the element contains an attribute with name. If the attribute was found true is returned, false otherwise.

Definition at line 421 of file GXmlElement.cpp.

References m_attr, and name().

Referenced by GModelSpatialComposite::read(), GCTARoi::read(), GSPIObservation::read(), GCTAPointing::read(), GModelSpatialDiffuseMap::read(), GTimeReference::read(), GModelSpectralTable::read(), GCTAResponseIrf::read(), GGti::read(), GModelTemporalPhaseCurve::read(), GCOMObservation::read(), GEbounds::read(), GModel::read_attributes(), GDaemon::update_countries_data(), GDaemon::update_daily(), GDaemon::update_versions_data(), GModelSpatialComposite::write(), and gammalib::xml_get_attr().

void GXmlElement::init_members ( void  )
protected

Initialise class members.

Definition at line 592 of file GXmlElement.cpp.

References m_attr, and m_name.

Referenced by clear(), GXmlElement(), and operator=().

int GXmlElement::integer ( void  ) const
inline

Return integer value.

Returns
Integer value.

Definition at line 170 of file GXmlElement.hpp.

References gammalib::toint(), and value().

const std::string & GXmlElement::name ( void  ) const
inline
void GXmlElement::name ( const std::string &  name)
inline

Set XML element name.

Parameters
[in]nameXML element name.

Definition at line 133 of file GXmlElement.hpp.

References m_name, and name().

GXmlElement & GXmlElement::operator= ( const GXmlElement node)

Assignment operator.

Parameters
[in]nodeXML element.
Returns
XML element.

Definition at line 221 of file GXmlElement.cpp.

References copy_members(), free_members(), init_members(), and GXmlNode::operator=().

void GXmlElement::parse_attribute ( size_t *  pos,
const std::string &  segment 
)
protected

Parse element attribute.

Parameters
[in]posStart position in string.
[in]segmentSegment string.
Exceptions
GException::invalid_valueXML syntax error.

Parse the segment string for one attribute, and if attribute was found, attach it to element.

Todo:
Verify XML validity of attribute name and value

Definition at line 786 of file GXmlElement.cpp.

References G_PARSE_ATTRIBUTE, m_attr, name(), and value().

Referenced by parse_start(), and GXml::process_markup().

void GXmlElement::parse_start ( const std::string &  segment)
protected

Parse element start segment string.

Parameters
[in]segmentSegment string.
Exceptions
GException::xml_syntax_errorXML syntax error.

Parse the segment string and set class members based on the information that is found. The method also performs syntax checking. It does not require brackets to be set.

Definition at line 658 of file GXmlElement.cpp.

References G_PARSE_START, m_name, and parse_attribute().

Referenced by GXmlElement().

void GXmlElement::parse_stop ( const std::string &  segment)
protected

Parse element stop segment string.

Parameters
[in]segmentSegment string.
Exceptions
GException::xml_syntax_errorXML syntax error.

Parse the stop segment string and verify the syntax.

Definition at line 723 of file GXmlElement.cpp.

References G_PARSE_STOP, m_name, and name().

Referenced by GXml::process_markup().

std::string GXmlElement::print ( const GChatter chatter = NORMAL,
const int &  indent = 0 
) const
virtual

Print XML element.

Parameters
[in]chatterChattiness.
[in]indentText indentation.
Returns
String containing XML element

Implements GXmlNode.

Definition at line 553 of file GXmlElement.cpp.

References gammalib::fill(), g_indent, m_attr, m_name, GXmlNode::m_nodes, and SILENT.

Referenced by GModelPar::read().

double GXmlElement::real ( void  ) const
inline

Return floating point value.

Returns
Floating point value.

Definition at line 158 of file GXmlElement.hpp.

References gammalib::todouble(), and value().

void GXmlElement::remove_attribute ( const std::string &  name)

Remove attribute from element.

Parameters
[in]nameAttribute name.

Remove the attribute with name from the XML element. If the requested attribute was not found the method does nothing.

Definition at line 447 of file GXmlElement.cpp.

References m_attr, and name().

std::string GXmlElement::string ( void  ) const
inline

Return string value.

Returns
String value.

Definition at line 146 of file GXmlElement.hpp.

References value().

GXmlNode::NodeType GXmlElement::type ( void  ) const
inlinevirtual

Return XML node type.

Returns
XML node type (NT_ELEMENT).

Implements GXmlNode.

Definition at line 182 of file GXmlElement.hpp.

References GXmlNode::NT_ELEMENT.

Referenced by write().

std::string GXmlElement::value ( void  ) const

Return string value.

Returns
String value.
Exceptions
GException::invalid_valueElement does not contain a single text element.

Returns the string value of the text element in case that the element contains one text element. An exception is returned in case that the element does not contain a text element.

Definition at line 295 of file GXmlElement.cpp.

References G_VALUE, GXmlNode::size(), gammalib::str(), and GXmlText::text().

Referenced by attribute(), integer(), parse_attribute(), real(), string(), GDaemon::update_countries_header(), and GDaemon::update_daily().

void GXmlElement::write ( GUrl url,
const int &  indent = 0 
) const
virtual

Write element into URL.

Parameters
[in]urlUnified Resource Locator.
[in]indentText indentation.

Writes the element into a Unified Resource Locator.

Implements GXmlNode.

Definition at line 481 of file GXmlElement.cpp.

References g_indent, GXmlNode::is_empty(), m_attr, m_name, GXmlNode::m_nodes, GXmlNode::NT_TEXT, GUrl::printf(), and type().

Friends And Related Function Documentation

friend class GXml
friend

Definition at line 51 of file GXmlElement.hpp.

Member Data Documentation

std::vector<GXmlAttribute*> GXmlElement::m_attr
protected
std::string GXmlElement::m_name
protected

Element name.

Definition at line 98 of file GXmlElement.hpp.

Referenced by copy_members(), GXmlElement(), init_members(), name(), parse_start(), parse_stop(), print(), and write().


The documentation for this class was generated from the following files: