Move xml tools to uptr, start adding test setup.
This commit is contained in:
parent
8771b721d1
commit
e0eccba62f
14 changed files with 44 additions and 9 deletions
29
src/web/xml/xml-elements/XmlAttribute.cpp
Normal file
29
src/web/xml/xml-elements/XmlAttribute.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
#include "XmlAttribute.h"
|
||||
|
||||
|
||||
XmlAttribute::XmlAttribute(const std::string& name)
|
||||
: mName(name),
|
||||
mValue()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
XmlAttributeUPtr XmlAttribute::Create(const std::string& name)
|
||||
{
|
||||
return std::make_unique<XmlAttribute>(name);
|
||||
}
|
||||
|
||||
void XmlAttribute::SetValue(const std::string& value)
|
||||
{
|
||||
mValue = value;
|
||||
}
|
||||
|
||||
std::string XmlAttribute::GetName() const
|
||||
{
|
||||
return mName;
|
||||
}
|
||||
|
||||
std::string XmlAttribute::GetValue() const
|
||||
{
|
||||
return mValue;
|
||||
}
|
26
src/web/xml/xml-elements/XmlAttribute.h
Normal file
26
src/web/xml/xml-elements/XmlAttribute.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
class XmlAttribute
|
||||
{
|
||||
private:
|
||||
std::string mName;
|
||||
std::string mValue;
|
||||
|
||||
public:
|
||||
|
||||
XmlAttribute(const std::string& name);
|
||||
|
||||
static std::unique_ptr<XmlAttribute> Create(const std::string& name);
|
||||
|
||||
void SetValue(const std::string& value);
|
||||
|
||||
std::string GetName() const;
|
||||
|
||||
std::string GetValue() const;
|
||||
};
|
||||
|
||||
using XmlAttributePtr = std::shared_ptr<XmlAttribute>;
|
||||
using XmlAttributeUPtr = std::unique_ptr<XmlAttribute>;
|
70
src/web/xml/xml-elements/XmlElement.cpp
Normal file
70
src/web/xml/xml-elements/XmlElement.cpp
Normal file
|
@ -0,0 +1,70 @@
|
|||
#include "XmlElement.h"
|
||||
|
||||
|
||||
XmlElement::XmlElement(const std::string& tagName)
|
||||
: mTagName(tagName),
|
||||
mChildren()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
XmlElementUPtr XmlElement::Create(const std::string& tagName)
|
||||
{
|
||||
return std::make_unique<XmlElement>(tagName);
|
||||
}
|
||||
|
||||
void XmlElement::SetTagName(const std::string& tagName)
|
||||
{
|
||||
mTagName = tagName;
|
||||
}
|
||||
|
||||
void XmlElement::AddChild(XmlElementUPtr child)
|
||||
{
|
||||
mChildren.push_back(std::move(child));
|
||||
}
|
||||
|
||||
void XmlElement::AddAttribute(XmlAttributeUPtr attribute)
|
||||
{
|
||||
mAttributes.push_back(std::move(attribute));
|
||||
}
|
||||
|
||||
std::string XmlElement::GetTagName() const
|
||||
{
|
||||
return mTagName;
|
||||
}
|
||||
|
||||
std::string XmlElement::GetText() const
|
||||
{
|
||||
return mText;
|
||||
}
|
||||
|
||||
void XmlElement::SetText(const std::string& text)
|
||||
{
|
||||
mText = text;
|
||||
}
|
||||
|
||||
XmlAttribute* XmlElement::GetAttribute(const std::string& attributeName) const
|
||||
{
|
||||
for(const auto& attribute : mAttributes)
|
||||
{
|
||||
if(attribute->GetName() == attributeName)
|
||||
{
|
||||
return attribute.get();
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
XmlAttribute* XmlElement::GetAttribute(std::size_t index) const
|
||||
{
|
||||
if(index < mAttributes.size())
|
||||
{
|
||||
return mAttributes[index].get();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::size_t XmlElement::GetNumAttributes() const
|
||||
{
|
||||
return mAttributes.size();
|
||||
}
|
36
src/web/xml/xml-elements/XmlElement.h
Normal file
36
src/web/xml/xml-elements/XmlElement.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "xml-elements/XmlAttribute.h"
|
||||
|
||||
class XmlElement
|
||||
{
|
||||
protected:
|
||||
std::string mTagName;
|
||||
std::vector<XmlAttributeUPtr> mAttributes;
|
||||
std::vector<std::unique_ptr<XmlElement> > mChildren;
|
||||
std::string mText;
|
||||
|
||||
public:
|
||||
XmlElement(const std::string& tagName);
|
||||
virtual ~XmlElement() = default;
|
||||
|
||||
static std::unique_ptr<XmlElement> Create(const std::string& tagName);
|
||||
|
||||
void AddAttribute(XmlAttributeUPtr attribute);
|
||||
void AddChild(std::unique_ptr<XmlElement> child);
|
||||
|
||||
std::string GetTagName() const;
|
||||
std::string GetText() const;
|
||||
XmlAttribute* GetAttribute(const std::string& attribute) const;
|
||||
XmlAttribute* GetAttribute(std::size_t index) const;
|
||||
std::size_t GetNumAttributes() const;
|
||||
|
||||
void SetText(const std::string& text);
|
||||
void SetTagName(const std::string& tagName);
|
||||
};
|
||||
|
||||
using XmlElementPtr = std::shared_ptr<XmlElement>;
|
||||
using XmlElementUPtr = std::unique_ptr<XmlElement>;
|
54
src/web/xml/xml-elements/XmlProlog.cpp
Normal file
54
src/web/xml/xml-elements/XmlProlog.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include "XmlProlog.h"
|
||||
|
||||
|
||||
XmlProlog::XmlProlog(const std::string& tagName)
|
||||
: XmlElement(tagName),
|
||||
mVersion(XmlProlog::Version::V1_0),
|
||||
mEncoding(XmlProlog::Encoding::UTF8)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
XmlPrologUPtr XmlProlog::Create(const std::string& tagName)
|
||||
{
|
||||
return std::make_unique<XmlProlog>(tagName);
|
||||
}
|
||||
|
||||
XmlProlog::Encoding XmlProlog::GetEncoding() const
|
||||
{
|
||||
return mEncoding;
|
||||
}
|
||||
|
||||
XmlProlog::Version XmlProlog::GetVersion() const
|
||||
{
|
||||
return mVersion;
|
||||
}
|
||||
|
||||
void XmlProlog::SetEncoding(const std::string& encoding)
|
||||
{
|
||||
if(encoding == "UTF-8")
|
||||
{
|
||||
mEncoding == XmlProlog::Encoding::UTF8;
|
||||
}
|
||||
}
|
||||
|
||||
void XmlProlog::SetVersion(const std::string& version)
|
||||
{
|
||||
if(version == "1.0")
|
||||
{
|
||||
mVersion == XmlProlog::Version::V1_0;
|
||||
}
|
||||
}
|
||||
|
||||
void XmlProlog::Update()
|
||||
{
|
||||
if(const auto version = GetAttribute("version"))
|
||||
{
|
||||
SetVersion(version->GetValue());
|
||||
}
|
||||
|
||||
if(const auto encoding = GetAttribute("encoding"))
|
||||
{
|
||||
SetEncoding(encoding->GetValue());
|
||||
}
|
||||
}
|
38
src/web/xml/xml-elements/XmlProlog.h
Normal file
38
src/web/xml/xml-elements/XmlProlog.h
Normal file
|
@ -0,0 +1,38 @@
|
|||
#pragma once
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "xml-elements/XmlElement.h"
|
||||
|
||||
class XmlProlog : public XmlElement
|
||||
{
|
||||
public:
|
||||
|
||||
enum class Version{
|
||||
V1_0
|
||||
};
|
||||
|
||||
enum class Encoding{
|
||||
UTF8
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
Version mVersion;
|
||||
Encoding mEncoding;
|
||||
|
||||
public:
|
||||
XmlProlog(const std::string& tagName);
|
||||
|
||||
static std::unique_ptr<XmlProlog> Create(const std::string& tagName);
|
||||
|
||||
Encoding GetEncoding() const;
|
||||
Version GetVersion() const;
|
||||
|
||||
void SetEncoding(const std::string& encoding);
|
||||
void SetVersion(const std::string& version);
|
||||
void Update();
|
||||
};
|
||||
|
||||
using XmlPrologPtr = std::shared_ptr<XmlProlog>;
|
||||
using XmlPrologUPtr = std::unique_ptr<XmlProlog>;
|
Loading…
Add table
Add a link
Reference in a new issue