Leosac  0.8.0
Open Source Access Control
XmlPropertyTree.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "XmlPropertyTree.hpp"
22 #include "log.hpp"
23 #include <boost/archive/text_iarchive.hpp>
24 #include <boost/property_tree/ptree_serialization.hpp>
25 #include <boost/property_tree/xml_parser.hpp>
26 #include <boost/serialization/string.hpp>
27 #include <boost/version.hpp>
28 
29 using boost::property_tree::xml_parser::read_xml;
30 using boost::property_tree::xml_parser::write_xml;
31 using boost::property_tree::xml_writer_settings;
32 using boost::property_tree::ptree_error;
33 using boost::property_tree::xml_parser::no_concat_text;
34 using boost::property_tree::xml_parser::no_comments;
35 using boost::property_tree::xml_parser::trim_whitespace;
36 using boost::property_tree::ptree;
37 
38 boost::property_tree::ptree
40 {
41  boost::property_tree::ptree cfg;
42  std::string filename(path);
43  std::ifstream cfg_stream(filename);
44 
45  if (!cfg_stream.good())
46  throw(ConfigException(filename, "Could not open file {" + path + "}"));
47  try
48  {
49  read_xml(cfg_stream, cfg, trim_whitespace | no_comments);
50  return cfg;
51  }
52  catch (ptree_error &e)
53  {
54  std::throw_with_nested(Ex::Config(filename));
55  }
56 }
57 
58 void Leosac::Tools::propertyTreeToXmlFile(const boost::property_tree::ptree &tree,
59  const std::string &path)
60 {
61  std::string filename(path);
62  std::ofstream cfg_stream(filename);
63 
64  cfg_stream << propertyTreeToXml(tree);
65 }
66 
67 std::string Leosac::Tools::propertyTreeToXml(const boost::property_tree::ptree &tree)
68 {
69  std::stringstream ss;
70  try
71  {
72 #if ((BOOST_VERSION / 100000) == 1 && (BOOST_VERSION / 100 % 1000) >= 58) || \
73  (BOOST_VERSION / 100000) > 1
74  write_xml(
75  ss, tree,
76  boost::property_tree::xml_writer_make_settings<std::string>('\t', 1));
77 #else
78  write_xml(ss, tree,
79  boost::property_tree::xml_writer_make_settings<char>('\t', 1));
80 #endif
81  }
82  catch (ptree_error &e)
83  {
84  std::throw_with_nested(std::runtime_error("Failed to serialize ptree"));
85  }
86  return ss.str();
87 }
88 
90  const std::string &data, boost::property_tree::ptree &tree) noexcept
91 {
92  boost::property_tree::ptree cfg;
93  std::istringstream iss(data);
94 
95  try
96  {
97  boost::archive::text_iarchive archive(iss);
98  boost::property_tree::load(archive, cfg, 1);
99  std::swap(cfg, tree);
100  return true;
101  }
102  catch (std::exception &e)
103  {
104  ERROR(
105  "Exception while extracting boost archive (as string) to property tree: "
106  << e.what());
107  return false;
108  }
109 }
ERROR
@ ERROR
Definition: log.hpp:32
Leosac::Tools::propertyTreeFromXmlFile
boost::property_tree::ptree propertyTreeFromXmlFile(const std::string &path)
Build a property tree from a xml file.
Definition: XmlPropertyTree.cpp:39
Leosac::Tools::propertyTreeToXmlFile
void propertyTreeToXmlFile(const boost::property_tree::ptree &tree, const std::string &path)
Write a property tree to an xml file.
Definition: XmlPropertyTree.cpp:58
Leosac::Tools::boost_text_archive_to_ptree
bool boost_text_archive_to_ptree(const std::string &data, boost::property_tree::ptree &tree) noexcept
Convert a boost text archive, whose content is represented as a string (data) to a property tree.
Definition: XmlPropertyTree.cpp:89
ConfigException
Definition: configexception.hpp:87
XmlPropertyTree.hpp
configexception.hpp
log.hpp
Leosac::Tools::propertyTreeToXml
std::string propertyTreeToXml(const boost::property_tree::ptree &tree)
Convert a property tree to an xml formatted string.
Definition: XmlPropertyTree.cpp:67
Leosac::Ex::Config
An exception related to configuration.
Definition: configexception.hpp:33