fix doc example typo
[boost.git] / boost / property_tree / detail / xml_parser_utils.hpp
blobab563de73781b7c511d03b370d152aa58d0e7098
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
3 //
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)
7 //
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>
16 #include <string>
17 #include <algorithm>
18 #include <locale>
20 namespace boost { namespace property_tree { namespace xml_parser
23 template<class Ch>
24 std::basic_string<Ch> condense(const std::basic_string<Ch> &s)
26 std::basic_string<Ch> r;
27 std::locale loc;
28 bool space = false;
29 typename std::basic_string<Ch>::const_iterator end = s.end();
30 for (typename std::basic_string<Ch>::const_iterator it = s.begin();
31 it != end; ++it)
33 if (isspace(*it, loc) || *it == Ch('\n'))
35 if (!space)
36 r += Ch(' '), space = true;
38 else
39 r += *it, space = false;
41 return r;
44 template<class Ch>
45 std::basic_string<Ch> encode_char_entities(const std::basic_string<Ch> &s)
47 typedef typename std::basic_string<Ch> Str;
48 Str r;
49 typename Str::const_iterator end = s.end();
50 for (typename Str::const_iterator it = s.begin(); it != end; ++it)
52 switch (*it)
54 case Ch('<'): r += detail::widen<Ch>("&lt;"); break;
55 case Ch('>'): r += detail::widen<Ch>("&gt;"); break;
56 case Ch('&'): r += detail::widen<Ch>("&amp;"); break;
57 case Ch('"'): r += detail::widen<Ch>("&quot;"); break;
58 case Ch('\''): r += detail::widen<Ch>("&apos;"); break;
59 default: r += *it; break;
62 return r;
65 template<class Ch>
66 std::basic_string<Ch> decode_char_entities(const std::basic_string<Ch> &s)
68 typedef typename std::basic_string<Ch> Str;
69 Str r;
70 typename Str::const_iterator end = s.end();
71 for (typename Str::const_iterator it = s.begin(); it != end; ++it)
73 if (*it == Ch('&'))
75 typename Str::const_iterator semicolon = std::find(it + 1, end, Ch(';'));
76 if (semicolon == end)
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('\'');
84 else
85 BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
86 it = semicolon;
88 else
89 r += *it;
91 return r;
94 template<class Ch>
95 const std::basic_string<Ch> &xmldecl()
97 static std::basic_string<Ch> s = detail::widen<Ch>("<?xml>");
98 return s;
101 template<class Ch>
102 const std::basic_string<Ch> &xmlattr()
104 static std::basic_string<Ch> s = detail::widen<Ch>("<xmlattr>");
105 return s;
108 template<class Ch>
109 const std::basic_string<Ch> &xmlcomment()
111 static std::basic_string<Ch> s = detail::widen<Ch>("<xmlcomment>");
112 return s;
115 template<class Ch>
116 const std::basic_string<Ch> &xmltext()
118 static std::basic_string<Ch> s = detail::widen<Ch>("<xmltext>");
119 return s;
122 } } }
124 #endif