-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathCoreWrapper.cpp
More file actions
289 lines (227 loc) · 11.6 KB
/
CoreWrapper.cpp
File metadata and controls
289 lines (227 loc) · 11.6 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "CoreWrapper.hpp"
#include <odr/document.hpp>
#include <odr/file.hpp>
#include <odr/html.hpp>
#include <odr/open_document_reader.hpp>
#include <odr/exceptions.hpp>
#include <odr/http_server.hpp>
#include <android/log.h>
#include <string>
#include <optional>
#include <filesystem>
std::optional<odr::Html> s_html;
JNIEXPORT jobject JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz,
jobject options) {
jboolean isCopy;
jclass resultClass = env->FindClass("at/tomtasche/reader/background/CoreWrapper$CoreResult");
jmethodID resultConstructor = env->GetMethodID(resultClass, "<init>", "()V");
jobject result = env->NewObject(resultClass, resultConstructor);
jfieldID errorField = env->GetFieldID(resultClass, "errorCode", "I");
jclass optionsClass = env->GetObjectClass(options);
jfieldID inputPathField = env->GetFieldID(optionsClass, "inputPath", "Ljava/lang/String;");
auto inputPath = (jstring) env->GetObjectField(options, inputPathField);
const auto inputPathC = env->GetStringUTFChars(inputPath, &isCopy);
auto inputPathCpp = std::string(inputPathC, env->GetStringUTFLength(inputPath));
env->ReleaseStringUTFChars(inputPath, inputPathC);
try {
std::optional<std::string> passwordCpp;
jfieldID passwordField = env->GetFieldID(optionsClass, "password", "Ljava/lang/String;");
auto password = (jstring) env->GetObjectField(options, passwordField);
if (password != nullptr) {
const auto passwordC = env->GetStringUTFChars(password, &isCopy);
passwordCpp = std::string(passwordC, env->GetStringUTFLength(password));
env->ReleaseStringUTFChars(password, passwordC);
}
jfieldID editableField = env->GetFieldID(optionsClass, "editable", "Z");
jboolean editable = env->GetBooleanField(options, editableField);
jfieldID outputPathField = env->GetFieldID(optionsClass, "outputPath",
"Ljava/lang/String;");
auto outputPath = (jstring) env->GetObjectField(options, outputPathField);
const auto outputPathC = env->GetStringUTFChars(outputPath, &isCopy);
auto outputPathCpp = std::string(outputPathC, env->GetStringUTFLength(outputPath));
env->ReleaseStringUTFChars(outputPath, outputPathC);
jclass listClass = env->FindClass("java/util/List");
jmethodID addMethod = env->GetMethodID(listClass, "add", "(Ljava/lang/Object;)Z");
jfieldID pageNamesField = env->GetFieldID(resultClass, "pageNames", "Ljava/util/List;");
auto pageNames = (jobject) env->GetObjectField(result, pageNamesField);
jfieldID pagePathsField = env->GetFieldID(resultClass, "pagePaths", "Ljava/util/List;");
auto pagePaths = (jobject) env->GetObjectField(result, pagePathsField);
jfieldID ooxmlField = env->GetFieldID(optionsClass, "ooxml", "Z");
jboolean ooxml = env->GetBooleanField(options, ooxmlField);
jfieldID txtField = env->GetFieldID(optionsClass, "txt", "Z");
jboolean txt = env->GetBooleanField(options, txtField);
jfieldID pagingField = env->GetFieldID(optionsClass, "paging", "Z");
jboolean paging = env->GetBooleanField(options, pagingField);
try {
odr::FileType fileType;
try {
const auto types = odr::OpenDocumentReader::types(inputPathCpp);
if (types.empty()) {
env->SetIntField(result, errorField, -5);
return result;
}
fileType = types.back();
} catch (odr::UnsupportedFileType &e) {
fileType = e.file_type;
}
const auto extensionCpp = odr::OpenDocumentReader::type_to_string(fileType);
const auto extensionC = extensionCpp.c_str();
jstring extension = env->NewStringUTF(extensionC);
jfieldID extensionField = env->GetFieldID(resultClass, "extension",
"Ljava/lang/String;");
env->SetObjectField(result, extensionField, extension);
// __android_log_print(ANDROID_LOG_VERBOSE, "smn", "%s", extensionCpp.c_str());
const auto file = odr::OpenDocumentReader::open(inputPathCpp);
const auto fileCategory = odr::OpenDocumentReader::category_by_type(file.file_type());
if (!ooxml &&
(file.file_type() == odr::FileType::office_open_xml_document ||
file.file_type() == odr::FileType::office_open_xml_workbook ||
file.file_type() == odr::FileType::office_open_xml_presentation ||
file.file_type() == odr::FileType::office_open_xml_encrypted)) {
env->SetIntField(result, errorField, -5);
return result;
}
if (!txt && fileCategory == odr::FileCategory::text) {
env->SetIntField(result, errorField, -5);
return result;
}
odr::HtmlConfig config;
config.editable = editable;
if (paging) {
config.text_document_margin = true;
}
s_html = odr::OpenDocumentReader::html(inputPathCpp, [&passwordCpp]() -> std::string {
if (passwordCpp.has_value()) {
return passwordCpp.value();
}
return "";
}, outputPathCpp, config);
{
const auto extensionCpp = odr::OpenDocumentReader::type_to_string(
s_html->file_type());
const auto extensionC = extensionCpp.c_str();
jstring extension = env->NewStringUTF(extensionC);
jfieldID extensionField = env->GetFieldID(resultClass, "extension",
"Ljava/lang/String;");
env->SetObjectField(result, extensionField, extension);
}
for (auto &&page: s_html->pages()) {
jstring pageName = env->NewStringUTF(page.name.c_str());
env->CallBooleanMethod(pageNames, addMethod, pageName);
jstring pagePath = env->NewStringUTF(page.path.c_str());
env->CallBooleanMethod(pagePaths, addMethod, pagePath);
}
} catch (odr::UnknownFileType &) {
env->SetIntField(result, errorField, -5);
return result;
} catch (odr::WrongPassword &) {
env->SetIntField(result, errorField, -2);
return result;
} catch (odr::UnsupportedFileType &) {
env->SetIntField(result, errorField, -5);
return result;
} catch (...) {
env->SetIntField(result, errorField, -4);
return result;
}
} catch (...) {
env->SetIntField(result, errorField, -3);
return result;
}
env->SetIntField(result, errorField, 0);
return result;
}
JNIEXPORT jobject JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env, jclass clazz,
jobject options,
jstring htmlDiff) {
jboolean isCopy;
jclass optionsClass = env->GetObjectClass(options);
jclass resultClass = env->FindClass("at/tomtasche/reader/background/CoreWrapper$CoreResult");
jmethodID resultConstructor = env->GetMethodID(resultClass, "<init>", "()V");
jobject result = env->NewObject(resultClass, resultConstructor);
jfieldID errorField = env->GetFieldID(resultClass, "errorCode", "I");
try {
jfieldID outputPathPrefixField = env->GetFieldID(optionsClass, "outputPath",
"Ljava/lang/String;");
jstring outputPathPrefix = (jstring) env->GetObjectField(options, outputPathPrefixField);
const auto outputPathPrefixC = env->GetStringUTFChars(outputPathPrefix, &isCopy);
auto outputPathPrefixCpp = std::string(outputPathPrefixC,
env->GetStringUTFLength(outputPathPrefix));
env->ReleaseStringUTFChars(outputPathPrefix, outputPathPrefixC);
const auto htmlDiffC = env->GetStringUTFChars(htmlDiff, &isCopy);
const auto extension = odr::OpenDocumentReader::type_to_string(s_html->file_type());
const auto outputPathCpp = outputPathPrefixCpp + "." + extension;
const char *outputPathC = outputPathCpp.c_str();
jstring outputPath = env->NewStringUTF(outputPathC);
jfieldID outputPathField = env->GetFieldID(resultClass, "outputPath", "Ljava/lang/String;");
env->SetObjectField(result, outputPathField, outputPath);
try {
s_html->edit(htmlDiffC);
env->ReleaseStringUTFChars(htmlDiff, htmlDiffC);
} catch (...) {
env->ReleaseStringUTFChars(htmlDiff, htmlDiffC);
env->SetIntField(result, errorField, -6);
return result;
}
try {
s_html->save(outputPathCpp);
} catch (...) {
env->SetIntField(result, errorField, -7);
return result;
}
} catch (...) {
env->SetIntField(result, errorField, -3);
return result;
}
env->SetIntField(result, errorField, 0);
return result;
}
JNIEXPORT void JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jclass clazz,
jobject options) {
s_html.reset();
}
std::optional<odr::HttpServer> s_server;
JNIEXPORT void JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jclass clazz, jstring outputPath) {
const char* outputPathC = env->GetStringUTFChars(outputPath, nullptr);
std::string output_path = outputPathC;
env->ReleaseStringUTFChars(outputPath, outputPathC);
std::filesystem::create_directories(output_path);
odr::HttpServer::Config config;
config.output_path = output_path;
s_server = odr::HttpServer(config);
}
JNIEXPORT jstring JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jclass clazz, jobject options) {
jboolean isCopy;
jclass optionsClass = env->GetObjectClass(options);
jfieldID inputPathField = env->GetFieldID(optionsClass, "inputPath", "Ljava/lang/String;");
auto inputPath = (jstring) env->GetObjectField(options, inputPathField);
const auto inputPathC = env->GetStringUTFChars(inputPath, &isCopy);
auto inputPathCpp = std::string(inputPathC, env->GetStringUTFLength(inputPath));
env->ReleaseStringUTFChars(inputPath, inputPathC);
odr::DecodePreference decodePreference;
decodePreference.engine_priority = {
odr::DecoderEngine::poppler, odr::DecoderEngine::wvware, odr::DecoderEngine::odr};
odr::DecodedFile file = odr::OpenDocumentReader::open(inputPathCpp, decodePreference);
__android_log_print(ANDROID_LOG_INFO, "smn", "file type %i", file.file_type());
try {
std::string id = s_server->host_file(file);
return env->NewStringUTF(id.c_str());
} catch (...) {
__android_log_print(ANDROID_LOG_ERROR, "smn", "error");
return env->NewStringUTF("error");
}
}
JNIEXPORT void JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jclass clazz) {
s_server->listen("127.0.0.1", 29665);
}
JNIEXPORT void JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jclass clazz) {
s_server->stop();
s_server.reset();
}