Chris@16
|
1 // ----------------------------------------------------------------------------
|
Chris@16
|
2 // Copyright (C) 2002-2006 Marcin Kalicinski
|
Chris@16
|
3 //
|
Chris@16
|
4 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
5 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 //
|
Chris@16
|
8 // For more information, see www.boost.org
|
Chris@16
|
9 // ----------------------------------------------------------------------------
|
Chris@16
|
10 #ifndef BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_UTILS_HPP_INCLUDED
|
Chris@16
|
11 #define BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_UTILS_HPP_INCLUDED
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/property_tree/detail/ptree_utils.hpp>
|
Chris@16
|
14 #include <boost/property_tree/detail/xml_parser_error.hpp>
|
Chris@16
|
15 #include <boost/property_tree/detail/xml_parser_writer_settings.hpp>
|
Chris@16
|
16 #include <string>
|
Chris@16
|
17 #include <algorithm>
|
Chris@16
|
18 #include <locale>
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost { namespace property_tree { namespace xml_parser
|
Chris@16
|
21 {
|
Chris@16
|
22
|
Chris@101
|
23 template<class Str>
|
Chris@101
|
24 Str condense(const Str &s)
|
Chris@16
|
25 {
|
Chris@101
|
26 typedef typename Str::value_type Ch;
|
Chris@101
|
27 Str r;
|
Chris@16
|
28 std::locale loc;
|
Chris@16
|
29 bool space = false;
|
Chris@101
|
30 typename Str::const_iterator end = s.end();
|
Chris@101
|
31 for (typename Str::const_iterator it = s.begin();
|
Chris@16
|
32 it != end; ++it)
|
Chris@16
|
33 {
|
Chris@16
|
34 if (isspace(*it, loc) || *it == Ch('\n'))
|
Chris@16
|
35 {
|
Chris@16
|
36 if (!space)
|
Chris@16
|
37 r += Ch(' '), space = true;
|
Chris@16
|
38 }
|
Chris@16
|
39 else
|
Chris@16
|
40 r += *it, space = false;
|
Chris@16
|
41 }
|
Chris@16
|
42 return r;
|
Chris@16
|
43 }
|
Chris@16
|
44
|
Chris@101
|
45
|
Chris@101
|
46 template<class Str>
|
Chris@101
|
47 Str encode_char_entities(const Str &s)
|
Chris@16
|
48 {
|
Chris@16
|
49 // Don't do anything for empty strings.
|
Chris@16
|
50 if(s.empty()) return s;
|
Chris@16
|
51
|
Chris@101
|
52 typedef typename Str::value_type Ch;
|
Chris@101
|
53
|
Chris@16
|
54 Str r;
|
Chris@16
|
55 // To properly round-trip spaces and not uglify the XML beyond
|
Chris@16
|
56 // recognition, we have to encode them IF the text contains only spaces.
|
Chris@16
|
57 Str sp(1, Ch(' '));
|
Chris@16
|
58 if(s.find_first_not_of(sp) == Str::npos) {
|
Chris@16
|
59 // The first will suffice.
|
Chris@101
|
60 r = detail::widen<Str>(" ");
|
Chris@16
|
61 r += Str(s.size() - 1, Ch(' '));
|
Chris@16
|
62 } else {
|
Chris@16
|
63 typename Str::const_iterator end = s.end();
|
Chris@16
|
64 for (typename Str::const_iterator it = s.begin(); it != end; ++it)
|
Chris@16
|
65 {
|
Chris@16
|
66 switch (*it)
|
Chris@16
|
67 {
|
Chris@101
|
68 case Ch('<'): r += detail::widen<Str>("<"); break;
|
Chris@101
|
69 case Ch('>'): r += detail::widen<Str>(">"); break;
|
Chris@101
|
70 case Ch('&'): r += detail::widen<Str>("&"); break;
|
Chris@101
|
71 case Ch('"'): r += detail::widen<Str>("""); break;
|
Chris@101
|
72 case Ch('\''): r += detail::widen<Str>("'"); break;
|
Chris@101
|
73 case Ch('\t'): r += detail::widen<Str>("	"); break;
|
Chris@101
|
74 case Ch('\n'): r += detail::widen<Str>(" "); break;
|
Chris@16
|
75 default: r += *it; break;
|
Chris@16
|
76 }
|
Chris@16
|
77 }
|
Chris@16
|
78 }
|
Chris@16
|
79 return r;
|
Chris@16
|
80 }
|
Chris@16
|
81
|
Chris@101
|
82 template<class Str>
|
Chris@101
|
83 Str decode_char_entities(const Str &s)
|
Chris@16
|
84 {
|
Chris@101
|
85 typedef typename Str::value_type Ch;
|
Chris@16
|
86 Str r;
|
Chris@16
|
87 typename Str::const_iterator end = s.end();
|
Chris@16
|
88 for (typename Str::const_iterator it = s.begin(); it != end; ++it)
|
Chris@16
|
89 {
|
Chris@16
|
90 if (*it == Ch('&'))
|
Chris@16
|
91 {
|
Chris@16
|
92 typename Str::const_iterator semicolon = std::find(it + 1, end, Ch(';'));
|
Chris@16
|
93 if (semicolon == end)
|
Chris@16
|
94 BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
|
Chris@16
|
95 Str ent(it + 1, semicolon);
|
Chris@101
|
96 if (ent == detail::widen<Str>("lt")) r += Ch('<');
|
Chris@101
|
97 else if (ent == detail::widen<Str>("gt")) r += Ch('>');
|
Chris@101
|
98 else if (ent == detail::widen<Str>("amp")) r += Ch('&');
|
Chris@101
|
99 else if (ent == detail::widen<Str>("quot")) r += Ch('"');
|
Chris@101
|
100 else if (ent == detail::widen<Str>("apos")) r += Ch('\'');
|
Chris@16
|
101 else
|
Chris@16
|
102 BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
|
Chris@16
|
103 it = semicolon;
|
Chris@16
|
104 }
|
Chris@16
|
105 else
|
Chris@16
|
106 r += *it;
|
Chris@16
|
107 }
|
Chris@16
|
108 return r;
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@101
|
111 template<class Str>
|
Chris@101
|
112 const Str &xmldecl()
|
Chris@16
|
113 {
|
Chris@101
|
114 static Str s = detail::widen<Str>("<?xml>");
|
Chris@16
|
115 return s;
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@101
|
118 template<class Str>
|
Chris@101
|
119 const Str &xmlattr()
|
Chris@16
|
120 {
|
Chris@101
|
121 static Str s = detail::widen<Str>("<xmlattr>");
|
Chris@16
|
122 return s;
|
Chris@16
|
123 }
|
Chris@16
|
124
|
Chris@101
|
125 template<class Str>
|
Chris@101
|
126 const Str &xmlcomment()
|
Chris@16
|
127 {
|
Chris@101
|
128 static Str s = detail::widen<Str>("<xmlcomment>");
|
Chris@16
|
129 return s;
|
Chris@16
|
130 }
|
Chris@16
|
131
|
Chris@101
|
132 template<class Str>
|
Chris@101
|
133 const Str &xmltext()
|
Chris@16
|
134 {
|
Chris@101
|
135 static Str s = detail::widen<Str>("<xmltext>");
|
Chris@16
|
136 return s;
|
Chris@16
|
137 }
|
Chris@16
|
138
|
Chris@16
|
139 } } }
|
Chris@16
|
140
|
Chris@16
|
141 #endif
|