Add PDF writer.

This commit is contained in:
jmsgrogan 2022-01-01 18:46:31 +00:00
parent c05b7b6315
commit 9c116b1efd
72 changed files with 1819 additions and 114 deletions

View file

@ -9,15 +9,14 @@ XmlParser::XmlParser()
: mDocument(XmlDocument::Create()),
mDocumentState(XmlParser::DocumentState::Await_Prolog),
mLineState(XmlParser::LineState::Await_Tag_Open),
mParentElement(nullptr),
mWorkingElement(nullptr)
mWorkingElements()
{
}
void XmlParser::ProcessLine(const std::string& input)
{
for(std::size_t idx=0; idx<input.size(); idx++)
for (std::size_t idx=0; idx<input.size(); idx++)
{
switch (input[idx])
{
@ -269,14 +268,11 @@ void XmlParser::OnTagClose()
}
else if(mDocumentState == DS::Close_Element)
{
if(mWorkingElement->GetTagName() == mWorkingTagName)
mDocumentState = DS::Await_Element;
mLineState = LS::Await_Tag_Open;
if (!mWorkingElements.empty())
{
mDocumentState = DS::Await_Element;
mLineState = LS::Await_Tag_Open;
if(mParentElement)
{
mWorkingElement = mParentElement;
}
mWorkingElements.pop();
}
}
}
@ -289,7 +285,7 @@ void XmlParser::OnTextStart(char c)
void XmlParser::OnTextEnd()
{
mWorkingElement->SetText(mWorkingText);
mWorkingElements.top()->SetText(mWorkingText);
}
void XmlParser::onElementTagEnd()
@ -318,14 +314,17 @@ void XmlParser::OnTagNameEnd()
else if(mDocumentState == DS::Build_Element)
{
auto new_element = XmlElement::Create(mWorkingTagName);
auto working_element = new_element.get();
mParentElement = mWorkingElement;
mWorkingElement = new_element.get();
if(!mDocument->GetRoot())
if (!mDocument->GetRoot())
{
mDocument->SetRoot(std::move(new_element));
}
else
{
mWorkingElements.top()->AddChild(std::move(new_element));
}
mWorkingElements.push(working_element);
mLineState = LS::Await_Attribute_Name;
}
}
@ -345,7 +344,7 @@ void XmlParser::OnAttributeNameEnd()
}
else if(mDocumentState == DS::Build_Element)
{
mWorkingElement->AddAttribute(std::move(attribute));
mWorkingElements.top()->AddAttribute(std::move(attribute));
}
mLineState = LS::Await_Attribute_Value;
}
@ -358,7 +357,14 @@ void XmlParser::OnAttributeValueStart()
void XmlParser::OnAttributeValueEnd()
{
mWorkingElement->GetAttribute(mWorkingAttributeName)->SetValue(mWorkingAttributeValue);
if(mDocumentState == DS::Build_Prolog)
{
mDocument->GetProlog()->GetAttribute(mWorkingAttributeName)->SetValue(mWorkingAttributeValue);
}
else if(mDocumentState == DS::Build_Element)
{
mWorkingElements.top()->GetAttribute(mWorkingAttributeName)->SetValue(mWorkingAttributeValue);
}
mLineState = LS::Await_Attribute_Name;
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <stack>
#include "XmlDocument.h"
class XmlParser
@ -33,8 +34,7 @@ private:
DocumentState mDocumentState;
LineState mLineState;
XmlDocumentPtr mDocument;
XmlElement* mParentElement;
XmlElement* mWorkingElement;
std::stack<XmlElement*> mWorkingElements;
std::string mWorkingAttributeName;
std::string mWorkingTagName;
std::string mWorkingAttributeValue;

View file

@ -1,6 +1,68 @@
#include "XmlWriter.h"
void XmlWriter::Write(File* file, XmlDocument* document)
{
std::string XmlWriter::ToString(XmlElement* element, unsigned depth)
{
const auto prefix = std::string(2*depth, ' ');
auto content = prefix + "<" + element->GetTagName();
for (std::size_t idx=0; idx< element->GetNumAttributes(); idx++)
{
auto attribute = element->GetAttribute(idx);
content += " " + attribute->GetName() + "=\"" + attribute->GetValue() + "\"";
}
const auto num_children = element->GetNumChildren();
if (num_children == 0 && element->GetText().empty())
{
content += "/>\n";
return content;
}
else
{
content += ">";
}
if (!element->GetText().empty())
{
content += element->GetText();
}
if (num_children>0)
{
content += "\n";
}
for (std::size_t idx=0; idx< element->GetNumChildren(); idx++)
{
auto child = element->GetChild(idx);
content += ToString(child, depth+1);
}
if (num_children>0)
{
content += prefix;
}
content += "</" + element->GetTagName() + ">\n";
return content;
}
std::string XmlWriter::ToString(XmlDocument* document)
{
std::string content;
if (auto prolog = document->GetProlog())
{
content += "<?xml";
for (std::size_t idx=0; idx< prolog->GetNumAttributes(); idx++)
{
auto attribute = prolog->GetAttribute(idx);
content += " " + attribute->GetName() + "=\"" + attribute->GetValue() + "\"";
}
content += "?>\n";
}
if (auto root = document->GetRoot())
{
content += ToString(root);
}
return content;
}

View file

@ -1,12 +1,15 @@
#pragma once
#include "File.h"
#include "XmlDocument.h"
#include <string>
class XmlWriter
{
public:
XmlWriter() = default;
void Write(File* file, XmlDocument* document);
std::string ToString(XmlDocument* document);
private:
std::string ToString(XmlElement* element, unsigned depth=0);
};

View file

@ -68,3 +68,13 @@ std::size_t XmlElement::GetNumAttributes() const
{
return mAttributes.size();
}
std::size_t XmlElement::GetNumChildren() const
{
return mChildren.size();
}
XmlElement* XmlElement::GetChild(std::size_t index) const
{
return mChildren[index].get();
}

View file

@ -1,18 +1,13 @@
#pragma once
#include "XmlAttribute.h"
#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;
@ -28,8 +23,17 @@ public:
XmlAttribute* GetAttribute(std::size_t index) const;
std::size_t GetNumAttributes() const;
std::size_t GetNumChildren() const;
XmlElement* GetChild(std::size_t index) const;
void SetText(const std::string& text);
void SetTagName(const std::string& tagName);
protected:
std::string mTagName;
std::vector<XmlAttributeUPtr> mAttributes;
std::vector<std::unique_ptr<XmlElement> > mChildren;
std::string mText;
};
using XmlElementPtr = std::shared_ptr<XmlElement>;

View file

@ -2,12 +2,11 @@
#include <memory>
#include <vector>
#include "xml-elements/XmlElement.h"
#include "XmlElement.h"
class XmlProlog : public XmlElement
{
public:
enum class Version{
V1_0
};