1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
10 #ifndef BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_UTILS_HPP_INCLUDED
11 #define BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_UTILS_HPP_INCLUDED
13 #include <boost/property_tree/detail/ptree_utils.hpp>
14 #include <boost/property_tree/detail/xml_parser_error.hpp>
15 #include <boost/property_tree/detail/xml_parser_writer_settings.hpp>
20 namespace boost
{ namespace property_tree
{ namespace xml_parser
24 std::basic_string
<Ch
> condense(const std::basic_string
<Ch
> &s
)
26 std::basic_string
<Ch
> r
;
29 typename
std::basic_string
<Ch
>::const_iterator end
= s
.end();
30 for (typename
std::basic_string
<Ch
>::const_iterator it
= s
.begin();
33 if (isspace(*it
, loc
) || *it
== Ch('\n'))
36 r
+= Ch(' '), space
= true;
39 r
+= *it
, space
= false;
45 std::basic_string
<Ch
> encode_char_entities(const std::basic_string
<Ch
> &s
)
47 typedef typename
std::basic_string
<Ch
> Str
;
49 typename
Str::const_iterator end
= s
.end();
50 for (typename
Str::const_iterator it
= s
.begin(); it
!= end
; ++it
)
54 case Ch('<'): r
+= detail::widen
<Ch
>("<"); break;
55 case Ch('>'): r
+= detail::widen
<Ch
>(">"); break;
56 case Ch('&'): r
+= detail::widen
<Ch
>("&"); break;
57 case Ch('"'): r
+= detail::widen
<Ch
>("""); break;
58 case Ch('\''): r
+= detail::widen
<Ch
>("'"); break;
59 default: r
+= *it
; break;
66 std::basic_string
<Ch
> decode_char_entities(const std::basic_string
<Ch
> &s
)
68 typedef typename
std::basic_string
<Ch
> Str
;
70 typename
Str::const_iterator end
= s
.end();
71 for (typename
Str::const_iterator it
= s
.begin(); it
!= end
; ++it
)
75 typename
Str::const_iterator semicolon
= std::find(it
+ 1, end
, Ch(';'));
77 BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
78 Str
ent(it
+ 1, semicolon
);
79 if (ent
== detail::widen
<Ch
>("lt")) r
+= Ch('<');
80 else if (ent
== detail::widen
<Ch
>("gt")) r
+= Ch('>');
81 else if (ent
== detail::widen
<Ch
>("amp")) r
+= Ch('&');
82 else if (ent
== detail::widen
<Ch
>("quot")) r
+= Ch('"');
83 else if (ent
== detail::widen
<Ch
>("apos")) r
+= Ch('\'');
85 BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
95 const std::basic_string
<Ch
> &xmldecl()
97 static std::basic_string
<Ch
> s
= detail::widen
<Ch
>("<?xml>");
102 const std::basic_string
<Ch
> &xmlattr()
104 static std::basic_string
<Ch
> s
= detail::widen
<Ch
>("<xmlattr>");
109 const std::basic_string
<Ch
> &xmlcomment()
111 static std::basic_string
<Ch
> s
= detail::widen
<Ch
>("<xmlcomment>");
116 const std::basic_string
<Ch
> &xmltext()
118 static std::basic_string
<Ch
> s
= detail::widen
<Ch
>("<xmltext>");