27#ifndef _SAXPARSER_HPP_
28#define _SAXPARSER_HPP_
33#include "boost/iostreams/positioning.hpp"
82 memcpy(
data(),rhs.c_str(),rhs.length());
83 (*this)[rhs.length()] = 0;
87 if (strchr(
c_str(),
'&')) {
106 init(rhs ? strlen(rhs) : 0);
115 size_t oldsize =
length();
123 size_t rhslen = rhs?strlen(rhs):0;
125 size_t oldsize =
length();
127 strcpy(
data()+oldsize,rhs);
133 return c && !strcmp(c,
c_str());
137 return !strcmp(
c_str(),s.c_str());
148 size_t new_used = size +
_lead;
152 throw std::runtime_error(
"SAXParser: cannot allocate memory");
183 for (
const char *c=
c_str(); *c && strchr(
" \n\r\t",*c); c++) {
190 return !strncmp(
c_str(),txt,strlen(txt));
193 size_t len = strlen(txt);
227template<
typename Target >
inline Target
textToValue(
const char *txt);
231 return (
float)
ATOF( txt ) ;
256 return (
unsigned int) strtoul( txt, NULL, 10 );
261 return strtoul( txt, NULL, 10 );
264#if defined(BOOST_HAS_LONG_LONG)
266template<>
inline long long textToValue(
const char *txt)
268#if defined(BOOST_HAS_MS_INT64)
275template<>
inline unsigned long long textToValue(
const char *txt)
277#if defined(BOOST_HAS_MS_INT64)
278 return _strtoui64(txt,NULL,10);
280 return strtoull( txt, NULL, 10 );
288 return strcmp(t,
"0") && strcmp(t,
"false");
296template<>
inline boost::logic::tribool
textToValue(
const char *txt)
298 using namespace boost::logic;
300 return tribool(indeterminate);
310 return std::string( txt );
357 Attributes(
const char * _source_text,
size_t _source_text_len,
bool _autoUnescape) :
360 size=_source_text_len;
409 for (
size_t n=
attrs.size();n--;)
421 for (
size_t n=
attrs.size();n--;)
459 while (*c && !strchr(
" \n\r\t/",*c)) c++;
461 while (*c && strchr(
" \n\r\t",*c)) c++;
498 template<
typename T >
506 return (
size_t)strtoul(
getValuePtr(Unescape),NULL,10);
514 void set(
const char *_name,
char *_value,
bool _needsUnescape)
527 attribute_list::const_iterator
begin()
const
530 return attrs.begin();
532 attribute_list::const_iterator
end()
const
537 attribute_list::const_iterator
find(
const std::string &name)
const
539 attribute_list::const_iterator it;
540 for (it =
begin(); it !=
end() ; it++ )
542 if (it->matchName(name.c_str()))
565 for (attribute_list::const_iterator it=
attrs.begin();it!=
attrs.end();it++)
567 if (it->matchName(name))
586 const std::string& data,
604 template <
typename T>
613 result = attr->
valueAs<T>(Unescape);
632 template <
typename T>
647 std::string& result)
const
658 template <
typename T>
660 const std::string &name,
const char * getName() const
bool matchName(const char *test) const
void set(const char *_name, char *_value, bool _needsUnescape)
std::string getValue(XMLUnescapeBehavior_t Unescape=XMLUnescapeDefault) const
T valueAs(XMLUnescapeBehavior_t Unescape) const
const char * getValuePtr(XMLUnescapeBehavior_t Unescape=XMLUnescapeDefault) const
size_t valueAs(XMLUnescapeBehavior_t Unescape) const
Attributes(const char *_source_text, size_t _source_text_len, bool _autoUnescape)
void test_invariant() const
attribute_list::const_iterator begin() const
attribute_list::const_iterator end() const
std::vector< attribute > attribute_list
attribute_list::const_iterator find(const std::string &name) const
Attributes & operator=(const Attributes &rhs)
Attributes(const Attributes &rhs)
Attributes(saxstring &str, bool _autoUnescape)
const char * findValueByName(const char *name, XMLUnescapeBehavior_t Unescape=XMLUnescapeDefault) const
const char * getTagName() const
const attribute * findAttributeByName(const char *name) const
const char * getTextBuffer() const
PWIZ_API_DECL void parseAttributes(std::string::size_type &index) const
SAX event handler interface.
bool parseCharacters
When false, no calls to characters() will be made.
bool autoUnescapeCharacters
T & getAttribute(const Attributes &attributes, const char *name, T &result) const
virtual Status processingInstruction(const std::string &name, const std::string &data, stream_offset position)
virtual Status characters(const SAXParser::saxstring &text, stream_offset position)
bool autoUnescapeAttributes
Setting these to false will disable the auto-unescaping feature of the parser; this is useful for han...
std::string & getAttribute(const Attributes &attributes, const char *name, std::string &result) const
T & getAttribute(const Attributes &attributes, const char *name, T &result, XMLUnescapeBehavior_t Unescape, T defaultValue=T()) const
virtual Status endElement(const std::string &name, stream_offset position)
T & getAttribute(const Attributes &attributes, const std::string &name, T &result, T defaultValue=T()) const
virtual Status startElement(const std::string &name, const Attributes &attributes, stream_offset position)
const char * getAttribute(const Attributes &attributes, const char *name, XMLUnescapeBehavior_t Unescape, const char *defaultValue=NULL) const
int version
contextual version available to control handler logic which support multiple versions of a schema; th...
boost::iostreams::stream_offset stream_offset
char & operator[](size_t n)
saxstring & operator=(const SAXParser::saxstring &rhs)
bool ends_with(const char *txt) const
char * resize(size_t size)
saxstring(const std::string &rhs)
saxstring & operator+=(const SAXParser::saxstring &rhs)
const char * c_str() const
saxstring(const SAXParser::saxstring &rhs)
bool starts_with(const char *txt) const
bool operator==(const char *c) const
PWIZ_API_DECL void parse(std::istream &is, Handler &handler)
Extract a single XML element from the istream, sending SAX events to the handler.
PWIZ_API_DECL size_t count_trail_ws(const char *data, size_t len)
std::ostream & operator<<(std::ostream &os, const saxstring &s)
Target textToValue(const char *txt)
PWIZ_API_DECL void unescapeXML(char *str)
bool istrue(const char *t)
PWIZ_API_DECL std::string decode_xml_id_copy(const std::string &str)
Decodes any characters encoded with their hexadecimal value, e.g.
PWIZ_API_DECL std::string xml_root_element(const std::string &fileheader)
Returns the root element from an XML buffer; throws runtime_error if no element is found.
PWIZ_API_DECL std::string xml_root_element_from_file(const std::string &filepath)
Returns the root element from an XML file; throws runtime_error if no element is found.
PWIZ_API_DECL std::string & decode_xml_id(std::string &str)
Decodes any characters encoded with their hexadecimal value, e.g.
Handler returns the Status struct as a means of changing the parser's behavior.
Status(Flag _flag=Ok, Handler *_delegate=0)