fix8  version 1.4.0
Open Source C++ FIX Framework
XmlElement Class Reference

A simple xml parser with Xpath style lookup. More...

#include <xml.hpp>

Classes

struct  EntityOrderComp
 Comparitor. More...
 

Public Types

enum  Flags { noextensions, nocase }
 
using XmlSet = std::set< const XmlElement *, EntityOrderComp >
 
using XmlAttrs = std::map< std::string, std::string >
 
using XmlFlags = FIX8::ebitset< Flags >
 

Public Member Functions

F8API XmlElement (std::istream &ifs, int subidx, XmlElement *parent=nullptr, int txtline=0, int depth=0, const char *rootAttr=nullptr)
 
virtual F8API ~XmlElement ()
 Dtor. More...
 
 XmlElement (const XmlElement &)=delete
 Copy Ctor. Non-copyable. More...
 
XmlElementoperator= (const XmlElement &)=delete
 Assignment operator. Non-copyable. More...
 
F8API int ParseAttrs (const std::string &attlst)
 
F8API const XmlElementfind (const std::string &what, const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const
 
F8API int find (const std::string &what, XmlSet &eset, const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const
 
F8API const XmlElementfind_child (const std::string &what, const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const
 
F8API int find_child (const std::string &what, XmlSet &eset, const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const
 
F8API bool GetAttr (const std::string &what, std::string &target) const
 
bool GetAttrValue (std::string &target) const
 
bool HasAttr (const std::string &what) const
 
bool FindAttrGetValue (const std::string &what, std::string &target)
 
F8API bool findAttrByValue (const std::string &what, const std::string &value) const
 
template<typename T >
FindAttr (const std::string &what, const T defValue) const
 
template<typename T >
T & FindAttrRef (const std::string &what, T &target) const
 
F8API bool Insert (XmlElement *what)
 
F8API const std::string & InplaceXlate (std::string &what)
 
int GetDepth () const
 
int GetErrorCnt () const
 
int GetLineCnt () const
 
int GetChildCnt () const
 
int GetLine () const
 
int GetSubIdx () const
 
int GetSequence () const
 
int GetMaxDepthPermitted () const
 
int GetMaxDepth () const
 
std::string GetPath () const
 
const std::string & GetTag () const
 
std::string GetLocString () const
 
const std::string * GetVal () const
 
const std::string * GetDecl () const
 
XmlAttrs::const_iterator abegin () const
 
XmlAttrs::const_iterator aend () const
 
XmlSet::const_iterator begin () const
 
XmlSet::const_iterator end () const
 
const XmlElementGetRoot () const
 
const XmlElementGetParent () const
 

Static Public Member Functions

static void set_flags (XmlFlags flags)
 
static F8API XmlElementFactory (std::istream &istr, const char *docpath=nullptr)
 
static F8API XmlElementFactory (const std::string &fname)
 

Public Attributes

XmlAttrsattrs_
 

Static Public Attributes

static F8API const XmlAttrs emptyattrs_
 
static XmlFlags flags_
 

Private Types

enum  { MaxDepth = 128 }
 Maximum depth levels supported. More...
 
using XmlSubEls = std::multimap< std::string, XmlElement * >
 

Private Attributes

XmlElementparent_
 
XmlElementroot_
 
int errors_
 
int line_
 
int incline_
 
int maxdepth_
 
int seq_
 
std::string inclusion_
 
std::string tag_
 
std::string * value_
 
std::string * decl_
 
int depth_
 
int sequence_
 
int txtline_
 
int chldcnt_
 
int subidx_
 
XmlSubElschildren_
 simple n-ary tree More...
 
bool _was_include
 
XmlSetordchildren_
 Set of all child elements in file order. More...
 

Static Private Attributes

static const Str2Chr stringtochar_
 XML entity char lookup. More...
 
static FIX8::RegExp rCE_
 
static FIX8::RegExp rCX_
 
static FIX8::RegExp rIn_
 
static FIX8::RegExp rEn_
 
static FIX8::RegExp rEv_
 
static F8API const XmlSet emptyset_
 

Friends

F8API std::ostream & operator<< (std::ostream &os, const XmlElement &en)
 

Detailed Description

A simple xml parser with Xpath style lookup.

Definition at line 48 of file xml.hpp.

Member Typedef Documentation

using XmlElement::XmlAttrs = std::map<std::string, std::string>

Definition at line 75 of file xml.hpp.

Definition at line 80 of file xml.hpp.

using XmlElement::XmlSet = std::set<const XmlElement *, EntityOrderComp>

XmlSet ordering preserved from source file

Definition at line 74 of file xml.hpp.

using XmlElement::XmlSubEls = std::multimap<std::string, XmlElement *>
private

Definition at line 85 of file xml.hpp.

Member Enumeration Documentation

anonymous enum
private

Maximum depth levels supported.

Enumerator
MaxDepth 

Definition at line 54 of file xml.hpp.

54 { MaxDepth = 128 };
Enumerator
noextensions 
nocase 

Definition at line 79 of file xml.hpp.

Constructor & Destructor Documentation

F8API XmlElement::XmlElement ( std::istream &  ifs,
int  subidx,
XmlElement parent = nullptr,
int  txtline = 0,
int  depth = 0,
const char *  rootAttr = nullptr 
)

Ctor.

Parameters
ifsinput file stream
subidxthe subindex for this child
parentparent XmlElement element
txtlinexml sourcefile line number
depthdepth nesting level
rootAttrroot attribute string
XmlElement::~XmlElement ( )
virtual

Dtor.

Definition at line 655 of file xml.cpp.

References _was_include, attrs_, children_, decl_, ordchildren_, and value_.

656 {
657  delete value_;
658  delete attrs_;
659  delete decl_;
660 
661  if (children_ && !_was_include)
662  for_each (children_->begin(), children_->end(), [](XmlSubEls::value_type& pp) { delete pp.second; });
663  delete children_;
664  delete ordchildren_;
665 }
XmlSet * ordchildren_
Set of all child elements in file order.
Definition: xml.hpp:91
XmlSubEls * children_
simple n-ary tree
Definition: xml.hpp:87
std::string * value_
Definition: xml.hpp:62
XmlAttrs * attrs_
Definition: xml.hpp:76
bool _was_include
Definition: xml.hpp:88
std::string * decl_
Definition: xml.hpp:62
XmlElement::XmlElement ( const XmlElement )
delete

Copy Ctor. Non-copyable.

Member Function Documentation

XmlAttrs::const_iterator XmlElement::abegin ( ) const
inline

Get an iterator to the first child attribute.

Returns
const_iterator to first attribute

Definition at line 324 of file xml.hpp.

Referenced by output_attributes().

324 { return attrs_ ? attrs_->begin() : emptyattrs_.end(); }
XmlAttrs * attrs_
Definition: xml.hpp:76
static F8API const XmlAttrs emptyattrs_
Definition: xml.hpp:77
XmlAttrs::const_iterator XmlElement::aend ( ) const
inline

Get an iterator to the last+1 child attribute.

Returns
const_iterator to last+1 attribute

Definition at line 328 of file xml.hpp.

Referenced by output_attributes().

328 { return attrs_ ? attrs_->end() : emptyattrs_.end(); }
XmlAttrs * attrs_
Definition: xml.hpp:76
static F8API const XmlAttrs emptyattrs_
Definition: xml.hpp:77
XmlSet::const_iterator XmlElement::begin ( ) const
inline

Get an iterator to the first child element.

Returns
const_iterator to first element

Definition at line 332 of file xml.hpp.

332 { return ordchildren_ ? ordchildren_->begin() : emptyset_.end(); }
XmlSet * ordchildren_
Set of all child elements in file order.
Definition: xml.hpp:91
static F8API const XmlSet emptyset_
Definition: xml.hpp:92
XmlSet::const_iterator XmlElement::end ( ) const
inline

Get an iterator to the last+1 child element.

Returns
const_iterator to last+1 element

Definition at line 336 of file xml.hpp.

Referenced by FIX8::Configuration::find_element().

336 { return ordchildren_ ? ordchildren_->end() : emptyset_.end(); }
XmlSet * ordchildren_
Set of all child elements in file order.
Definition: xml.hpp:91
static F8API const XmlSet emptyset_
Definition: xml.hpp:92
static F8API XmlElement* XmlElement::Factory ( std::istream &  istr,
const char *  docpath = nullptr 
)
static

Create a new root element (and recursively parse) from a given xml filename.

Parameters
istran open std::istream of an xml document
docpathstring providing info about the document or path
Returns
the new element

Referenced by main().

static F8API XmlElement* XmlElement::Factory ( const std::string &  fname)
static

Create a new root element (and recursively parse) from a given xml istream.

Parameters
fnamethe xml filename
Returns
the new element
F8API const XmlElement* XmlElement::find ( const std::string &  what,
const std::string *  atag = nullptr,
const std::string *  aval = nullptr,
const char  delim = '/' 
) const

Find an element with a given name, attribute name and attribute value.

Parameters
whatthe name to search for
atagthe attribute name
avalthe attribute value
delimthe Xpath delimiter
Returns
the found or 0 if not found

Referenced by FIX8::Configuration::create_clients(), find_child(), FindAttrGetValue(), FIX8::Configuration::get_addresses(), load_fields(), load_fix_version(), FIX8::Configuration::load_map(), load_messages(), main(), precomp(), process(), and process_message_fields().

F8API int XmlElement::find ( const std::string &  what,
XmlSet eset,
const std::string *  atag = nullptr,
const std::string *  aval = nullptr,
const char  delim = '/' 
) const

Recursively find all elements with a given name, attribute name and attribute value.

Parameters
whatthe name to search for
esettarget XmlSet to place results
atagthe attribute name
avalthe attribute value
delimthe Xpath delimiter
Returns
the number of found elements
F8API const XmlElement* XmlElement::find_child ( const std::string &  what,
const std::string *  atag = nullptr,
const std::string *  aval = nullptr,
const char  delim = '/' 
) const
inline

Find a child element with a given name, attribute name and attribute value. This version assumes the base of the search term already contains the current xpath tag and delimiter.

Parameters
whatthe name to search for
atagthe attribute name
avalthe attribute value
delimthe Xpath delimiter
Returns
the found or 0 if not found

Definition at line 144 of file xml.hpp.

References find().

146  {
147  const std::string sterm(tag_ + '/' + what);
148  return find(sterm, atag, aval, delim);
149  }
std::string tag_
Definition: xml.hpp:62
F8API const XmlElement * find(const std::string &what, const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const
F8API int XmlElement::find_child ( const std::string &  what,
XmlSet eset,
const std::string *  atag = nullptr,
const std::string *  aval = nullptr,
const char  delim = '/' 
) const
inline

Recursively find all child elements with a given name, attribute name and attribute value. This version assumes the base of the search term already contains the current xpath tag and delimiter.

Parameters
whatthe name to search for
esettarget XmlSet to place results
atagthe attribute name
avalthe attribute value
delimthe Xpath delimiter
Returns
the number of found elements

Definition at line 159 of file xml.hpp.

References find().

161  {
162  const std::string sterm(tag_ + '/' + what);
163  return find(sterm, eset, atag, aval, delim);
164  }
std::string tag_
Definition: xml.hpp:62
F8API const XmlElement * find(const std::string &what, const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const
template<typename T >
T XmlElement::FindAttr ( const std::string &  what,
const T  defValue 
) const
inline

Find an attribute with the given name and return its typed value.

Template Parameters
typeof target attribute
Parameters
whatattribute to find
defValuevalue to return if attribute was not found
Returns
the value of the found attribute

Definition at line 209 of file xml.hpp.

Referenced by FIX8::Configuration::create_persister(), FIX8::Configuration::create_schedule(), FIX8::Configuration::create_session_schedule(), FIX8::Configuration::find_or_default(), FIX8::Configuration::get_logname(), and FIX8::recover_line().

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  }
XmlAttrs * attrs_
Definition: xml.hpp:76
bool XmlElement::findAttrByValue ( const std::string &  what,
const std::string &  value 
) const

Find an attribute with the given name and value.

Parameters
whatattribute to find
valueattribute value
Returns
true if found

Definition at line 736 of file xml.cpp.

References attrs_.

737 {
738  if (attrs_)
739  {
740  XmlAttrs::iterator itr(attrs_->find(what));
741  return itr != attrs_->end() && itr->second == val;
742  }
743 
744  return false;
745 }
XmlAttrs * attrs_
Definition: xml.hpp:76
bool XmlElement::FindAttrGetValue ( const std::string &  what,
std::string &  target 
)
inline

Find an element and obtain the attribute's value with the name "value".

Parameters
whatname to find
targetwhere to place value
Returns
true if found

Definition at line 191 of file xml.hpp.

References find(), and GetAttrValue().

192  {
193  const XmlElement *inst(find(what));
194  return inst ? inst->GetAttrValue(target) : false;
195  }
A simple xml parser with Xpath style lookup.
Definition: xml.hpp:48
F8API const XmlElement * find(const std::string &what, const std::string *atag=nullptr, const std::string *aval=nullptr, const char delim='/') const
template<typename T >
T& XmlElement::FindAttrRef ( const std::string &  what,
T &  target 
) const
inline

Find an attribute with the given name and populate supplied target with its typed value.

Template Parameters
typeof target attribute
Parameters
whatattribute to find
targetlocation to return value
Returns
reference to value of the found attribute

Definition at line 227 of file xml.hpp.

Referenced by FIX8::Configuration::create_logger().

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  }
XmlAttrs * attrs_
Definition: xml.hpp:76
bool XmlElement::GetAttr ( const std::string &  what,
std::string &  target 
) const

Find an attribute's with the given name.

Parameters
whatattribute to find
targetwhere to place value
Returns
true if found

Definition at line 748 of file xml.cpp.

References attrs_.

Referenced by FIX8::Configuration::create_logger(), FIX8::Configuration::create_persister(), FIX8::Configuration::create_schedule(), FIX8::Configuration::create_session_schedule(), FIX8::Configuration::from_or_default(), FIX8::Configuration::get_logflags(), FIX8::Configuration::get_time_field(), and GetAttrValue().

749 {
750  if (attrs_)
751  {
752  XmlAttrs::iterator itr(attrs_->find(what));
753  if (itr != attrs_->end())
754  {
755  target = itr->second;
756  return true;
757  }
758  }
759 
760  return false;
761 }
XmlAttrs * attrs_
Definition: xml.hpp:76
bool XmlElement::GetAttrValue ( std::string &  target) const
inline

Find an attribute's value with the name "value".

Parameters
targetwhere to place value
Returns
true if found

Definition at line 175 of file xml.hpp.

References GetAttr().

Referenced by FindAttrGetValue().

176  {
177  static const std::string valstr("value");
178  return GetAttr(valstr, target);
179  }
F8API bool GetAttr(const std::string &what, std::string &target) const
Definition: xml.cpp:748
int XmlElement::GetChildCnt ( ) const
inline

Get the count of children this element has.

Returns
the number of children

Definition at line 264 of file xml.hpp.

References chldcnt_.

264 { return chldcnt_; }
int chldcnt_
Definition: xml.hpp:63
const std::string* XmlElement::GetDecl ( ) const
inline

Get the declaration if available.

Returns
the declaration

Definition at line 309 of file xml.hpp.

References decl_.

309 { return decl_; }
std::string * decl_
Definition: xml.hpp:62
int XmlElement::GetDepth ( ) const
inline

Get the depth of this element

Returns
the depth

Definition at line 252 of file xml.hpp.

References depth_.

252 { return depth_; }
int depth_
Definition: xml.hpp:63
int XmlElement::GetErrorCnt ( ) const
inline

Get the global error count.

Returns
the error count

Definition at line 256 of file xml.hpp.

References errors_.

256 { return errors_; }
int errors_
Definition: xml.hpp:59
int XmlElement::GetLine ( ) const
inline

Get the source line of this element (element order).

Returns
the line

Definition at line 268 of file xml.hpp.

References txtline_.

Referenced by output_attributes(), and FIX8::recover_line().

268 { return txtline_; }
int txtline_
Definition: xml.hpp:63
int XmlElement::GetLineCnt ( ) const
inline

Get the actual current source line.

Returns
the source line

Definition at line 260 of file xml.hpp.

References line_.

260 { return line_; }
int line_
Definition: xml.hpp:59
std::string XmlElement::GetLocString ( ) const
inline

Generate a formatted string for error reporting

Returns
formatted string

Definition at line 296 of file xml.hpp.

References GetPath().

297  {
298  std::ostringstream ostr;
299  ostr << '"' << tag_ << "\" (" << GetPath() << ':' << txtline_ << ')';
300  return ostr.str();
301  }
std::string GetPath() const
Definition: xml.hpp:288
int txtline_
Definition: xml.hpp:63
std::string tag_
Definition: xml.hpp:62
int XmlElement::GetMaxDepth ( ) const
inline

Get the maximum depth from this node

Returns
the maximum depth

Definition at line 284 of file xml.hpp.

References maxdepth_.

284 { return maxdepth_; }
int maxdepth_
Definition: xml.hpp:59
int XmlElement::GetMaxDepthPermitted ( ) const
inline

Get the maximum depth supported.

Returns
the maximum depth

Definition at line 280 of file xml.hpp.

References MaxDepth.

280 { return MaxDepth; }
const XmlElement* XmlElement::GetParent ( ) const
inline

Get the parent element of this tree, const version

Returns
the parent element, 0 if no parent

Definition at line 344 of file xml.hpp.

References parent_.

344 { return parent_; }
XmlElement * parent_
Definition: xml.hpp:57
std::string XmlElement::GetPath ( ) const
inline

Get the full xpath for the element; recursive

Returns
the path

Definition at line 288 of file xml.hpp.

References GetPath(), and tag_.

Referenced by GetLocString(), and GetPath().

288 { return parent_ ? parent_->GetPath() + '/' + tag_ : "//" + tag_; }
XmlElement * parent_
Definition: xml.hpp:57
std::string GetPath() const
Definition: xml.hpp:288
std::string tag_
Definition: xml.hpp:62
const XmlElement* XmlElement::GetRoot ( ) const
inline

Get the root element of this tree, const version

Returns
the root element

Definition at line 340 of file xml.hpp.

References root_.

340 { return parent_ ? parent_->root_ : this; }
XmlElement * parent_
Definition: xml.hpp:57
XmlElement * root_
Definition: xml.hpp:57
int XmlElement::GetSequence ( ) const
inline

Get the sequence number for this element (incremented for each new element).

Returns
the sequence number

Definition at line 276 of file xml.hpp.

References sequence_.

Referenced by XmlElement::EntityOrderComp::operator()().

276 { return sequence_; }
int sequence_
Definition: xml.hpp:63
int XmlElement::GetSubIdx ( ) const
inline

Get the subindex.

Returns
the subindex

Definition at line 272 of file xml.hpp.

References subidx_.

272 { return subidx_; }
int subidx_
Definition: xml.hpp:63
const std::string& XmlElement::GetTag ( ) const
inline

Get the element tag name.

Returns
the tag

Definition at line 292 of file xml.hpp.

References tag_.

Referenced by Insert(), and precomp().

292 { return tag_; }
std::string tag_
Definition: xml.hpp:62
const std::string* XmlElement::GetVal ( ) const
inline

Get the element value.

Returns
the value

Definition at line 305 of file xml.hpp.

References value_.

305 { return value_; }
std::string * value_
Definition: xml.hpp:62
bool XmlElement::HasAttr ( const std::string &  what) const
inline

Check if an attribute with the given name is present.

Parameters
whatattribute to find
Returns
true if found

Definition at line 184 of file xml.hpp.

Referenced by FIX8::Configuration::create_logger(), and FIX8::Configuration::find_or_default().

185  { return attrs_ ? attrs_->find(what) != attrs_->end() : false; }
XmlAttrs * attrs_
Definition: xml.hpp:76
const string & XmlElement::InplaceXlate ( std::string &  what)

Perform xml translation on the supplied string inplace. Translate predefined entities and numeric character references.

Parameters
whatsource string to translate
Returns
the translated string

Definition at line 764 of file xml.cpp.

References flags_, noextensions, rCE_, rCX_, rEn_, FIX8::RegExp::Replace(), rEv_, FIX8::RegExp::SearchString(), stringtochar_, and FIX8::RegExp::SubExpr().

765 {
766  RegMatch match;
767  while (rCX_.SearchString(match, what, 2) == 2)
768  {
769  string whatv;
770  rCX_.SubExpr(match, what, whatv, 0, 1);
771  const auto sitr(stringtochar_.find(whatv));
772  rCX_.Replace(match, what, sitr == stringtochar_.cend() ? '?' : sitr->second); // not found character entity replaces string with '?'
773  }
774 
775  while (rCE_.SearchString(match, what, 2) == 2) // translate Numeric character references &#x12d; or &#12;
776  {
777  string whatv;
778  rCE_.SubExpr(match, what, whatv, 0, 1);
779  istringstream istr(whatv);
780  int value;
781  if (whatv[0] == 'x')
782  {
783  istr.ignore();
784  istr >> hex >> value;
785  }
786  else
787  istr >> dec >> value;
788  string oval;
789  if (value & 0xff00) // handle hi byte
790  oval += static_cast<char>(value >> 8 & 0xff);
791  oval += static_cast<char>(value & 0xff);
792  rCE_.Replace(match, what, oval);
793  }
794 
795  if (!(flags_ & noextensions))
796  {
797  if (rEn_.SearchString(match, what, 2) == 2) // environment var replacement ${XXX}
798  {
799  string whatv;
800  rEn_.SubExpr(match, what, whatv, 0, 1);
801  const char *gresult(getenv(whatv.c_str()));
802  if (gresult)
803  {
804  const string result(gresult);
805  if (!result.empty())
806  rEn_.Replace(match, what, result);
807  }
808  }
809 
810  if (rEv_.SearchString(match, what, 2) == 2) // evaluate shell command and replace with result !{XXX}
811  {
812  string whatv;
813  rEv_.SubExpr(match, what, whatv, 0, 1);
814  string result;
815  if (exec_cmd(whatv, result))
816  rEv_.Replace(match, what, result);
817  }
818  }
819 
820  return what;
821 }
static std::string & SubExpr(RegMatch &match, const std::string &source, std::string &target, const int offset=0, const int num=0)
Definition: f8utils.hpp:452
static FIX8::RegExp rEv_
Definition: xml.hpp:55
static const Str2Chr stringtochar_
XML entity char lookup.
Definition: xml.hpp:51
static XmlFlags flags_
Definition: xml.hpp:81
static std::string & Replace(RegMatch &match, std::string &source, const std::string &with, const int num=0)
Definition: f8utils.hpp:489
static FIX8::RegExp rEn_
Definition: xml.hpp:55
int SearchString(RegMatch &match, const std::string &source, const int subExpr, const int offset=0) const
Definition: f8utils.hpp:431
A class to contain regex matches using RegExp.
Definition: f8utils.hpp:308
static FIX8::RegExp rCE_
Definition: xml.hpp:55
static FIX8::RegExp rCX_
Definition: xml.hpp:55
bool XmlElement::Insert ( XmlElement what)

Insert an element as a child of this element

Parameters
whatelekent to insert
Returns
true if successful

Definition at line 523 of file xml.cpp.

References children_, chldcnt_, GetTag(), and ordchildren_.

Referenced by main().

524 {
525  if (!what)
526  return false;
527 
528  if (!children_)
529  {
530  children_ = new XmlSubEls;
531  ordchildren_ = new XmlSet;
532  }
533 
534  ++chldcnt_;
535  children_->insert({what->GetTag(), what});
536  ordchildren_->insert(what);
537 
538  return true;
539 }
XmlSet * ordchildren_
Set of all child elements in file order.
Definition: xml.hpp:91
XmlSubEls * children_
simple n-ary tree
Definition: xml.hpp:87
std::multimap< std::string, XmlElement * > XmlSubEls
Definition: xml.hpp:85
std::set< const XmlElement *, EntityOrderComp > XmlSet
Definition: xml.hpp:74
const std::string & GetTag() const
Definition: xml.hpp:292
int chldcnt_
Definition: xml.hpp:63
XmlElement& XmlElement::operator= ( const XmlElement )
delete

Assignment operator. Non-copyable.

int XmlElement::ParseAttrs ( const std::string &  attlst)

Parse the xml attributes from an element.

Parameters
attlststring of attributes
Returns
number of attributes extracted

Definition at line 542 of file xml.cpp.

References attrs_, errors_, flags_, incline_, inclusion_, line_, noextensions, and root_.

543 {
544  istringstream istr(attlst);
545  enum { ews, tag, es, oq, value, oc0, comment, cc0 } state(ews);
546  string tmptag, tmpval;
547  char comchar(0);
548 
549  while (istr.good())
550  {
551  char c;
552  istr >> noskipws >> c;
553 
554  switch (state)
555  {
556  case comment:
557  if (c == '*')
558  state = cc0;
559  break;
560  case cc0:
561  if (c == '/')
562  state = ews;
563  else
564  state = comment;
565  break;
566  case ews:
567  if (c == '/')
568  state = oc0;
569  else if (!isspace(c))
570  {
571  tmptag += c;
572  state = tag;
573  }
574  break;
575  case oc0:
576  if (c == '*' && !(flags_ & noextensions))
577  {
578  state = comment;
579  break;
580  }
581  else
582  {
583  tmptag += '/';
584  tmptag += c;
585  state = tag;
586  }
587  case tag:
588  if (isspace(c))
589  state = es;
590  else if (c == '=')
591  state = oq;
592  else if (c == '"' || c == '\'')
593  {
594 illegal_char:
595  ostringstream ostr;
596  ostr << "Error (" << root_->line_ << ") attribute \'" << tmptag << "\' illegal character defined";
597  if (!root_->inclusion_.empty())
598  ostr << " in inclusion " << root_->inclusion_ << " (" << root_->incline_ << ')';
599  throw XMLError(ostr.str());
600  }
601  else
602  tmptag += c;
603  break;
604  case es:
605  if (c == '=')
606  state = oq;
607  else if (c == '"' || c == '\'')
608  goto illegal_char;
609  break;
610  case oq:
611  if (c == '"' || c == '\'')
612  {
613  comchar = c;
614  state = value;
615  }
616  else if (!isspace(c))
617  goto illegal_char;
618  break;
619  case value:
620  if (c != comchar)
621  tmpval += c;
622  else
623  {
624  if (tmptag.find_first_of("\\\'\"=") != string::npos)
625  goto illegal_char;
626  if (tmptag != "docpath")
627  {
628  if (!attrs_)
629  attrs_ = new XmlAttrs;
630  if (!attrs_->insert({tmptag, InplaceXlate(tmpval)}).second)
631  {
632  ++root_->errors_;
633  ostringstream ostr;
634  ostr << "Error (" << root_->line_ << ") attribute \'" << tmptag << "\' already defined";
635  if (!root_->inclusion_.empty())
636  ostr << " in inclusion " << root_->inclusion_ << " (" << root_->incline_ << ')';
637  throw XMLError(ostr.str());
638  }
639  }
640  tmptag.clear();
641  tmpval.clear();
642  state = ews;
643  comchar = 0;
644  }
645  break;
646  default:
647  break;
648  }
649  }
650 
651  return attrs_ ? static_cast<int>(attrs_->size()) : 0;
652 }
std::string inclusion_
Definition: xml.hpp:60
std::map< std::string, std::string > XmlAttrs
Definition: xml.hpp:75
XmlElement * root_
Definition: xml.hpp:57
static XmlFlags flags_
Definition: xml.hpp:81
XmlAttrs * attrs_
Definition: xml.hpp:76
An invalid configuration parameter was passed.
int errors_
Definition: xml.hpp:59
int line_
Definition: xml.hpp:59
int incline_
Definition: xml.hpp:59
static void XmlElement::set_flags ( XmlFlags  flags)
inlinestatic

Definition at line 82 of file xml.hpp.

82 { flags_ = flags; }
static XmlFlags flags_
Definition: xml.hpp:81

Friends And Related Function Documentation

F8API std::ostream& operator<< ( std::ostream &  os,
const XmlElement en 
)
friend

Inserter friend.

Parameters
osstream to send to
enXmlElement REFErence
Returns
stream

Member Data Documentation

bool XmlElement::_was_include
private

Definition at line 88 of file xml.hpp.

Referenced by ~XmlElement().

XmlAttrs* XmlElement::attrs_

Definition at line 76 of file xml.hpp.

Referenced by findAttrByValue(), GetAttr(), operator<<(), ParseAttrs(), and ~XmlElement().

XmlSubEls* XmlElement::children_
private

simple n-ary tree

Definition at line 87 of file xml.hpp.

Referenced by Insert(), operator<<(), and ~XmlElement().

int XmlElement::chldcnt_
private

Definition at line 63 of file xml.hpp.

Referenced by GetChildCnt(), and Insert().

std::string * XmlElement::decl_
private

Definition at line 62 of file xml.hpp.

Referenced by GetDecl(), operator<<(), and ~XmlElement().

int XmlElement::depth_
private

Definition at line 63 of file xml.hpp.

Referenced by GetDepth(), and operator<<().

const XmlElement::XmlAttrs XmlElement::emptyattrs_
static

Definition at line 77 of file xml.hpp.

const XmlElement::XmlSet XmlElement::emptyset_
staticprivate

Definition at line 92 of file xml.hpp.

int XmlElement::errors_
private

Definition at line 59 of file xml.hpp.

Referenced by GetErrorCnt(), and ParseAttrs().

XmlElement::XmlFlags XmlElement::flags_
static

Definition at line 81 of file xml.hpp.

Referenced by InplaceXlate(), and ParseAttrs().

int XmlElement::incline_
private

Definition at line 59 of file xml.hpp.

Referenced by ParseAttrs().

std::string XmlElement::inclusion_
private

Definition at line 60 of file xml.hpp.

Referenced by ParseAttrs().

int XmlElement::line_
private

Definition at line 59 of file xml.hpp.

Referenced by GetLineCnt(), and ParseAttrs().

int XmlElement::maxdepth_
private

Definition at line 59 of file xml.hpp.

Referenced by GetMaxDepth().

XmlSet* XmlElement::ordchildren_
private

Set of all child elements in file order.

Definition at line 91 of file xml.hpp.

Referenced by Insert(), operator<<(), and ~XmlElement().

XmlElement* XmlElement::parent_
private

Definition at line 57 of file xml.hpp.

Referenced by GetParent().

RegExp XmlElement::rCE_
staticprivate

Definition at line 55 of file xml.hpp.

Referenced by InplaceXlate().

RegExp XmlElement::rCX_
staticprivate

Definition at line 55 of file xml.hpp.

Referenced by InplaceXlate().

RegExp XmlElement::rEn_
staticprivate

Definition at line 55 of file xml.hpp.

Referenced by InplaceXlate().

RegExp XmlElement::rEv_
staticprivate

Definition at line 55 of file xml.hpp.

Referenced by InplaceXlate().

RegExp XmlElement::rIn_
staticprivate

Definition at line 55 of file xml.hpp.

XmlElement * XmlElement::root_
private

Definition at line 57 of file xml.hpp.

Referenced by GetRoot(), and ParseAttrs().

int XmlElement::seq_
private

Definition at line 59 of file xml.hpp.

int XmlElement::sequence_
private

Definition at line 63 of file xml.hpp.

Referenced by GetSequence().

const Str2Chr XmlElement::stringtochar_
staticprivate
Initial value:
{
{"amp", '&'}, {"lt", '<'}, {"gt", '>'},
{"apos", '\''}, {"quot", '"'}, {"nbsp", 160},
{"iexcl", 161}, {"cent", 162}, {"pound", 163},
{"curren", 164}, {"yen", 165}, {"brvbar", 166},
{"sect", 167}, {"uml", 168}, {"copy", 169},
{"ordf", 170}, {"laquo", 171}, {"not", 172},
{"shy", 173}, {"reg", 174}, {"macr", 175},
{"deg", 176}, {"plusmn", 177}, {"sup2", 178},
{"sup3", 179}, {"acute", 180}, {"micro", 181},
{"para", 182}, {"middot", 183}, {"cedil", 184},
{"sup1", 185}, {"ordm", 186}, {"raquo", 187},
{"frac14", 188}, {"frac12", 189}, {"frac34", 190},
{"iquest", 191}
}

XML entity char lookup.

Definition at line 51 of file xml.hpp.

Referenced by InplaceXlate().

int XmlElement::subidx_
private

Definition at line 63 of file xml.hpp.

Referenced by GetSubIdx().

std::string XmlElement::tag_
private

Definition at line 62 of file xml.hpp.

Referenced by GetPath(), GetTag(), and operator<<().

int XmlElement::txtline_
private

Definition at line 63 of file xml.hpp.

Referenced by GetLine(), and operator<<().

std::string * XmlElement::value_
private

Definition at line 62 of file xml.hpp.

Referenced by GetVal(), operator<<(), and ~XmlElement().


The documentation for this class was generated from the following files: