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