[libxml++] Examples dom_parse_entities and dom_parser: Fix when LC_ALL=C.
- From: Kjell Ahlstedt <kjellahl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libxml++] Examples dom_parse_entities and dom_parser: Fix when LC_ALL=C.
- Date: Tue, 18 Jun 2013 07:36:32 +0000 (UTC)
commit 1c56ed110075f4f8529fb67a9a235a751645a05f
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date: Tue Jun 18 09:30:27 2013 +0200
Examples dom_parse_entities and dom_parser: Fix when LC_ALL=C.
* examples/testutilities.[h|cc]: New files with class CatchConvertError.
* examples/Makefile.am: Add testutilities.[h|cc].
* examples/dom_parse_entities/main.cc:
* examples/dom_parser/main.cc: Change some strings from Glib::ustring to
CatchConvertError before they are printed. It catches Glib::ConvertError
locally. 'make check' can pass even if the global locale does not support all
printed characters. Bug #702136.
ChangeLog | 12 +++++++++
examples/Makefile.am | 6 +++-
examples/dom_parse_entities/main.cc | 15 ++++++-----
examples/dom_parser/main.cc | 24 ++++++++++--------
examples/testutilities.cc | 33 +++++++++++++++++++++++++
examples/testutilities.h | 45 +++++++++++++++++++++++++++++++++++
6 files changed, 115 insertions(+), 20 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 0b836fe..2a44863 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2013-06-18 Kjell Ahlstedt <kjell ahlstedt bredband net>
+
+ Examples dom_parse_entities and dom_parser: Fix when LC_ALL=C.
+
+ * examples/testutilities.[h|cc]: New files with class CatchConvertError.
+ * examples/Makefile.am: Add testutilities.[h|cc].
+ * examples/dom_parse_entities/main.cc:
+ * examples/dom_parser/main.cc: Change some strings from Glib::ustring to
+ CatchConvertError before they are printed. It catches Glib::ConvertError
+ locally. 'make check' can pass even if the global locale does not support all
+ printed characters. Bug #702136.
+
2.36.0:
2012-10-25 Kjell Ahlstedt <kjell ahlstedt bredband net>
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 1f43f08..1ce5618 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -40,12 +40,14 @@ check_PROGRAMS = \
check_SCRIPTS = $(addsuffix make-check-sh,$(dir $(check_PROGRAMS)))
TESTS = $(check_SCRIPTS)
+xmlpp_test_util = testutilities.h testutilities.cc
+
dom_build_dom_build_SOURCES = \
dom_build/main.cc
dom_parse_entities_dom_parse_entities_SOURCES = \
- dom_parse_entities/main.cc
+ dom_parse_entities/main.cc $(xmlpp_test_util)
dom_parser_dom_parser_SOURCES = \
- dom_parser/main.cc
+ dom_parser/main.cc $(xmlpp_test_util)
dom_parser_raw_dom_parser_raw_SOURCES = \
dom_parser_raw/main.cc
dom_read_write_dom_read_write_SOURCES = \
diff --git a/examples/dom_parse_entities/main.cc b/examples/dom_parse_entities/main.cc
index 1525f51..bf97994 100644
--- a/examples/dom_parse_entities/main.cc
+++ b/examples/dom_parse_entities/main.cc
@@ -23,9 +23,10 @@
#include <config.h>
#endif
+#include "../testutilities.h"
#include <libxml++/libxml++.h>
#include <iostream>
-#include <stdlib.h>
+#include <cstdlib>
void print_node(const xmlpp::Node* node, bool substitute_entities, unsigned int indentation = 0)
{
@@ -38,7 +39,7 @@ void print_node(const xmlpp::Node* node, bool substitute_entities, unsigned int
const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
if (nodeText && !nodeText->is_white_space())
{
- std::cout << indent << "text = " << nodeText->get_content() << std::endl;
+ std::cout << indent << "text = " << CatchConvertError(nodeText->get_content()) << std::endl;
}
}
else
@@ -47,9 +48,9 @@ void print_node(const xmlpp::Node* node, bool substitute_entities, unsigned int
const xmlpp::EntityReference* nodeEntityReference = dynamic_cast<const xmlpp::EntityReference*>(node);
if (nodeEntityReference)
{
- std::cout << indent << "entity reference name = " << nodeEntityReference->get_name() << std::endl;
- std::cout << indent << " resolved text = " << nodeEntityReference->get_resolved_text() << std::endl;
- std::cout << indent << " original text = " << nodeEntityReference->get_original_text() << std::endl;
+ std::cout << indent << "entity reference name = " <<
CatchConvertError(nodeEntityReference->get_name()) << std::endl;
+ std::cout << indent << " resolved text = " <<
CatchConvertError(nodeEntityReference->get_resolved_text()) << std::endl;
+ std::cout << indent << " original text = " <<
CatchConvertError(nodeEntityReference->get_original_text()) << std::endl;
}
} // end if (substitute_entities)
@@ -67,8 +68,8 @@ void print_node(const xmlpp::Node* node, bool substitute_entities, unsigned int
int main(int argc, char* argv[])
{
- // Set the global C++ locale to the user-configured locale,
- // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+ // Set the global C++ locale to the user-specified locale. Then we can
+ // hopefully use std::cout with UTF-8, via Glib::ustring, without exceptions.
std::locale::global(std::locale(""));
std::string filepath;
diff --git a/examples/dom_parser/main.cc b/examples/dom_parser/main.cc
index 40cb9b6..81e32b1 100644
--- a/examples/dom_parser/main.cc
+++ b/examples/dom_parser/main.cc
@@ -23,9 +23,10 @@
#include <config.h>
#endif
+#include "../testutilities.h"
#include <libxml++/libxml++.h>
#include <iostream>
-#include <stdlib.h>
+#include <cstdlib>
void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
{
@@ -47,8 +48,8 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
std::cout << indent << "Node name = ";
if(!namespace_prefix.empty())
- std::cout << namespace_prefix << ":";
- std::cout << nodename << std::endl;
+ std::cout << CatchConvertError(namespace_prefix) << ":";
+ std::cout << CatchConvertError(nodename) << std::endl;
}
else if(nodeText) //Let's say when it's text. - e.g. let's say what that white space is.
{
@@ -58,15 +59,15 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
//Treat the various node types differently:
if(nodeText)
{
- std::cout << indent << "text = \"" << nodeText->get_content() << "\"" << std::endl;
+ std::cout << indent << "text = \"" << CatchConvertError(nodeText->get_content()) << "\"" << std::endl;
}
else if(nodeComment)
{
- std::cout << indent << "comment = " << nodeComment->get_content() << std::endl;
+ std::cout << indent << "comment = " << CatchConvertError(nodeComment->get_content()) << std::endl;
}
else if(nodeContent)
{
- std::cout << indent << "content = " << nodeContent->get_content() << std::endl;
+ std::cout << indent << "content = " << CatchConvertError(nodeContent->get_content()) << std::endl;
}
else if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node))
{
@@ -84,14 +85,15 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
std::cout << indent << " Attribute ";
if(!namespace_prefix.empty())
- std::cout << namespace_prefix << ":";
- std::cout << attribute->get_name() << " = " << attribute->get_value() << std::endl;
+ std::cout << CatchConvertError(namespace_prefix) << ":";
+ std::cout << CatchConvertError(attribute->get_name()) << " = "
+ << CatchConvertError(attribute->get_value()) << std::endl;
}
const xmlpp::Attribute* attribute = nodeElement->get_attribute("title");
if(attribute)
{
- std::cout << indent << "title = " << attribute->get_value() << std::endl;
+ std::cout << indent << "title = " << CatchConvertError(attribute->get_value()) << std::endl;
}
}
@@ -108,8 +110,8 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
int main(int argc, char* argv[])
{
- // Set the global C++ locale to the user-configured locale,
- // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+ // Set the global C++ locale to the user-specified locale. Then we can
+ // hopefully use std::cout with UTF-8, via Glib::ustring, without exceptions.
std::locale::global(std::locale(""));
bool validate = false;
diff --git a/examples/testutilities.cc b/examples/testutilities.cc
new file mode 100644
index 0000000..4509e82
--- /dev/null
+++ b/examples/testutilities.cc
@@ -0,0 +1,33 @@
+/* Copyright (C) 2013 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/>.
+ */
+
+#include "testutilities.h"
+#include <glibmm/convert.h>
+
+std::ostream& operator<<(std::ostream& os, const CatchConvertError& utf8_string)
+{
+ try
+ {
+ os << static_cast<const Glib::ustring&>(utf8_string);
+ }
+ catch (const Glib::ConvertError& ex)
+ {
+ os << "[Glib::ConvertError: " << ex.what() << "]";
+ }
+ return os;
+}
diff --git a/examples/testutilities.h b/examples/testutilities.h
new file mode 100644
index 0000000..31576d0
--- /dev/null
+++ b/examples/testutilities.h
@@ -0,0 +1,45 @@
+/* Copyright (C) 2013 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/>.
+ */
+
+#include <glibmm/ustring.h>
+#include <iostream>
+
+// CatchConvertError is identical to Glib::ustring, except that
+// std::ostream& operator<<(std::ostream&, const CatchConvertError&)
+// catches Glib::ConvertError, and prints the exception message.
+//
+// If a printed string contains characters that don't exist in the global
+// locale's character set, an exception is thrown, but it's caught locally,
+// and the program continues.
+// This is particularly useful when the example programs are run by 'make check'.
+// If the user-specified locale's character set does not contain all characters
+// in the Glib::ustring, operator<<(std::ostream&, const Glib::ustring&) throws
+// a Glib::ConvertError exception. That exception is not the result of an error
+// in libxml++ or libxml2.
+// See also https://bugzilla.gnome.org/show_bug.cgi?id=702136
+
+class CatchConvertError : public Glib::ustring
+{
+public:
+ explicit CatchConvertError(const Glib::ustring& str)
+ : Glib::ustring(str)
+ { }
+};
+
+std::ostream& operator<<(std::ostream& os, const CatchConvertError& utf8_string);
+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]