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