-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathOpenDocumentTranslator.cpp
More file actions
197 lines (161 loc) · 7.14 KB
/
OpenDocumentTranslator.cpp
File metadata and controls
197 lines (161 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "OpenDocumentTranslator.h"
#include <fstream>
#include "tinyxml2.h"
#include "nlohmann/json.hpp"
#include "../Constants.h"
#include "odr/FileMeta.h"
#include "odr/TranslationConfig.h"
#include "../TranslationContext.h"
#include "../XmlUtil.h"
#include "../io/Path.h"
#include "../io/StreamUtil.h"
#include "../io/StorageUtil.h"
#include "../io/ZipStorage.h"
#include "OpenDocumentStyleTranslator.h"
#include "OpenDocumentContentTranslator.h"
namespace odr {
class OpenDocumentTranslator::Impl final {
public:
bool translate(const std::string &outPath, TranslationContext &context) const {
std::ofstream out(outPath);
if (!out.is_open()) return false;
context.output = &out;
out << Constants::getHtmlBeginToStyle();
generateStyle(out, context);
context.content = XmlUtil::parse(*context.storage, "content.xml");
tinyxml2::XMLHandle contentHandle(context.content.get());
generateContentStyle(contentHandle, context);
out << Constants::getHtmlStyleToBody();
generateContent(contentHandle, context);
out << Constants::getHtmlBodyToScript();
generateScript(out, context);
out << Constants::getHtmlScriptToEnd();
out.close();
return true;
}
void generateStyle(std::ofstream &out, TranslationContext &context) const {
// TODO: get styles from translators?
// default css
out << Constants::getOpenDocumentDefaultCss();
if (context.meta->type == FileType::OPENDOCUMENT_SPREADSHEET) {
out << Constants::getOpenDocumentSpreadsheetDefaultCss();
}
const auto stylesXml = XmlUtil::parse(*context.storage, "styles.xml");
tinyxml2::XMLHandle stylesHandle(stylesXml.get());
const tinyxml2::XMLElement *fontFaceDecls = stylesHandle
.FirstChildElement("office:document-styles")
.FirstChildElement("office:font-face-decls")
.ToElement();
if (fontFaceDecls != nullptr) {
OpenDocumentStyleTranslator::translate(*fontFaceDecls, context);
}
const tinyxml2::XMLElement *styles = stylesHandle
.FirstChildElement("office:document-styles")
.FirstChildElement("office:styles")
.ToElement();
if (styles != nullptr) {
OpenDocumentStyleTranslator::translate(*styles, context);
}
const tinyxml2::XMLElement *automaticStyles = stylesHandle
.FirstChildElement("office:document-styles")
.FirstChildElement("office:automatic-styles")
.ToElement();
if (automaticStyles != nullptr) {
OpenDocumentStyleTranslator::translate(*automaticStyles, context);
}
}
void generateContentStyle(tinyxml2::XMLHandle &in, TranslationContext &context) const {
const tinyxml2::XMLElement *fontFaceDecls = in
.FirstChildElement("office:document-content")
.FirstChildElement("office:font-face-decls")
.ToElement();
if (fontFaceDecls != nullptr) {
OpenDocumentStyleTranslator::translate(*fontFaceDecls, context);
}
const tinyxml2::XMLElement *automaticStyles = in
.FirstChildElement("office:document-content")
.FirstChildElement("office:automatic-styles")
.ToElement();
if (automaticStyles != nullptr) {
OpenDocumentStyleTranslator::translate(*automaticStyles, context);
}
}
void generateScript(std::ofstream &of, TranslationContext &) const {
of << Constants::getDefaultScript();
}
void generateContent(tinyxml2::XMLHandle &in, TranslationContext &context) const {
tinyxml2::XMLHandle bodyHandle = in
.FirstChildElement("office:document-content")
.FirstChildElement("office:body");
const tinyxml2::XMLElement *body = bodyHandle.ToElement();
// TODO breaks back translation
if ((context.config->entryOffset > 0) | (context.config->entryCount > 0)) {
tinyxml2::XMLElement *content = nullptr;
const char *entryName = nullptr;
switch (context.meta->type) {
case FileType::OPENDOCUMENT_TEXT:
case FileType::OPENDOCUMENT_GRAPHICS:
break;
case FileType::OPENDOCUMENT_PRESENTATION:
content = bodyHandle.FirstChildElement("office:presentation").ToElement();
entryName = "draw:page";
break;
case FileType::OPENDOCUMENT_SPREADSHEET:
content = bodyHandle.FirstChildElement("office:spreadsheet").ToElement();
entryName = "table:table";
break;
default:
break;
}
if (content != nullptr) {
std::uint32_t i = 0;
tinyxml2::XMLElement *e = content->FirstChildElement(entryName);
while (e != nullptr) {
tinyxml2::XMLElement *next = e->NextSiblingElement(entryName);
if ((i < context.config->entryOffset) ||
((context.config->entryCount == 0) ||
(i >= context.config->entryOffset + context.config->entryCount))) {
content->DeleteChild(e);
}
++i;
e = next;
}
}
}
OpenDocumentContentTranslator::translate(*body, context);
}
bool backTranslate(const std::string &diff, const std::string &out, TranslationContext &context) const {
// TODO exit on encrypted files
const auto json = nlohmann::json::parse(diff);
if (json.contains("modifiedText")) {
for (auto &&i : json["modifiedText"].items()) {
const auto it = context.textTranslation.find(std::stoi(i.key()));
// TODO dirty const off-cast
if (it == context.textTranslation.end()) continue;
((tinyxml2::XMLText *) it->second)->SetValue(i.value().get<std::string>().c_str());
}
}
ZipWriter writer(out);
StorageUtil::deepVisit(*context.storage, [&](const auto &p) {
if (p == "content.xml") return;
const auto in = context.storage->read(p);
const auto out = writer.write(p);
StreamUtil::pipe(*in, *out);
});
tinyxml2::XMLPrinter printer(nullptr, true, 0);
context.content->Print(&printer);
writer.write("content.xml")->write(printer.CStr(), printer.CStrSize() - 1);
return true;
}
};
OpenDocumentTranslator::OpenDocumentTranslator() :
impl(std::make_unique<Impl>()){
}
OpenDocumentTranslator::~OpenDocumentTranslator() = default;
bool OpenDocumentTranslator::translate(const std::string &outPath, TranslationContext &context) const {
return impl->translate(outPath, context);
}
bool OpenDocumentTranslator::backTranslate(const std::string &diff, const std::string &outPath, TranslationContext &context) const {
return impl->backTranslate(diff, outPath, context);
}
}