fix8  version 1.4.0
Open Source C++ FIX Framework
xml.hpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------------------
2 /*
3 
4 Fix8 is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3.
5 
6 Fix8 Open Source FIX Engine.
7 Copyright (C) 2010-16 David L. Dight <fix@fix8.org>
8 
9 Fix8 is free software: you can redistribute it and / or modify it under the terms of the
10 GNU Lesser General Public License as published by the Free Software Foundation, either
11 version 3 of the License, or (at your option) any later version.
12 
13 Fix8 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
14 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 
16 You should have received a copy of the GNU Lesser General Public License along with Fix8.
17 If not, see <http://www.gnu.org/licenses/>.
18 
19 BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO
20 THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
21 COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
22 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
24 THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
25 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
26 
27 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT
28 HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED
29 ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
30 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
31 NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
32 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
33 HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 
35 */
36 //-----------------------------------------------------------------------------------------
37 #ifndef _XML_ELEMENT_HPP_
38 # define _XML_ELEMENT_HPP_
39 
40 //-----------------------------------------------------------------------------------------
41 #include <set>
42 
43 //----------------------------------------------------------------------------------------
44 using Str2Chr = std::map<std::string, unsigned char>;
45 
46 //----------------------------------------------------------------------------------------
49 {
51  static const Str2Chr stringtochar_;
52 
54  enum { MaxDepth = 128 };
56 
58 
60  std::string inclusion_;
61 
62  std::string tag_, *value_, *decl_;
64 
67  {
68  bool operator()(const XmlElement *a, const XmlElement *b) const
69  { return a->GetSequence() < b->GetSequence(); }
70  };
71 
72 public:
74  using XmlSet = std::set<const XmlElement *, EntityOrderComp>;
75  using XmlAttrs = std::map<std::string, std::string>;
77  F8API static const XmlAttrs emptyattrs_;
78 
81  static XmlFlags flags_;
82  static void set_flags(XmlFlags flags) { flags_ = flags; }
83 
84 private:
85  using XmlSubEls = std::multimap<std::string, XmlElement *>;
89 
92  F8API static const XmlSet emptyset_;
93 
94 public:
102  F8API XmlElement(std::istream& ifs, int subidx, XmlElement *parent=nullptr, int txtline=0, int depth=0, const char *rootAttr=nullptr);
103 
105  F8API virtual ~XmlElement();
106 
108  XmlElement(const XmlElement&) = delete;
109 
111  XmlElement& operator=(const XmlElement&) = delete;
112 
116  F8API int ParseAttrs(const std::string& attlst);
117 
124  F8API const XmlElement *find(const std::string& what,
125  const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const;
126 
134  F8API int find(const std::string& what, XmlSet& eset,
135  const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const;
136 
144  F8API const XmlElement *find_child(const std::string& what,
145  const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const
146  {
147  const std::string sterm(tag_ + '/' + what);
148  return find(sterm, atag, aval, delim);
149  }
150 
159  F8API int find_child(const std::string& what, XmlSet& eset,
160  const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const
161  {
162  const std::string sterm(tag_ + '/' + what);
163  return find(sterm, eset, atag, aval, delim);
164  }
165 
170  F8API bool GetAttr(const std::string& what, std::string& target) const;
171 
175  bool GetAttrValue(std::string& target) const
176  {
177  static const std::string valstr("value");
178  return GetAttr(valstr, target);
179  }
180 
184  bool HasAttr(const std::string& what) const
185  { return attrs_ ? attrs_->find(what) != attrs_->end() : false; }
186 
191  bool FindAttrGetValue(const std::string& what, std::string& target)
192  {
193  const XmlElement *inst(find(what));
194  return inst ? inst->GetAttrValue(target) : false;
195  }
196 
201  F8API bool findAttrByValue(const std::string& what, const std::string& value) const;
202 
208  template<typename T>
209  T FindAttr(const std::string& what, const T defValue) const
210  {
211  if (attrs_)
212  {
213  XmlAttrs::iterator itr(attrs_->find(what));
214  if (itr != attrs_->end())
215  return FIX8::get_value<T>(itr->second);
216  }
217 
218  return defValue;
219  }
220 
226  template<typename T>
227  T& FindAttrRef(const std::string& what, T& target) const
228  {
229  if (attrs_)
230  {
231  XmlAttrs::iterator itr(attrs_->find(what));
232  if (itr != attrs_->end())
233  target = FIX8::get_value<T>(itr->second);
234  }
235 
236  return target;
237  }
238 
242  F8API bool Insert( XmlElement *what );
243 
248  F8API const std::string& InplaceXlate( std::string& what );
249 
252  int GetDepth() const { return depth_; }
253 
256  int GetErrorCnt() const { return errors_; }
257 
260  int GetLineCnt() const { return line_; }
261 
264  int GetChildCnt() const { return chldcnt_; }
265 
268  int GetLine() const { return txtline_; }
269 
272  int GetSubIdx() const { return subidx_; }
273 
276  int GetSequence() const { return sequence_; }
277 
280  int GetMaxDepthPermitted() const { return MaxDepth; }
281 
284  int GetMaxDepth() const { return maxdepth_; }
285 
288  std::string GetPath() const { return parent_ ? parent_->GetPath() + '/' + tag_ : "//" + tag_; }
289 
292  const std::string& GetTag() const { return tag_; }
293 
296  std::string GetLocString() const
297  {
298  std::ostringstream ostr;
299  ostr << '"' << tag_ << "\" (" << GetPath() << ':' << txtline_ << ')';
300  return ostr.str();
301  }
302 
305  const std::string *GetVal() const { return value_; }
306 
309  const std::string *GetDecl() const { return decl_; }
310 
315  F8API static XmlElement *Factory(std::istream& istr, const char *docpath=nullptr);
316 
320  F8API static XmlElement *Factory(const std::string& fname);
321 
324  XmlAttrs::const_iterator abegin() const { return attrs_ ? attrs_->begin() : emptyattrs_.end(); }
325 
328  XmlAttrs::const_iterator aend() const { return attrs_ ? attrs_->end() : emptyattrs_.end(); }
329 
332  XmlSet::const_iterator begin() const { return ordchildren_ ? ordchildren_->begin() : emptyset_.end(); }
333 
336  XmlSet::const_iterator end() const { return ordchildren_ ? ordchildren_->end() : emptyset_.end(); }
337 
340  const XmlElement *GetRoot() const { return parent_ ? parent_->root_ : this; }
341 
344  const XmlElement *GetParent() const { return parent_; }
345 
350  friend F8API std::ostream& operator<<(std::ostream& os, const XmlElement& en);
351 };
352 
353 F8API std::ostream& operator<<( std::ostream& os, const XmlElement& en );
354 
355 #endif // _XML_ELEMENT_HPP_
356 
int GetSequence() const
Definition: xml.hpp:276
static F8API XmlElement * Factory(std::istream &istr, const char *docpath=nullptr)
F8API const XmlElement * find_child(const std::string &what, const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const
Definition: xml.hpp:144
F8API std::ostream & operator<<(std::ostream &os, const XmlElement &en)
Definition: xml.cpp:71
std::string inclusion_
Definition: xml.hpp:60
int GetLine() const
Definition: xml.hpp:268
int maxdepth_
Definition: xml.hpp:59
T FindAttr(const std::string &what, const T defValue) const
Definition: xml.hpp:209
XmlElement & operator=(const XmlElement &)=delete
Assignment operator. Non-copyable.
F8API int ParseAttrs(const std::string &attlst)
Definition: xml.cpp:542
XmlElement * parent_
Definition: xml.hpp:57
XmlSet * ordchildren_
Set of all child elements in file order.
Definition: xml.hpp:91
Bitset for enums.
Definition: f8utils.hpp:842
POSIX regex wrapper class.
Definition: f8utils.hpp:370
const XmlElement * GetRoot() const
Definition: xml.hpp:340
std::map< std::string, std::string > XmlAttrs
Definition: xml.hpp:75
F8API bool findAttrByValue(const std::string &what, const std::string &value) const
Definition: xml.cpp:736
XmlElement * root_
Definition: xml.hpp:57
static FIX8::RegExp rEv_
Definition: xml.hpp:55
XmlSubEls * children_
simple n-ary tree
Definition: xml.hpp:87
std::map< std::string, unsigned char > Str2Chr
Definition: xml.hpp:44
static const Str2Chr stringtochar_
XML entity char lookup.
Definition: xml.hpp:51
T & FindAttrRef(const std::string &what, T &target) const
Definition: xml.hpp:227
XmlSet::const_iterator begin() const
Definition: xml.hpp:332
static FIX8::RegExp rIn_
Definition: xml.hpp:55
int GetMaxDepthPermitted() const
Definition: xml.hpp:280
int subidx_
Definition: xml.hpp:63
static XmlFlags flags_
Definition: xml.hpp:81
bool GetAttrValue(std::string &target) const
Definition: xml.hpp:175
int depth_
Definition: xml.hpp:63
int GetMaxDepth() const
Definition: xml.hpp:284
const XmlElement * GetParent() const
Definition: xml.hpp:344
int GetLineCnt() const
Definition: xml.hpp:260
F8API int find_child(const std::string &what, XmlSet &eset, const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const
Definition: xml.hpp:159
const std::string * GetDecl() const
Definition: xml.hpp:309
int GetSubIdx() const
Definition: xml.hpp:272
std::multimap< std::string, XmlElement * > XmlSubEls
Definition: xml.hpp:85
bool FindAttrGetValue(const std::string &what, std::string &target)
Definition: xml.hpp:191
F8API bool Insert(XmlElement *what)
Definition: xml.cpp:523
std::string * value_
Definition: xml.hpp:62
A simple xml parser with Xpath style lookup.
Definition: xml.hpp:48
#define F8API
Definition: f8dll.h:60
int GetChildCnt() const
Definition: xml.hpp:264
XmlAttrs * attrs_
Definition: xml.hpp:76
static FIX8::RegExp rEn_
Definition: xml.hpp:55
XmlAttrs::const_iterator abegin() const
Definition: xml.hpp:324
std::string GetPath() const
Definition: xml.hpp:288
std::string GetLocString() const
Definition: xml.hpp:296
virtual F8API ~XmlElement()
Dtor.
Definition: xml.cpp:655
std::set< const XmlElement *, EntityOrderComp > XmlSet
Definition: xml.hpp:74
static F8API const XmlSet emptyset_
Definition: xml.hpp:92
F8API XmlElement(std::istream &ifs, int subidx, XmlElement *parent=nullptr, int txtline=0, int depth=0, const char *rootAttr=nullptr)
friend F8API std::ostream & operator<<(std::ostream &os, const XmlElement &en)
bool _was_include
Definition: xml.hpp:88
F8API bool GetAttr(const std::string &what, std::string &target) const
Definition: xml.cpp:748
std::string * decl_
Definition: xml.hpp:62
F8API const std::string & InplaceXlate(std::string &what)
Definition: xml.cpp:764
int txtline_
Definition: xml.hpp:63
bool operator()(const XmlElement *a, const XmlElement *b) const
Definition: xml.hpp:68
XmlAttrs::const_iterator aend() const
Definition: xml.hpp:328
const std::string & GetTag() const
Definition: xml.hpp:292
static FIX8::RegExp rCE_
Definition: xml.hpp:55
static void set_flags(XmlFlags flags)
Definition: xml.hpp:82
static FIX8::RegExp rCX_
Definition: xml.hpp:55
bool HasAttr(const std::string &what) const
Definition: xml.hpp:184
int seq_
Definition: xml.hpp:59
std::string tag_
Definition: xml.hpp:62
int chldcnt_
Definition: xml.hpp:63
int GetDepth() const
Definition: xml.hpp:252
const std::string * GetVal() const
Definition: xml.hpp:305
F8API const XmlElement * find(const std::string &what, const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const
int errors_
Definition: xml.hpp:59
int line_
Definition: xml.hpp:59
int sequence_
Definition: xml.hpp:63
int GetErrorCnt() const
Definition: xml.hpp:256
static F8API const XmlAttrs emptyattrs_
Definition: xml.hpp:77
int incline_
Definition: xml.hpp:59
XmlSet::const_iterator end() const
Definition: xml.hpp:336