A library for working with phylogenetic and population genetic data.
v0.27.0
xml/document.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_FORMATS_XML_DOCUMENT_H_
2 #define GENESIS_UTILS_FORMATS_XML_DOCUMENT_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <memory>
35 #include <string>
36 #include <unordered_map>
37 #include <vector>
38 
40 
41 namespace genesis {
42 namespace utils {
43 
44 // =================================================================================================
45 // Xml Value
46 // =================================================================================================
47 
51 class XmlValue
52 {
53 public:
54  enum Type {
58  };
59 
60  inline static std::string type_to_string (const Type t)
61  {
62  switch (t) {
63  case kComment : return "Comment";
64  case kMarkup : return "Markup";
65  case kElement : return "Element";
66  default : return "Unknown";
67  }
68  }
69 
70  inline std::string type_to_string () const
71  {
72  return type_to_string(type_);
73  }
74 
75  inline Type type() const
76  {
77  return type_;
78  }
79 
80  inline bool is_comment()
81  {
82  return type_ == kComment;
83  }
84 
85  inline bool is_markup()
86  {
87  return type_ == kMarkup;
88  }
89 
90  inline bool is_element()
91  {
92  return type_ == kElement;
93  }
94 
95  virtual ~XmlValue() {}
96 
97 protected:
104  XmlValue (const Type type) : type_(type) {}
105 
107  const Type type_;
108 };
109 
110 // =================================================================================================
111 // Xml Comment
112 // =================================================================================================
113 
117 class XmlComment : public XmlValue
118 {
119 public:
121  XmlComment(const std::string& comment) : XmlValue(kComment), content(comment) {}
122 
123  std::string content;
124 };
125 
126 // =================================================================================================
127 // Xml Markup
128 // =================================================================================================
129 
133 class XmlMarkup : public XmlValue
134 {
135 public:
137  XmlMarkup(const std::string& content) : XmlValue(kMarkup), content(content) {}
138 
139  std::string content;
140 };
141 
142 // =================================================================================================
143 // Xml Element
144 // =================================================================================================
145 
149 class XmlElement : public XmlValue
150 {
151 public:
152  typedef std::unordered_map<std::string, std::string> StringMapType;
153 
154  // -------------------------------------------------------------
155  // Constructors and Rule of Five
156  // -------------------------------------------------------------
157 
159  XmlElement(const std::string& tag) : XmlValue(kElement), tag(tag) {}
160 
161  XmlElement( XmlElement const& ) = delete;
162  XmlElement( XmlElement&& ) = delete;
163 
164  XmlElement& operator= ( XmlElement const& ) = delete;
165  XmlElement& operator= ( XmlElement&& ) = delete;
166 
167  virtual ~XmlElement() override
168  {
169  clear();
170  }
171 
172  // -------------------------------------------------------------
173  // Content Functions
174  // -------------------------------------------------------------
175 
176  inline void clear()
177  {
178  tag = "";
179  attributes.clear();
180  content.clear();
181  }
182 
183  inline XmlComment* append_comment (const std::string& comment)
184  {
185  auto elm = utils::make_unique<XmlComment>(comment);
186  auto ptr = elm.get();
187  content.push_back(std::move(elm));
188  return ptr;
189  }
190 
191  inline XmlMarkup* append_markup (const std::string& text)
192  {
193  auto elm = utils::make_unique<XmlMarkup>(text);
194  auto ptr = elm.get();
195  content.push_back(std::move(elm));
196  return ptr;
197  }
198 
199  inline XmlElement* append_element (const std::string& tag_value)
200  {
201  auto elm = utils::make_unique<XmlElement>(tag_value);
202  auto ptr = elm.get();
203  content.push_back(std::move(elm));
204  return ptr;
205  }
206 
207  std::string tag;
209  std::vector<std::unique_ptr<XmlValue>> content;
210 };
211 
212 // =================================================================================================
213 // Xml Document
214 // =================================================================================================
215 
219 class XmlDocument : public XmlElement
220 {
221 public:
222  virtual ~XmlDocument() override
223  {
224  clear();
225  }
226 
227  inline void clear()
228  {
230  xml_tag = "";
231  declarations.clear();
232  }
233 
234  std::string xml_tag;
236 };
237 
238 // =================================================================================================
239 // Converter Functions
240 // =================================================================================================
241 
242 const XmlComment* xml_value_to_comment (const XmlValue* v);
243 const XmlMarkup* xml_value_to_markup (const XmlValue* v);
244 const XmlElement* xml_value_to_element (const XmlValue* v);
245 
246 } // namespace utils
247 } // namespace genesis
248 
249 #endif // include guard
genesis::utils::XmlValue::type
Type type() const
Definition: xml/document.hpp:75
genesis::utils::XmlValue::type_
const Type type_
Stores the type of an object. Set by the constructor.
Definition: xml/document.hpp:107
genesis::utils::XmlValue::kComment
@ kComment
Definition: xml/document.hpp:55
genesis::utils::XmlValue
Definition: xml/document.hpp:51
genesis::utils::XmlElement::operator=
XmlElement & operator=(XmlElement const &)=delete
genesis::utils::xml_value_to_element
const XmlElement * xml_value_to_element(const XmlValue *v)
Converts a pointer to an XmlValue to XmlElement if appropriate.
Definition: xml/document.cpp:77
genesis::utils::XmlDocument::declarations
StringMapType declarations
Definition: xml/document.hpp:235
genesis::utils::XmlElement
Definition: xml/document.hpp:149
genesis::utils::XmlDocument::xml_tag
std::string xml_tag
Definition: xml/document.hpp:234
genesis::utils::xml_value_to_comment
const XmlComment * xml_value_to_comment(const XmlValue *v)
Converts a pointer to an XmlValue to XmlComment if appropriate.
Definition: xml/document.cpp:47
genesis::utils::XmlComment
Definition: xml/document.hpp:117
genesis::utils::XmlValue::kElement
@ kElement
Definition: xml/document.hpp:57
std.hpp
Provides some valuable additions to STD.
genesis::utils::XmlMarkup::XmlMarkup
XmlMarkup()
Definition: xml/document.hpp:136
genesis::utils::XmlElement::XmlElement
XmlElement()
Definition: xml/document.hpp:158
genesis::utils::XmlDocument::~XmlDocument
virtual ~XmlDocument() override
Definition: xml/document.hpp:222
genesis::utils::XmlValue::is_comment
bool is_comment()
Definition: xml/document.hpp:80
genesis::utils::XmlComment::content
std::string content
Definition: xml/document.hpp:123
genesis::utils::XmlMarkup
Definition: xml/document.hpp:133
genesis::utils::XmlValue::XmlValue
XmlValue(const Type type)
Protected constructor that allows derived classes to set their value type.
Definition: xml/document.hpp:104
genesis::utils::XmlElement::content
std::vector< std::unique_ptr< XmlValue > > content
Definition: xml/document.hpp:209
genesis::utils::XmlMarkup::XmlMarkup
XmlMarkup(const std::string &content)
Definition: xml/document.hpp:137
genesis::utils::XmlElement::clear
void clear()
Definition: xml/document.hpp:176
genesis::utils::XmlValue::is_element
bool is_element()
Definition: xml/document.hpp:90
genesis::utils::XmlValue::type_to_string
static std::string type_to_string(const Type t)
Definition: xml/document.hpp:60
genesis::utils::XmlValue::Type
Type
Definition: xml/document.hpp:54
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::XmlElement::append_element
XmlElement * append_element(const std::string &tag_value)
Definition: xml/document.hpp:199
genesis::utils::XmlElement::append_comment
XmlComment * append_comment(const std::string &comment)
Definition: xml/document.hpp:183
genesis::utils::XmlComment::XmlComment
XmlComment()
Definition: xml/document.hpp:120
genesis::utils::XmlValue::is_markup
bool is_markup()
Definition: xml/document.hpp:85
genesis::utils::XmlElement::attributes
StringMapType attributes
Definition: xml/document.hpp:208
genesis::utils::XmlElement::StringMapType
std::unordered_map< std::string, std::string > StringMapType
Definition: xml/document.hpp:152
genesis::utils::XmlComment::XmlComment
XmlComment(const std::string &comment)
Definition: xml/document.hpp:121
genesis::utils::XmlElement::~XmlElement
virtual ~XmlElement() override
Definition: xml/document.hpp:167
genesis::utils::xml_value_to_markup
const XmlMarkup * xml_value_to_markup(const XmlValue *v)
Converts a pointer to an XmlValue to XmlMarkup if appropriate.
Definition: xml/document.cpp:62
genesis::utils::XmlElement::append_markup
XmlMarkup * append_markup(const std::string &text)
Definition: xml/document.hpp:191
genesis::utils::XmlValue::type_to_string
std::string type_to_string() const
Definition: xml/document.hpp:70
genesis::utils::XmlDocument::clear
void clear()
Definition: xml/document.hpp:227
genesis::utils::XmlValue::~XmlValue
virtual ~XmlValue()
Definition: xml/document.hpp:95
genesis::utils::XmlElement::tag
std::string tag
Definition: xml/document.hpp:207
genesis::utils::XmlElement::XmlElement
XmlElement(const std::string &tag)
Definition: xml/document.hpp:159
genesis::utils::XmlDocument
Definition: xml/document.hpp:219
genesis::utils::XmlMarkup::content
std::string content
Definition: xml/document.hpp:139
genesis::utils::XmlValue::kMarkup
@ kMarkup
Definition: xml/document.hpp:56