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

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