source: XMLIO_V2/dev/dev_rv/xmlio_xml_node.hpp @ 97

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

Plus de fuite mémoire

File size: 5.3 KB
RevLine 
[91]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
[96]11#include <Poco/DOM/AutoPtr.h>
[91]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;
21using std::deque;
22
23using std::istream;
24using std::ostream;
25using std::ostringstream;
26using std::ifstream;
27
28// Utilisation de la biliothÚque POCO
29using Poco::XML::DOMParser;
30using Poco::XML::InputSource;
31
32using Poco::XML::Document;
33using Poco::XML::Node;
34using Poco::XML::Element;
35
36using Poco::XML::NamedNodeMap;
37
38using Poco::HashMap;
39
[96]40using Poco::XML::AutoPtr;
41
[91]42namespace XMLIOSERVER
43{
44   namespace XML
45   {
46     
47      typedef HashMap<string, string> THashAttributes;
48         
49      // TODO Mettre des auto_ptr ici car gestion de la mémoire lamentable sans.
[96]50      typedef AutoPtr<Document> PDocument;
51      typedef Node*    PNode;
[91]52         
53      class XMLNode
54      {
55         public :
56           
[96]57            XMLNode(const string& _rootName) : rootName(_rootName) 
58            { /* Ne rien faire de plus */}         
59
60            static XMLNode& CreateNode(XMLNode& node, istream& _istr, const string& _rootName = string("simulation"))
[91]61            {
62               if ((_istr.good()))
63               { // S'il est possible de lire le flux en entrée ...
64                  InputSource src(_istr);
65                  DOMParser parser;
66                     
67                  // On parse la source XML et on vérifie que le premier noeud (racine) est du type "Element"
68                  // ... et à pour valeur la chaîne rootName.
[96]69                  node.pDoc = parser.parse(&src);
70                  if (!(node.pDoc->documentElement()->nodeName().compare(_rootName)))
[91]71                  {
[96]72                     node.setCNode(node.pDoc->documentElement());
[91]73                     node.goToChildElement();
74                  }
75                  else
76                  {
77                     ostringstream oss;
[96]78                     oss << "L'élément racine doit avoir pour valeur <" << _rootName << "> (\"" <<  (node.pDoc->documentElement()->nodeName()) <<"\" lue)";
[91]79                     throw XMLParsingException(oss.str());
80                  }   
81               }
82               else
83                  throw XMLIOStreamException("Impossible de lire le flux en entrée pour le parsing XML !");
84                 
85               return (node);
86            }
87               
[96]88            string getElementName(void) const {return (this->getCNode()->nodeName());}
[91]89               
[96]90            bool goToNextElement(void) 
[91]91            {
[96]92               PNode nextElement = this->getCNode()->nextSibling();
[91]93             
94               // On parcourt la liste des "siblings" jusqu'à trouver un élément quelconque.
95               for(; ; nextElement = nextElement->nextSibling())
[96]96                  if (IsPtrNull(nextElement)) break;
97                  else if (nextElement->nodeType() == 1)
98                  {// Si l'un des noeuds est un élément...
99                     this->setCNode(nextElement) ;
100                     return (true);
101                  } 
102               return (false);
103            }
[91]104             
[96]105            bool goToChildElement(void)
106            {
107               PNode nextElement = this->getCNode()->firstChild();
108               
109               // On parcourt la liste des enfants jusqu'à trouver un élément quelconque.
110               if (!IsPtrNull(nextElement))
111               {
112                  for(; ; nextElement = nextElement->nextSibling())
113                     if (IsPtrNull(nextElement)) break;
114                     else if (nextElement->nodeType() == 1)
115                     {// Si l'un des noeuds est un élément...
116                        this->setCNode(nextElement) ;
117                        return (true);
118                     }
119                  return (false);
120               }
[91]121               
[96]122               return (false);
123            }
[91]124             
[96]125            bool goToParentElement(void)
126            { 
127               // Pas de retour au parent si on est à la racine.
128               if (!(this->getElementName().compare(rootName))) return (false);
129               this->setCNode(this->getCNode()->parentNode());
130               return (true);
131            }
[91]132             
[96]133            bool getAttributes(THashAttributes& attributes) const
134            {
135               
136               if(!this->getCNode()->hasAttributes()) return (false);
137               AutoPtr<NamedNodeMap> map = this->getCNode()->attributes();
[91]138             
[97]139               for(unsigned int i = 0; i< map->length(); i++)
140                  attributes[map->item(i)->nodeName()] = map->item(i)->nodeValue();
[91]141
[97]142               return (true);
[96]143            }
[91]144           
145            ~XMLNode() 
146            { /* Ne rien faire de plus */ }
[96]147           
148         protected :
149         
150            PNode getCNode(void) const {return (this->cNode); }
151            void setCNode(PNode other) { this->cNode = other; }
152           
153            static bool IsPtrNull(PNode ptr) {return (ptr==NULL);}
154           
[91]155         private :
[96]156            PDocument pDoc; 
[91]157            PNode  cNode;
[96]158           
[91]159            string rootName;     
160         
161      };// class XMLNode
162     
163         }; // namespace XML
164
165};// namespace XMLIOSERVER
166
167
168#endif // __XMLIO_XML_NODE__
Note: See TracBrowser for help on using the repository browser.