UTILLib 2.0
Common C++ library with utilities.
Loading...
Searching...
No Matches
ixmlfile.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 Ingemar Hedvall
3 * SPDX-License-Identifier: MIT
4 */
8#pragma once
9#include <memory>
10#include <string>
11
12#include "util/ixmlnode.h"
13
14namespace util::xml {
15
21class IXmlFile {
22 public:
23 virtual ~IXmlFile() = default;
24
29 void FileName(const std::string &filename);
30
35 [[nodiscard]] std::string FileName() const { return filename_; }
36
37 [[nodiscard]] std::string FileNameWithoutPath()
38 const;
39
45 [[nodiscard]] std::string Version() const { return version_; }
46
52 void Version(const std::string &version) { version_ = version; }
53
59 [[nodiscard]] std::string Encoding() const { return encoding_; }
65 void Encoding(const std::string &encoding) { encoding_ = encoding; }
66
71 [[nodiscard]] bool Standalone() const { return standalone_; }
72
77 [[nodiscard]] std::string RootName()
78 const {
79 return root_node_ ? root_node_->TagName() : "";
80 }
81
86 IXmlNode &RootName(const std::string &name);
87
94 [[nodiscard]] IXmlNode *RootNode() const { return root_node_.get(); }
95
101 [[nodiscard]] std::string StyleSheet() const { return style_sheet_; }
102
108 void StyleSheet(const std::string &style_sheet) {
109 style_sheet_ = style_sheet;
110 }
111
120 template <typename T>
121 [[nodiscard]] T Property(const std::string &key, const T &def = {}) const {
122 return root_node_ ? root_node_->Property<T>(key, def) : def;
123 }
124
131 template <typename T>
132 void SetProperty(const std::string &key, const T &value) {
133 if (root_node_) {
134 root_node_->SetProperty(key, value);
135 }
136 }
137
144 const;
145
152 [[nodiscard]] const IXmlNode *GetNode(
153 const std::string &tag) const;
154
155 virtual bool ParseFile();
156 virtual bool ParseString(
157 const std::string &input);
158 virtual void Reset();
159 virtual bool WriteFile();
160 std::string WriteString(bool skip_header = false);
162 protected:
163 IXmlFile() = default;
164 std::string filename_;
165 std::string version_ = "1.0";
166 std::string encoding_ = "UTF-8";
167 std::string style_sheet_;
168
169 bool standalone_ = true;
170 std::unique_ptr<IXmlNode> root_node_;
171
172 virtual std::unique_ptr<IXmlNode> CreateNode(
173 const std::string &name);
174 private:
175 void WriteRoot(std::ostream &dest, bool skip_header) const;
176};
177
187[[nodiscard]] std::unique_ptr<IXmlFile> CreateXmlFile(
188 const std::string &type = "Expat");
189} // namespace util::xml
std::string Encoding() const
Return the character encoding of the XML file.
Definition ixmlfile.h:59
std::unique_ptr< IXmlNode > root_node_
Pointer to the root node.
Definition ixmlfile.h:170
std::string version_
Version of the XML file.
Definition ixmlfile.h:165
IXmlFile()=default
Default constructor is hidden from external users.
const IXmlNode * GetNode(const std::string &tag) const
Returns a node with a tag name.
virtual bool ParseString(const std::string &input)
Parses a string instead of a file.
bool Standalone() const
Returns true is the XML file is stand-alone.
Definition ixmlfile.h:71
std::string encoding_
Encoding of strings in the XML file.
Definition ixmlfile.h:166
void Encoding(const std::string &encoding)
Sets the XML file encoding.
Definition ixmlfile.h:65
std::string WriteString(bool skip_header=false)
void Version(const std::string &version)
Sets the version of the XML file.
Definition ixmlfile.h:52
std::string style_sheet_
Optional style sheet.
Definition ixmlfile.h:167
virtual bool ParseFile()
Parses the XML file.
std::string StyleSheet() const
Returns the used style sheet.
Definition ixmlfile.h:101
virtual void Reset()
Reset the internals i.e. ready for a new parsing.
void FileName(const std::string &filename)
Sets the file name (full path).
virtual bool WriteFile()
Writes the XML file.
virtual ~IXmlFile()=default
Destructor.
std::string FileName() const
Return the full path file name.
Definition ixmlfile.h:35
bool standalone_
True of the file is standalone.
Definition ixmlfile.h:169
std::string FileNameWithoutPath() const
File name without path.
IXmlNode & RootName(const std::string &name)
Create the root tag in an XML file object.
void GetChildList(IXmlNode::ChildList &child_list) const
Returns a list of child tags below the root tag.
virtual std::unique_ptr< IXmlNode > CreateNode(const std::string &name)
Creates a new node.
std::string filename_
File name with full path.
Definition ixmlfile.h:164
void SetProperty(const std::string &key, const T &value)
Sets a property tag value.
Definition ixmlfile.h:132
std::string Version() const
Returns the XML file version.
Definition ixmlfile.h:45
void StyleSheet(const std::string &style_sheet)
Sets the style sheet.
Definition ixmlfile.h:108
IXmlNode * RootNode() const
Returns the root node in XML file.
Definition ixmlfile.h:94
T Property(const std::string &key, const T &def={}) const
Returns an XML tags value.
Definition ixmlfile.h:121
std::string RootName() const
Returns the name of the root tag.
Definition ixmlfile.h:77
Interface class against a XML tag (node) in a XML file.
Definition ixmlnode.h:61
std::vector< const IXmlNode * > ChildList
List of pointer to child nodes.
Definition ixmlnode.h:179
The xml namespace is used for XML related classes and functions.
Definition isuperviseapplication.h:22
std::unique_ptr< IXmlFile > CreateXmlFile(const std::string &type="Expat")
Creates an XML object that either parse or write.