File indexing completed on 2025-01-30 09:58:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_UTILS_HPP_INCLUDED
0011 #define BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_UTILS_HPP_INCLUDED
0012
0013 #include <boost/property_tree/detail/ptree_utils.hpp>
0014 #include <boost/property_tree/detail/xml_parser_error.hpp>
0015 #include <boost/property_tree/detail/xml_parser_writer_settings.hpp>
0016 #include <string>
0017 #include <algorithm>
0018 #include <locale>
0019
0020 namespace boost { namespace property_tree { namespace xml_parser
0021 {
0022
0023 template<class Str>
0024 Str condense(const Str &s)
0025 {
0026 typedef typename Str::value_type Ch;
0027 Str r;
0028 std::locale loc;
0029 bool space = false;
0030 typename Str::const_iterator end = s.end();
0031 for (typename Str::const_iterator it = s.begin();
0032 it != end; ++it)
0033 {
0034 if (isspace(*it, loc) || *it == Ch('\n'))
0035 {
0036 if (!space)
0037 r += Ch(' '), space = true;
0038 }
0039 else
0040 r += *it, space = false;
0041 }
0042 return r;
0043 }
0044
0045
0046 template<class Str>
0047 Str encode_char_entities(const Str &s)
0048 {
0049
0050 if(s.empty()) return s;
0051
0052 typedef typename Str::value_type Ch;
0053
0054 Str r;
0055
0056
0057 Str sp(1, Ch(' '));
0058 if(s.find_first_not_of(sp) == Str::npos) {
0059
0060 r = detail::widen<Str>(" ");
0061 r += Str(s.size() - 1, Ch(' '));
0062 } else {
0063 typename Str::const_iterator end = s.end();
0064 for (typename Str::const_iterator it = s.begin(); it != end; ++it)
0065 {
0066 switch (*it)
0067 {
0068 case Ch('<'): r += detail::widen<Str>("<"); break;
0069 case Ch('>'): r += detail::widen<Str>(">"); break;
0070 case Ch('&'): r += detail::widen<Str>("&"); break;
0071 case Ch('"'): r += detail::widen<Str>("""); break;
0072 case Ch('\''): r += detail::widen<Str>("'"); break;
0073 default: r += *it; break;
0074 }
0075 }
0076 }
0077 return r;
0078 }
0079
0080 template<class Str>
0081 Str decode_char_entities(const Str &s)
0082 {
0083 typedef typename Str::value_type Ch;
0084 Str r;
0085 typename Str::const_iterator end = s.end();
0086 for (typename Str::const_iterator it = s.begin(); it != end; ++it)
0087 {
0088 if (*it == Ch('&'))
0089 {
0090 typename Str::const_iterator semicolon = std::find(it + 1, end, Ch(';'));
0091 if (semicolon == end)
0092 BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
0093 Str ent(it + 1, semicolon);
0094 if (ent == detail::widen<Str>("lt")) r += Ch('<');
0095 else if (ent == detail::widen<Str>("gt")) r += Ch('>');
0096 else if (ent == detail::widen<Str>("amp")) r += Ch('&');
0097 else if (ent == detail::widen<Str>("quot")) r += Ch('"');
0098 else if (ent == detail::widen<Str>("apos")) r += Ch('\'');
0099 else
0100 BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
0101 it = semicolon;
0102 }
0103 else
0104 r += *it;
0105 }
0106 return r;
0107 }
0108
0109 template<class Str>
0110 const Str &xmldecl()
0111 {
0112 static Str s = detail::widen<Str>("<?xml>");
0113 return s;
0114 }
0115
0116 template<class Str>
0117 const Str &xmlattr()
0118 {
0119 static Str s = detail::widen<Str>("<xmlattr>");
0120 return s;
0121 }
0122
0123 template<class Str>
0124 const Str &xmlcomment()
0125 {
0126 static Str s = detail::widen<Str>("<xmlcomment>");
0127 return s;
0128 }
0129
0130 template<class Str>
0131 const Str &xmltext()
0132 {
0133 static Str s = detail::widen<Str>("<xmltext>");
0134 return s;
0135 }
0136
0137 } } }
0138
0139 #endif