atomicrex  0.1
An advanced atomistic model building tool
XMLUtilities.h
1 //
3 // Copyright (C) 2017, Alexander Stukowski and Paul Erhart
4 //
5 // This file is part of atomicrex.
6 //
7 // Atomicrex is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Atomicrex is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //
21 
22 #pragma once
23 
24 #include "../../Atomicrex.h"
25 
26 #include <libxml/tree.h>
27 #include <libxml/parser.h>
28 #include <libxml/xinclude.h>
29 #include <libxml/xmlschemastypes.h>
30 #ifndef LIBXML_XINCLUDE_ENABLED
31  #error "Program requires libxml2 with XInclude support."
32 #endif
33 #ifndef LIBXML_SCHEMAS_ENABLED
34  #error "Program requires libxml2 with XSD Schema support."
35 #endif
36 
37 namespace atomicrex {
38 namespace XML {
39 
43 struct Context {
44  Context() {
45  ::xmlInitParser();
46  LIBXML_TEST_VERSION
47  }
48  ~Context() {
49  ::xmlCleanupParser();
50  }
51 };
52 
54 template<class T, class D>
55 static std::unique_ptr<T, D> make_scoped(T* const ptr, D deleter) {
56  return std::unique_ptr<T, D>(ptr, deleter);
57 }
58 
60 inline FPString toFPString(const xmlChar* s) { return (const char*)s; }
61 
62 class OElement; // Defined below.
63 
69 class Element
70 {
71 public:
72 
74  Element(xmlNodePtr e = nullptr) : _el(e) {}
75 
77  FPString textContent() const;
78 
81  int parseIntParameterElement(const char* tagName) const;
82 
85  int parseOptionalIntParameterElement(const char* tagName, int defaultValue) const;
86 
89  double parseFloatParameterElement(const char* tagName) const;
90 
93  double parseOptionalFloatParameterElement(const char* tagName, double defaultValue) const;
94 
97  FPString parseStringParameterElement(const char* tagName) const;
98 
101  FPString parseOptionalStringParameterElement(const char* tagName, const FPString& defaultValue = FPString()) const;
102 
105  FPString parsePathParameterElement(const char* tagName) const;
106 
109  FPString parseOptionalPathParameterElement(const char* tagName, const FPString& defaultValue = FPString()) const;
110 
113  double parseFloatParameterAttribute(const char* attributeName) const;
114 
117  double parseOptionalFloatParameterAttribute(const char* attributeName, double defaultValue) const;
118 
121  int parseIntParameterAttribute(const char* attributeName) const;
122 
125  int parseOptionalIntParameterAttribute(const char* attributeName, int defaultValue) const;
126 
129  FPString parseStringParameterAttribute(const char* attributeName) const;
130 
133  FPString parseOptionalStringParameterAttribute(const char* attributeName, const FPString& defaultValue = FPString()) const;
134 
137  FPString parsePathParameterAttribute(const char* attributeName) const;
138 
141  bool parseBooleanParameterAttribute(const char* attributeName) const;
142 
145  bool parseOptionalBooleanParameterAttribute(const char* attributeName, bool defaultValue) const;
146 
148  Element firstChildElement() const;
149 
151  Element firstChildElement(const char* tagName) const;
152 
154  Element expectChildElement(const char* tagName) const;
155 
157  explicit operator bool() const noexcept { return _el != nullptr; }
158 
160  bool tagEquals(const char* tagName) const;
161 
163  void expectTag(const char* tagName) const;
164 
166  bool hasAttribute(const char* attrName) const;
167 
169  int lineNumber() const;
170 
172  Element nextSibling() const;
173 
175  Element nextSibling(const char* tagName) const;
176 
178  const char* tag() const { return (const char*)_el->name; }
179 
181  explicit operator xmlNodePtr() const noexcept { return _el; }
182 
184  Element createIntParameterElement(const char* tagName, int value);
185 
187  Element createFloatParameterElement(const char* tagName, double value);
188 
190  Element createStringParameterElement(const char* tagName, const FPString& value);
191 
193  int attributeCount() const;
194 
196  void appendChild(OElement& element);
197 
199  void appendChild(OElement&& element);
200 
202  void setAttribute(const char* attr, const FPString& value);
203 
205  void setTagName(const FPString& tagName);
206 
209  void setTextContent(const FPString& text);
210 
211 protected:
212 
215  FPString resolveFilename(const FPString& filename) const;
216 
218  xmlNodePtr _el;
219 };
220 
226 class OElement : public std::unique_ptr<xmlNode, void(*)(xmlNodePtr)>, public Element
227 {
228 public:
229 
231  OElement() : std::unique_ptr<xmlNode, void(*)(xmlNodePtr)>(nullptr, xmlFreeNode) {}
232 
234  OElement(const char* name);
235 
237  explicit operator bool() const noexcept { return _el != nullptr; }
238 };
239 
240 } // End of namespace
241 } // End of namespace
const char * tag() const
Returns the tag name of this element.
Definition: XMLUtilities.h:178
OElement()
Constructor that creates a null element.
Definition: XMLUtilities.h:231
Element(xmlNodePtr e=nullptr)
Constructor that takes a LibXml element.
Definition: XMLUtilities.h:74
This file collects the definition of classes that define various simple crystal structures.
Definition: Atomicrex.h:67
Definition: XMLUtilities.h:43
xmlNodePtr _el
The internal pointer to the xmllib element.
Definition: XMLUtilities.h:218
std::string FPString
The default string type used throughout the code:
Definition: Atomicrex.h:70
Definition: XMLUtilities.h:226
Definition: XMLUtilities.h:69