From 927b97baaaf69e318ff7a0ce76d096375ec09da2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 15 Mar 2013 08:22:58 +0200 Subject: Add support for returning XML attributes as map --- cutl/xml/parser.hxx | 68 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 7 deletions(-) (limited to 'cutl/xml/parser.hxx') diff --git a/cutl/xml/parser.hxx b/cutl/xml/parser.hxx index 5d1e9e5..c84268a 100644 --- a/cutl/xml/parser.hxx +++ b/cutl/xml/parser.hxx @@ -5,8 +5,9 @@ #ifndef CUTL_XML_PARSER_HXX #define CUTL_XML_PARSER_HXX -#include +#include #include +#include #include #include // std::size_t @@ -82,14 +83,18 @@ namespace cutl typedef xml::qname qname_type; typedef unsigned short feature_type; + // If both receive_attributes_event and receive_attributes_map are + // specified, then receive_attributes_event is assumed. + // static const feature_type receive_elements = 0x0001; static const feature_type receive_characters = 0x0002; - static const feature_type receive_attributes = 0x0004; - static const feature_type receive_namespace_decls = 0x0008; + static const feature_type receive_attributes_map = 0x0004; + static const feature_type receive_attributes_event = 0x0008; + static const feature_type receive_namespace_decls = 0x0010; static const feature_type receive_default = receive_elements | receive_characters | - receive_attributes; + receive_attributes_map; // Parse std::istream. Input name is used in diagnostics to identify // the document being parsed. std::ios_base::failure exception is @@ -178,6 +183,42 @@ namespace cutl unsigned long long line () const {return line_;} unsigned long long column () const {return column_;} + // Attribute map lookup. If attribute is not found, then the version + // without the default value thows an appropriate parsing exception + // while the version with the default value returns that value. + // + // Note also that there is no attribute(ns,name) version since it + // would conflict with attribute(name,dv) (qualified attributes + // are not very common). + // + const std::string& + attribute (const std::string& name) const; + + template + T + attribute (const std::string& name) const; + + std::string + attribute (const std::string& name, const std::string& dv) const; + + template + T + attribute (const std::string& name, const T& dv) const; + + const std::string& + attribute (const qname_type& qname) const; + + template + T + attribute (const qname_type& qname) const; + + std::string + attribute (const qname_type& qname, const std::string& dv) const; + + template + T + attribute (const qname_type& qname, const T& dv) const; + // Optional content processing. // public: @@ -255,15 +296,27 @@ namespace cutl unsigned long long line_; unsigned long long column_; - // Attributes. + // Attributes as a map. + // + struct attribute_value + { + std::string value; + mutable bool handled; + }; + + typedef std::map attribute_map; + attribute_map attr_map_; + mutable attribute_map::size_type attr_unhandled_; + + // Attributes as events. // - struct attribute + struct attribute_type { qname_type qname; std::string value; }; - typedef std::vector attributes; + typedef std::vector attributes; attributes attr_; attributes::size_type attr_i_; // Index of the current attribute. @@ -299,5 +352,6 @@ namespace cutl } #include +#include #endif // CUTL_XML_PARSER_HXX -- cgit v1.1