VAPoR  0.1
ExpatParseMgr.h
Go to the documentation of this file.
1 //
2 //
3 
4 
5 #ifndef EXPATPARSEMGR_H
6 #define EXPATPARSEMGR_H
7 
8 #include <stack>
9 #include <expat.h>
10 #include <vector>
11 #include <vapor/MyBase.h>
12 #include <vapor/common.h>
13 #ifdef WIN32
14 #pragma warning(disable : 4251)
15 #endif
16 
17 namespace VAPoR {
18 class ExpatParseMgr;
19 class XmlNode;
20 //abstract base class for classes that do xml parsing
22 public:
23  virtual ~ParsedXml(){}
24  //Callbacks from xml parse
25  //Start and end handlers return false on failure.
26  virtual bool elementStartHandler(ExpatParseMgr*, int /* depth*/ , std::string& /*tag*/, const char ** /*attribs*/) = 0;
27  virtual bool elementEndHandler(ExpatParseMgr*, int /*depth*/ , std::string& /*tag*/) = 0;
28  virtual bool charHandler (ExpatParseMgr*, const XML_Char *, int ) {return true;}
29 
30  //Store previous class for stack
32 
33 protected:
34  // known xml attribute values
35  //
36  static const string _stringType;
37  static const string _longType;
38  static const string _doubleType;
39  static const string _typeAttr;
40 
41 };
42 // Structure used for parsing metadata files
43 //
45  public:
46  string tag; // xml element tag
47  string data_type; // Type of element data (string, double, or long)
48  int has_data; // does the element have data?
49  int user_defined; // is the element user defined?
50 };
52 public:
53 
54  ExpatParseMgr(ParsedXml* topLevelClass);
55 
56  ~ExpatParseMgr();
57 
58  void parse(ifstream& is);
59  ExpatStackElement* getStateStackTop() {return _expatStateStack.top();}
60  // Report an XML parsing error
61  //
62  void parseError(const char *format, ...);
63  vector<long>& getLongData() {return _expatLongData;}
64  vector<double>& getDoubleData() {return _expatDoubleData;}
65  string& getStringData() {return _expatStringData;}
66  //Following two methods are to allow different classes to be created during one
67  //XML parsing. These help maintain a stack of classes.
68  //When the parsing is to be passed to another class, the original class must call
69  //pushClassStack when the xml for the new class is encountered in the startElementHandler.
70 
72  pc->previousClass = currentParsedClass;
73  currentParsedClass = pc;
74  }
76  currentParsedClass = currentParsedClass->previousClass;
77  return currentParsedClass;
78  }
79 
80  void skipElement(string tag, int depth);
81 
82 protected:
84 
85  stack<VDF_API ExpatStackElement *> _expatStateStack;
86 
87  XML_Parser _expatParser; // XML Expat parser handle
88  string _expatStringData; // temp storage for XML element character data
89  vector <long> _expatLongData; // temp storage for XML long data
90  vector <double> _expatDoubleData; // temp storage for XML double data
91 
92 
93  // known xml attribute values
94  //
95  static const string _stringType;
96  static const string _longType;
97  static const string _doubleType;
98 
99 #ifdef DEAD
100  // XML Expat element handlers
101  friend void _StartElementHandler(
102  void *userData, const XML_Char *tag, const XML_Char **attrs
103  ) {
104  ExpatParseMgr* mgr = (ExpatParseMgr *) userData;
105  mgr->_startElementHandler(tag, attrs);
106  }
107 
108 
109  friend void _EndElementHandler(void *userData, const XML_Char *tag) {
110  ExpatParseMgr* mgr = (ExpatParseMgr *) userData;
111  mgr->_endElementHandler(tag);
112  }
113 
114  friend void _CharDataHandler(
115  void *userData, const XML_Char *s, int len
116  ) {
117  ExpatParseMgr* mgr = (ExpatParseMgr *) userData;
118  mgr->_charDataHandler(s, len);
119  }
120 
121 #else
122  // XML Expat element handlers
123  friend void _StartElementHandler(
124  void *userData, const XML_Char *tag, const XML_Char **attrs
125  );
126 
127 
128  friend void _EndElementHandler(void *userData, const XML_Char *tag);
129 
130  friend void _CharDataHandler(
131  void *userData, const XML_Char *s, int len
132  );
133 
134 #endif
135  void _startElementHandler(const XML_Char *tag, const char **attrs);
136  void _endElementHandler(const XML_Char *tag);
137  void _charDataHandler(const XML_Char *s, int len);
138 
139 
140  //Function pointers that handle custom parsing
141 private:
142  bool _skipFlag;
143  string _skipTag;
144  int _skipDepth;
145 
146 };
147 
148 
149 };
150 
151 #endif // EXPATPARSEMGR_H
string & getStringData()
Definition: ExpatParseMgr.h:65
ParsedXml * popClassStack()
Definition: ExpatParseMgr.h:75
virtual ~ParsedXml()
Definition: ExpatParseMgr.h:23
vector< double > _expatDoubleData
Definition: ExpatParseMgr.h:90
#define VDF_API
Definition: common.h:61
void _charDataHandler(const XML_Char *s, int len)
vector< double > & getDoubleData()
Definition: ExpatParseMgr.h:64
static const string _longType
Definition: ExpatParseMgr.h:37
void _startElementHandler(const XML_Char *tag, const char **attrs)
static const string _longType
Definition: ExpatParseMgr.h:96
void pushClassStack(ParsedXml *pc)
Definition: ExpatParseMgr.h:71
vector< long > _expatLongData
Definition: ExpatParseMgr.h:89
ParsedXml * previousClass
Definition: ExpatParseMgr.h:31
static const string _doubleType
Definition: ExpatParseMgr.h:97
stack< VDF_API ExpatStackElement * > _expatStateStack
Definition: ExpatParseMgr.h:85
virtual bool charHandler(ExpatParseMgr *, const XML_Char *, int)
Definition: ExpatParseMgr.h:28
vector< long > & getLongData()
Definition: ExpatParseMgr.h:63
XML_Parser _expatParser
Definition: ExpatParseMgr.h:87
static const string _typeAttr
Definition: ExpatParseMgr.h:39
static const string _doubleType
Definition: ExpatParseMgr.h:38
VetsUtil base class.
Definition: MyBase.h:68
ParsedXml * currentParsedClass
Definition: ExpatParseMgr.h:83
ExpatStackElement * getStateStackTop()
Definition: ExpatParseMgr.h:59
static const string _stringType
Definition: ExpatParseMgr.h:95
void _endElementHandler(const XML_Char *tag)
static const string _stringType
Definition: ExpatParseMgr.h:36