Chris@16
|
1 // ----------------------------------------------------------------------------
|
Chris@16
|
2 // Copyright (C) 2002-2006 Marcin Kalicinski
|
Chris@16
|
3 // Copyright (C) 2009 Sebastian Redl
|
Chris@16
|
4 //
|
Chris@16
|
5 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
6 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 //
|
Chris@16
|
9 // For more information, see www.boost.org
|
Chris@16
|
10 // ----------------------------------------------------------------------------
|
Chris@16
|
11 #ifndef BOOST_PROPERTY_TREE_XML_PARSER_HPP_INCLUDED
|
Chris@16
|
12 #define BOOST_PROPERTY_TREE_XML_PARSER_HPP_INCLUDED
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/property_tree/ptree.hpp>
|
Chris@16
|
15 #include <boost/property_tree/detail/xml_parser_write.hpp>
|
Chris@16
|
16 #include <boost/property_tree/detail/xml_parser_error.hpp>
|
Chris@16
|
17 #include <boost/property_tree/detail/xml_parser_writer_settings.hpp>
|
Chris@16
|
18 #include <boost/property_tree/detail/xml_parser_flags.hpp>
|
Chris@16
|
19 #include <boost/property_tree/detail/xml_parser_read_rapidxml.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 #include <fstream>
|
Chris@16
|
22 #include <string>
|
Chris@16
|
23 #include <locale>
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost { namespace property_tree { namespace xml_parser
|
Chris@16
|
26 {
|
Chris@16
|
27
|
Chris@16
|
28 /**
|
Chris@16
|
29 * Reads XML from an input stream and translates it to property tree.
|
Chris@16
|
30 * @note Clears existing contents of property tree. In case of error the
|
Chris@16
|
31 * property tree unmodified.
|
Chris@16
|
32 * @note XML attributes are placed under keys named @c \<xmlattr\>.
|
Chris@16
|
33 * @throw xml_parser_error In case of error deserializing the property tree.
|
Chris@16
|
34 * @param stream Stream from which to read in the property tree.
|
Chris@16
|
35 * @param[out] pt The property tree to populate.
|
Chris@16
|
36 * @param flags Flags controlling the behaviour of the parser.
|
Chris@16
|
37 * The following flags are supported:
|
Chris@16
|
38 * @li @c no_concat_text -- Prevents concatenation of text nodes into
|
Chris@16
|
39 * datastring of property tree. Puts them in
|
Chris@16
|
40 * separate @c \<xmltext\> strings instead.
|
Chris@16
|
41 * @li @c no_comments -- Skip XML comments.
|
Chris@16
|
42 * @li @c trim_whitespace -- Trim leading and trailing whitespace from text,
|
Chris@16
|
43 * and collapse sequences of whitespace.
|
Chris@16
|
44 */
|
Chris@16
|
45 template<class Ptree>
|
Chris@16
|
46 void read_xml(std::basic_istream<
|
Chris@16
|
47 typename Ptree::key_type::value_type
|
Chris@16
|
48 > &stream,
|
Chris@16
|
49 Ptree &pt,
|
Chris@16
|
50 int flags = 0)
|
Chris@16
|
51 {
|
Chris@16
|
52 read_xml_internal(stream, pt, flags, std::string());
|
Chris@16
|
53 }
|
Chris@16
|
54
|
Chris@16
|
55 /**
|
Chris@16
|
56 * Reads XML from a file using the given locale and translates it to
|
Chris@16
|
57 * property tree.
|
Chris@16
|
58 * @note Clears existing contents of property tree. In case of error the
|
Chris@16
|
59 * property tree unmodified.
|
Chris@16
|
60 * @note XML attributes are placed under keys named @c \<xmlattr\>.
|
Chris@16
|
61 * @throw xml_parser_error In case of error deserializing the property tree.
|
Chris@16
|
62 * @param filename The file from which to read in the property tree.
|
Chris@16
|
63 * @param[out] pt The property tree to populate.
|
Chris@16
|
64 * @param flags Flags controlling the bahviour of the parser.
|
Chris@16
|
65 * The following flags are supported:
|
Chris@16
|
66 * @li @c no_concat_text -- Prevents concatenation of text nodes into
|
Chris@16
|
67 * datastring of property tree. Puts them in
|
Chris@16
|
68 * separate @c \<xmltext\> strings instead.
|
Chris@16
|
69 * @li @c no_comments -- Skip XML comments.
|
Chris@16
|
70 * @param loc The locale to use when reading in the file contents.
|
Chris@16
|
71 */
|
Chris@16
|
72 template<class Ptree>
|
Chris@16
|
73 void read_xml(const std::string &filename,
|
Chris@16
|
74 Ptree &pt,
|
Chris@16
|
75 int flags = 0,
|
Chris@16
|
76 const std::locale &loc = std::locale())
|
Chris@16
|
77 {
|
Chris@16
|
78 BOOST_ASSERT(validate_flags(flags));
|
Chris@16
|
79 std::basic_ifstream<typename Ptree::key_type::value_type>
|
Chris@16
|
80 stream(filename.c_str());
|
Chris@16
|
81 if (!stream)
|
Chris@16
|
82 BOOST_PROPERTY_TREE_THROW(xml_parser_error(
|
Chris@16
|
83 "cannot open file", filename, 0));
|
Chris@16
|
84 stream.imbue(loc);
|
Chris@16
|
85 read_xml_internal(stream, pt, flags, filename);
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 /**
|
Chris@16
|
89 * Translates the property tree to XML and writes it the given output
|
Chris@16
|
90 * stream.
|
Chris@16
|
91 * @throw xml_parser_error In case of error translating the property tree to
|
Chris@16
|
92 * XML or writing to the output stream.
|
Chris@16
|
93 * @param stream The stream to which to write the XML representation of the
|
Chris@16
|
94 * property tree.
|
Chris@16
|
95 * @param pt The property tree to tranlsate to XML and output.
|
Chris@16
|
96 * @param settings The settings to use when writing out the property tree as
|
Chris@16
|
97 * XML.
|
Chris@16
|
98 */
|
Chris@16
|
99 template<class Ptree>
|
Chris@16
|
100 void write_xml(std::basic_ostream<
|
Chris@16
|
101 typename Ptree::key_type::value_type
|
Chris@16
|
102 > &stream,
|
Chris@16
|
103 const Ptree &pt,
|
Chris@16
|
104 const xml_writer_settings<
|
Chris@101
|
105 typename Ptree::key_type
|
Chris@16
|
106 > & settings = xml_writer_settings<
|
Chris@101
|
107 typename Ptree::key_type>() )
|
Chris@16
|
108 {
|
Chris@16
|
109 write_xml_internal(stream, pt, std::string(), settings);
|
Chris@16
|
110 }
|
Chris@16
|
111
|
Chris@16
|
112 /**
|
Chris@16
|
113 * Translates the property tree to XML and writes it the given file.
|
Chris@16
|
114 * @throw xml_parser_error In case of error translating the property tree to
|
Chris@16
|
115 * XML or writing to the output stream.
|
Chris@16
|
116 * @param filename The file to which to write the XML representation of the
|
Chris@16
|
117 * property tree.
|
Chris@16
|
118 * @param pt The property tree to tranlsate to XML and output.
|
Chris@16
|
119 * @param loc The locale to use when writing the output to file.
|
Chris@16
|
120 * @param settings The settings to use when writing out the property tree as
|
Chris@16
|
121 * XML.
|
Chris@16
|
122 */
|
Chris@16
|
123 template<class Ptree>
|
Chris@16
|
124 void write_xml(const std::string &filename,
|
Chris@16
|
125 const Ptree &pt,
|
Chris@16
|
126 const std::locale &loc = std::locale(),
|
Chris@16
|
127 const xml_writer_settings<
|
Chris@101
|
128 typename Ptree::key_type
|
Chris@101
|
129 > & settings = xml_writer_settings<typename Ptree::key_type>())
|
Chris@16
|
130 {
|
Chris@16
|
131 std::basic_ofstream<typename Ptree::key_type::value_type>
|
Chris@16
|
132 stream(filename.c_str());
|
Chris@16
|
133 if (!stream)
|
Chris@16
|
134 BOOST_PROPERTY_TREE_THROW(xml_parser_error(
|
Chris@16
|
135 "cannot open file", filename, 0));
|
Chris@16
|
136 stream.imbue(loc);
|
Chris@16
|
137 write_xml_internal(stream, pt, filename, settings);
|
Chris@16
|
138 }
|
Chris@16
|
139
|
Chris@16
|
140 } } }
|
Chris@16
|
141
|
Chris@16
|
142 namespace boost { namespace property_tree
|
Chris@16
|
143 {
|
Chris@16
|
144 using xml_parser::read_xml;
|
Chris@16
|
145 using xml_parser::write_xml;
|
Chris@16
|
146 using xml_parser::xml_parser_error;
|
Chris@16
|
147
|
Chris@16
|
148 using xml_parser::xml_writer_settings;
|
Chris@16
|
149 using xml_parser::xml_writer_make_settings;
|
Chris@16
|
150 } }
|
Chris@16
|
151
|
Chris@16
|
152 #endif
|