GammaLib  2.0.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GXmlNode Class Referenceabstract

Abstract XML node base class. More...

#include <GXmlNode.hpp>

Inheritance diagram for GXmlNode:
GContainer GBase GXmlComment GXmlDocument GXmlElement GXmlPI GXmlText

Public Types

enum  NodeType {
  NT_DOCUMENT, NT_ELEMENT, NT_COMMENT, NT_UNKNOWN,
  NT_TEXT, NT_DECLARATION, NT_PI, NT_TYPECOUNT
}
 

Public Member Functions

 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 void clear (void)=0
 Clear object. More...
 
virtual GXmlNodeclone (void) const =0
 Clones object. More...
 
virtual std::string classname (void) const =0
 Return class name. 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 void write (GUrl &url, const int &indent) const =0
 
virtual NodeType type (void) const =0
 
virtual std::string print (const GChatter &chatter=NORMAL, const int &indent=0) const =0
 
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 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

GXmlNodem_parent
 Pointer on parent node. More...
 
std::vector< GXmlNode * > m_nodes
 Pointer to child nodes. More...
 

Detailed Description

Abstract XML node base class.

This class defines the interface for all XML nodes. Each XML node can be the container for a number of child nodes. The GXmlNode class is thus designed as a container class that holds a list of pointers of type GXmlNode. This allows implementing an arbitrary complex tree structure.

The only member of this class is a list of XML node pointers. GXmlNode handles the proper allocation and deallocation of the node memory.

Most methods are identical to those of the GXml class. Please refer to the documentation of this class for a description of the methods.

Definition at line 57 of file GXmlNode.hpp.

Member Enumeration Documentation

Enumerator
NT_DOCUMENT 
NT_ELEMENT 
NT_COMMENT 
NT_UNKNOWN 
NT_TEXT 
NT_DECLARATION 
NT_PI 
NT_TYPECOUNT 

Definition at line 71 of file GXmlNode.hpp.

Constructor & Destructor Documentation

GXmlNode::GXmlNode ( void  )

Void constructor.

Definition at line 65 of file GXmlNode.cpp.

References init_members().

GXmlNode::GXmlNode ( const GXmlNode node)

Copy constructor.

Parameters
[in]nodeXML node.

Definition at line 80 of file GXmlNode.cpp.

References copy_members(), and init_members().

GXmlNode::~GXmlNode ( void  )
virtual

Destructor.

Definition at line 96 of file GXmlNode.cpp.

References free_members().

Member Function Documentation

GXmlNode * GXmlNode::append ( const GXmlNode node)
virtual
GXmlElement * GXmlNode::append ( const std::string &  segment)
virtual

Append XML element child node.

Parameters
[in]segmentXML child node.
Returns
Pointer to appended child node
Exceptions
GException::invalid_valueNot allowed to append to a text, comment or PI node.

Appends XML element that is constructed from a text segment. The text segment is parsed and the element name and attributes are extracted using the GXmlElement::parse_start() method. The method returns a pointer to the XML element child node that has been appended.

Definition at line 346 of file GXmlNode.cpp.

References G_APPEND1, m_nodes, NT_COMMENT, NT_PI, NT_TEXT, and type().

virtual std::string GXmlNode::classname ( void  ) const
pure virtual

Return class name.

Returns
String containing the class name.

Returns the class name for non-abstract classes in a human readable way.

Implements GBase.

Implemented in GXmlDocument, GXmlElement, GXmlComment, GXmlPI, and GXmlText.

virtual void GXmlNode::clear ( void  )
pure virtual

Clear object.

Sets the object to a clean initial state. After calling the method the object will be in the same state as it were if an empty instance of the object would have been created.

Implements GBase.

Implemented in GXmlDocument, GXmlElement, GXmlComment, GXmlPI, and GXmlText.

virtual GXmlNode* GXmlNode::clone ( void  ) const
pure virtual

Clones object.

Returns
Pointer to deep copy of object.

Creates a deep copy of the object and returns a pointer to the object.

Implements GBase.

Implemented in GXmlDocument, GXmlElement, GXmlComment, GXmlPI, and GXmlText.

Referenced by append(), extend(), insert(), and set().

void GXmlNode::copy_members ( const GXmlNode node)
protected

Copy class members.

Parameters
[in]nodeXML node.
Todo:
Is copying the parent node pointer correct?

Definition at line 887 of file GXmlNode.cpp.

References m_nodes, and m_parent.

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

GXmlElement * GXmlNode::element ( const int &  index)
virtual

Return pointer to GXMLElement child.

Parameters
[in]indexNode index [0,...,elements()-1].
Returns
Pointer to child element (NULL if element does not exist).
Exceptions
GException::out_of_rangeChild element index is out of range.

Returns a pointer to the child number index of the XML node. An exception will be thrown if the index is not valid.

Definition at line 634 of file GXmlNode.cpp.

Referenced by element(), GXml::element(), GVOClient::get_response_value(), GVOHub::get_response_value(), GVOHub::get_subscriptions(), GModelSky::GModelSky(), GLATObservation::read(), GCTAModelSpatialGaussSpectrum::read(), GCTAModelIrfBackground::read(), GModelSpatialComposite::read(), GCOMModelDRBFitting::read(), GCTAModelAeffBackground::read(), GCTAModelSpatialMultiplicative::read(), GCTAModelCubeBackground::read(), GModelSpectralComposite::read(), GModelSpectralMultiplicative::read(), GCTAModelRadialAcceptance::read(), GCTAModelRadialGauss::read(), GCTAModelRadialPolynom::read(), GModelSpectralExponential::read(), GCTAModelRadialProfile::read(), GCTAModelBackground::read(), GSPIModelDataSpace::read(), GModelSpectralFunc::read(), GCOMObservation::read(), GCTAObservation::read(), GModelSpectralNodes::read(), GObservations::read(), GModelSky::read(), GModels::read(), GModel::read_scales(), GVOClient::response_error_code(), GVOClient::response_error_message(), GModels::save(), GLATObservation::write(), GCTAModelSpatialGaussSpectrum::write(), GCTAModelIrfBackground::write(), GModelSpatialComposite::write(), GCTAModelAeffBackground::write(), GCTAModelSpatialMultiplicative::write(), GCOMModelDRBFitting::write(), GModelSpectralComposite::write(), GCTAModelCubeBackground::write(), GCTAModelRadialGauss::write(), GModelSpectralMultiplicative::write(), GCTAModelRadialAcceptance::write(), GModelSpectralExponential::write(), GModelSpatialDiffuseMap::write(), GCTAModelRadialPolynom::write(), GCTAModelRadialProfile::write(), GCTAModelBackground::write(), GSPIModelDataSpace::write(), GModelSpatialDiffuseCube::write(), GModelSpectralFunc::write(), GCOMObservation::write(), GModelSpectralNodes::write(), GModelSky::write(), GModel::write_scales(), gammalib::xml_get_name_value_pair(), gammalib::xml_get_par(), gammalib::xml_has_par(), and gammalib::xml_need_par().

const GXmlElement * GXmlNode::element ( const int &  index) const
virtual

Return pointer to GXMLElement child (const variant)

Parameters
[in]indexNode index [0,...,elements()-1].
Returns
Pointer to child element (NULL if element does not exist).
Exceptions
GException::out_of_rangeChild element index is out of range.

Returns a pointer to the child number index of the XML node. An exception will be thrown if the index is not valid.

Definition at line 653 of file GXmlNode.cpp.

References element(), elements(), G_ELEMENT1, m_nodes, NT_ELEMENT, and type().

GXmlElement * GXmlNode::element ( const std::string &  name)
virtual

Return pointer on child walking down a hierarchy of tags.

Parameters
[in]nameChild element hierarchy.
Returns
Pointer to child element (NULL if element does not exist).
Exceptions
GException::invalid_argumentHierarchy string invalid.

Returns a pointer to the child element described by a hierarchy of the following syntax

params > param[1] > value > struct

The > symbols indicate subsequent hierarchy levels, the square brackets provides the index in case that multiple tags with the same name exist at a given hierarchy level. Omitting the index means that the first tag with the specified name is accessed.

If the specified element does not exist the method returns a NULL pointer.

Definition at line 700 of file GXmlNode.cpp.

References element().

const GXmlElement * GXmlNode::element ( const std::string &  name) const
virtual

Return pointer on child walking down a hierarchy of tags (const version)

Parameters
[in]nameChild element hierarchy.
Returns
Pointer to child element (NULL if element does not exist).
Exceptions
GException::invalid_argumentHierarchy string invalid.

Returns a pointer to the child element described by a hierarchy of the following syntax

params > param[1] > value > struct

The > symbols indicate subsequent hierarchy levels, the square brackets provides the index in case that multiple tags with the same name exist at a given hierarchy level. Omitting the index means that the first tag with the specified name is accessed.

If the specified element does not exist the method returns a NULL pointer.

Definition at line 729 of file GXmlNode.cpp.

References element(), elements(), extract_index(), gammalib::split(), and gammalib::strip_whitespace().

GXmlElement * GXmlNode::element ( const std::string &  name,
const int &  index 
)
virtual

Return pointer on GXMLElement child of a given name.

Parameters
[in]nameName of child element.
[in]indexNode index [0,...,elements()-1].
Returns
Pointer to child element (NULL if element does not exist).
Exceptions
GException::xml_name_not_foundChild element name not found.
GException::out_of_rangeChild element index is out of range.

Returns a pointer to the child number index with name of the XML node. An exception will be thrown if the index is not valid.

Definition at line 784 of file GXmlNode.cpp.

References element().

const GXmlElement * GXmlNode::element ( const std::string &  name,
const int &  index 
) const
virtual

Return pointer on GXMLElement child of a given name (const variant)

Parameters
[in]nameName of child element.
[in]indexNode index [0,...,elements()-1].
Returns
Pointer to child element (NULL if element does not exist).
Exceptions
GException::xml_name_not_foundChild element name not found.
GException::out_of_rangeChild element index is out of range.

Returns a pointer to the child number index with name of the XML node. An exception will be thrown if the index is not valid.

Definition at line 806 of file GXmlNode.cpp.

References element(), elements(), G_ELEMENT3, m_nodes, GXmlElement::name(), NT_ELEMENT, and type().

int GXmlNode::elements ( void  ) const
virtual

Return number of GXMLElement children of node.

Returns
Number of child elements.

Returns the number of GXMLElement child elements of the XML node. GXMLElement child elements are nodes of type NT_ELEMENT.

Definition at line 580 of file GXmlNode.cpp.

References m_nodes, NT_ELEMENT, and type().

Referenced by element(), elements(), GXml::elements(), GVOClient::get_response_value(), GVOHub::get_response_value(), GVOHub::get_subscriptions(), GLATObservation::read(), GCTAModelIrfBackground::read(), GModelSpatialComposite::read(), GCOMModelDRBFitting::read(), GCTAModelAeffBackground::read(), GCTAModelSpatialMultiplicative::read(), GModelSpatialRadialDisk::read(), GModelSpatialRadialRing::read(), GModelSpatialRadialGauss::read(), GModelSpatialEllipticalDisk::read(), GCTAModelCubeBackground::read(), GModelSpatialEllipticalGauss::read(), GModelSpectralComposite::read(), GCTAModelRadialGauss::read(), GModelSpectralMultiplicative::read(), GCTAModelRadialPolynom::read(), GModelSpectralExponential::read(), GCTAModelRadialProfile::read(), GCTAModelBackground::read(), GSPIModelDataSpace::read(), GModelSpatialRadialShell::read(), GModelSpectralFunc::read(), GCOMObservation::read(), GCTAObservation::read(), GModelSpectralNodes::read(), GObservations::read(), GModelSky::read(), GModels::read(), GModel::read_scales(), GLATObservation::write(), GCTAModelSpatialGaussSpectrum::write(), GCTAModelIrfBackground::write(), GModelSpatialComposite::write(), GCTAModelAeffBackground::write(), GCOMModelDRBFitting::write(), GCTAModelSpatialMultiplicative::write(), GModelSpectralComposite::write(), GCTAModelCubeBackground::write(), GCTAModelRadialAcceptance::write(), GModelSpectralMultiplicative::write(), GCTAModelRadialGauss::write(), GModelSpatialDiffuseMap::write(), GCTAModelRadialPolynom::write(), GCTAModelRadialProfile::write(), GCTAModelBackground::write(), GSPIModelDataSpace::write(), GModelSpatialDiffuseCube::write(), GModelSpectralFunc::write(), GCOMObservation::write(), GModelSpectralNodes::write(), GModelSky::write(), GModel::write_scales(), gammalib::xml_get_par(), gammalib::xml_has_par(), and gammalib::xml_need_par().

int GXmlNode::elements ( const std::string &  name) const
virtual

Return number of GXMLElement children with a given name.

Parameters
[in]nameName of GXMLElement elements.
Returns
Number of child elements with a given name.

Returns the number of GXMLElement child elements of the XML node that have a given name. GXMLElement child elements are nodes of type NT_ELEMENT.

Definition at line 605 of file GXmlNode.cpp.

References elements(), m_nodes, NT_ELEMENT, and type().

void GXmlNode::extend ( const GXmlNode node)
virtual

Append all XML child nodes from another XML node.

Parameters
[in]nodeXML node.

Append all XML child nodes found in node to the actual object. Nodes are copied deeply so that they live now on their on in the actual object.

Definition at line 504 of file GXmlNode.cpp.

References clone(), is_empty(), m_nodes, reserve(), and size().

Referenced by GXml::extend().

int GXmlNode::extract_index ( std::string &  tag) const
protected

Extract index from tag.

Parameters
[in,out]tagTag.
Returns
Index.
Exceptions
GException::invalid_argumentTag string invalid.

Extracts the index from a tag string of the following syntax

param[1]

The value within the squared brackets provides the index of the element. If the squared brackets are omitted, an index of 0 will be returned.

Definition at line 939 of file GXmlNode.cpp.

References G_EXTRACT_INDEX, and gammalib::toint().

Referenced by element().

GFilename GXmlNode::filename ( void  ) const

Return filename of XML file.

Returns
Filename.

Returns the file name of the XML file by moving up the XML file hierarchy to the root. In case that the XML node cannot move up to the root XML node an empty file name is returned (this may happen if the XML node is an orphan node). The file name will also be empty if the XML file has not been read from disk or written to disk.

Definition at line 540 of file GXmlNode.cpp.

References GXmlDocument::filename(), and parent().

Referenced by gammalib::xml_file_expand(), and gammalib::xml_file_reduce().

void GXmlNode::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 910 of file GXmlNode.cpp.

References m_nodes.

Referenced by GXmlPI::clear(), GXmlText::clear(), GXmlComment::clear(), GXmlElement::clear(), GXmlDocument::clear(), operator=(), and ~GXmlNode().

void GXmlNode::init_members ( void  )
protected

Initialise class members.

Definition at line 869 of file GXmlNode.cpp.

References m_nodes, and m_parent.

Referenced by GXmlPI::clear(), GXmlText::clear(), GXmlComment::clear(), GXmlElement::clear(), GXmlDocument::clear(), GXmlNode(), and operator=().

GXmlNode * GXmlNode::insert ( const int &  index,
const GXmlNode node 
)
virtual

Insert XML child node.

Parameters
[in]indexChild node index [0,...,size()-1].
[in]nodeXML child node.
Returns
Pointer to inserted child node
Exceptions
GException::invalid_argumentNot allowed to append root node.
GException::invalid_valueNot allowed to append to a text, comment or PI node.
GException::out_of_rangeChild node index is out of range.

Inserts the XML child node before the node with the specified index. A deep copy of the node will be made and the pointer to this node will be stored.

Definition at line 404 of file GXmlNode.cpp.

References clone(), G_INSERT, is_empty(), m_nodes, NT_COMMENT, NT_DOCUMENT, NT_PI, NT_TEXT, size(), and type().

Referenced by GXml::insert().

bool GXmlNode::is_empty ( void  ) const
inlinevirtual

Signals if node has no child nodes.

Returns
True if node has no child nodes.

Implements GContainer.

Definition at line 145 of file GXmlNode.hpp.

References m_nodes.

Referenced by extend(), insert(), GXml::is_empty(), GXml::process_markup(), and GXmlElement::write().

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

Assignment operator.

Parameters
[in]nodeXML node.
Returns
XML node.

Definition at line 118 of file GXmlNode.cpp.

References copy_members(), free_members(), and init_members().

Referenced by GXmlPI::operator=(), GXmlText::operator=(), GXmlComment::operator=(), GXmlElement::operator=(), and GXmlDocument::operator=().

GXmlNode * GXmlNode::operator[] ( const int &  index)

Return pointer to XML child node.

Parameters
[in]indexChild node index [0,...,size()-1].
Returns
Pointer to XML child node at index.
Exceptions
GException::out_of_rangeChild node index is out of range.

Returns a pointer to the XML child node with the specified index.

Definition at line 150 of file GXmlNode.cpp.

References G_ACCESS, m_nodes, and size().

const GXmlNode * GXmlNode::operator[] ( const int &  index) const

Return pointer to XML child node (const version)

Parameters
[in]indexChild node index [0,...,size()-1].
Returns
Pointer to XML child node at index.
Exceptions
GException::out_of_rangeChild node index is out of range.

Returns a const pointer to the XML child node with the specified index.

Definition at line 175 of file GXmlNode.cpp.

References G_ACCESS, m_nodes, and size().

GXmlNode * GXmlNode::parent ( void  ) const
inline

Return parent XML node.

Returns
Parent of XML node.

Definition at line 170 of file GXmlNode.hpp.

References m_parent.

Referenced by filename(), parent(), and GXml::process_markup().

void GXmlNode::parent ( GXmlNode parent)
inline

Set parent of XML node.

Parameters
[in]parentParent of XML node.

Definition at line 182 of file GXmlNode.hpp.

References m_parent, and parent().

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

Implemented in GXmlElement, GXmlDocument, GXmlComment, GXmlPI, and GXmlText.

Referenced by print().

std::string GXmlNode::print ( const GChatter chatter = NORMAL) const
virtual

Print XML node in string.

Parameters
[in]chatterChattiness (defaults to NORMAL).
Returns
String containing XML information.

Implements GBase.

Definition at line 850 of file GXmlNode.cpp.

References print().

void GXmlNode::remove ( const int &  index)
virtual

Remove XML child node.

Parameters
[in]indexChild node index [0,...,size()-1].
Exceptions
GException::out_of_rangeChild node index is out of range.

Remove XML child node at index.

Implements GContainer.

Definition at line 476 of file GXmlNode.cpp.

References G_REMOVE, m_nodes, and size().

Referenced by GXml::remove(), and GCOMObservation::write().

void GXmlNode::reserve ( const int &  num)
inlinevirtual

Reserve space for child nodes.

Parameters
[in]numNumber of child nodes for which space should be reserved.

Implements GContainer.

Definition at line 157 of file GXmlNode.hpp.

References m_nodes.

Referenced by extend(), and GXml::reserve().

GXmlNode * GXmlNode::set ( const int &  index,
const GXmlNode node 
)
virtual

Set XML child node.

Parameters
[in]indexChild node index [0,...,size()-1].
[in]nodeXML child node.
Returns
Pointer to deep copy of child node
Exceptions
GException::invalid_argumentNot allowed to append root node.
GException::invalid_valueNot allowed to append to a text, comment or PI node.
GException::out_of_rangeChild node index is out of range.

Set XML child node. A deep copy of the node will be made and the pointer to this node will be stored.

Definition at line 212 of file GXmlNode.cpp.

References clone(), G_SET, m_nodes, NT_COMMENT, NT_DOCUMENT, NT_PI, NT_TEXT, size(), and type().

Referenced by GXml::set().

int GXmlNode::size ( void  ) const
inlinevirtual

Return number of child nodes.

Returns
Number of child nodes in node.

Implements GContainer.

Definition at line 133 of file GXmlNode.hpp.

References m_nodes.

Referenced by extend(), insert(), operator[](), GXml::process_markup(), remove(), set(), and GXml::size().

virtual NodeType GXmlNode::type ( void  ) const
pure virtual
virtual void GXmlNode::write ( GUrl url,
const int &  indent 
) const
pure virtual

Member Data Documentation

GXmlNode* GXmlNode::m_parent
protected

Pointer on parent node.

Definition at line 122 of file GXmlNode.hpp.

Referenced by copy_members(), init_members(), and parent().


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