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

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

Mise à jour intermédiaire ...
A venir : commit d'une version stable intégrant l'écriture de fichiers NetCDF4.
(en cours de finalisation actuellement)

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