8 #include <unordered_map>
12 #include <xercesc/dom/DOM.hpp>
50 operator std::string_view()
const {
return value; };
51 operator std::string()
const {
return value; };
53 operator int()
const {
try {
return std::stoi(
value); }
catch(...) {
throw std::runtime_error(
"Cannot convert '" +
value +
"' to int"); } };
54 operator double()
const {
try {
return std::stod(
value); }
catch(...) {
throw std::runtime_error(
"Cannot convert '" +
value +
"' to double"); } };
63 inline static std::string
True =
"true";
64 inline static std::string
False =
"false";
81 typedef std::vector<std::unique_ptr<XMLObject>>
Children;
158 template<
typename T>
inline T*
is() {
159 return dynamic_cast<T*
>(
this);
162 template<
typename T>
inline const T*
is()
const {
163 return dynamic_cast<const T*
>(
this);
171 template<
typename T>
inline T*
get() {
172 T* ptr =
dynamic_cast<T*
>(
this);
173 if ( ptr ==
nullptr ) {
174 throw std::runtime_error(
"XMLObject: Illegal cast");
184 template<
typename T>
inline const T*
get()
const {
185 const T* ptr =
dynamic_cast<const T*
>(
this);
186 if ( ptr ==
nullptr ) {
187 throw std::runtime_error(
"XMLObject: Illegal cast");
194 void findRecursive(std::vector<std::reference_wrapper<T> >& result,
const Children& descendants)
196 for (
auto& descendant : descendants) {
197 if (descendant->is<T>()) {
198 result.push_back(*descendant->get<T>());
200 findRecursive(result, descendant->children );
205 void findRecursive(std::vector<std::reference_wrapper<const T> >& result,
const Children& descendants)
const
207 for (
auto& descendant : descendants) {
208 if (descendant->is<
const T>()) {
209 result.push_back(*descendant->get<
const T>());
211 findRecursive(result, descendant->children );
222 std::vector<std::reference_wrapper<T> >
find()
224 std::vector<std::reference_wrapper<T> > result;
235 std::vector<std::reference_wrapper<const T> >
find()
const
237 std::vector<std::reference_wrapper<const T> > result;
264 std::string
format(std::string indentation =
"\t",
unsigned int depth = 0)
const;
274 if ( child->is<T>() ) {
275 return *child->get<T>();
278 throw std::runtime_error(
"Failed to get required child of element '" +
elementName +
"'");
289 if ( child->is<T>() ) {
290 return *child->get<T>();
301 template<
typename T> std::vector< std::reference_wrapper<T> >
getChildren() {
302 std::vector< std::reference_wrapper<T> > result;
304 if ( child->is<T>() ) {
305 result.push_back(*child->get<T>());
359 std::ostream&
operator<<(std::ostream& os,
const XMLObject* obj);
361 std::ostream&
operator<<(std::ostream& os,
const XMLObject& obj);
A class representing a node in an XML-tree.
std::optional< std::reference_wrapper< T > > getOptionalChild()
Get an optional child of type T.
static XMLObject * createFromFile(const std::string &filename)
Create an XMLObject from an XML file.
std::vector< std::reference_wrapper< T > > find()
Find all descendants of type T.
static XMLObject * createFromString(const std::string &xmlString)
Create an XMLObject from a string representation of XML.
static XMLObject * createObject(const xercesc::DOMElement *element)
std::optional< std::reference_wrapper< Attribute > > getOptionalAttributeByName(const AttributeName &attributeName)
Get an optional attribute with the specified attribute name.
const ClassName className
friend XMLObject * createInstance(const Namespace &xmlns, const ClassName &className, const xercesc::DOMElement *element)
Template function used to store in factory.
std::string format(std::string indentation="\t", unsigned int depth=0) const
Creates formated string representing the XMLObject including its children.
Children children
Child nodes of the XML element.
T & getRequiredChild()
Get a required child of type T.
XMLObject & getRequiredChildByName(const ElementName &elementName)
Get a required child with the specified element name.
const T * get() const
Attempt to cast the current instance to the specified type T.
T * is()
Returns a pointer of type T of the object.
TextContent textContent
Textual content of XML element without children.
std::string stringify() const
Convert the XMLObject and its children to a string representation.
std::vector< std::reference_wrapper< XMLObject > > getChildrenByName(const ElementName &elementName)
Get all children with the specified element name.
XMLObject(const Namespace &xmlns, const ClassName &className, const xercesc::DOMElement *element, const Attributes &defaultAttributes)
static XMLObject * createFromStream(std::istream &xmlStream)
Create an XMLObject from the input stream.
std::vector< std::reference_wrapper< const T > > find() const
Find all descendants of type T.
static const Attributes defaults
Attributes of the XML element.
std::optional< std::reference_wrapper< XMLObject > > getOptionalChildByName(const ElementName &elementName)
Get the optional child with the specified element name.
std::vector< std::reference_wrapper< T > > getChildren()
Get all children of type T.
T * get()
Attempt to cast the current instance to the specified type T.
Attribute & getRequiredAttributeByName(const AttributeName &attributeName)
Get a required attribute with the specified attribute name.
The XML namespace contains classes representing XML-nodes defined in given XML-schema(s).
std::ostream & operator<<(std::ostream &os, const XMLObject *obj)
Allows printing of stringified XML object.
std::vector< Attribute > Attributes
std::vector< std::unique_ptr< XMLObject > > Children
std::unordered_map< ElementName, XMLObject *(*)(const Namespace &xmlns, const ClassName &className, const xercesc::DOMElement *element)> Factory
Factory used to create instance depending on element name.
XMLObject * createInstance(const Namespace &xmlns, const ClassName &className, const xercesc::DOMElement *element)
Template function used to store in factory.
std::string AttributeName
A struct representing an attribute of an XML-node.
A struct representing the value of an XML-node attribute.
Value(const std::string &s)
Value & operator=(double d)
Value & operator=(bool b)
Value & operator=(const std::string &s)