1 | /* ************************************************************************** * |
---|
2 | * Copyright © IPSL/LSCE, XMLIOServer, Avril 2010 - Octobre 2011 * |
---|
3 | * ************************************************************************** */ |
---|
4 | |
---|
5 | /** |
---|
6 | * \file xml_node.cpp |
---|
7 | * \brief Manipulation des noeuds dans l'arborescence des documents xml (implémentation). |
---|
8 | * \author Hervé Ozdoba |
---|
9 | * \version 0.4 |
---|
10 | * \date 12 Juin 2011 |
---|
11 | */ |
---|
12 | |
---|
13 | // XMLIOServer headers |
---|
14 | #include "xml_parser.hpp" |
---|
15 | |
---|
16 | // /////////////////////////////// Définitions ////////////////////////////// // |
---|
17 | |
---|
18 | namespace xmlioserver { |
---|
19 | namespace xml { |
---|
20 | |
---|
21 | // ------------------------ Propriété statique privée ----------------------- |
---|
22 | |
---|
23 | // Nom de l'élément racine du document xml. |
---|
24 | std::string CXMLNode::RootName("simulation"); |
---|
25 | |
---|
26 | // ------------------------------ Constructeurs ----------------------------- |
---|
27 | |
---|
28 | // Constructeur d'un noeud xml. |
---|
29 | CXMLNode::CXMLNode(rapidxml::xml_node<char> * const _root) |
---|
30 | : node(_root) |
---|
31 | { /* Ne rien faire de plus */ } |
---|
32 | |
---|
33 | // ------------------------------- Destructeur ------------------------------ |
---|
34 | |
---|
35 | // Destructeur de l'objet. |
---|
36 | CXMLNode::~CXMLNode(void) |
---|
37 | { /* Ne rien faire de plus */ } |
---|
38 | |
---|
39 | // ------------------------------- Accesseurs ------------------------------- |
---|
40 | |
---|
41 | // Retourne les valeurs d'attributs de l'élément courant (pointé dans le document xml). |
---|
42 | std::map <std::string, std::string> CXMLNode::getAttributes(void) const |
---|
43 | { |
---|
44 | std::map <std::string, std::string> attributes; |
---|
45 | rapidxml::xml_attribute<char> * currentAttr = NULL; |
---|
46 | |
---|
47 | if ((currentAttr = this->node->first_attribute()) != NULL) |
---|
48 | { |
---|
49 | do |
---|
50 | { |
---|
51 | attributes.insert(std::pair<std::string, std::string> |
---|
52 | (std::string(currentAttr->name()), |
---|
53 | std::string(currentAttr->value()))); |
---|
54 | } while ((currentAttr = currentAttr->next_attribute()) != NULL); |
---|
55 | } |
---|
56 | |
---|
57 | return (attributes) ; |
---|
58 | } |
---|
59 | |
---|
60 | // Retourne le nom de l'élément courant (pointé dans le document xml). |
---|
61 | std::string CXMLNode::getElementName(void) const |
---|
62 | { |
---|
63 | return (this->node->name()); |
---|
64 | } |
---|
65 | |
---|
66 | // ------------------------------- Mutateurs -------------------------------- |
---|
67 | |
---|
68 | // Déplace le pointeur du noeud actuel vers le suivant si possible. |
---|
69 | bool CXMLNode::goToNextElement(void) |
---|
70 | { |
---|
71 | bool retvalue = false; |
---|
72 | for(rapidxml::xml_node<char> * nextElement = this->node->next_sibling(); |
---|
73 | ; nextElement = this->node->next_sibling()) |
---|
74 | { |
---|
75 | if (nextElement == NULL) break; |
---|
76 | else if (nextElement->type() == rapidxml::node_element) |
---|
77 | { |
---|
78 | node = nextElement; |
---|
79 | return (!retvalue); |
---|
80 | } |
---|
81 | } |
---|
82 | return (retvalue); |
---|
83 | } |
---|
84 | |
---|
85 | // Déplace le pointeur du noeud actuel vers son premier enfant si possible. |
---|
86 | bool CXMLNode::goToChildElement(void) |
---|
87 | { |
---|
88 | bool retvalue = false; |
---|
89 | rapidxml::xml_node<char> * nextElement = this->node->first_node(); |
---|
90 | if (nextElement != NULL) |
---|
91 | { |
---|
92 | for(;;nextElement = this->node->next_sibling()) |
---|
93 | { |
---|
94 | if (nextElement == NULL) break; |
---|
95 | else if (nextElement->type() == rapidxml::node_element) |
---|
96 | { |
---|
97 | node = nextElement; |
---|
98 | return (!retvalue); |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | return (retvalue); |
---|
103 | } |
---|
104 | |
---|
105 | // Déplace le pointeur du noeud actuel vers son parent si possible. |
---|
106 | bool CXMLNode::goToParentElement(void) |
---|
107 | { |
---|
108 | bool retvalue = false; |
---|
109 | if (!(this->getElementName().compare(CXMLNode::RootName))) |
---|
110 | return (retvalue); |
---|
111 | node = node->parent(); |
---|
112 | return (!retvalue); |
---|
113 | } |
---|
114 | |
---|
115 | // -------------------------- Accesseurs statiques -------------------------- |
---|
116 | |
---|
117 | // Retourne le nom de l'élément racine du document xml. |
---|
118 | const std::string & CXMLNode::GetRootName(void) |
---|
119 | { |
---|
120 | return (CXMLNode::RootName); |
---|
121 | } |
---|
122 | |
---|
123 | } // namespace xml |
---|
124 | } // namespace xmlioserver |
---|