[112] | 1 | #ifndef __XMLIO_XML_NODE__ |
---|
| 2 | #define __XMLIO_XML_NODE__ |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | namespace XMLIOSERVER |
---|
| 6 | { |
---|
| 7 | namespace XML |
---|
| 8 | { |
---|
[120] | 9 | typedef Poco::HashMap<std::string, std::string> THashAttributes; |
---|
[112] | 10 | |
---|
| 11 | class XMLNode |
---|
| 12 | { |
---|
| 13 | public : |
---|
| 14 | |
---|
| 15 | string getElementName(void) const |
---|
[120] | 16 | { |
---|
| 17 | std::string _str(this->getCNode()->nodeName()); |
---|
| 18 | return (_toLower(_str)); |
---|
| 19 | } |
---|
[112] | 20 | |
---|
| 21 | bool goToNextElement(void) |
---|
| 22 | { |
---|
[120] | 23 | Poco::XML::Node* nextElement = this->getCNode()->nextSibling(); |
---|
[112] | 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 | { |
---|
[120] | 38 | Poco::XML::Node* nextElement = this->getCNode()->firstChild(); |
---|
[112] | 39 | |
---|
| 40 | // On parcourt la liste des enfants jusqu'à trouver un élément quelconque. |
---|
| 41 | if (!IsPtrNull(nextElement)) |
---|
| 42 | { |
---|
| 43 | for(; ; nextElement = nextElement->nextSibling()) |
---|
[120] | 44 | if (IsPtrNull(nextElement)) return (false); |
---|
[112] | 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 | |
---|
[120] | 62 | bool getAttributes(THashAttributes& _attributes) const |
---|
[112] | 63 | { |
---|
| 64 | if(!this->getCNode()->hasAttributes()) return (false); |
---|
[120] | 65 | Poco::XML::AutoPtr<Poco::XML::NamedNodeMap> map = this->getCNode()->attributes(); |
---|
[112] | 66 | |
---|
| 67 | for(unsigned int i = 0; i< map->length(); i++) |
---|
[117] | 68 | { // Suppression des espaces en début et fin de valeur d'attribut xml. |
---|
[120] | 69 | std::string _str(map->item(i)->nodeName()); |
---|
[117] | 70 | size_t d = map->item(i)->nodeValue().find_first_not_of(' '); |
---|
[112] | 71 | size_t f = map->item(i)->nodeValue().find_last_not_of (' '); |
---|
[120] | 72 | |
---|
| 73 | _attributes[_toLower(_str)] = map->item(i)->nodeValue().substr(d,f-d+1); |
---|
[112] | 74 | } |
---|
| 75 | return (true); |
---|
| 76 | } |
---|
| 77 | |
---|
[126] | 78 | THashAttributes getAttributes(void) const // Idem que précédemment mais avec copie. |
---|
| 79 | { THashAttributes __attributes; this->getAttributes(__attributes); return (__attributes); } |
---|
| 80 | |
---|
[112] | 81 | ~XMLNode() |
---|
| 82 | { /* Ne rien faire de plus */ } |
---|
| 83 | |
---|
[126] | 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 | |
---|
[112] | 107 | protected : |
---|
| 108 | |
---|
[120] | 109 | Poco::XML::Node * getCNode(void) const { return (this->cNode); } |
---|
[127] | 110 | |
---|
[120] | 111 | void setCNode(Poco::XML::Node * _otherNode) { this->cNode = _otherNode; } |
---|
[112] | 112 | |
---|
[126] | 113 | protected : /* static */ |
---|
| 114 | |
---|
[120] | 115 | static bool IsPtrNull(const Poco::XML::Node * const _ptr) { return (_ptr == NULL); } |
---|
[112] | 116 | |
---|
| 117 | private : |
---|
| 118 | |
---|
[120] | 119 | XMLNode(const std::string& _rootName) : rootName(_rootName) |
---|
| 120 | { /* Ne rien faire de plus */} |
---|
| 121 | |
---|
| 122 | const string& _toLower(std::string& _str) const |
---|
[112] | 123 | { |
---|
| 124 | for (unsigned int i = 0; i < _str.size(); i++) |
---|
| 125 | _str[i] = tolower(_str[i]); |
---|
| 126 | return (_str); |
---|
| 127 | } |
---|
| 128 | |
---|
[120] | 129 | Poco::XML::AutoPtr<Poco::XML::Document> pDoc; |
---|
[126] | 130 | Poco::XML::Node* cNode; |
---|
[112] | 131 | |
---|
[127] | 132 | std::string rootName; |
---|
[112] | 133 | |
---|
| 134 | };// class XMLNode |
---|
| 135 | |
---|
[114] | 136 | } // namespace XML |
---|
[112] | 137 | |
---|
[114] | 138 | }// namespace XMLIOSERVER |
---|
[112] | 139 | |
---|
| 140 | |
---|
| 141 | #endif // __XMLIO_XML_NODE__ |
---|