| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the libepubgen project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "EPUBXMLContent.h" |
| 11 |
|
| 12 |
#include <string> |
| 13 |
|
| 14 |
#include <libepubgen/EPUBPackage.h> |
| 15 |
|
| 16 |
namespace libepubgen |
| 17 |
{ |
| 18 |
|
| 19 |
using librevenge::RVNGPropertyList; |
| 20 |
using librevenge::RVNGString; |
| 21 |
|
| 22 |
class EPUBXMLElement |
| 23 |
{ |
| 24 |
public: |
| 25 |
virtual ~EPUBXMLElement() = 0; |
| 26 |
|
| 27 |
virtual void writeTo(EPUBPackage &package) const = 0; |
| 28 |
}; |
| 29 |
|
| 30 |
EPUBXMLElement::~EPUBXMLElement() |
| 31 |
{ |
| 32 |
} |
| 33 |
|
| 34 |
namespace |
| 35 |
{ |
| 36 |
|
| 37 |
class OpenElement : public EPUBXMLElement |
| 38 |
{ |
| 39 |
// disable copying |
| 40 |
OpenElement(const OpenElement &); |
| 41 |
OpenElement &operator=(const OpenElement &); |
| 42 |
|
| 43 |
public: |
| 44 |
OpenElement(const char *name, const RVNGPropertyList &attributes); |
| 45 |
|
| 46 |
private: |
| 47 |
void writeTo(EPUBPackage &package) const override; |
| 48 |
|
| 49 |
private: |
| 50 |
const std::string m_name; |
| 51 |
const RVNGPropertyList m_attributes; |
| 52 |
}; |
| 53 |
|
| 54 |
OpenElement::OpenElement(const char *name, const RVNGPropertyList &attributes) |
| 55 |
: m_name(name) |
| 56 |
, m_attributes(attributes) |
| 57 |
{ |
| 58 |
} |
| 59 |
|
| 60 |
void OpenElement::writeTo(EPUBPackage &package) const |
| 61 |
{ |
| 62 |
package.openElement(m_name.c_str(), m_attributes); |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
namespace |
| 68 |
{ |
| 69 |
|
| 70 |
class CloseElement : public EPUBXMLElement |
| 71 |
{ |
| 72 |
// disable copying |
| 73 |
CloseElement(const CloseElement &); |
| 74 |
CloseElement &operator=(const CloseElement &); |
| 75 |
|
| 76 |
public: |
| 77 |
explicit CloseElement(const char *name); |
| 78 |
|
| 79 |
private: |
| 80 |
void writeTo(EPUBPackage &package) const override; |
| 81 |
|
| 82 |
private: |
| 83 |
const std::string m_name; |
| 84 |
}; |
| 85 |
|
| 86 |
CloseElement::CloseElement(const char *name) |
| 87 |
: m_name(name) |
| 88 |
{ |
| 89 |
} |
| 90 |
|
| 91 |
void CloseElement::writeTo(EPUBPackage &package) const |
| 92 |
{ |
| 93 |
package.closeElement(m_name.c_str()); |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
namespace |
| 99 |
{ |
| 100 |
|
| 101 |
class InsertCharacters : public EPUBXMLElement |
| 102 |
{ |
| 103 |
// disable copying |
| 104 |
InsertCharacters(const InsertCharacters &); |
| 105 |
InsertCharacters &operator=(const InsertCharacters &); |
| 106 |
|
| 107 |
public: |
| 108 |
explicit InsertCharacters(const RVNGString &characters); |
| 109 |
|
| 110 |
private: |
| 111 |
void writeTo(EPUBPackage &package) const override; |
| 112 |
|
| 113 |
private: |
| 114 |
const RVNGString m_characters; |
| 115 |
}; |
| 116 |
|
| 117 |
InsertCharacters::InsertCharacters(const RVNGString &characters) |
| 118 |
: m_characters(characters) |
| 119 |
{ |
| 120 |
} |
| 121 |
|
| 122 |
void InsertCharacters::writeTo(EPUBPackage &package) const |
| 123 |
{ |
| 124 |
package.insertCharacters(m_characters); |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
EPUBXMLContent::EPUBXMLContent() |
| 130 |
: m_elements() |
| 131 |
{ |
| 132 |
} |
| 133 |
|
| 134 |
void EPUBXMLContent::openElement(const char *const name, const librevenge::RVNGPropertyList &attributes) |
| 135 |
{ |
| 136 |
m_elements.push_back(std::make_shared<OpenElement>(name, attributes)); |
| 137 |
} |
| 138 |
|
| 139 |
void EPUBXMLContent::closeElement(const char *const name) |
| 140 |
{ |
| 141 |
m_elements.push_back(std::make_shared<CloseElement>(name)); |
| 142 |
} |
| 143 |
|
| 144 |
void EPUBXMLContent::insertEmptyElement(const char *const name, const librevenge::RVNGPropertyList &attributes) |
| 145 |
{ |
| 146 |
openElement(name, attributes); |
| 147 |
closeElement(name); |
| 148 |
} |
| 149 |
|
| 150 |
void EPUBXMLContent::insertCharacters(const librevenge::RVNGString &characters) |
| 151 |
{ |
| 152 |
m_elements.push_back(std::make_shared<InsertCharacters>(characters)); |
| 153 |
} |
| 154 |
|
| 155 |
void EPUBXMLContent::append(const EPUBXMLContent &other) |
| 156 |
{ |
| 157 |
m_elements.insert(m_elements.end(), other.m_elements.begin(), other.m_elements.end()); |
| 158 |
} |
| 159 |
|
| 160 |
bool EPUBXMLContent::empty() const |
| 161 |
{ |
| 162 |
return m_elements.empty(); |
| 163 |
} |
| 164 |
|
| 165 |
void EPUBXMLContent::writeTo(EPUBPackage &package, const char *const name) |
| 166 |
{ |
| 167 |
package.openXMLFile(name); |
| 168 |
for (const auto &element : m_elements) |
| 169 |
element->writeTo(package); |
| 170 |
package.closeXMLFile(); |
| 171 |
} |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |