3D Repo Bouncer  1.4
repo_property_tree.h
1 
18 #pragma once
19 
20 #include <boost/property_tree/ptree.hpp>
21 #include <boost/property_tree/xml_parser.hpp>
22 #include <boost/algorithm/string/replace.hpp>
23 //#include <boost/property_tree/json_parser.hpp>
24 #include "json_parser.h"
25 #include "../core/model/repo_node_utils.h"
26 
27 namespace repo{
28  namespace lib{
30  {
31  public:
32  PropertyTree();
33  PropertyTree(const bool &enableJSONWorkAround);
34  ~PropertyTree();
35 
43  const std::string &label,
44  const std::vector<PropertyTree> &value)
45  {
46  boost::property_tree::ptree arraytree;
47  for (size_t i = 0; i < value.size(); ++i)
48  {
49  arraytree.push_back(std::make_pair("", value[i].tree));
50  }
51 
52  tree.add_child(label, arraytree);
53  }
54 
66  template <typename T>
68  const std::string &label,
69  const std::string &attribute,
70  const T &value
71  )
72  {
73  addFieldAttribute(label, attribute, boost::lexical_cast<std::string>(value));
74  }
75 
87  template <typename T>
89  const std::string &label,
90  const std::string &attribute,
91  const std::vector<T> &value,
92  const bool &useDelimiter = true
93  )
94  {
95  std::stringstream ss;
96  for (uint32_t i = 0; i < value.size(); ++i)
97  {
98  ss << value[i];
99  if (useDelimiter && i != value.size() - 1)
100  {
101  ss << ",";
102  }
103  else
104  ss << " ";
105  }
106  std::string val = ss.str();
107  addFieldAttribute(label, attribute, val);
108  }
109 
118  template <typename T>
119  void addToTree(
120  const std::string &label,
121  const T &value)
122  {
123  if (label.empty())
124  tree.put(label, value);
125  else
126  tree.add(label, value);
127  }
128 
129  template <std::size_t N>
130  void addToTree(
131  const std::string &label,
132  const char(&value)[N])
133  {
134  addToTree(label, std::string(value));
135  }
136 
144  template <typename T>
145  void addToTree(
146  const std::string &label,
147  const std::vector<T> &value,
148  const bool &join = true)
149  {
150  if (join)
151  {
152  boost::property_tree::ptree arrayTree;
153  for (const auto &child : value)
154  {
155  PropertyTree childTree;
156  childTree.addToTree("", child);
157  arrayTree.push_back(std::make_pair("", childTree.tree));
158  }
159 
160  tree.add_child(label, arrayTree);
161  }
162  else
163  {
164  std::stringstream ss;
165  for (size_t i = 0; i < value.size(); ++i)
166  {
167  ss << boost::lexical_cast<std::string>(value[i]);
168  if (i != value.size() - 1)
169  ss << " ";
170  }
171 
172  std::string valueInStr = ss.str();
173  addToTree(label, valueInStr);
174  }
175  }
176 
187  {
188  hackStrings = false;
189  }
190 
197  const std::string &label,
198  const PropertyTree &subTree)
199  {
200  tree.add_child(label, subTree.tree);
201  }
202 
208  std::iostream &stream
209  ) const
210  {
211  boost::property_tree::write_json(stream, tree, false);
212  }
213 
218  void write_xml(
219  std::iostream &stream
220  ) const
221  {
222  std::stringstream ss;
223  boost::property_tree::write_xml(ss, tree);
224  std::string stringxml = ss.str();
225  //revert the xml_parser's utility where it swaps tabs and newlines with codes
226  boost::replace_all(stringxml, "&#9;", "\t");
227  boost::replace_all(stringxml, "&#10;", "\n");
228  stream << stringxml;
229  }
230 
231  private:
232  bool hackStrings;
233  boost::property_tree::ptree tree;
234 
241  std::string sanitizeStr(const std::string &value);
242 
249  int findSpecialWords(
250  const std::string &value,
251  const int &start);
252 
253  int findBackSlash(
254  const std::string &value,
255  const int &start);
256  };
257  // Template specialization
258  template <>
259  void PropertyTree::addToTree<repo_vector_t>(
260  const std::string &label,
261  const repo_vector_t &value);
262 
263  template <>
264  void PropertyTree::addToTree<std::string>(
265  const std::string &label,
266  const std::string &value);
267 
268  template <>
269  void PropertyTree::addToTree<repoUUID>(
270  const std::string &label,
271  const repoUUID &value);
272 
273  template <>
275  const std::string &label,
276  const std::vector<PropertyTree> &value,
277  const bool &join);
278 
279  template <>
281  const std::string &label,
282  const std::string &attribute,
283  const std::string &value
284  );
285 
286  template <>
288  const std::string &label,
289  const std::string &attribute,
290  const repo_vector_t &value
291  );
292  }
293 }
void addFieldAttribute(const std::string &label, const std::string &attribute, const T &value)
Definition: repo_property_tree.h:67
void addToTree(const std::string &label, const std::vector< T > &value, const bool &join=true)
Definition: repo_property_tree.h:145
void write_json(std::iostream &stream) const
Definition: repo_property_tree.h:207
Definition: repo_connection_pool_mongo.h:32
Definition: repo_property_tree.h:29
Definition: repo_node_utils.h:67
void mergeSubTree(const std::string &label, const PropertyTree &subTree)
Definition: repo_property_tree.h:196
void write_xml(std::iostream &stream) const
Definition: repo_property_tree.h:218
void addArrayObjects(const std::string &label, const std::vector< PropertyTree > &value)
Definition: repo_property_tree.h:42
void addToTree(const std::string &label, const T &value)
Definition: repo_property_tree.h:119
void addFieldAttribute(const std::string &label, const std::string &attribute, const std::vector< T > &value, const bool &useDelimiter=true)
Definition: repo_property_tree.h:88
void disableJSONWorkaround()
Definition: repo_property_tree.h:186