Skip to content

Commit ade9636

Browse files
committed
Add more methods that return std::optional<xmlpp::ustring>
* libxml++/attribute.[cc|h]: * libxml++/attributedeclaration.[cc|h]: * libxml++/attributenode.[cc|h]: Add get_value2(). * libxml++/document.[cc|h]: Add get_encoding2() * libxml++/dtd.[cc|h]: Add get_external_id2(), get_name2(), get_system_id2(). * libxml++/nodes/contentnode.[cc|h]: Add get_content2(). * libxml++/nodes/element.[cc|h]: Add get_attribute_value2(). * libxml++/nodes/entitydeclaration.[cc|h]: * libxml++/nodes/entityreference.[cc|h]: Add get_original_text2(), get_resolved_text2(). * libxml++/nodes/node.[cc|h]: Add get_name2(), get_namespace_prefix2(), get_namespace_uri2(), get_path2(). Deprecate the corresponding methods that return xmlpp::ustring. See #71 * examples/dom_build/main.cc: * examples/dom_parse_entities/main.cc: * examples/dom_parser/main.cc: * examples/dom_parser_raw/main.cc: * examples/dom_xinclude/main.cc: * examples/dom_xpath/main.cc: * examples/sax_parser_build_dom/main.cc: * tests/istream_ioparser/main.cc: Use the new methods instead of the deprecated ones.
1 parent 6cdfd91 commit ade9636

File tree

28 files changed

+416
-87
lines changed

28 files changed

+416
-87
lines changed

examples/dom_build/main.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,13 @@
1717
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1818
*/
1919

20-
#ifdef HAVE_CONFIG_H
21-
#include <config.h>
22-
#endif
23-
2420
#include <cstdlib>
2521
#include <iostream>
2622
#include <libxml++/libxml++.h>
2723

2824
int
2925
main(int /* argc */, char** /* argv */)
3026
{
31-
3227
try
3328
{
3429
xmlpp::Document document;
@@ -64,7 +59,7 @@ main(int /* argc */, char** /* argv */)
6459

6560
auto whole = document.write_to_string();
6661
std::cout << "XML built at runtime: " << std::endl << whole << std::endl;
67-
std::cout << "namespace of root node: " << nodeRoot->get_namespace_uri() << std::endl;
62+
std::cout << "namespace of root node: " << nodeRoot->get_namespace_uri2().value_or("{[(no URI)]}") << std::endl;
6863
}
6964
catch(const std::exception& ex)
7065
{

examples/dom_parse_entities/main.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1818
*/
1919

20-
#ifdef HAVE_CONFIG_H
21-
#include <config.h>
22-
#endif
23-
2420
#include <libxml++/libxml++.h>
2521
#include <iostream>
2622
#include <fstream>
2723
#include <cstdlib>
2824

25+
std::ostream& operator<<(std::ostream& o, const std::optional<xmlpp::ustring>& s)
26+
{
27+
o << s.value_or("{[(no value)]}");
28+
return o;
29+
}
30+
2931
void print_node(const xmlpp::Node* node, bool substitute_entities, unsigned int indentation = 0)
3032
{
3133
const std::string indent(indentation, ' ');
@@ -37,7 +39,7 @@ void print_node(const xmlpp::Node* node, bool substitute_entities, unsigned int
3739
const auto nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
3840
if (nodeText && !nodeText->is_white_space())
3941
{
40-
std::cout << indent << "text = " << nodeText->get_content() << std::endl;
42+
std::cout << indent << "text = " << nodeText->get_content2() << std::endl;
4143
}
4244
}
4345
else
@@ -46,9 +48,9 @@ void print_node(const xmlpp::Node* node, bool substitute_entities, unsigned int
4648
const auto nodeEntityReference = dynamic_cast<const xmlpp::EntityReference*>(node);
4749
if (nodeEntityReference)
4850
{
49-
std::cout << indent << "entity reference name = " << nodeEntityReference->get_name() << std::endl;
50-
std::cout << indent << " resolved text = " << nodeEntityReference->get_resolved_text() << std::endl;
51-
std::cout << indent << " original text = " << nodeEntityReference->get_original_text() << std::endl;
51+
std::cout << indent << "entity reference name = " << nodeEntityReference->get_name2() << std::endl;
52+
std::cout << indent << " resolved text = " << nodeEntityReference->get_resolved_text2() << std::endl;
53+
std::cout << indent << " original text = " << nodeEntityReference->get_original_text2() << std::endl;
5254
}
5355
} // end if (substitute_entities)
5456

examples/dom_parser/main.cc

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1818
*/
1919

20-
#ifdef HAVE_CONFIG_H
21-
#include <config.h>
22-
#endif
23-
2420
#include <libxml++/libxml++.h>
2521
#include <iostream>
2622
#include <cstdlib>
2723

24+
std::ostream& operator<<(std::ostream& o, const std::optional<xmlpp::ustring>& s)
25+
{
26+
o << s.value_or("{[(no value)]}");
27+
return o;
28+
}
29+
2830
void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
2931
{
3032
const std::string indent(indentation, ' ');
@@ -37,14 +39,14 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
3739
if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
3840
return;
3941

40-
const auto nodename = node->get_name();
42+
const auto nodename = node->get_name2();
4143

42-
if(!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text".
44+
if(!nodeText && !nodeComment && nodename) //Let's not say "name: text".
4345
{
44-
const auto namespace_prefix = node->get_namespace_prefix();
46+
const auto namespace_prefix = node->get_namespace_prefix2();
4547

4648
std::cout << indent << "Node name = ";
47-
if(!namespace_prefix.empty())
49+
if(namespace_prefix)
4850
std::cout << namespace_prefix << ":";
4951
std::cout << nodename << std::endl;
5052
}
@@ -56,15 +58,15 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
5658
//Treat the various node types differently:
5759
if(nodeText)
5860
{
59-
std::cout << indent << "text = \"" << nodeText->get_content() << "\"" << std::endl;
61+
std::cout << indent << "text = \"" << nodeText->get_content2() << "\"" << std::endl;
6062
}
6163
else if(nodeComment)
6264
{
63-
std::cout << indent << "comment = " << nodeComment->get_content() << std::endl;
65+
std::cout << indent << "comment = " << nodeComment->get_content2() << std::endl;
6466
}
6567
else if(nodeContent)
6668
{
67-
std::cout << indent << "content = " << nodeContent->get_content() << std::endl;
69+
std::cout << indent << "content = " << nodeContent->get_content2() << std::endl;
6870
}
6971
else if(auto nodeElement = dynamic_cast<const xmlpp::Element*>(node))
7072
{
@@ -76,13 +78,13 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
7678
//Print attributes:
7779
for (const auto& attribute : nodeElement->get_attributes())
7880
{
79-
const auto namespace_prefix = attribute->get_namespace_prefix();
81+
const auto namespace_prefix = attribute->get_namespace_prefix2();
8082

8183
std::cout << indent << " Attribute ";
82-
if(!namespace_prefix.empty())
84+
if(namespace_prefix)
8385
std::cout << namespace_prefix << ":";
84-
std::cout << attribute->get_name() << " = "
85-
<< attribute->get_value() << std::endl;
86+
std::cout << attribute->get_name2() << " = "
87+
<< attribute->get_value2() << std::endl;
8688
}
8789

8890
const auto attribute = nodeElement->get_attribute("title");
@@ -93,7 +95,7 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
9395
std::cout << "AttributeNode ";
9496
else if (dynamic_cast<const xmlpp::AttributeDeclaration*>(attribute))
9597
std::cout << "AttributeDeclaration ";
96-
std::cout << "title = " << attribute->get_value() << std::endl;
98+
std::cout << "title = " << attribute->get_value2() << std::endl;
9799
}
98100
}
99101

examples/dom_parser_raw/main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
2727
{
2828
std::cout << std::endl; //Separate nodes by an empty line.
2929

30-
std::cout << "Node name = " << node->get_name() << std::endl;
30+
std::cout << "Node name = " << node->get_name2().value_or("{[(no name)]}") << std::endl;
3131

3232
//Recurse through child nodes:
3333
for(const auto& child : node->get_children())

examples/dom_xinclude/main.cc

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
#ifdef HAVE_CONFIG_H
20-
#include <config.h>
21-
#endif
22-
2319
#include <cstdlib>
2420
#include <iostream>
2521
#include <libxml++/libxml++.h>
2622

23+
std::ostream& operator<<(std::ostream& o, const std::optional<xmlpp::ustring>& s)
24+
{
25+
o << s.value_or("{[(no value)]}");
26+
return o;
27+
}
28+
2729
void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
2830
{
2931
const std::string indent(indentation, ' ');
@@ -36,14 +38,14 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
3638
if (nodeText && nodeText->is_white_space())
3739
return;
3840

39-
const auto nodename = node->get_name();
41+
const auto nodename = node->get_name2();
4042

41-
if (!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text".
43+
if (!nodeText && !nodeComment && nodename) //Let's not say "name: text".
4244
{
43-
const auto namespace_prefix = node->get_namespace_prefix();
45+
const auto namespace_prefix = node->get_namespace_prefix2();
4446

4547
std::cout << indent << "Node name = ";
46-
if (!namespace_prefix.empty())
48+
if (namespace_prefix)
4749
std::cout << namespace_prefix << ":";
4850
std::cout << nodename << std::endl;
4951
}
@@ -55,15 +57,15 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
5557
//Treat the various node types differently:
5658
if (nodeText)
5759
{
58-
std::cout << indent << "text = \"" << nodeText->get_content() << "\"" << std::endl;
60+
std::cout << indent << "text = \"" << nodeText->get_content2() << "\"" << std::endl;
5961
}
6062
else if (nodeComment)
6163
{
62-
std::cout << indent << "comment = " << nodeComment->get_content() << std::endl;
64+
std::cout << indent << "comment = " << nodeComment->get_content2() << std::endl;
6365
}
6466
else if (nodeContent)
6567
{
66-
std::cout << indent << "content = " << nodeContent->get_content() << std::endl;
68+
std::cout << indent << "content = " << nodeContent->get_content2() << std::endl;
6769
}
6870
else if (auto nodeElement = dynamic_cast<const xmlpp::Element*>(node))
6971
{
@@ -73,18 +75,18 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
7375
//Print attributes:
7476
for (const auto& attribute : nodeElement->get_attributes())
7577
{
76-
const auto namespace_prefix = attribute->get_namespace_prefix();
78+
const auto namespace_prefix = attribute->get_namespace_prefix2();
7779

7880
std::cout << indent << " Attribute ";
79-
if (!namespace_prefix.empty())
81+
if (namespace_prefix)
8082
std::cout << namespace_prefix << ":";
81-
std::cout << attribute->get_name() << " = " << attribute->get_value() << std::endl;
83+
std::cout << attribute->get_name2() << " = " << attribute->get_value2() << std::endl;
8284
}
8385

8486
const auto attribute = nodeElement->get_attribute("title");
8587
if (attribute)
8688
{
87-
std::cout << indent << "title = " << attribute->get_value() << std::endl;
89+
std::cout << indent << "title = " << attribute->get_value2() << std::endl;
8890
}
8991
}
9092
else if (dynamic_cast<const xmlpp::XIncludeStart*>(node))

examples/dom_xpath/main.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1818
*/
1919

20-
#ifdef HAVE_CONFIG_H
21-
#include <config.h>
22-
#endif
23-
2420
#include <cstdlib>
2521
#include <iostream>
2622
#include <libxml++/libxml++.h>
2723

24+
std::ostream& operator<<(std::ostream& o, const std::optional<xmlpp::ustring>& s)
25+
{
26+
o << s.value_or("{[(no value)]}");
27+
return o;
28+
}
29+
2830
std::string result_type_to_ustring(xmlpp::XPathResultType result_type)
2931
{
3032
switch (result_type)
@@ -45,26 +47,26 @@ void print_nodeset(const xmlpp::Node::const_NodeSet& set)
4547
// Print the structural paths and the values:
4648
for(const auto& child : set)
4749
{
48-
std::cout << " " << child->get_path();
50+
std::cout << " " << child->get_path2();
4951

5052
auto attribute = dynamic_cast<const xmlpp::Attribute*>(child);
5153
if (attribute)
5254
std::cout << ", value=\"" << attribute->get_value() << "\"";
5355

5456
auto content_node = dynamic_cast<const xmlpp::ContentNode*>(child);
5557
if (content_node)
56-
std::cout << ", content=\"" << content_node->get_content() << "\"";
58+
std::cout << ", content=\"" << content_node->get_content2() << "\"";
5759

5860
auto entity_reference = dynamic_cast<const xmlpp::EntityReference*>(child);
5961
if (entity_reference)
60-
std::cout << ", text=\"" << entity_reference->get_original_text() << "\"";
62+
std::cout << ", text=\"" << entity_reference->get_original_text2() << "\"";
6163

6264
auto element = dynamic_cast<const xmlpp::Element*>(child);
6365
if (element)
6466
{
6567
auto text_node = element->get_first_child_text();
6668
if (text_node)
67-
std::cout << ", first_child_text=\"" << text_node->get_content() << "\"";
69+
std::cout << ", first_child_text=\"" << text_node->get_content2() << "\"";
6870
}
6971
std::cout << std::endl;
7072
}

examples/sax_parser_build_dom/main.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2020
*/
2121

22-
#ifdef HAVE_CONFIG_H
23-
#include <config.h>
24-
#endif
25-
2622
#include <cstdlib>
2723
#include <fstream>
2824
#include <iostream>
@@ -55,7 +51,7 @@ main(int argc, char* argv[])
5551

5652
// Use the custom DOM
5753
auto element = doc.get_root();
58-
std::cout << "root's name is \"" << element->get_name() << "\"" << std::endl;
54+
std::cout << "root's name is \"" << element->get_name2().value_or("{[(no name)]}") << "\"" << std::endl;
5955
auto nl = element->find("//path[@style != '']");
6056
if(!nl.empty())
6157
{

libxml++/attribute.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
#include "libxml++/attribute.h"
8+
#include "libxml++/attributedeclaration.h"
9+
#include "libxml++/attributenode.h"
810

911
#include <libxml/tree.h>
1012

@@ -20,4 +22,15 @@ Attribute::~Attribute()
2022
{
2123
}
2224

25+
std::optional<ustring> Attribute::get_value2() const
26+
{
27+
auto attr_decl = dynamic_cast<const AttributeDeclaration*>(this);
28+
if (attr_decl)
29+
return attr_decl->get_value2();
30+
auto attr_node = dynamic_cast<const AttributeNode*>(this);
31+
if (attr_node)
32+
return attr_node->get_value2();
33+
return {};
34+
}
35+
2336
} //namespace xmlpp

libxml++/attribute.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
#ifndef __LIBXMLPP_ATTRIBUTE_H
88
#define __LIBXMLPP_ATTRIBUTE_H
99

10-
1110
#include "libxml++/ustring.h"
12-
1311
#include <libxml++/nodes/node.h>
12+
#include <optional>
1413

14+
//TODO: When we can break API/ABI, remove get_value(), rename get_value2()
15+
// to get_value(), make it virtual. Do the same in AttributeDeclaration and
16+
// AttributeNmode.
1517
namespace xmlpp
1618
{
1719

@@ -26,8 +28,15 @@ class LIBXMLPP_API Attribute : public Node
2628

2729
/** Get the value of this attribute.
2830
* @returns The attribute's value.
31+
* @deprecated 5.6: Use get_value2() instead.
2932
*/
3033
virtual ustring get_value() const = 0;
34+
35+
/** Get the value of this attribute.
36+
* @returns The attribute's value, or no value if the attribute has no value.
37+
* @newin{5,6}
38+
*/
39+
std::optional<ustring> get_value2() const;
3140
};
3241

3342
} // namespace xmlpp

libxml++/attributedeclaration.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ AttributeDeclaration::~AttributeDeclaration()
2222

2323
ustring AttributeDeclaration::get_value() const
2424
{
25+
return cobj()->defaultValue ? (const char*)cobj()->defaultValue : "";
26+
}
27+
28+
std::optional<ustring> AttributeDeclaration::get_value2() const
29+
{
30+
if (!cobj()->defaultValue)
31+
return {};
2532
return (const char*)cobj()->defaultValue;
2633
}
2734

0 commit comments

Comments
 (0)