Tidy up some xml structures.

This commit is contained in:
jmsgrogan 2020-05-09 15:29:45 +01:00
parent 875cdc84ff
commit 8771b721d1
31 changed files with 885 additions and 563 deletions

View file

@ -5,7 +5,7 @@ target_include_directories(sample_gui PUBLIC
"${PROJECT_SOURCE_DIR}/src/client" "${PROJECT_SOURCE_DIR}/src/client"
) )
target_link_libraries(sample_gui PUBLIC client windows console core target_link_libraries(sample_gui PUBLIC client windows console core
network database geometry audio graphics) network database geometry audio graphics web)
# Sample Console # Sample Console
add_executable(sample_console console-main.cpp) add_executable(sample_console console-main.cpp)
@ -13,7 +13,7 @@ target_include_directories(sample_console PUBLIC
"${PROJECT_SOURCE_DIR}/src/console" "${PROJECT_SOURCE_DIR}/src/console"
) )
target_link_libraries(sample_console PUBLIC console core network target_link_libraries(sample_console PUBLIC console core network
database geometry audio) database geometry audio web)
# Xml practice # Xml practice
add_executable(xml_practice xml-practice.cpp) add_executable(xml_practice xml-practice.cpp)
@ -22,11 +22,3 @@ target_include_directories(xml_practice PUBLIC
"${PROJECT_SOURCE_DIR}/src/web/xml" "${PROJECT_SOURCE_DIR}/src/web/xml"
) )
target_link_libraries(xml_practice PUBLIC core web) target_link_libraries(xml_practice PUBLIC core web)
# Markdown practice
add_executable(markdown_practice markdown-practice.cpp)
target_include_directories(markdown_practice PUBLIC
"${PROJECT_SOURCE_DIR}/src/core"
"${PROJECT_SOURCE_DIR}/src/web/markdown"
)
target_link_libraries(markdown_practice PUBLIC web core)

View file

@ -1,36 +1,17 @@
#include <filesystem>
#include <unistd.h>
#include <iostream>
#include "CommandLineArgs.h" #include "CommandLineArgs.h"
#include "MainApplication.h" #include "MainApplication.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
CommandLineArgs command_line_args; auto command_line_args = CommandLineArgs::CreateUnique();
command_line_args.Process(argc, argv); command_line_args->Process(argc, argv);
command_line_args->RecordLaunchPath();
std::string program_type;
if(command_line_args.GetNumberOfArgs() > 1)
{
program_type = command_line_args.GetArg(1);
}
// Start the main app // Start the main app
auto main_app = MainApplication::Create(); auto main_app = MainApplication::Create();
main_app->Initialize(std::filesystem::current_path()); main_app->Initialize(std::move(command_line_args));
if(program_type == "server") main_app->Run();
{
main_app->RunServer();
}
else if(program_type == "audio")
{
main_app->PlayAudio();
}
else
{
std::cerr << "Unknown program type" << std::endl;
}
main_app->ShutDown(); main_app->ShutDown();
return 0; return 0;

View file

@ -1,14 +1,18 @@
#include <filesystem>
#include <memory> #include <memory>
#include "GuiApplication.h" #include "GuiApplication.h"
#include "MainApplication.h" #include "MainApplication.h"
#include "CommandLineArgs.h"
int main() int main(int argc, char *argv[])
{ {
auto command_line_args = CommandLineArgs::CreateUnique();
command_line_args->Process(argc, argv);
command_line_args->RecordLaunchPath();
// Start the main app // Start the main app
auto main_app = MainApplication::Create(); auto main_app = MainApplication::Create();
main_app->Initialize(std::filesystem::current_path()); main_app->Initialize(std::move(command_line_args));
// Start the gui app // Start the gui app
auto gui_app = std::make_shared<GuiApplication>(); auto gui_app = std::make_shared<GuiApplication>();

View file

@ -1,47 +0,0 @@
#include <filesystem>
#include <iostream>
#include <fstream>
#include <ostream>
#include "CommandLineArgs.h"
#include "MarkdownParser.h"
#include "HtmlDocument.h"
#include "HtmlWriter.h"
int main(int argc, char *argv[])
{
CommandLineArgs command_line_args;
command_line_args.Process(argc, argv);
if(command_line_args.GetNumberOfArgs() < 2)
{
std::cerr << "Expected a filepath argument" << std::endl;
return -1;
}
MarkdownParser parser;
const auto filepath = command_line_args.GetArg(1);
if(!std::filesystem::exists(filepath))
{
std::cerr << "Couldn't find file: " << filepath << std::endl;
return -1;
}
std::ifstream md_file;
md_file.open(filepath, std::ifstream::in);
while(md_file.good())
{
std::string line;
std::getline(md_file, line);
parser.ProcessLine(line);
}
md_file.close();
auto html_document = parser.GetHtml();
HtmlWriter writer;
std::string html_string = writer.ToString(html_document);
std::ofstream out("/home/james/test.html");
out << html_string;
out.close();
return 0;
}

View file

@ -3,6 +3,8 @@ list(APPEND console_LIB_INCLUDES MainApplication.cpp)
add_library(console SHARED ${console_LIB_INCLUDES}) add_library(console SHARED ${console_LIB_INCLUDES})
target_include_directories(console PUBLIC target_include_directories(console PUBLIC
"${PROJECT_SOURCE_DIR}/src/core/"
"${PROJECT_SOURCE_DIR}/src/core/file_utilities"
"${PROJECT_SOURCE_DIR}/src/core/loggers" "${PROJECT_SOURCE_DIR}/src/core/loggers"
"${PROJECT_SOURCE_DIR}/src/database" "${PROJECT_SOURCE_DIR}/src/database"
"${PROJECT_SOURCE_DIR}/src/database/database_interfaces" "${PROJECT_SOURCE_DIR}/src/database/database_interfaces"
@ -11,4 +13,5 @@ target_include_directories(console PUBLIC
"${PROJECT_SOURCE_DIR}/src/audio" "${PROJECT_SOURCE_DIR}/src/audio"
"${PROJECT_SOURCE_DIR}/src/audio/midi" "${PROJECT_SOURCE_DIR}/src/audio/midi"
"${PROJECT_SOURCE_DIR}/src/audio/audio_interfaces" "${PROJECT_SOURCE_DIR}/src/audio/audio_interfaces"
"${PROJECT_SOURCE_DIR}/src/web"
) )

View file

@ -1,9 +1,13 @@
#include "MainApplication.h" #include "MainApplication.h"
#include "FileLogger.h" #include "FileLogger.h"
#include "MidiReader.h" #include "MidiReader.h"
#include "File.h"
#include "DocumentConverter.h"
#include <filesystem>
MainApplication::MainApplication() MainApplication::MainApplication()
: mDatabaseManager() : mDatabaseManager(),
mCommandLineArgs()
{ {
} }
@ -13,20 +17,64 @@ MainApplication::~MainApplication()
} }
void MainApplication::Initialize(const std::filesystem::path& workDir) void MainApplication::Initialize(CommandLineArgsUPtr commandLineArgs)
{ {
FileLogger::GetInstance().SetWorkDirectory(workDir.string()); mCommandLineArgs = std::move(commandLineArgs);
std::string launch_path = mCommandLineArgs->GetLaunchPath().string();
FileLogger::GetInstance().SetWorkDirectory(launch_path);
FileLogger::GetInstance().Open(); FileLogger::GetInstance().Open();
MLOG_INFO("Launched"); MLOG_INFO("Launched");
mDatabaseManager = DatabaseManager::Create(); mDatabaseManager = DatabaseManager::Create();
mDatabaseManager->CreateDatabase(workDir.string() + "/database.db"); mDatabaseManager->CreateDatabase(launch_path + "/database.db");
mNetworkManager = NetworkManager::Create(); mNetworkManager = NetworkManager::Create();
mAudioManager = AudioManager::Create(); mAudioManager = AudioManager::Create();
} }
void MainApplication::Run()
{
std::string program_type;
if(mCommandLineArgs->GetNumberOfArgs() > 1)
{
program_type = mCommandLineArgs->GetArg(1);
}
std::string input_path;
std::string output_path;
for(unsigned idx=1; idx<mCommandLineArgs->GetNumberOfArgs(); idx++)
{
auto arg = mCommandLineArgs->GetArg(idx);
if(arg == "-input" && mCommandLineArgs->GetNumberOfArgs() > idx+1)
{
input_path = mCommandLineArgs->GetArg(idx + 1);
}
else if(arg == "-output" && mCommandLineArgs->GetNumberOfArgs() > idx+1)
{
output_path = mCommandLineArgs->GetArg(idx + 1);
}
}
if(program_type == "-server")
{
RunServer();
}
else if(program_type == "-audio")
{
PlayAudio();
}
else if(program_type == "-convert")
{
ConvertDocument(input_path, output_path);
}
else
{
MLOG_ERROR("Unknown program type: " + program_type);
}
}
void MainApplication::RunServer() void MainApplication::RunServer()
{ {
mNetworkManager->RunHttpServer(); mNetworkManager->RunHttpServer();
@ -41,6 +89,15 @@ void MainApplication::PlayAudio()
mAudioManager->GetAudioInterface()->Play(device); mAudioManager->GetAudioInterface()->Play(device);
} }
void MainApplication::ConvertDocument(const std::string& inputPath, const std::string& outputPath)
{
auto input_file = File(std::filesystem::path(inputPath));
auto output_file = File(std::filesystem::path(outputPath));
MLOG_INFO("Converting: " + inputPath + " to " + outputPath);
DocumentConverter converter;
converter.Convert(&input_file, &output_file);
}
void MainApplication::ShutDown() void MainApplication::ShutDown()
{ {
mDatabaseManager->OnShutDown(); mDatabaseManager->OnShutDown();

View file

@ -7,11 +7,13 @@
#include "FileLogger.h" #include "FileLogger.h"
#include "DatabaseManager.h" #include "DatabaseManager.h"
#include "NetworkManager.h" #include "NetworkManager.h"
#include "CommandLineArgs.h"
class MainApplication class MainApplication
{ {
private: private:
CommandLineArgsUPtr mCommandLineArgs;
DatabaseManagerPtr mDatabaseManager; DatabaseManagerPtr mDatabaseManager;
NetworkManagerPtr mNetworkManager; NetworkManagerPtr mNetworkManager;
AudioManagerPtr mAudioManager; AudioManagerPtr mAudioManager;
@ -22,15 +24,20 @@ public:
~MainApplication(); ~MainApplication();
void Initialize(const std::filesystem::path& workDir); void Initialize(CommandLineArgsUPtr commandLineArgs);
void RunServer(); void Run();
void PlayAudio();
void ShutDown(); void ShutDown();
static std::shared_ptr<MainApplication> Create(); static std::shared_ptr<MainApplication> Create();
private:
void RunServer();
void PlayAudio();
void ConvertDocument(const std::string& inputPath, const std::string& outputPath);
}; };
using MainApplicationPtr = std::shared_ptr<MainApplication>; using MainApplicationPtr = std::shared_ptr<MainApplication>;

View file

@ -4,6 +4,8 @@ list(APPEND core_LIB_INCLUDES
CommandLineArgs.cpp CommandLineArgs.cpp
loggers/FileLogger.cpp loggers/FileLogger.cpp
file_utilities/BinaryFile.cpp file_utilities/BinaryFile.cpp
file_utilities/File.cpp
file_utilities/FileFormats.cpp
StringUtils.cpp StringUtils.cpp
http/HttpResponse.cpp) http/HttpResponse.cpp)
@ -12,4 +14,6 @@ add_library(core SHARED ${core_LIB_INCLUDES})
target_include_directories(core PUBLIC target_include_directories(core PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/file_utilities"
"${CMAKE_CURRENT_SOURCE_DIR}/loggers"
) )

View file

@ -1,11 +1,27 @@
#include "CommandLineArgs.h" #include "CommandLineArgs.h"
CommandLineArgs::CommandLineArgs() CommandLineArgs::CommandLineArgs()
: mArugments() : mArugments(),
mLaunchPath()
{ {
} }
std::unique_ptr<CommandLineArgs> CommandLineArgs::CreateUnique()
{
return std::make_unique<CommandLineArgs>();
}
std::filesystem::path CommandLineArgs::GetLaunchPath()
{
return mLaunchPath;
}
void CommandLineArgs::RecordLaunchPath()
{
mLaunchPath = std::filesystem::current_path();
}
void CommandLineArgs::Process(int argc, char *argv[]) void CommandLineArgs::Process(int argc, char *argv[])
{ {
for(int idx=0; idx<argc; idx++) for(int idx=0; idx<argc; idx++)

View file

@ -2,18 +2,28 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <string> #include <string>
#include <filesystem>
class CommandLineArgs class CommandLineArgs
{ {
std::vector<std::string> mArugments; std::vector<std::string> mArugments;
std::filesystem::path mLaunchPath;
public: public:
CommandLineArgs(); CommandLineArgs();
static std::unique_ptr<CommandLineArgs> CreateUnique();
void RecordLaunchPath();
std::filesystem::path GetLaunchPath();
void Process(int argc, char *argv[]); void Process(int argc, char *argv[]);
std::size_t GetNumberOfArgs() const; std::size_t GetNumberOfArgs() const;
std::string GetArg(std::size_t index) const; std::string GetArg(std::size_t index) const;
}; };
using CommandLineArgsUPtr = std::unique_ptr<CommandLineArgs>;

View file

@ -1,5 +1,6 @@
#include "StringUtils.h" #include "StringUtils.h"
#include <locale> #include <locale>
#include <algorithm>
bool StringUtils::IsAlphaNumeric(char c) bool StringUtils::IsAlphaNumeric(char c)
{ {
@ -12,3 +13,11 @@ bool StringUtils::IsSpace(char c)
std::locale loc; std::locale loc;
return std::isspace(c, loc); return std::isspace(c, loc);
} }
std::string StringUtils::ToLower(const std::string& s)
{
std::string ret;
std::transform(s.begin(), s.end(), ret.begin(),
[](unsigned char c){ return std::tolower(c); });
return ret;
}

View file

@ -16,4 +16,5 @@ public:
static bool IsAlphaNumeric(char c); static bool IsAlphaNumeric(char c);
static bool IsSpace(char c); static bool IsSpace(char c);
static std::string ToLower(const std::string& s);
}; };

View file

@ -0,0 +1,68 @@
#include "File.h"
#include "FileLogger.h"
File::File(std::filesystem::path path)
: mFullPath(path),
mInHandle(),
mOutHandle(),
mAccessMode(AccessMode::Read)
{
}
std::string File::GetExtension() const
{
return mFullPath.extension();
}
void File::SetAccessMode(AccessMode mode)
{
mAccessMode = mode;
}
std::ifstream* File::GetInHandle() const
{
return mInHandle.get();
}
std::ofstream* File::GetOutHandle() const
{
return mOutHandle.get();
}
void File::Open()
{
if(mAccessMode == AccessMode::Read)
{
mInHandle = std::make_unique<std::ifstream>();
mInHandle->open(mFullPath, std::ifstream::in);
}
else
{
mOutHandle = std::make_unique<std::ofstream>();
mOutHandle->open(mFullPath, std::ofstream::out);
}
}
void File::Close()
{
if(mOutHandle)
{
mOutHandle->close();
}
if(mInHandle)
{
mInHandle->close();
}
}
FileFormat::Format File::InferFormat() const
{
const auto extension = GetExtension();
return FileFormat::InferFormat(extension);
}
bool File::PathExists() const
{
return std::filesystem::exists(mFullPath);
}

View file

@ -0,0 +1,43 @@
#pragma once
#include <filesystem>
#include <string>
#include "FileFormats.h"
#include <fstream>
#include <memory>
class File
{
public:
enum class AccessMode{
Read,
Write
};
private:
std::filesystem::path mFullPath;
std::unique_ptr<std::ifstream> mInHandle;
std::unique_ptr<std::ofstream> mOutHandle;
AccessMode mAccessMode;
public:
File(std::filesystem::path fullPath);
std::string GetExtension() const;
FileFormat::Format InferFormat() const;
std::ifstream* GetInHandle() const;
std::ofstream* GetOutHandle() const;
bool PathExists() const;
void SetAccessMode(AccessMode mode);
void Open();
void Close();
};

View file

@ -0,0 +1,9 @@
#include "FileFormats.h"
FileFormat::ExtensionMap FileFormat::mExtensions = []
{
ExtensionMap ret;
ret[Format::Markdown] = ".md";
ret[Format::Html] = ".html";
return ret;
}();

View file

@ -0,0 +1,57 @@
#pragma once
#include <map>
#include <string>
#include "StringUtils.h"
class FileFormat{
public:
enum class Encoding{
Ascii,
UTF8,
Binary
};
enum class Format{
Html,
Xml,
Markdown,
Pdf,
Json,
Png,
Wav,
Midi,
Unknown
};
using ExtensionMap = std::map<Format, std::string>;
private:
static ExtensionMap mExtensions;
public:
bool IsFormat(const std::string& extension, Format format)
{
return StringUtils::ToLower(extension) == mExtensions[format];
}
static Format InferFormat(const std::string& query)
{
for(const auto& extension : mExtensions)
{
if(extension.second == query)
{
return extension.first;
}
}
return Format::Unknown;
}
std::string GetExtension(Format format)
{
return mExtensions[format];
}
};

View file

@ -6,14 +6,18 @@ list(APPEND web_LIB_INCLUDES
xml/XmlProlog.cpp xml/XmlProlog.cpp
markdown/MarkdownParser.cpp markdown/MarkdownParser.cpp
html/HtmlWriter.cpp html/HtmlWriter.cpp
html/HtmlDocument.cpp) html/HtmlDocument.cpp
DocumentConverter.cpp)
# add the executable # add the executable
add_library(web SHARED ${web_LIB_INCLUDES}) add_library(web SHARED ${web_LIB_INCLUDES})
target_include_directories(web PUBLIC target_include_directories(web PUBLIC
"${PROJECT_SOURCE_DIR}/src/core/" "${PROJECT_SOURCE_DIR}/src/core/"
"${PROJECT_SOURCE_DIR}/src/core/loggers"
"${PROJECT_SOURCE_DIR}/src/core/file_utilities"
"${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/xml" "${CMAKE_CURRENT_SOURCE_DIR}/xml"
"${CMAKE_CURRENT_SOURCE_DIR}/html" "${CMAKE_CURRENT_SOURCE_DIR}/html"
"${CMAKE_CURRENT_SOURCE_DIR}/markdown"
) )

View file

@ -0,0 +1,76 @@
#include "DocumentConverter.h"
#include "MarkdownParser.h"
#include "HtmlWriter.h"
#include "FileLogger.h"
#include <fstream>
DocumentConverter::DocumentConverter()
{
}
void DocumentConverter::Convert(File* input, File* output)
{
if(!input || !output)
{
return;
}
const auto input_format = input->InferFormat();
switch (input_format)
{
case FileFormat::Format::Markdown:
{
ConvertMarkdown(input, output);
break;
}
default:
{
break;
}
}
}
void DocumentConverter::ConvertMarkdown(File* input, File* output)
{
const auto output_format = output->InferFormat();
switch (output_format)
{
case FileFormat::Format::Html:
{
MarkdownToHtml(input, output);
break;
}
default:
{
break;
}
}
}
void DocumentConverter::MarkdownToHtml(File* input, File* output)
{
MLOG_INFO("Converting Markdown to Html");
input->SetAccessMode(File::AccessMode::Read);
input->Open();
MarkdownParser parser;
auto handle = input->GetInHandle();
while(handle->good())
{
std::string line;
std::getline(*handle, line);
parser.ProcessLine(line);
};
input->Close();
auto html_document = parser.GetHtml();
HtmlWriter writer;
std::string html_string = writer.ToString(html_document);
output->SetAccessMode(File::AccessMode::Write);
output->Open();
*(output->GetOutHandle()) << html_string;
output->Close();
}

View file

@ -0,0 +1,16 @@
#pragma once
#include <string>
#include "File.h"
class DocumentConverter
{
public:
DocumentConverter();
void Convert(File* input, File* output);
void ConvertMarkdown(File* input, File* output);
void MarkdownToHtml(File* input, File* output);
};

View file

@ -1,11 +1,12 @@
#include "HtmlElement.h" #include "HtmlElement.h"
HtmlElement::HtmlElement() HtmlElement::HtmlElement(const std::string& tagName)
: XmlElement(tagName)
{ {
} }
static std::shared_ptr<HtmlElement> HtmlElement::Create() static std::unique_ptr<HtmlElement> HtmlElement::CreateUnique(const std::string& tagName)
{ {
return std::make_shared<HtmlElement>(); return std::make_unique<HtmlElement>(tagName);
} }

View file

@ -4,9 +4,9 @@
class HtmlElement : public XmlElement class HtmlElement : public XmlElement
{ {
HtmlElement(); HtmlElement(const std::string& tagName);
static std::shared_ptr<HtmlElement> Create(); static std::unique_ptr<HtmlElement> CreateUnique(const std::string& tagName);
}; };
using HtmlElementPtr = std::shared_ptr<HtmlElement>; using HtmlElementUPtr = std::unique_ptr<HtmlElement>;

View file

@ -8,9 +8,9 @@ XmlAttribute::XmlAttribute(const std::string& name)
} }
std::shared_ptr<XmlAttribute> XmlAttribute::Create(const std::string& name) XmlAttributeUPtr XmlAttribute::Create(const std::string& name)
{ {
return std::make_shared<XmlAttribute>(name); return std::make_unique<XmlAttribute>(name);
} }
void XmlAttribute::SetValue(const std::string& value) void XmlAttribute::SetValue(const std::string& value)

View file

@ -5,7 +5,7 @@
class XmlAttribute class XmlAttribute
{ {
private:
std::string mName; std::string mName;
std::string mValue; std::string mValue;
@ -13,7 +13,7 @@ public:
XmlAttribute(const std::string& name); XmlAttribute(const std::string& name);
static std::shared_ptr<XmlAttribute> Create(const std::string& name); static std::unique_ptr<XmlAttribute> Create(const std::string& name);
void SetValue(const std::string& value); void SetValue(const std::string& value);
@ -23,3 +23,4 @@ public:
}; };
using XmlAttributePtr = std::shared_ptr<XmlAttribute>; using XmlAttributePtr = std::shared_ptr<XmlAttribute>;
using XmlAttributeUPtr = std::unique_ptr<XmlAttribute>;

View file

@ -1,34 +1,33 @@
#include "XmlDocument.h" #include "XmlDocument.h"
XmlDocument::XmlDocument() XmlDocument::XmlDocument()
:mProlog(XmlProlog::Create("xml")) :mProlog(XmlProlog::Create("xml"))
{ {
} }
std::shared_ptr<XmlDocument> XmlDocument::Create() XmlDocumentUPtr XmlDocument::Create()
{ {
return std::make_shared<XmlDocument>(); return std::make_unique<XmlDocument>();
} }
void XmlDocument::SetProlog(XmlPrologPtr prolog) void XmlDocument::SetProlog(XmlPrologUPtr prolog)
{ {
mProlog = prolog; mProlog = std::move(prolog);
} }
XmlPrologPtr XmlDocument::GetProlog() const XmlProlog* XmlDocument::GetProlog() const
{ {
return mProlog; return mProlog.get();
} }
void XmlDocument::SetRoot(XmlElementPtr root) void XmlDocument::SetRoot(XmlElementUPtr root)
{ {
mRoot = root; mRoot = std::move(root);
} }
XmlElementPtr XmlDocument::GetRoot() const XmlElement* XmlDocument::GetRoot() const
{ {
return mRoot; return mRoot.get();
} }

View file

@ -6,21 +6,20 @@
class XmlDocument class XmlDocument
{ {
XmlPrologPtr mProlog; XmlPrologUPtr mProlog;
XmlElementPtr mRoot; XmlElementUPtr mRoot;
public: public:
XmlDocument(); XmlDocument();
static std::shared_ptr<XmlDocument> Create(); static std::unique_ptr<XmlDocument> Create();
void SetProlog(XmlPrologPtr prolog);
XmlPrologPtr GetProlog() const;
void SetRoot(XmlElementPtr root);
XmlElementPtr GetRoot() const;
XmlProlog* GetProlog() const;
XmlElement* GetRoot() const;
void SetProlog(XmlPrologUPtr prolog);
void SetRoot(XmlElementUPtr root);
}; };
using XmlDocumentPtr = std::shared_ptr<XmlDocument>; using XmlDocumentPtr = std::shared_ptr<XmlDocument>;
using XmlDocumentUPtr = std::unique_ptr<XmlDocument>;

View file

@ -8,14 +8,24 @@ XmlElement::XmlElement(const std::string& tagName)
} }
std::shared_ptr<XmlElement> XmlElement::Create(const std::string& tagName) XmlElementUPtr XmlElement::Create(const std::string& tagName)
{ {
return std::make_shared<XmlElement>(tagName); return std::make_unique<XmlElement>(tagName);
} }
void XmlElement::AddChild(std::shared_ptr<XmlElement> child) void XmlElement::SetTagName(const std::string& tagName)
{ {
mChildren.push_back(child); 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 std::string XmlElement::GetTagName() const
@ -33,29 +43,28 @@ void XmlElement::SetText(const std::string& text)
mText = text; mText = text;
} }
void XmlElement::AddAttribute(XmlAttributePtr attribute) XmlAttribute* XmlElement::GetAttribute(const std::string& attributeName) const
{
mAttributes.push_back(attribute);
}
XmlAttributePtr XmlElement::GetAttribute(const std::string& attributeName) const
{ {
for(const auto& attribute : mAttributes) for(const auto& attribute : mAttributes)
{ {
if(attribute->GetName() == attributeName) if(attribute->GetName() == attributeName)
{ {
return attribute; return attribute.get();
} }
} }
return nullptr; 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 std::size_t XmlElement::GetNumAttributes() const
{ {
return mAttributes.size(); return mAttributes.size();
} }
std::vector<XmlAttributePtr> XmlElement::GetAttributes()
{
return mAttributes;
}

View file

@ -8,25 +8,28 @@ class XmlElement
{ {
protected: protected:
std::string mTagName; std::string mTagName;
std::vector<XmlAttributePtr> mAttributes; std::vector<XmlAttributeUPtr> mAttributes;
std::vector<std::shared_ptr<XmlElement> > mChildren; std::vector<std::unique_ptr<XmlElement> > mChildren;
std::string mText; std::string mText;
public: public:
XmlElement(const std::string& tagName); XmlElement(const std::string& tagName);
virtual ~XmlElement() = default; virtual ~XmlElement() = default;
static std::shared_ptr<XmlElement> Create(const std::string& tagName); static std::unique_ptr<XmlElement> Create(const std::string& tagName);
void AddAttribute(XmlAttributeUPtr attribute);
void AddChild(std::unique_ptr<XmlElement> child);
void AddChild(std::shared_ptr<XmlElement> child);
std::string GetTagName() const; std::string GetTagName() const;
std::string GetText() const; std::string GetText() const;
void SetText(const std::string& text); XmlAttribute* GetAttribute(const std::string& attribute) const;
XmlAttribute* GetAttribute(std::size_t index) const;
void AddAttribute(XmlAttributePtr attribute);
XmlAttributePtr GetAttribute(const std::string& attribute) const;
std::size_t GetNumAttributes() const; std::size_t GetNumAttributes() const;
std::vector<XmlAttributePtr> GetAttributes();
void SetText(const std::string& text);
void SetTagName(const std::string& tagName);
}; };
using XmlElementPtr = std::shared_ptr<XmlElement>; using XmlElementPtr = std::shared_ptr<XmlElement>;
using XmlElementUPtr = std::unique_ptr<XmlElement>;

View file

@ -8,7 +8,9 @@ using DS = XmlParser::DocumentState;
XmlParser::XmlParser() XmlParser::XmlParser()
: mDocument(XmlDocument::Create()), : mDocument(XmlDocument::Create()),
mDocumentState(XmlParser::DocumentState::Await_Prolog), mDocumentState(XmlParser::DocumentState::Await_Prolog),
mLineState(XmlParser::LineState::Await_Tag_Open) mLineState(XmlParser::LineState::Await_Tag_Open),
mParentElement(nullptr),
mWorkingElement(nullptr)
{ {
} }
@ -310,18 +312,19 @@ void XmlParser::OnTagNameEnd()
{ {
if(mDocumentState == DS::Build_Prolog) if(mDocumentState == DS::Build_Prolog)
{ {
mWorkingProlog = XmlProlog::Create(mWorkingTagName); mDocument->GetProlog()->SetTagName(mWorkingTagName);
mLineState = LS::Await_Attribute_Name; mLineState = LS::Await_Attribute_Name;
} }
else if(mDocumentState == DS::Build_Element) else if(mDocumentState == DS::Build_Element)
{ {
auto new_element = XmlElement::Create(mWorkingTagName); auto new_element = XmlElement::Create(mWorkingTagName);
mParentElement = mWorkingElement; mParentElement = mWorkingElement;
mWorkingElement = new_element; mWorkingElement = new_element.get();
if(!mDocument->GetRoot()) if(!mDocument->GetRoot())
{ {
mDocument->SetRoot(mWorkingElement); mDocument->SetRoot(std::move(new_element));
} }
mLineState = LS::Await_Attribute_Name; mLineState = LS::Await_Attribute_Name;
} }
@ -335,14 +338,14 @@ void XmlParser::OnAttributeNameStart(char c)
void XmlParser::OnAttributeNameEnd() void XmlParser::OnAttributeNameEnd()
{ {
mWorkingAttribute = XmlAttribute::Create(mWorkingAttributeName); auto attribute = XmlAttribute::Create(mWorkingAttributeName);
if(mDocumentState == DS::Build_Prolog) if(mDocumentState == DS::Build_Prolog)
{ {
mWorkingProlog->AddAttribute(mWorkingAttribute); mDocument->GetProlog()->AddAttribute(std::move(attribute));
} }
else if(mDocumentState == DS::Build_Element) else if(mDocumentState == DS::Build_Element)
{ {
mWorkingElement->AddAttribute(mWorkingAttribute); mWorkingElement->AddAttribute(std::move(attribute));
} }
mLineState = LS::Await_Attribute_Value; mLineState = LS::Await_Attribute_Value;
} }
@ -355,7 +358,7 @@ void XmlParser::OnAttributeValueStart()
void XmlParser::OnAttributeValueEnd() void XmlParser::OnAttributeValueEnd()
{ {
mWorkingAttribute->SetValue(mWorkingAttributeValue); mWorkingElement->GetAttribute(mWorkingAttributeName)->SetValue(mWorkingAttributeValue);
mLineState = LS::Await_Attribute_Name; mLineState = LS::Await_Attribute_Name;
} }
@ -367,9 +370,7 @@ void XmlParser::OnStartProlog()
void XmlParser::onFinishProlog() void XmlParser::onFinishProlog()
{ {
mWorkingProlog->Update(); mDocument->GetProlog()->Update();
mDocument->SetProlog(mWorkingProlog);
mWorkingProlog = nullptr;
mDocumentState = DS::Await_Element; mDocumentState = DS::Await_Element;
mLineState = LS::Await_Tag_Open; mLineState = LS::Await_Tag_Open;
} }

View file

@ -33,10 +33,8 @@ private:
DocumentState mDocumentState; DocumentState mDocumentState;
LineState mLineState; LineState mLineState;
XmlDocumentPtr mDocument; XmlDocumentPtr mDocument;
XmlPrologPtr mWorkingProlog; XmlElement* mParentElement;
XmlElementPtr mParentElement; XmlElement* mWorkingElement;
XmlElementPtr mWorkingElement;
XmlAttributePtr mWorkingAttribute;
std::string mWorkingAttributeName; std::string mWorkingAttributeName;
std::string mWorkingTagName; std::string mWorkingTagName;
std::string mWorkingAttributeValue; std::string mWorkingAttributeValue;

View file

@ -9,9 +9,9 @@ XmlProlog::XmlProlog(const std::string& tagName)
} }
std::shared_ptr<XmlProlog> XmlProlog::Create(const std::string& tagName) XmlPrologUPtr XmlProlog::Create(const std::string& tagName)
{ {
return std::make_shared<XmlProlog>(tagName); return std::make_unique<XmlProlog>(tagName);
} }
XmlProlog::Encoding XmlProlog::GetEncoding() const XmlProlog::Encoding XmlProlog::GetEncoding() const

View file

@ -19,18 +19,19 @@ private:
Version mVersion; Version mVersion;
Encoding mEncoding; Encoding mEncoding;
std::string mTagName;
public: public:
XmlProlog(const std::string& tagName); XmlProlog(const std::string& tagName);
static std::shared_ptr<XmlProlog> Create(const std::string& tagName); static std::unique_ptr<XmlProlog> Create(const std::string& tagName);
void Update();
Encoding GetEncoding() const; Encoding GetEncoding() const;
Version GetVersion() const; Version GetVersion() const;
void SetEncoding(const std::string& encoding); void SetEncoding(const std::string& encoding);
void SetVersion(const std::string& version); void SetVersion(const std::string& version);
void Update();
}; };
using XmlPrologPtr = std::shared_ptr<XmlProlog>; using XmlPrologPtr = std::shared_ptr<XmlProlog>;
using XmlPrologUPtr = std::unique_ptr<XmlProlog>;