#ifndef __XMLIO_XML_NODE__ #define __XMLIO_XML_NODE__ namespace XMLIOSERVER { namespace XML { typedef Poco::HashMap THashAttributes; class XMLNode { public : string getElementName(void) const { std::string _str(this->getCNode()->nodeName()); return (_toLower(_str)); } bool goToNextElement(void) { Poco::XML::Node* nextElement = this->getCNode()->nextSibling(); // On parcourt la liste des "siblings" jusqu'à trouver un élément quelconque. for(; ; nextElement = nextElement->nextSibling()) if (IsPtrNull(nextElement)) break; else if (nextElement->nodeType() == 1) {// Si l'un des noeuds est un élément... this->setCNode(nextElement) ; return (true); } return (false); } bool goToChildElement(void) { Poco::XML::Node* nextElement = this->getCNode()->firstChild(); // On parcourt la liste des enfants jusqu'à trouver un élément quelconque. if (!IsPtrNull(nextElement)) { for(; ; nextElement = nextElement->nextSibling()) if (IsPtrNull(nextElement)) return (false); else if (nextElement->nodeType() == 1) {// Si l'un des noeuds est un élément... this->setCNode(nextElement) ; return (true); } } return (false); } bool goToParentElement(void) { // Pas de retour au parent si on est à la racine. if (!(this->getElementName().compare(rootName))) return (false); this->setCNode(this->getCNode()->parentNode()); return (true); } bool getAttributes(THashAttributes& _attributes) const { if(!this->getCNode()->hasAttributes()) return (false); Poco::XML::AutoPtr map = this->getCNode()->attributes(); for(unsigned int i = 0; i< map->length(); i++) { // Suppression des espaces en début et fin de valeur d'attribut xml. std::string _str(map->item(i)->nodeName()); size_t d = map->item(i)->nodeValue().find_first_not_of(' '); size_t f = map->item(i)->nodeValue().find_last_not_of (' '); _attributes[_toLower(_str)] = map->item(i)->nodeValue().substr(d,f-d+1); } return (true); } THashAttributes getAttributes(void) const // Idem que précédemment mais avec copie. { THashAttributes __attributes; this->getAttributes(__attributes); return (__attributes); } ~XMLNode() { /* Ne rien faire de plus */ } public : /* static */ static XMLNode CreateNode(std::istream& _istr, const std::string& _rootName) { XMLNode node(_rootName); if (_istr.good()) { // S'il est possible de lire le flux en entrée ... Poco::XML::InputSource src(_istr); Poco::XML::DOMParser parser; node.pDoc = parser.parse(&src); node.setCNode(node.pDoc->documentElement()); if (node.getElementName().compare(_rootName) != 0) // << A passer en avertissement. throw XMLParsingException("L'élément racine doit avoir pour valeur <" + _rootName + "> (\"" + node.getElementName() + "\" lue)"); } else throw XMLIOStreamException("Impossible de lire le flux en entrée pour le parsing XML !"); return (node); } protected : Poco::XML::Node * getCNode(void) const { return (this->cNode); } void setCNode(Poco::XML::Node * _otherNode) { this->cNode = _otherNode; } protected : /* static */ static bool IsPtrNull(const Poco::XML::Node * const _ptr) { return (_ptr == NULL); } private : XMLNode(const std::string& _rootName) : rootName(_rootName) { /* Ne rien faire de plus */} const string& _toLower(std::string& _str) const { for (unsigned int i = 0; i < _str.size(); i++) _str[i] = tolower(_str[i]); return (_str); } Poco::XML::AutoPtr pDoc; Poco::XML::Node* cNode; string rootName; };// class XMLNode } // namespace XML }// namespace XMLIOSERVER #endif // __XMLIO_XML_NODE__