source: XMLIO_V2/dev/dev_rv/src/XMLIO/xml_node.hpp @ 126

Last change on this file since 126 was 126, checked in by hozdoba, 14 years ago

Amélioration de quelques portions de code.
Ajout de contructeurs par copie.

File size: 4.8 KB
Line 
1#ifndef __XMLIO_XML_NODE__
2#define __XMLIO_XML_NODE__
3
4
5namespace XMLIOSERVER
6{
7   namespace XML
8   {
9      typedef Poco::HashMap<std::string, std::string> THashAttributes;
10
11      class XMLNode
12      {
13         public :
14
15            string getElementName(void) const
16            {
17               std::string _str(this->getCNode()->nodeName());
18               return (_toLower(_str));
19            }
20
21            bool goToNextElement(void)
22            {
23               Poco::XML::Node* nextElement = this->getCNode()->nextSibling();
24
25               // On parcourt la liste des "siblings" jusqu'à trouver un élément quelconque.
26               for(; ; nextElement = nextElement->nextSibling())
27                  if (IsPtrNull(nextElement)) break;
28                  else if (nextElement->nodeType() == 1)
29                  {// Si l'un des noeuds est un élément...
30                     this->setCNode(nextElement) ;
31                     return (true);
32                  }
33               return (false);
34            }
35
36            bool goToChildElement(void)
37            {
38               Poco::XML::Node* nextElement = this->getCNode()->firstChild();
39
40               // On parcourt la liste des enfants jusqu'à trouver un élément quelconque.
41               if (!IsPtrNull(nextElement))
42               {
43                  for(; ; nextElement = nextElement->nextSibling())
44                     if (IsPtrNull(nextElement)) return (false);
45                     else if (nextElement->nodeType() == 1)
46                     {// Si l'un des noeuds est un élément...
47                        this->setCNode(nextElement) ;
48                        return (true);
49                     }
50               }
51               return (false);
52            }
53
54            bool goToParentElement(void)
55            {
56               // Pas de retour au parent si on est à la racine.
57               if (!(this->getElementName().compare(rootName))) return (false);
58               this->setCNode(this->getCNode()->parentNode());
59               return (true);
60            }
61
62            bool getAttributes(THashAttributes& _attributes) const
63            {
64               if(!this->getCNode()->hasAttributes()) return (false);
65               Poco::XML::AutoPtr<Poco::XML::NamedNodeMap> map = this->getCNode()->attributes();
66
67               for(unsigned int i = 0; i< map->length(); i++)
68               { // Suppression des espaces en début et fin de valeur d'attribut xml.
69                  std::string _str(map->item(i)->nodeName());
70                  size_t d = map->item(i)->nodeValue().find_first_not_of(' ');
71                  size_t f = map->item(i)->nodeValue().find_last_not_of (' ');
72
73                  _attributes[_toLower(_str)] = map->item(i)->nodeValue().substr(d,f-d+1);
74               }
75               return (true);
76            }
77
78            THashAttributes getAttributes(void) const // Idem que précédemment mais avec copie.
79            { THashAttributes __attributes; this->getAttributes(__attributes); return (__attributes); }
80
81            ~XMLNode()
82            { /* Ne rien faire de plus */ }
83
84         public : /* static */
85
86            static XMLNode CreateNode(std::istream& _istr, const std::string& _rootName)
87            {
88               XMLNode node(_rootName);
89
90               if (_istr.good())
91               { // S'il est possible de lire le flux en entrée ...
92
93                  Poco::XML::InputSource src(_istr);
94                  Poco::XML::DOMParser parser;
95
96                  node.pDoc = parser.parse(&src);
97                  node.setCNode(node.pDoc->documentElement());
98                  if (node.getElementName().compare(_rootName) != 0) // << A passer en avertissement.
99                     throw XMLParsingException("L'élément racine doit avoir pour valeur <" + _rootName + "> (\"" + node.getElementName() + "\" lue)");
100               }
101               else
102                  throw XMLIOStreamException("Impossible de lire le flux en entrée pour le parsing XML !");
103
104               return (node);
105            }
106
107         protected :
108
109            Poco::XML::Node * getCNode(void) const { return (this->cNode); }
110            void setCNode(Poco::XML::Node * _otherNode) { this->cNode = _otherNode; }
111
112         protected : /* static */
113
114            static bool IsPtrNull(const Poco::XML::Node * const _ptr) { return (_ptr == NULL); }
115
116         private :
117
118            XMLNode(const std::string& _rootName) : rootName(_rootName)
119            { /* Ne rien faire de plus */}
120
121            const string& _toLower(std::string& _str) const
122            {
123               for (unsigned int i = 0; i < _str.size(); i++)
124                  _str[i] = tolower(_str[i]);
125               return (_str);
126            }
127
128            Poco::XML::AutoPtr<Poco::XML::Document> pDoc;
129            Poco::XML::Node* cNode;
130
131            string rootName;
132
133      };// class XMLNode
134
135   } // namespace XML
136
137}// namespace XMLIOSERVER
138
139
140#endif // __XMLIO_XML_NODE__
Note: See TracBrowser for help on using the repository browser.