summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKohei Yoshida <kohei.yoshida@gmail.com>2021-12-07 23:10:41 -0500
committerKohei Yoshida <kohei.yoshida@gmail.com>2021-12-07 23:10:41 -0500
commit7ef047ae9ff3508d3c41749c55c893dcd2e1defb (patch)
tree985908fc530f1496bec9992951ec0f68bb2131a9
parent06f7d237fce4e47f388e58e721ac60e84b3f9b2e (diff)
downloadorcus-7ef047ae9ff3508d3c41749c55c893dcd2e1defb.tar.gz
Print ':' only when the namespace is not empty. feature/138-xml-misplaced-elements
Also add dump_state() to xmlns_context for debugging. Sometimes the code fails to fetch correct alias string from a namespace value.
-rw-r--r--include/orcus/xml_namespace.hpp5
-rw-r--r--src/liborcus/xml_util.cpp13
-rw-r--r--src/parser/xml_namespace.cpp22
3 files changed, 38 insertions, 2 deletions
diff --git a/include/orcus/xml_namespace.hpp b/include/orcus/xml_namespace.hpp
index 89a9f4f7..fd5d9ff5 100644
--- a/include/orcus/xml_namespace.hpp
+++ b/include/orcus/xml_namespace.hpp
@@ -147,6 +147,11 @@ public:
void dump(std::ostream& os) const;
+ /**
+ * Dump the internal state for debugging in YAML format.
+ */
+ void dump_state(std::ostream& os) const;
+
void swap(xmlns_context& other) noexcept;
};
diff --git a/src/liborcus/xml_util.cpp b/src/liborcus/xml_util.cpp
index 18cbcdda..0291be98 100644
--- a/src/liborcus/xml_util.cpp
+++ b/src/liborcus/xml_util.cpp
@@ -10,6 +10,8 @@
#include <orcus/xml_namespace.hpp>
#include <orcus/tokens.hpp>
+#include <sstream>
+
namespace orcus {
xml_element_printer::xml_element_printer(const tokens& t) :
@@ -37,8 +39,15 @@ void xml_element_printer::print_namespace(std::ostream& os, xmlns_id_t ns) const
void xml_element_printer::print_element(std::ostream& os, xmlns_id_t ns, xml_token_t name) const
{
os << '<';
- print_namespace(os, ns);
- os << ':' << m_tokens.get_token_name(name) << '>';
+
+ std::ostringstream os_ns;
+ print_namespace(os_ns, ns);
+ std::string ns_str = os_ns.str();
+
+ if (!ns_str.empty())
+ os << ns_str << ':';
+
+ os << m_tokens.get_token_name(name) << '>';
}
} // namespace orcus
diff --git a/src/parser/xml_namespace.cpp b/src/parser/xml_namespace.cpp
index 66077d9c..e7dc6666 100644
--- a/src/parser/xml_namespace.cpp
+++ b/src/parser/xml_namespace.cpp
@@ -452,6 +452,28 @@ void xmlns_context::dump(std::ostream& os) const
}
}
+void xmlns_context::dump_state(std::ostream& os) const
+{
+ os << "namespaces:" << std::endl;
+ for (xmlns_id_t ns_id : get_all_namespaces())
+ {
+ size_t num_id = get_index(ns_id);
+ if (num_id == index_not_found)
+ continue;
+
+ os << " ns" << num_id << ": \"" << ns_id << '"' << std::endl;
+ }
+
+ os << "aliases:" << std::endl;
+ for (const auto& [alias, ns_list] : mp_impl->m_map)
+ {
+ os << " " << alias << ":" << std::endl;
+
+ for (const xmlns_id_t ns : ns_list)
+ os << " - " << ns << std::endl;
+ }
+}
+
void xmlns_context::swap(xmlns_context& other) noexcept
{
mp_impl.swap(other.mp_impl);