Debian Codegraph @

protolib/src/common/protoXml.cpp

raw
1 #include "protoXml.h"
2
3 #include <protoDebug.h>
4
5
6 bool ProtoXml::GetPropAsDouble(xmlNodePtr nodePtr, const char* attrName, double& value)
7 {
8 xmlChar* attrValue = xmlGetProp(nodePtr, (xmlChar*)attrName);
9 if (NULL == attrValue)
10 {
11 PLOG(PL_WARN, "ProtoXml::GetPropAsDouble() error: no attr found!\n");
12 return false;
13 }
14 bool result = true;
15 if (1 != sscanf((const char*)attrValue, "%lf", &value))
16 {
17 PLOG(PL_WARN, "ProtoXml::GetPropAsDouble() error: invalid attr format!\n");
18 result = false;
19 }
20 xmlFree(attrValue);
21 return result;
22 } // end ProtoXml::GetPropAsDouble()
23
24
25 bool ProtoXml::GetPropAsInt(xmlNodePtr nodePtr, const char* attrName, int& value)
26 {
27 xmlChar* attrValue = xmlGetProp(nodePtr, (xmlChar*)attrName);
28 if (NULL == attrValue)
29 {
30 PLOG(PL_WARN, "ProtoXml::GetPropAsInt() error: no attr found!\n");
31 return false;
32 }
33 bool result = true;
34 if (1 != sscanf((const char*)attrValue, "%d", &value))
35 {
36 PLOG(PL_WARN, "ProtoXml::GetPropAsInt() error: invalid attr format!\n");
37 result = false;
38 }
39 xmlFree(attrValue);
40 return result;
41 } // end ProtoXml::GetPropAsInt()
42
43 ProtoXml::IterFilterBase::Filter::Filter(const char* filterPath)
44 {
45 filter_path[FILTER_PATH_MAX] = '\0';
46 SetPath(filterPath);
47 }
48
49 void ProtoXml::IterFilterBase::Filter::SetPath(const char* filterPath)
50 {
51 if (NULL != filterPath)
52 strncpy(filter_path, filterPath, FILTER_PATH_MAX);
53 else
54 filter_path[0] = '\0';
55 } // end ProtoXML::IterFilterBase::Filter::SetPath()
56
57 ProtoXml::IterFilterBase::IterFilterBase(const char* filterPath)
58 : path_filter(filterPath)
59 {
60 path_current[FILTER_PATH_MAX] = '\0';
61 Reset();
62 }
63
64 void ProtoXml::IterFilterBase::SetFilter(const char* filterPath)
65 {
66 path_filter_list.Destroy();
67 path_filter.SetPath(filterPath);
68 } // end ProtoXml::IterFilterBase::SetFilter()
69
70 bool ProtoXml::IterFilterBase::AddFilter(const char* filterPath)
71 {
72 if (path_filter.IsSet())
73 {
74 Filter* filter = new Filter(filterPath);
75 if (NULL == filter)
76 {
77 PLOG(PL_ERROR, "ProtoXml::IterFilterBase::AddFilter() new Filter error: %s\n", GetErrorString());
78 return false;
79 }
80 path_filter_list.Insert(*filter);
81 }
82 else
83 {
84 path_filter.SetPath(filterPath);
85 }
86 return true;
87 } // end ProtoXml::IterFilterBase::SetFilter()
88
89 void ProtoXml::IterFilterBase::Reset()
90 {
91 path_current[0] = '\0';
92 path_current_len = 0;
93 path_depth = 0;
94 } // end ProtoXml::IterFilterBase::Reset()
95
96 bool ProtoXml::IterFilterBase::UpdateCurrentPath(int nodeDepth, const char* nodeName)
97 {
98 if (nodeDepth == path_depth)
99 {
100 // Replace "path_current" tail with "/nodeName"
101 char* ptr = strrchr(path_current, '/');
102 if (NULL == ptr) ptr = path_current;
103 size_t tailLen = strlen(ptr);
104 size_t nameLen = strlen(nodeName);
105 if ((path_current_len - tailLen + 1 + nameLen) > FILTER_PATH_MAX)
106 {
107 PLOG(PL_WARN, "ProtoXml::IterFilterBase::UpdatePath() error: XML path name exceeds filter maximum\n");
108 return false;
109 }
110 *ptr++ = '/';
111 strcpy(ptr, nodeName);
112 path_current_len += nameLen + 1 - tailLen;
113 }
114 else if (nodeDepth > path_depth)
115 {
116 ASSERT(1 == (nodeDepth - path_depth));
117 // Append path_current with '/' + "nodeName"
118 size_t nameLen = strlen((const char*)nodeName);
119 if ((path_current_len + 1 + nameLen) > FILTER_PATH_MAX)
120 {
121 PLOG(PL_ERROR, "ProtoXml::IterParser::GetNext() error: XML path name exceeds filter maximum\n");
122 return false;
123 }
124 //ASSERT(0 != path_current_len);
125 char* ptr = path_current + path_current_len;
126 *ptr++ = '/';
127 strcpy(ptr, (char*)nodeName);
128 path_current_len += (1 + nameLen);
129 path_depth++;
130 }
131 else // if (nodeDepth < path_depth)
132 {
133 //TRACE("path_depth:%d nodeDepth:%d\n", path_depth, nodeDepth);
134 char* ptr = path_current;
135 for (int i = 0; i <= nodeDepth; i++)
136 {
137 ptr = strchr(ptr, '/');
138 ASSERT(NULL != ptr);
139 ptr++;
140 }
141 size_t headLen = ptr - path_current;
142 size_t nameLen = strlen((const char*)nodeName);
143 if ((headLen + nameLen) > FILTER_PATH_MAX)
144 {
145 PLOG(PL_ERROR, "ProtoXml::IterParser::GetNext() error: XML path name exceeds filter maximum\n");
146 return false;
147 }
148 strcpy(ptr, nodeName);
149 path_current_len = headLen + nameLen;
150 path_depth = nodeDepth;
151 }
152 //TRACE("path_current = %s (depth:%d)\n", path_current, path_depth);
153 return true;
154 } // end ProtoXml::IterFilterBase::UpdateCurrentPath()
155
156 bool ProtoXml::IterFilterBase::IsMatch()
157 {
158 // Does "path_current" match any of our path filters
159 if (!path_filter.IsSet())
160 return true;
161 else if (0 == strcmp(path_filter.GetPath(), path_current))
162 return true;
163 else if (NULL != path_filter_list.FindString(path_current))
164 return true;
165 else
166 return false;
167 } // end ProtoXml::IterFilterBase::IsMatch()
168
169 ProtoXml::IterParser::IterParser(const char* filterPath)
170 : IterFilterBase(filterPath), reader_ptr(NULL), prev_node(NULL)
171 {
172 }
173
174 ProtoXml::IterParser::~IterParser()
175 {
176 Close();
177 }
178
179 bool ProtoXml::IterParser::Open(const char* fileName, const char* filterPath)
180 {
181 Close(); // just in case already open
182 if (NULL == (reader_ptr = xmlNewTextReaderFilename(fileName)))
183 {
184 PLOG(PL_ERROR, "ProtoXml::IterParser::Open() xmlNewTextReaderFilename() error: %s\n",
185 GetErrorString());
186 return false;
187 }
188 if (NULL != filterPath)
189 IterFilterBase::SetFilter(filterPath);
190 return true;
191 } // end ProtoXml::IterParser::Open()
192
193 void ProtoXml::IterParser::Close()
194 {
195 if (NULL != reader_ptr)
196 {
197 xmlFreeTextReader(reader_ptr);
198 reader_ptr = NULL;
199 prev_node = NULL;
200 IterFilterBase::Reset();
201 }
202 } // end ProtoXml::IterParser::Close()
203
204 xmlNodePtr ProtoXml::IterParser::GetNext()
205 {
206 //TRACE("enter ProtoXml::IterParser::GetNext() prev_node:%s\n", prev_node ? (const char*)prev_node->name : "(null)");
207 int result;
208 // If we didn't just return a "prev_node", then read the
209 // very next sequential node w/ xmlTextReaderRead(), else
210 // skip entire "prev_node" sub-tree w/ xmlTextReaderNext()
211 if (NULL == prev_node)
212 result = xmlTextReaderRead(reader_ptr);
213 else
214 result = xmlTextReaderNext(reader_ptr);
215 prev_node = NULL;
216 switch (result)
217 {
218 case 0:
219 return NULL;
220 case -1:
221 PLOG(PL_ERROR, "ProtoXml::IterParser::GetNext() xmlTextReaderRead error!\n");
222 return NULL;
223 default:
224 break;
225 }
226 do
227 {
228 int nodeType = xmlTextReaderNodeType(reader_ptr);
229 if (XML_READER_TYPE_ELEMENT != nodeType)
230 continue;
231 const char* nodeName = (const char*)xmlTextReaderConstLocalName(reader_ptr);
232 if (NULL == nodeName)
233 {
234 PLOG(PL_ERROR, "ProtoXml::IterParser::GetNext() xmlTextReaderConstLocalName() error\n");
235 return NULL;
236 }
237 int nodeDepth = xmlTextReaderDepth(reader_ptr);
238 if (nodeDepth < 0)
239 {
240 PLOG(PL_ERROR, "ProtoXml::IterParser::GetNext() xmlTextReaderDepth() error\n");
241 return NULL;
242 }
243
244 if (!IterFilterBase::UpdateCurrentPath(nodeDepth, nodeName))
245 {
246 PLOG(PL_WARN, "ProtoXml::IterParser::GetNext() error: unable to update current path\n");
247 continue;
248 }
249 if (!IterFilterBase::IsMatch())
250 continue; // no match, so continue
251 prev_node = xmlTextReaderExpand(reader_ptr);
252 if (NULL == prev_node)
253 PLOG(PL_ERROR, "ProtoXml::IterParser::GetNext() xmlTextReaderExpand() error!\n");
254 return prev_node;
255
256 } while ((result = xmlTextReaderRead(reader_ptr)) > 0);
257 if (result < 0)
258 PLOG(PL_ERROR, "ProtoXml::IterParser::GetNext() xmlTextReaderRead() error!\n");
259 return NULL;
260 } // end ProtoXml::IterParser::GetNext()
261
262
263 ProtoXml::IterFinder::IterFinder(xmlNodePtr rootElem, const char* filterPath)
264 : IterFilterBase(filterPath), root_elem(rootElem), prev_elem(rootElem), iter_depth(0)
265 {
266 }
267
268 ProtoXml::IterFinder::~IterFinder()
269 {
270 }
271
272 void ProtoXml::IterFinder::Reset(const char* filterPath)
273 {
274 prev_elem = root_elem;
275 if (NULL != filterPath)
276 SetFilter(filterPath);
277 IterFilterBase::Reset();
278 } // end ProtoXml::IterFinder::Reset()
279
280 // Depth-first traversal of sub-tree (below root_elem)
281 xmlNodePtr ProtoXml::IterFinder::GetNext()
282 {
283 //TRACE("enter ProtoXml::IterFinder::GetNext() prev_elem:%s\n", prev_elem ? (const char*)prev_elem->name : "(null)");
284 if(NULL == prev_elem)
285 {
286 return NULL;
287 }
288 else if (prev_elem != root_elem)
289 {
290 // We need to skip the subtree of the prev_elem
291 // This involves moving right or up and right
292 xmlNodePtr nextElem = prev_elem->next;
293 while (NULL == nextElem)
294 {
295 prev_elem = prev_elem->parent; // move up
296 iter_depth--;
297 if (prev_elem != root_elem)
298 nextElem = prev_elem->next; // move right
299 else
300 break; // we're done (returned to root_elem)
301 }
302 prev_elem = nextElem;
303 // Check for match before descending
304 if (NULL != nextElem)
305 {
306 if (!UpdateCurrentPath(iter_depth, (const char*)nextElem->name))
307 PLOG(PL_WARN, "ProtoXml::IterFinder::GetNext() error: unable to update current path\n");
308 else if (IsMatch())
309 return nextElem; // found match, done for now
310 }
311 }
312 // Descend down or across tree
313 while (NULL != prev_elem)
314 {
315 // First, try to move down or the right of the tree
316 xmlNodePtr nextElem = prev_elem->xmlChildrenNode;
317 if (NULL == nextElem)
318 {
319 if (prev_elem != root_elem)
320 {
321 nextElem = prev_elem->next; // no children, try next sibling
322 }
323 else
324 {
325 prev_elem = NULL; // done, root_elem had no children
326 return NULL;
327 }
328 }
329 else if (prev_elem != root_elem)
330 {
331 iter_depth++;
332 }
333 // Second, if needed, move back up the tree and to the right
334 while (NULL == nextElem)
335 {
336 // No child or sibling, so move up a level
337 // and right to parent's next sibling
338 prev_elem = prev_elem->parent; // move up
339 iter_depth--;
340 if (prev_elem != root_elem)
341 nextElem = prev_elem->next; // move right
342 else
343 break; // we're done (returned to root_elem)
344 }
345 prev_elem = nextElem;
346 if (NULL != nextElem)
347 {
348 if (!UpdateCurrentPath(iter_depth, (const char*)nextElem->name))
349 {
350
351 PLOG(PL_WARN, "ProtoXml::IterFinder::GetNext() error: unable to update current path\n");
352 continue;
353 }
354 if (IsMatch()) break; // found match, done for now
355 }
356 }
357 return prev_elem;
358 } // end ProtoXml::IterFinder::GetNext()