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 Str condense(const Str &s)
26 typedef typename Str::value_type Ch;
30 typename Str::const_iterator end = s.end();
31 for (typename Str::const_iterator it = s.begin();
34 if (isspace(*it, loc) || *it == Ch('\n'))
37 r += Ch(' '), space = true;
40 r += *it, space = false;
47 Str encode_char_entities(const Str &s)
49 // Don't do anything for empty strings.
50 if(s.empty()) return s;
52 typedef typename Str::value_type Ch;
55 // To properly round-trip spaces and not uglify the XML beyond
56 // recognition, we have to encode them IF the text contains only spaces.
58 if(s.find_first_not_of(sp) == Str::npos) {
59 // The first will suffice.
60 r = detail::widen<Str>(" ");
61 r += Str(s.size() - 1, Ch(' '));
63 typename Str::const_iterator end = s.end();
64 for (typename Str::const_iterator it = s.begin(); it != end; ++it)
68 case Ch('<'): r += detail::widen<Str>("<"); break;
69 case Ch('>'): r += detail::widen<Str>(">"); break;
70 case Ch('&'): r += detail::widen<Str>("&"); break;
71 case Ch('"'): r += detail::widen<Str>("""); break;
72 case Ch('\''): r += detail::widen<Str>("'"); break;
73 default: r += *it; break;
81 Str decode_char_entities(const Str &s)
83 typedef typename Str::value_type Ch;
85 typename Str::const_iterator end = s.end();
86 for (typename Str::const_iterator it = s.begin(); it != end; ++it)
90 typename Str::const_iterator semicolon = std::find(it + 1, end, Ch(';'));
92 BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
93 Str ent(it + 1, semicolon);
94 if (ent == detail::widen<Str>("lt")) r += Ch('<');
95 else if (ent == detail::widen<Str>("gt")) r += Ch('>');
96 else if (ent == detail::widen<Str>("amp")) r += Ch('&');
97 else if (ent == detail::widen<Str>("quot")) r += Ch('"');
98 else if (ent == detail::widen<Str>("apos")) r += Ch('\'');
100 BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
112 static Str s = detail::widen<Str>("<?xml>");
119 static Str s = detail::widen<Str>("<xmlattr>");
124 const Str &xmlcomment()
126 static Str s = detail::widen<Str>("<xmlcomment>");
133 static Str s = detail::widen<Str>("<xmltext>");