meta_oxide 0.1.0

Universal metadata extraction library supporting 13 formats (HTML Meta, Open Graph, Twitter Cards, JSON-LD, Microdata, Microformats, RDFa, Dublin Core, Web App Manifest, oEmbed, rel-links, Images, SEO) with 7 language bindings
Documentation
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/**
 * JNI Bindings for MetaOxide
 *
 * This file implements the JNI bridge between Java and the MetaOxide C library.
 * It handles:
 * - Type conversions between Java and C
 * - Memory management for strings and structs
 * - Error handling and exception throwing
 * - Thread-safe operations
 *
 * @version 0.1.0
 * @license MIT OR Apache-2.0
 */

#include <jni.h>
#include <string.h>
#include <stdlib.h>
#include "../../../../../../include/meta_oxide.h"

// ===== Helper Functions =====

/**
 * Throw a MetaOxideException with the given message.
 */
static void throw_exception(JNIEnv *env, const char *message) {
    jclass exception_class = (*env)->FindClass(env, "io/github/yfedoseev/metaoxide/MetaOxideException");
    if (exception_class != NULL) {
        (*env)->ThrowNew(env, exception_class, message);
        (*env)->DeleteLocalRef(env, exception_class);
    }
}

/**
 * Throw a MetaOxideException with the given error code and message.
 */
static void throw_exception_with_code(JNIEnv *env, int error_code, const char *message) {
    jclass exception_class = (*env)->FindClass(env, "io/github/yfedoseev/metaoxide/MetaOxideException");
    if (exception_class == NULL) {
        return;
    }

    jmethodID constructor = (*env)->GetMethodID(env, exception_class, "<init>", "(ILjava/lang/String;)V");
    if (constructor == NULL) {
        (*env)->DeleteLocalRef(env, exception_class);
        return;
    }

    jstring j_message = (*env)->NewStringUTF(env, message);
    if (j_message == NULL) {
        (*env)->DeleteLocalRef(env, exception_class);
        return;
    }

    jobject exception = (*env)->NewObject(env, exception_class, constructor, error_code, j_message);
    if (exception != NULL) {
        (*env)->Throw(env, (jthrowable) exception);
        (*env)->DeleteLocalRef(env, exception);
    }

    (*env)->DeleteLocalRef(env, j_message);
    (*env)->DeleteLocalRef(env, exception_class);
}

/**
 * Get the last error from MetaOxide and throw a Java exception.
 */
static void throw_last_error(JNIEnv *env) {
    int error_code = meta_oxide_last_error();
    const char *error_message = meta_oxide_error_message();

    if (error_message != NULL && strlen(error_message) > 0) {
        throw_exception_with_code(env, error_code, error_message);
    } else {
        throw_exception(env, "Unknown error occurred in MetaOxide");
    }
}

/**
 * Convert a Java string to a C string.
 * Returns NULL if the Java string is null or empty.
 * The caller must free the returned string.
 */
static char *java_string_to_c(JNIEnv *env, jstring j_str) {
    if (j_str == NULL) {
        return NULL;
    }

    const char *utf_str = (*env)->GetStringUTFChars(env, j_str, NULL);
    if (utf_str == NULL) {
        return NULL;
    }

    // Check if string is empty
    if (strlen(utf_str) == 0) {
        (*env)->ReleaseStringUTFChars(env, j_str, utf_str);
        return NULL;
    }

    // Create a copy
    char *result = strdup(utf_str);
    (*env)->ReleaseStringUTFChars(env, j_str, utf_str);

    return result;
}

/**
 * Convert a C string to a Java string.
 * Returns NULL if the C string is NULL.
 */
static jstring c_string_to_java(JNIEnv *env, const char *c_str) {
    if (c_str == NULL) {
        return NULL;
    }
    return (*env)->NewStringUTF(env, c_str);
}

// ===== JNI Native Method Implementations =====

/**
 * Extract all metadata formats at once.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeExtractAll(
        JNIEnv *env, jclass cls, jstring html, jstring base_url) {

    // Convert Java strings to C strings
    const char *c_html = (*env)->GetStringUTFChars(env, html, NULL);
    if (c_html == NULL) {
        throw_exception(env, "Failed to convert HTML string");
        return NULL;
    }

    char *c_base_url = java_string_to_c(env, base_url);

    // Call C function
    struct MetaOxideResult *result = meta_oxide_extract_all(c_html, c_base_url);

    // Release Java string
    (*env)->ReleaseStringUTFChars(env, html, c_html);
    if (c_base_url != NULL) {
        free(c_base_url);
    }

    // Check for error
    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    // Build JSON result manually (simple concatenation)
    // Estimate size: all 11 fields + JSON structure
    size_t total_size = 1024; // Base size for structure
    if (result->meta != NULL) total_size += strlen(result->meta);
    if (result->open_graph != NULL) total_size += strlen(result->open_graph);
    if (result->twitter != NULL) total_size += strlen(result->twitter);
    if (result->json_ld != NULL) total_size += strlen(result->json_ld);
    if (result->microdata != NULL) total_size += strlen(result->microdata);
    if (result->microformats != NULL) total_size += strlen(result->microformats);
    if (result->rdfa != NULL) total_size += strlen(result->rdfa);
    if (result->dublin_core != NULL) total_size += strlen(result->dublin_core);
    if (result->manifest != NULL) total_size += strlen(result->manifest);
    if (result->oembed != NULL) total_size += strlen(result->oembed);
    if (result->rel_links != NULL) total_size += strlen(result->rel_links);

    char *json = (char *) malloc(total_size);
    if (json == NULL) {
        meta_oxide_result_free(result);
        throw_exception(env, "Out of memory");
        return NULL;
    }

    // Build JSON object
    strcpy(json, "{");

    if (result->meta != NULL) {
        strcat(json, "\"meta\":");
        strcat(json, result->meta);
    } else {
        strcat(json, "\"meta\":{}");
    }

    strcat(json, ",\"openGraph\":");
    if (result->open_graph != NULL) {
        strcat(json, result->open_graph);
    } else {
        strcat(json, "{}");
    }

    strcat(json, ",\"twitter\":");
    if (result->twitter != NULL) {
        strcat(json, result->twitter);
    } else {
        strcat(json, "{}");
    }

    strcat(json, ",\"jsonLd\":");
    if (result->json_ld != NULL) {
        strcat(json, result->json_ld);
    } else {
        strcat(json, "[]");
    }

    strcat(json, ",\"microdata\":");
    if (result->microdata != NULL) {
        strcat(json, result->microdata);
    } else {
        strcat(json, "[]");
    }

    strcat(json, ",\"microformats\":");
    if (result->microformats != NULL) {
        strcat(json, result->microformats);
    } else {
        strcat(json, "{}");
    }

    strcat(json, ",\"rdfa\":");
    if (result->rdfa != NULL) {
        strcat(json, result->rdfa);
    } else {
        strcat(json, "[]");
    }

    strcat(json, ",\"dublinCore\":");
    if (result->dublin_core != NULL) {
        strcat(json, result->dublin_core);
    } else {
        strcat(json, "{}");
    }

    strcat(json, ",\"manifest\":");
    if (result->manifest != NULL) {
        strcat(json, result->manifest);
    } else {
        strcat(json, "{}");
    }

    strcat(json, ",\"oembed\":");
    if (result->oembed != NULL) {
        strcat(json, result->oembed);
    } else {
        strcat(json, "{}");
    }

    strcat(json, ",\"relLinks\":");
    if (result->rel_links != NULL) {
        strcat(json, result->rel_links);
    } else {
        strcat(json, "{}");
    }

    strcat(json, "}");

    // Create Java string
    jstring j_result = c_string_to_java(env, json);

    // Free resources
    free(json);
    meta_oxide_result_free(result);

    return j_result;
}

/**
 * Extract standard HTML meta tags.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeExtractMeta(
        JNIEnv *env, jclass cls, jstring html, jstring base_url) {

    const char *c_html = (*env)->GetStringUTFChars(env, html, NULL);
    if (c_html == NULL) {
        throw_exception(env, "Failed to convert HTML string");
        return NULL;
    }

    char *c_base_url = java_string_to_c(env, base_url);
    char *result = meta_oxide_extract_meta(c_html, c_base_url);

    (*env)->ReleaseStringUTFChars(env, html, c_html);
    if (c_base_url != NULL) free(c_base_url);

    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    jstring j_result = c_string_to_java(env, result);
    meta_oxide_string_free(result);
    return j_result;
}

/**
 * Extract Open Graph metadata.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeExtractOpenGraph(
        JNIEnv *env, jclass cls, jstring html, jstring base_url) {

    const char *c_html = (*env)->GetStringUTFChars(env, html, NULL);
    if (c_html == NULL) {
        throw_exception(env, "Failed to convert HTML string");
        return NULL;
    }

    char *c_base_url = java_string_to_c(env, base_url);
    char *result = meta_oxide_extract_open_graph(c_html, c_base_url);

    (*env)->ReleaseStringUTFChars(env, html, c_html);
    if (c_base_url != NULL) free(c_base_url);

    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    jstring j_result = c_string_to_java(env, result);
    meta_oxide_string_free(result);
    return j_result;
}

/**
 * Extract Twitter Card metadata.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeExtractTwitter(
        JNIEnv *env, jclass cls, jstring html, jstring base_url) {

    const char *c_html = (*env)->GetStringUTFChars(env, html, NULL);
    if (c_html == NULL) {
        throw_exception(env, "Failed to convert HTML string");
        return NULL;
    }

    char *c_base_url = java_string_to_c(env, base_url);
    char *result = meta_oxide_extract_twitter(c_html, c_base_url);

    (*env)->ReleaseStringUTFChars(env, html, c_html);
    if (c_base_url != NULL) free(c_base_url);

    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    jstring j_result = c_string_to_java(env, result);
    meta_oxide_string_free(result);
    return j_result;
}

/**
 * Extract JSON-LD structured data.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeExtractJsonLd(
        JNIEnv *env, jclass cls, jstring html, jstring base_url) {

    const char *c_html = (*env)->GetStringUTFChars(env, html, NULL);
    if (c_html == NULL) {
        throw_exception(env, "Failed to convert HTML string");
        return NULL;
    }

    char *c_base_url = java_string_to_c(env, base_url);
    char *result = meta_oxide_extract_json_ld(c_html, c_base_url);

    (*env)->ReleaseStringUTFChars(env, html, c_html);
    if (c_base_url != NULL) free(c_base_url);

    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    jstring j_result = c_string_to_java(env, result);
    meta_oxide_string_free(result);
    return j_result;
}

/**
 * Extract Microdata.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeExtractMicrodata(
        JNIEnv *env, jclass cls, jstring html, jstring base_url) {

    const char *c_html = (*env)->GetStringUTFChars(env, html, NULL);
    if (c_html == NULL) {
        throw_exception(env, "Failed to convert HTML string");
        return NULL;
    }

    char *c_base_url = java_string_to_c(env, base_url);
    char *result = meta_oxide_extract_microdata(c_html, c_base_url);

    (*env)->ReleaseStringUTFChars(env, html, c_html);
    if (c_base_url != NULL) free(c_base_url);

    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    jstring j_result = c_string_to_java(env, result);
    meta_oxide_string_free(result);
    return j_result;
}

/**
 * Extract Microformats.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeExtractMicroformats(
        JNIEnv *env, jclass cls, jstring html, jstring base_url) {

    const char *c_html = (*env)->GetStringUTFChars(env, html, NULL);
    if (c_html == NULL) {
        throw_exception(env, "Failed to convert HTML string");
        return NULL;
    }

    char *c_base_url = java_string_to_c(env, base_url);
    char *result = meta_oxide_extract_microformats(c_html, c_base_url);

    (*env)->ReleaseStringUTFChars(env, html, c_html);
    if (c_base_url != NULL) free(c_base_url);

    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    jstring j_result = c_string_to_java(env, result);
    meta_oxide_string_free(result);
    return j_result;
}

/**
 * Extract RDFa structured data.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeExtractRdfa(
        JNIEnv *env, jclass cls, jstring html, jstring base_url) {

    const char *c_html = (*env)->GetStringUTFChars(env, html, NULL);
    if (c_html == NULL) {
        throw_exception(env, "Failed to convert HTML string");
        return NULL;
    }

    char *c_base_url = java_string_to_c(env, base_url);
    char *result = meta_oxide_extract_rdfa(c_html, c_base_url);

    (*env)->ReleaseStringUTFChars(env, html, c_html);
    if (c_base_url != NULL) free(c_base_url);

    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    jstring j_result = c_string_to_java(env, result);
    meta_oxide_string_free(result);
    return j_result;
}

/**
 * Extract Dublin Core metadata.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeExtractDublinCore(
        JNIEnv *env, jclass cls, jstring html) {

    const char *c_html = (*env)->GetStringUTFChars(env, html, NULL);
    if (c_html == NULL) {
        throw_exception(env, "Failed to convert HTML string");
        return NULL;
    }

    char *result = meta_oxide_extract_dublin_core(c_html);
    (*env)->ReleaseStringUTFChars(env, html, c_html);

    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    jstring j_result = c_string_to_java(env, result);
    meta_oxide_string_free(result);
    return j_result;
}

/**
 * Extract Web App Manifest link.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeExtractManifest(
        JNIEnv *env, jclass cls, jstring html, jstring base_url) {

    const char *c_html = (*env)->GetStringUTFChars(env, html, NULL);
    if (c_html == NULL) {
        throw_exception(env, "Failed to convert HTML string");
        return NULL;
    }

    char *c_base_url = java_string_to_c(env, base_url);
    char *result = meta_oxide_extract_manifest(c_html, c_base_url);

    (*env)->ReleaseStringUTFChars(env, html, c_html);
    if (c_base_url != NULL) free(c_base_url);

    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    jstring j_result = c_string_to_java(env, result);
    meta_oxide_string_free(result);
    return j_result;
}

/**
 * Parse Web App Manifest JSON.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeParseManifest(
        JNIEnv *env, jclass cls, jstring json, jstring base_url) {

    const char *c_json = (*env)->GetStringUTFChars(env, json, NULL);
    if (c_json == NULL) {
        throw_exception(env, "Failed to convert JSON string");
        return NULL;
    }

    char *c_base_url = java_string_to_c(env, base_url);
    char *result = meta_oxide_parse_manifest(c_json, c_base_url);

    (*env)->ReleaseStringUTFChars(env, json, c_json);
    if (c_base_url != NULL) free(c_base_url);

    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    jstring j_result = c_string_to_java(env, result);
    meta_oxide_string_free(result);
    return j_result;
}

/**
 * Extract oEmbed endpoint discovery.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeExtractOembed(
        JNIEnv *env, jclass cls, jstring html, jstring base_url) {

    const char *c_html = (*env)->GetStringUTFChars(env, html, NULL);
    if (c_html == NULL) {
        throw_exception(env, "Failed to convert HTML string");
        return NULL;
    }

    char *c_base_url = java_string_to_c(env, base_url);
    char *result = meta_oxide_extract_oembed(c_html, c_base_url);

    (*env)->ReleaseStringUTFChars(env, html, c_html);
    if (c_base_url != NULL) free(c_base_url);

    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    jstring j_result = c_string_to_java(env, result);
    meta_oxide_string_free(result);
    return j_result;
}

/**
 * Extract rel-* link relationships.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeExtractRelLinks(
        JNIEnv *env, jclass cls, jstring html, jstring base_url) {

    const char *c_html = (*env)->GetStringUTFChars(env, html, NULL);
    if (c_html == NULL) {
        throw_exception(env, "Failed to convert HTML string");
        return NULL;
    }

    char *c_base_url = java_string_to_c(env, base_url);
    char *result = meta_oxide_extract_rel_links(c_html, c_base_url);

    (*env)->ReleaseStringUTFChars(env, html, c_html);
    if (c_base_url != NULL) free(c_base_url);

    if (result == NULL) {
        throw_last_error(env);
        return NULL;
    }

    jstring j_result = c_string_to_java(env, result);
    meta_oxide_string_free(result);
    return j_result;
}

/**
 * Get library version.
 */
JNIEXPORT jstring JNICALL
Java_io_github_yfedoseev_metaoxide_Extractor_nativeVersion(
        JNIEnv *env, jclass cls) {
    const char *version = meta_oxide_version();
    return c_string_to_java(env, version);
}