From 3a1b35c227d8b51d1bb488db511f82c666a38ea0 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 28 Mar 2013 10:54:31 +0200 Subject: Add direct access to attribute map in XML parser --- cutl/xml/parser.cxx | 10 +++++----- cutl/xml/parser.hxx | 32 ++++++++++++++++++++------------ cutl/xml/parser.ixx | 12 ++++++++++++ cutl/xml/parser.txx | 2 +- 4 files changed, 38 insertions(+), 18 deletions(-) (limited to 'cutl') diff --git a/cutl/xml/parser.cxx b/cutl/xml/parser.cxx index 42ce9ce..219fb00 100644 --- a/cutl/xml/parser.cxx +++ b/cutl/xml/parser.cxx @@ -200,7 +200,7 @@ namespace cutl { if (const element_entry* e = get_element ()) { - attribute_map::const_iterator i (e->attr_map_.find (qn)); + attribute_map_type::const_iterator i (e->attr_map_.find (qn)); if (i != e->attr_map_.end ()) { @@ -221,7 +221,7 @@ namespace cutl { if (const element_entry* e = get_element ()) { - attribute_map::const_iterator i (e->attr_map_.find (qn)); + attribute_map_type::const_iterator i (e->attr_map_.find (qn)); if (i != e->attr_map_.end ()) { @@ -242,7 +242,7 @@ namespace cutl { if (const element_entry* e = get_element ()) { - attribute_map::const_iterator i (e->attr_map_.find (qn)); + attribute_map_type::const_iterator i (e->attr_map_.find (qn)); if (i != e->attr_map_.end ()) { @@ -308,7 +308,7 @@ namespace cutl { // Find the first unhandled attribute and report it. // - for (attribute_map::const_iterator i (e.attr_map_.begin ()); + for (attribute_map_type::const_iterator i (e.attr_map_.begin ()); i != e.attr_map_.end (); ++i) { if (!i->second.handled) @@ -662,7 +662,7 @@ namespace cutl { qname_type qn; split_name (*atts, qn); - attribute_map::value_type v (qn, attribute_value ()); + attribute_map_type::value_type v (qn, attribute_value_type ()); v.second.value = *(atts + 1); v.second.handled = false; pe->attr_map_.insert (v); diff --git a/cutl/xml/parser.hxx b/cutl/xml/parser.hxx index 7806041..67beaf4 100644 --- a/cutl/xml/parser.hxx +++ b/cutl/xml/parser.hxx @@ -255,6 +255,20 @@ namespace cutl bool attribute_present (const qname_type& qname) const; + // Low-level attribute map access. Note that this API assumes + // all attributes are handled. + // + struct attribute_value_type + { + std::string value; + mutable bool handled; + }; + + typedef std::map attribute_map_type; + + const attribute_map_type& + attribute_map () const; + // Optional content processing. // public: @@ -363,16 +377,6 @@ namespace cutl namespace_decls end_ns_; namespace_decls::size_type end_ns_i_; // Index of the current decl. - // Attributes as a map. - // - struct attribute_value - { - std::string value; - mutable bool handled; - }; - - typedef std::map attribute_map; - // Element state consisting of the content model and attribute map. // struct element_entry @@ -382,13 +386,17 @@ namespace cutl std::size_t depth; content_type content; - attribute_map attr_map_; - mutable attribute_map::size_type attr_unhandled_; + attribute_map_type attr_map_; + mutable attribute_map_type::size_type attr_unhandled_; }; typedef std::vector element_state; std::vector element_state_; + // Empty attribute map to return when an element has no attributes. + // + const attribute_map_type empty_attr_map_; + // Return the element entry corresponding to the current depth, if // exists, and NULL otherwise. // diff --git a/cutl/xml/parser.ixx b/cutl/xml/parser.ixx index 2bedae4..65834b6 100644 --- a/cutl/xml/parser.ixx +++ b/cutl/xml/parser.ixx @@ -47,6 +47,18 @@ namespace cutl return attribute_present (qname_type (n)); } + inline const parser::attribute_map_type& parser:: + attribute_map () const + { + if (const element_entry* e = get_element ()) + { + e->attr_unhandled_ = 0; // Assume all handled. + return e->attr_map_; + } + + return empty_attr_map_; + } + inline void parser:: next_expect (event_type e, const qname_type& qn) { diff --git a/cutl/xml/parser.txx b/cutl/xml/parser.txx index e5286f8..8189883 100644 --- a/cutl/xml/parser.txx +++ b/cutl/xml/parser.txx @@ -14,7 +14,7 @@ namespace cutl { if (const element_entry* e = get_element ()) { - attribute_map::const_iterator i (e->attr_map_.find (qn)); + attribute_map_type::const_iterator i (e->attr_map_.find (qn)); if (i != e->attr_map_.end ()) { -- cgit v1.1