-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathjni-adapter.cpp
More file actions
258 lines (227 loc) · 8.44 KB
/
jni-adapter.cpp
File metadata and controls
258 lines (227 loc) · 8.44 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
#include "MD4CParser.hpp"
#include <android/log.h>
#include <jni.h>
#include <string>
using namespace Markdown;
#define ENRICHEDMARKDOWN_LOG_TAG "EnrichedMarkdownJNI"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, ENRICHEDMARKDOWN_LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, ENRICHEDMARKDOWN_LOG_TAG, __VA_ARGS__)
// Helper function to convert C++ NodeType to Kotlin enum ordinal
static jint nodeTypeToJavaOrdinal(NodeType type) {
switch (type) {
case NodeType::Document:
return 0;
case NodeType::Paragraph:
return 1;
case NodeType::Text:
return 2;
case NodeType::Link:
return 3;
case NodeType::Heading:
return 4;
case NodeType::LineBreak:
return 5;
case NodeType::Strong:
return 6;
case NodeType::Emphasis:
return 7;
case NodeType::Strikethrough:
return 8;
case NodeType::Underline:
return 9;
case NodeType::Code:
return 10;
case NodeType::Image:
return 11;
case NodeType::Blockquote:
return 12;
case NodeType::UnorderedList:
return 13;
case NodeType::OrderedList:
return 14;
case NodeType::ListItem:
return 15;
case NodeType::CodeBlock:
return 16;
case NodeType::ThematicBreak:
return 17;
case NodeType::Table:
return 18;
case NodeType::TableHead:
return 19;
case NodeType::TableBody:
return 20;
case NodeType::TableRow:
return 21;
case NodeType::TableHeaderCell:
return 22;
case NodeType::TableCell:
return 23;
case NodeType::LatexMathInline:
return 24;
case NodeType::LatexMathDisplay:
return 25;
case NodeType::Spoiler:
return 26;
case NodeType::Superscript:
return 27;
case NodeType::Subscript:
return 28;
default:
return 0;
}
}
// Helper function to create a Kotlin MarkdownASTNode object from C++ AST node
static jobject createJavaNode(JNIEnv *env, std::shared_ptr<MarkdownASTNode> node) {
if (!node) {
return nullptr;
}
// Find the MarkdownASTNode class
jclass nodeClass = env->FindClass("com/swmansion/enriched/markdown/parser/MarkdownASTNode");
if (!nodeClass) {
LOGE("Failed to find MarkdownASTNode class");
return nullptr;
}
// Find the NodeType enum class
jclass nodeTypeClass = env->FindClass("com/swmansion/enriched/markdown/parser/MarkdownASTNode$NodeType");
if (!nodeTypeClass) {
LOGE("Failed to find NodeType enum class");
return nullptr;
}
// Get the enum values array
jmethodID valuesMethod = env->GetStaticMethodID(
nodeTypeClass, "values", "()[Lcom/swmansion/enriched/markdown/parser/MarkdownASTNode$NodeType;");
if (!valuesMethod) {
LOGE("Failed to find NodeType.values() method");
return nullptr;
}
jobjectArray enumValues = (jobjectArray)env->CallStaticObjectMethod(nodeTypeClass, valuesMethod);
if (!enumValues) {
LOGE("Failed to get NodeType enum values");
return nullptr;
}
// Get the enum value for this node type
jint ordinal = nodeTypeToJavaOrdinal(node->type);
jobject nodeTypeEnum = env->GetObjectArrayElement(enumValues, ordinal);
if (!nodeTypeEnum) {
LOGE("Failed to get NodeType enum value at index %d", ordinal);
return nullptr;
}
// Create content string
jstring contentStr = env->NewStringUTF(node->content.c_str());
if (!contentStr && !node->content.empty()) {
LOGE("Failed to create content string");
return nullptr;
}
// Create attributes HashMap
jclass mapClass = env->FindClass("java/util/HashMap");
jmethodID mapInit = env->GetMethodID(mapClass, "<init>", "(I)V");
jmethodID mapPut = env->GetMethodID(mapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
jobject attributesMap = env->NewObject(mapClass, mapInit, static_cast<jint>(node->attributes.size()));
for (const auto &pair : node->attributes) {
jstring key = env->NewStringUTF(pair.first.c_str());
jstring value = env->NewStringUTF(pair.second.c_str());
env->CallObjectMethod(attributesMap, mapPut, key, value);
env->DeleteLocalRef(key);
env->DeleteLocalRef(value);
}
// Create children ArrayList
jclass listClass = env->FindClass("java/util/ArrayList");
jmethodID listInit = env->GetMethodID(listClass, "<init>", "(I)V");
jmethodID listAdd = env->GetMethodID(listClass, "add", "(Ljava/lang/Object;)Z");
jobject childrenList = env->NewObject(listClass, listInit, static_cast<jint>(node->children.size()));
for (const auto &child : node->children) {
jobject childObj = createJavaNode(env, child);
if (childObj) {
env->CallBooleanMethod(childrenList, listAdd, childObj);
env->DeleteLocalRef(childObj);
}
}
// Find the MarkdownASTNode constructor
// Constructor signature: (Lcom/swmansion/enriched/markdown/parser/MarkdownASTNode$NodeType;Ljava/lang/String;Ljava/util/Map;Ljava/util/List;)V
jmethodID constructor = env->GetMethodID(nodeClass, "<init>",
"(Lcom/swmansion/enriched/markdown/parser/MarkdownASTNode$NodeType;Ljava/"
"lang/String;Ljava/util/Map;Ljava/util/List;)V");
if (!constructor) {
LOGE("Failed to find MarkdownASTNode constructor");
return nullptr;
}
// Create the Kotlin MarkdownASTNode object
jobject javaNode = env->NewObject(nodeClass, constructor, nodeTypeEnum, contentStr, attributesMap, childrenList);
// Clean up local references
env->DeleteLocalRef(nodeTypeClass);
env->DeleteLocalRef(enumValues);
env->DeleteLocalRef(nodeTypeEnum);
if (contentStr)
env->DeleteLocalRef(contentStr);
env->DeleteLocalRef(attributesMap);
env->DeleteLocalRef(childrenList);
return javaNode;
}
extern "C" {
JNIEXPORT jobject JNICALL Java_com_swmansion_enriched_markdown_parser_Parser_nativeParseMarkdown(JNIEnv *env,
jobject /* this */,
jstring markdown,
jobject flags) {
if (!markdown) {
LOGE("Markdown string is null");
return nullptr;
}
const char *markdownStr = env->GetStringUTFChars(markdown, nullptr);
if (!markdownStr) {
LOGE("Failed to get UTF-8 chars from markdown string");
return nullptr;
}
try {
// Extract flags from Kotlin Md4cFlags data class
Md4cFlags md4cFlags;
if (flags) {
jclass flagsClass = env->GetObjectClass(flags);
if (flagsClass) {
jfieldID underlineField = env->GetFieldID(flagsClass, "underline", "Z");
if (underlineField) {
md4cFlags.underline = env->GetBooleanField(flags, underlineField) == JNI_TRUE;
}
jfieldID latexMathField = env->GetFieldID(flagsClass, "latexMath", "Z");
if (latexMathField) {
md4cFlags.latexMath = env->GetBooleanField(flags, latexMathField) == JNI_TRUE;
}
jfieldID superscriptField = env->GetFieldID(flagsClass, "superscript", "Z");
if (superscriptField) {
md4cFlags.superscript = env->GetBooleanField(flags, superscriptField) == JNI_TRUE;
}
jfieldID subscriptField = env->GetFieldID(flagsClass, "subscript", "Z");
if (subscriptField) {
md4cFlags.subscript = env->GetBooleanField(flags, subscriptField) == JNI_TRUE;
}
jfieldID permissiveAutolinksField = env->GetFieldID(flagsClass, "permissiveAutolinks", "Z");
if (permissiveAutolinksField) {
md4cFlags.permissiveAutolinks = env->GetBooleanField(flags, permissiveAutolinksField) == JNI_TRUE;
}
env->DeleteLocalRef(flagsClass);
}
}
MD4CParser parser;
auto ast = parser.parse(std::string(markdownStr), md4cFlags);
env->ReleaseStringUTFChars(markdown, markdownStr);
if (!ast) {
LOGE("Parser returned null AST");
return nullptr;
}
// Convert C++ AST to Kotlin MarkdownASTNode object
jobject javaNode = createJavaNode(env, ast);
if (!javaNode) {
LOGE("Failed to create Java node from AST");
}
return javaNode;
} catch (const std::exception &e) {
env->ReleaseStringUTFChars(markdown, markdownStr);
LOGE("Exception during parsing: %s", e.what());
return nullptr;
} catch (...) {
env->ReleaseStringUTFChars(markdown, markdownStr);
LOGE("Unknown exception during parsing");
return nullptr;
}
}
} // extern "C"