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