[libxml++] Add dom_update_namespace example
- From: Kjell Ahlstedt <kjellahl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libxml++] Add dom_update_namespace example
- Date: Fri, 10 Oct 2014 15:08:28 +0000 (UTC)
commit 7fbaf2a95edd6a1f45f238e9dd5bedbb4bef18ad
Author: Mathias Lorente <mathias lorente gadz org>
Date: Fri Oct 10 16:42:18 2014 +0200
Add dom_update_namespace example
* .gitignore: add /examples/dom_update_namespace/dom_update_namespace
* examples/Makefile.am: Add dom_update_namespace files.
* examples/dom_update_namespace/main.cc:
* examples/dom_update_namespace/example1.xml:
* examples/dom_update_namespace/example2.xml: New files. Bug #737682.
.gitignore | 1 +
examples/Makefile.am | 6 +
examples/dom_update_namespace/example1.xml | 6 +
examples/dom_update_namespace/example2.xml | 4 +
examples/dom_update_namespace/main.cc | 189 ++++++++++++++++++++++++++++
5 files changed, 206 insertions(+), 0 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 08b969a..270b5bf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -51,6 +51,7 @@ stamp-h?
/examples/dom_parser_raw/dom_parser_raw
/examples/dom_read_write/dom_read_write
/examples/dom_read_write/example_output.xml
+/examples/dom_update_namespace/dom_update_namespace
/examples/dom_xinclude/dom_xinclude
/examples/dom_xpath/dom_xpath
/examples/dtdvalidation/dtdvalidation
diff --git a/examples/Makefile.am b/examples/Makefile.am
index d9541ca..57ebcc9 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -25,6 +25,7 @@ check_PROGRAMS = \
dom_parser/dom_parser \
dom_parser_raw/dom_parser_raw \
dom_read_write/dom_read_write \
+ dom_update_namespace/dom_update_namespace \
dom_xinclude/dom_xinclude \
dom_xpath/dom_xpath \
dtdvalidation/dtdvalidation \
@@ -43,6 +44,7 @@ check_SCRIPTS = \
dom_parser/make_check.sh \
dom_parser_raw/make_check.sh \
dom_read_write/make_check.sh \
+ dom_update_namespace/make_check.sh \
dom_xinclude/make_check.sh \
dom_xpath/make_check.sh \
dtdvalidation/make_check.sh \
@@ -68,6 +70,8 @@ dom_parser_raw_dom_parser_raw_SOURCES = \
dom_parser_raw/main.cc
dom_read_write_dom_read_write_SOURCES = \
dom_read_write/main.cc
+dom_update_namespace_dom_update_namespace_SOURCES = \
+ dom_update_namespace/main.cc
dom_xinclude_dom_xinclude_SOURCES = \
dom_xinclude/main.cc
dom_xpath_dom_xpath_SOURCES = \
@@ -117,6 +121,8 @@ dist_noinst_DATA = \
dom_read_write/README \
dom_read_write/example.xml \
dom_read_write/example.dtd \
+ dom_update_namespace/example1.xml \
+ dom_update_namespace/example2.xml \
dom_xinclude/example.xml \
dom_xinclude/include1.txt \
dom_xinclude/include2.xml \
diff --git a/examples/dom_update_namespace/example1.xml b/examples/dom_update_namespace/example1.xml
new file mode 100644
index 0000000..1c7ec41
--- /dev/null
+++ b/examples/dom_update_namespace/example1.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<root xmlns="http://default.namespace/root">
+ <child>
+ <sub_child/>
+ </child>
+</root>
diff --git a/examples/dom_update_namespace/example2.xml b/examples/dom_update_namespace/example2.xml
new file mode 100644
index 0000000..5af6dfa
--- /dev/null
+++ b/examples/dom_update_namespace/example2.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ns0:root xmlns:ns0="http://default.namespace/root">
+ <ns0:child/>
+</ns0:root>
diff --git a/examples/dom_update_namespace/main.cc b/examples/dom_update_namespace/main.cc
new file mode 100644
index 0000000..ce8037f
--- /dev/null
+++ b/examples/dom_update_namespace/main.cc
@@ -0,0 +1,189 @@
+/* Copyright (C) 2014 The libxml++ development team
+ *
+ * This file is part of libxml++.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <libxml++/libxml++.h>
+#include <iostream>
+#include <cstdlib>
+#include <exception>
+
+class Tests
+{
+ public:
+ virtual ~Tests() {}
+
+ protected:
+ template<typename RefType, typename ValueType>
+ void assert_equal(const RefType& reference, const ValueType& value, const std::string& msg);
+ template<typename RefType, typename ValueType>
+ void assert_not_equal(const RefType& reference, const ValueType& value, const std::string& msg);
+
+ template <typename RefType, typename ValueType>
+ void fail(const RefType& reference, const ValueType& value, const std::string& msg);
+};
+
+class fail_exception : public std::exception
+{
+ public:
+ fail_exception(const std::string& msg) : msg_(msg) {}
+ virtual ~fail_exception() throw() {}
+ virtual const char* what() const throw() { return msg_.c_str(); }
+
+ private:
+ std::string msg_;
+};
+
+
+////////////////////////////////////////////////////////////////////////////////
+
+class TestNamespace : public Tests
+{
+ public:
+ TestNamespace();
+ void test_create_new_node_with_default_namespace();
+ void test_create_new_node_using_existing_namespace_prefix();
+
+ private:
+ void setup(const std::string& filename);
+
+ xmlpp::Node::PrefixNsMap nsmap_;
+ xmlpp::DomParser parser_;
+ xmlpp::Node* root_;
+};
+
+
+TestNamespace::TestNamespace()
+:
+root_(0)
+{
+ nsmap_["ns0"] = "http://default.namespace/root";
+ nsmap_["ns1"] = "http://default.namespace/child";
+}
+
+void TestNamespace::setup(const std::string& filename)
+{
+ parser_.set_throw_messages(true);
+ parser_.set_substitute_entities(true);
+ parser_.parse_file(filename);
+
+ xmlpp::Document* document = parser_.get_document();
+ root_ = document->get_root_node();
+}
+
+void TestNamespace::test_create_new_node_with_default_namespace()
+{
+ const Glib::ustring filename = "example1.xml";
+ setup(filename);
+
+ // Check original document
+ assert_equal(1, root_->find("/ns0:root/ns0:child", nsmap_).size(),
+ "Input file should have one child in default namespace");
+ assert_equal(0, root_->find("/ns0:root/ns1:child", nsmap_).size(),
+ "Input file shouldn't have any child in alternate default namespace");
+
+ // Add child node in default namespace and check again document
+ xmlpp::Element* child = root_->add_child("child");
+ child->set_namespace_declaration(nsmap_["ns1"], "");
+
+ std::cout << " File " << filename << " after modification" << std::endl
+ << parser_.get_document()->write_to_string_formatted() << std::endl;
+
+ // Here we should have two children nodes into two different namespaces
+ assert_equal(1, root_->find("/ns0:root/ns0:child", nsmap_).size(),
+ "Updated input file should have one child in default namespace");
+ assert_equal(1, root_->find("/ns0:root/ns1:child", nsmap_).size(),
+ "Updated input file should have one child in alternate default namespace");
+}
+
+void TestNamespace::test_create_new_node_using_existing_namespace_prefix()
+{
+ const Glib::ustring filename = "example2.xml";
+ setup(filename);
+
+ // Check original document
+ assert_equal(1, root_->find("/ns0:root/ns0:child", nsmap_).size(),
+ "Input file should have one child in root namespace");
+ assert_equal(0, root_->find("/ns0:root/ns1:child", nsmap_).size(),
+ "Input file shouldn't have any child in child namespace");
+
+ // Add child node with specific namespace and check again document
+ xmlpp::Element* child = root_->add_child("child", "ns0");
+ child->set_namespace_declaration(nsmap_["ns1"], "");
+
+ std::cout << " File " << filename << " after modification" << std::endl
+ << parser_.get_document()->write_to_string_formatted() << std::endl;
+
+ // Here we should have two children nodes in same namespace
+ assert_equal(2, root_->find("/ns0:root/ns0:child", nsmap_).size(),
+ "Updated input file should have two children in root namespace");
+ assert_equal(0, root_->find("/ns0:root/ns1:child", nsmap_).size(),
+ "Updated input file shouldn't have any child in child namespace");
+}
+
+
+////////////////////////////////////////////////////////////////////////////////
+template <typename RefType, typename ValueType>
+void Tests::assert_equal(const RefType& reference, const ValueType& value, const std::string& msg)
+{
+ if (ValueType(reference) == value) return;
+ fail(reference, value, msg);
+}
+
+template <typename RefType, typename ValueType>
+void Tests::assert_not_equal(const RefType& reference, const ValueType& value, const std::string& msg)
+{
+ if (ValueType(reference) != value) return;
+ fail(reference, value, msg);
+}
+
+template <typename RefType, typename ValueType>
+void Tests::fail(const RefType& reference, const ValueType& value, const std::string& msg)
+{
+ std::stringstream error;
+ error << msg << " (reference: " << reference << ", found: " << value << ")";
+ throw fail_exception(error.str());
+}
+
+
+////////////////////////////////////////////////////////////////////////////////
+
+int
+main(int /* argc */, char** /* argv */)
+{
+ // Set the global C and C++ locale to the user-configured locale,
+ // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+ std::locale::global(std::locale(""));
+
+ TestNamespace tests;
+
+ try
+ {
+ tests.test_create_new_node_with_default_namespace();
+ tests.test_create_new_node_using_existing_namespace_prefix();
+ }
+ catch (const std::exception& ex)
+ {
+ std::cerr << "Exception caught: " << ex.what() << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ return EXIT_SUCCESS;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]