GammaLib  1.7.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...
 
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...
 
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 47 of file GXmlElement.hpp.

Constructor & Destructor Documentation

GXmlElement::GXmlElement ( void  )

Void constructor.

Definition at line 63 of file GXmlElement.cpp.

References init_members().

Referenced by clone().

GXmlElement::GXmlElement ( const GXmlElement node)

Copy constructor.

Parameters
[in]nodeXML element.

Definition at line 78 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 100 of file GXmlElement.cpp.

References init_members(), and parse_start().

GXmlElement::~GXmlElement ( void  )
virtual

Destructor.

Definition at line 116 of file GXmlElement.cpp.

References free_members().

Member Function Documentation

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

Return attribute.

Parameters
[in]indexAttribute index.
Returns
Pointer to attribute.

Definition at line 206 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(), GXml::process_markup(), GLATObservation::read(), GCTARoi::read(), GCTAModelSpatialMultiplicative::read(), GModelSpatialComposite::read(), GModelSpectralComposite::read(), GModelSpectralMultiplicative::read(), GCTAModelRadialGauss::read(), GCTAPointing::read(), GCTAModelRadialPolynom::read(), GTimeReference::read(), GCTAModelRadialProfile::read(), GModelSpatialDiffuseMap::read(), GModelTemporalLightCurve::read(), GCTAModelSpatialLookup::read(), GModelSpectralFunc::read(), GModelSpatialDiffuseCube::read(), GCOMObservation::read(), GCTAObservation::read(), GCTAResponseCube::read(), GCTAResponseIrf::read(), GGti::read(), GCTAOnOffObservation::read(), GModelPar::read(), GEbounds::read(), GModelTemporalPhaseCurve::read(), GObservations::read(), GModels::read(), GModel::read_attributes(), GModel::read_scales(), GLATObservation::write(), GCTAModelSpatialGaussSpectrum::write(), GMWLObservation::write(), GCTAModelIrfBackground::write(), GCTAModelSpatialGradient::write(), GCTARoi::write(), GCTAModelAeffBackground::write(), GCOMModelDRBFitting::write(), GCTAModelSpatialMultiplicative::write(), GModelSpatialComposite::write(), GModelTemporalConst::write(), GModelSpatialDiffuseConst::write(), GModelSpatialPointSource::write(), GCTAModelCubeBackground::write(), GModelSpectralComposite::write(), GCTAModelRadialAcceptance::write(), GModelSpectralMultiplicative::write(), GCTAModelRadialGauss::write(), GCTAPointing::write(), GCTAModelRadialPolynom::write(), GModelSpectralExponential::write(), GTimeReference::write(), GCTAModelRadialProfile::write(), GCTAModelBackground::write(), GModelSpatialDiffuseMap::write(), GCTAModelSpatialLookup::write(), GModelTemporalLightCurve::write(), GModelSpatialRadial::write(), GModelSpectralConst::write(), GModelSpatialElliptical::write(), GModelSpectralFunc::write(), GModelSpectralGauss::write(), GModelSpatialDiffuseCube::write(), GCOMObservation::write(), GModelSpectralPlaw::write(), GCTAResponseCube::write(), GCTAResponseIrf::write(), GCTAObservation::write(), GTestSuites::write(), GGti::write(), GModelSpectralExpPlaw::write(), GModelSpectralLogParabola::write(), GModelSpectralBrokenPlaw::write(), GCTAOnOffObservation::write(), GModelSpectralExpInvPlaw::write(), GModelPar::write(), GModelSpectralSuperExpPlaw::write(), GModelTemporalPhaseCurve::write(), GEbounds::write(), GModelSpectralPlawPhotonFlux::write(), GModelSpectralPlawEnergyFlux::write(), GModelSpectralNodes::write(), GModelSpectralSmoothBrokenPlaw::write(), GObservations::write(), GModelSky::write(), GModel::write_attributes(), GModel::write_scales(), 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 229 of file GXmlElement.cpp.

References m_attr, and name().

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 260 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 150 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 101 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 173 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 193 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 487 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 510 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 298 of file GXmlElement.cpp.

References m_attr, and name().

Referenced by GCTARoi::read(), GModelSpatialComposite::read(), GCTAPointing::read(), GTimeReference::read(), GModelSpatialDiffuseMap::read(), GCTAResponseIrf::read(), GGti::read(), GModelTemporalPhaseCurve::read(), GEbounds::read(), GModel::read_attributes(), GModelSpatialComposite::write(), and gammalib::xml_get_attr().

void GXmlElement::init_members ( void  )
protected

Initialise class members.

Definition at line 469 of file GXmlElement.cpp.

References m_attr, and m_name.

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

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

Return XML element name.

Returns
XML element name.

Definition at line 113 of file GXmlElement.hpp.

References m_name.

Referenced by attribute(), GXmlNode::element(), has_attribute(), name(), GXml::parse(), parse_attribute(), parse_stop(), remove_attribute(), and GTestSuites::write().

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

Set XML element name.

Parameters
[in]nameXML element name.

Definition at line 125 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 138 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::xml_syntax_errorXML 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 649 of file GXmlElement.cpp.

References G_PARSE_ATTRIBUTE, m_attr, and name().

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 535 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 594 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 430 of file GXmlElement.cpp.

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

Referenced by GModelPar::read().

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 324 of file GXmlElement.cpp.

References m_attr, and name().

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

Return XML node type.

Returns
XML node type (NT_ELEMENT).

Implements GXmlNode.

Definition at line 138 of file GXmlElement.hpp.

References GXmlNode::NT_ELEMENT.

Referenced by write().

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 358 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 50 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 90 of file GXmlElement.hpp.

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


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