| 1 |
#ifndef _PROTO_XML |
| 2 |
#define _PROTO_XML |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <string.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <libxml/xmlreader.h> |
| 8 |
|
| 9 |
#include <protoTree.h> |
| 10 |
|
| 11 |
namespace ProtoXml |
| 12 |
{ |
| 13 |
// Here are some helper functions that can be applied to various libxml2 types |
| 14 |
|
| 15 |
bool GetPropAsDouble(xmlNodePtr nodePtr, const char* attrName, double& value); |
| 16 |
bool GetPropAsInt(xmlNodePtr nodePtr, const char* attrName, int& value); |
| 17 |
|
| 18 |
enum {FILTER_PATH_MAX = 1023}; // TBD - make this dynamically managed? |
| 19 |
|
| 20 |
// This is a base class we use for common path filter stuff |
| 21 |
class IterFilterBase |
| 22 |
{ |
| 23 |
protected: |
| 24 |
IterFilterBase(const char* filterPath); |
| 25 |
void SetFilter(const char* filterPath); // sets filter to a single path for matching |
| 26 |
bool AddFilter(const char* filterPath); // |
| 27 |
|
| 28 |
bool UpdateCurrentPath(int depth, const char* nodeName); |
| 29 |
bool IsMatch(); |
| 30 |
void Reset(); |
| 31 |
|
| 32 |
private: |
| 33 |
class Filter : public ProtoTree::Item |
| 34 |
{ |
| 35 |
public: |
| 36 |
Filter(const char* filterPath); |
| 37 |
void SetPath(const char* filterPath); |
| 38 |
bool IsSet() const |
| 39 |
{return ('\0' != filter_path[0]);} |
| 40 |
const char* GetPath() const |
| 41 |
{return filter_path;} |
| 42 |
|
| 43 |
private: |
| 44 |
// ProtoTree::Item required overrides |
| 45 |
const char* GetKey() const |
| 46 |
{return filter_path;} |
| 47 |
unsigned int GetKeysize() const |
| 48 |
{return (unsigned int)(strlen(filter_path) << 3);} |
| 49 |
char filter_path[FILTER_PATH_MAX+1]; |
| 50 |
|
| 51 |
}; // end class IterFilterBase::Filter |
| 52 |
class FilterList : public ProtoTreeTemplate<Filter> {}; |
| 53 |
|
| 54 |
Filter path_filter; // we have one filter available |
| 55 |
FilterList path_filter_list; |
| 56 |
char path_current[FILTER_PATH_MAX+1]; |
| 57 |
size_t path_current_len; |
| 58 |
int path_depth; |
| 59 |
|
| 60 |
}; // end class ProtoXml::IterFilterBase |
| 61 |
|
| 62 |
|
| 63 |
// The ProtoXML::IterParseFilter() use the libxml2 xmlTextReader API to |
| 64 |
// provide incremental reading of XML document, providing xmlNodePtr |
| 65 |
// values that match a hierarchical (slash-delimited) "filter" specification |
| 66 |
// of element tags. The "GetNext()" method here gives xmlNodePtr that correspond to "end" |
| 67 |
// event (i.e. the end of the element/node matching the filter spec has been reached. |
| 68 |
// In the future, we may expand this provide a "start" event (perhaps via a "PeekNext()" |
| 69 |
// method?) iteration option |
| 70 |
class IterParser : public IterFilterBase |
| 71 |
{ |
| 72 |
public: |
| 73 |
IterParser(const char* filterPath = NULL); |
| 74 |
virtual ~IterParser(); |
| 75 |
|
| 76 |
bool Open(const char* fileName, const char* filterPath = NULL); |
| 77 |
void Close(); |
| 78 |
|
| 79 |
void SetFilter(const char* filterPath) |
| 80 |
{IterFilterBase::SetFilter(filterPath);} |
| 81 |
|
| 82 |
bool AddFilter(const char* filterPath) |
| 83 |
{return IterFilterBase::AddFilter(filterPath);} |
| 84 |
|
| 85 |
// note returned pointer is only valid until next "GetNext()" call |
| 86 |
xmlNodePtr GetNext(); |
| 87 |
|
| 88 |
private: |
| 89 |
xmlTextReaderPtr reader_ptr; |
| 90 |
xmlNodePtr prev_node; |
| 91 |
|
| 92 |
}; // end class ProtoXml::IterParser |
| 93 |
|
| 94 |
|
| 95 |
// This iterates over an xmlNodePtr sub-tree looking for matches against |
| 96 |
// the "filterPath" (only fully-qualifed paths are supported, the path |
| 97 |
// must begin with a '/' and name elements _below_ root node. No |
| 98 |
// filter returns all direct children |
| 99 |
class IterFinder : public IterFilterBase |
| 100 |
{ |
| 101 |
public: |
| 102 |
IterFinder(xmlNodePtr rootElem, const char* filterPath = NULL); |
| 103 |
~IterFinder(); |
| 104 |
|
| 105 |
// reset and optionally change filterPath |
| 106 |
void Reset(const char* filterPath = NULL); |
| 107 |
|
| 108 |
xmlNodePtr GetNext(); |
| 109 |
|
| 110 |
private: |
| 111 |
xmlNodePtr root_elem; |
| 112 |
xmlNodePtr prev_elem; |
| 113 |
int iter_depth; |
| 114 |
|
| 115 |
}; // end class ProtoXml::IterFinder |
| 116 |
|
| 117 |
}; // end class ProtoXml |
| 118 |
|
| 119 |
#endif // _PROTO_XML |