comparison mupdf-source/platform/java/jni/dom.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright (C) 2004-2025 Artifex Software, Inc.
2 //
3 // This file is part of MuPDF.
4 //
5 // MuPDF is free software: you can redistribute it and/or modify it under the
6 // terms of the GNU Affero General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or (at your option)
8 // any later version.
9 //
10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Affero General Public License
16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17 //
18 // Alternative licensing terms are available from the licensor.
19 // For commercial licensing, see <https://www.artifex.com/> or contact
20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21 // CA 94129, USA, for further information.
22
23 /* DOM interface */
24
25 JNIEXPORT void JNICALL
26 FUN(DOM_finalize)(JNIEnv *env, jobject self)
27 {
28 fz_context *ctx = get_context(env);
29 fz_xml *xml = from_DOM_safe(env, self);
30 if (!ctx || !xml) return;
31 (*env)->SetLongField(env, self, fid_DOM_pointer, 0);
32 fz_drop_xml(ctx, xml);
33 }
34
35 JNIEXPORT void JNICALL
36 FUN(DOM_insertBefore)(JNIEnv *env, jobject self, jobject jxml)
37 {
38 fz_context *ctx = get_context(env);
39 fz_xml *me = from_DOM_safe(env, self);
40 fz_xml *xml = from_DOM_safe(env, jxml);
41
42 fz_try(ctx)
43 fz_dom_insert_before(ctx, me, xml);
44 fz_catch(ctx)
45 jni_rethrow_void(env, ctx);
46 }
47
48 JNIEXPORT void JNICALL
49 FUN(DOM_insertAfter)(JNIEnv *env, jobject self, jobject jxml)
50 {
51 fz_context *ctx = get_context(env);
52 fz_xml *me = from_DOM_safe(env, self);
53 fz_xml *xml = from_DOM_safe(env, jxml);
54
55 fz_try(ctx)
56 fz_dom_insert_after(ctx, me, xml);
57 fz_catch(ctx)
58 jni_rethrow_void(env, ctx);
59 }
60
61 JNIEXPORT void JNICALL
62 FUN(DOM_appendChild)(JNIEnv *env, jobject self, jobject jxml)
63 {
64 fz_context *ctx = get_context(env);
65 fz_xml *me = from_DOM_safe(env, self);
66 fz_xml *xml = from_DOM_safe(env, jxml);
67
68 fz_try(ctx)
69 fz_dom_append_child(ctx, me, xml);
70 fz_catch(ctx)
71 jni_rethrow_void(env, ctx);
72 }
73
74 JNIEXPORT void JNICALL
75 FUN(DOM_remove)(JNIEnv *env, jobject self)
76 {
77 fz_context *ctx = get_context(env);
78 fz_xml *me = from_DOM_safe(env, self);
79
80 fz_try(ctx)
81 fz_dom_remove(ctx, me);
82 fz_catch(ctx)
83 jni_rethrow_void(env, ctx);
84 }
85
86 JNIEXPORT jobject JNICALL
87 FUN(DOM_clone)(JNIEnv *env, jobject self)
88 {
89 fz_context *ctx = get_context(env);
90 fz_xml *me = from_DOM_safe(env, self);
91 fz_xml *clone = NULL;
92
93 fz_var(clone);
94
95 fz_try(ctx)
96 clone = fz_dom_clone(ctx, me);
97 fz_catch(ctx)
98 jni_rethrow(env, ctx);
99
100 return to_DOM_safe(ctx, env, clone);
101 }
102
103 JNIEXPORT jobject JNICALL
104 FUN(DOM_parent)(JNIEnv *env, jobject self)
105 {
106 fz_context *ctx = get_context(env);
107 fz_xml *me = from_DOM_safe(env, self);
108 fz_xml *parent = NULL;
109
110 fz_var(parent);
111
112 fz_try(ctx)
113 parent = fz_dom_clone(ctx, me);
114 fz_catch(ctx)
115 jni_rethrow(env, ctx);
116
117 return to_DOM_safe(ctx, env, parent);
118 }
119
120 JNIEXPORT jobject JNICALL
121 FUN(DOM_firstChild)(JNIEnv *env, jobject self)
122 {
123 fz_context *ctx = get_context(env);
124 fz_xml *me = from_DOM_safe(env, self);
125 fz_xml *child = NULL;
126
127 fz_var(child);
128
129 fz_try(ctx)
130 child = fz_dom_first_child(ctx, me);
131 fz_catch(ctx)
132 jni_rethrow(env, ctx);
133
134 return to_DOM_safe(ctx, env, child);
135 }
136
137 JNIEXPORT jobject JNICALL
138 FUN(DOM_next)(JNIEnv *env, jobject self)
139 {
140 fz_context *ctx = get_context(env);
141 fz_xml *me = from_DOM_safe(env, self);
142 fz_xml *next = NULL;
143
144 fz_var(next);
145
146 fz_try(ctx)
147 next = fz_dom_next(ctx, me);
148 fz_catch(ctx)
149 jni_rethrow(env, ctx);
150
151 return to_DOM_safe(ctx, env, next);
152 }
153
154 JNIEXPORT jobject JNICALL
155 FUN(DOM_previous)(JNIEnv *env, jobject self)
156 {
157 fz_context *ctx = get_context(env);
158 fz_xml *me = from_DOM_safe(env, self);
159 fz_xml *prev = NULL;
160
161 fz_var(prev);
162
163 fz_try(ctx)
164 prev = fz_dom_previous(ctx, me);
165 fz_catch(ctx)
166 jni_rethrow(env, ctx);
167
168 return to_DOM_safe(ctx, env, prev);
169 }
170
171 JNIEXPORT jobject JNICALL
172 FUN(DOM_addAttribute)(JNIEnv *env, jobject self, jstring jatt, jstring jval)
173 {
174 fz_context *ctx = get_context(env);
175 fz_xml *me = from_DOM_safe(env, self);
176 const char *att = NULL;
177 const char *val = NULL;
178
179 if (jatt)
180 {
181 att = (*env)->GetStringUTFChars(env, jatt, NULL);
182 if (!att) jni_throw_run(env, "cannot get characters in attribute name");
183 }
184 if (jval)
185 {
186 val = (*env)->GetStringUTFChars(env, jval, NULL);
187 if (!val)
188 {
189 if (jatt)
190 (*env)->ReleaseStringUTFChars(env, jatt, att);
191 jni_throw_run(env, "cannot get characters in attribute value");
192 }
193 }
194
195 fz_try(ctx)
196 fz_dom_add_attribute(ctx, me, att, val);
197 fz_always(ctx)
198 {
199 if (jval)
200 (*env)->ReleaseStringUTFChars(env, jval, val);
201 if (jatt)
202 (*env)->ReleaseStringUTFChars(env, jatt, att);
203 }
204 fz_catch(ctx)
205 jni_rethrow(env, ctx);
206
207 return self;
208 }
209
210 JNIEXPORT jobject JNICALL
211 FUN(DOM_removeAttribute)(JNIEnv *env, jobject self, jstring jatt)
212 {
213 fz_context *ctx = get_context(env);
214 fz_xml *dom = from_DOM_safe(env, self);
215 const char *att = NULL;
216
217 if (!jatt)
218 return NULL;
219
220 att = (*env)->GetStringUTFChars(env, jatt, NULL);
221 if (!att) jni_throw_run(env, "cannot get characters in attribute name");
222
223 fz_try(ctx)
224 fz_dom_remove_attribute(ctx, dom, att);
225 fz_always(ctx)
226 (*env)->ReleaseStringUTFChars(env, jatt, att);
227 fz_catch(ctx)
228 jni_rethrow(env, ctx);
229
230 return self;
231 }
232
233 JNIEXPORT jstring JNICALL
234 FUN(DOM_attribute)(JNIEnv *env, jobject self, jstring jatt)
235 {
236 fz_context *ctx = get_context(env);
237 fz_xml *dom = from_DOM_safe(env, self);
238 const char *att = NULL;
239 const char *val = NULL;
240
241 if (!jatt)
242 return NULL;
243
244 att = (*env)->GetStringUTFChars(env, jatt, NULL);
245 if (!att) jni_throw_run(env, "cannot get characters in attribute name");
246
247 fz_try(ctx)
248 val = fz_dom_attribute(ctx, dom, att);
249 fz_always(ctx)
250 (*env)->ReleaseStringUTFChars(env, jatt, att);
251 fz_catch(ctx)
252 jni_rethrow(env, ctx);
253
254 return to_String_safe(ctx, env, val);
255 }
256
257 JNIEXPORT jobjectArray JNICALL
258 FUN(DOM_attributes)(JNIEnv *env, jobject self)
259 {
260 fz_context *ctx = get_context(env);
261 fz_xml *dom = from_DOM_safe(env, self);
262 const char *att = NULL;
263 const char *val = NULL;
264 jobjectArray jarr;
265 jstring jatt, jval;
266 jobject jobj;
267 size_t i;
268 int n;
269
270 fz_try(ctx)
271 {
272 n = 0;
273 while (1)
274 {
275 val = fz_dom_get_attribute(ctx, dom, n, &att);
276 if (att == NULL)
277 break;
278 n++;
279 }
280 }
281 fz_catch(ctx)
282 jni_rethrow(env, ctx);
283
284 jarr = (*env)->NewObjectArray(env, n, cls_DOMAttribute, NULL);
285 if (!jarr) jni_throw_run(env, "cannot create attribute array");
286
287 for (i = 0; i < (size_t)n; i++)
288 {
289 fz_try(ctx)
290 val = fz_dom_get_attribute(ctx, dom, i, &att);
291 fz_catch(ctx)
292 jni_rethrow(env, ctx);
293
294 jobj = (*env)->NewObject(env, cls_DOMAttribute, mid_DOMAttribute_init);
295 if (!jobj) jni_throw_run(env, "cannot create DOMAttribute");
296 jatt = (*env)->NewStringUTF(env, att);
297 if (!jatt) jni_throw_run(env, "cannot create String from attribute");
298 if (val == NULL)
299 jval = NULL;
300 else {
301 jval = (*env)->NewStringUTF(env, val);
302 if (!jval) jni_throw_run(env, "cannot create String from attribute");
303 }
304 (*env)->SetObjectField(env, jobj, fid_DOMAttribute_attribute, jatt);
305 (*env)->SetObjectField(env, jobj, fid_DOMAttribute_value, jval);
306 (*env)->SetObjectArrayElement(env, jarr, i, jobj);
307 if ((*env)->ExceptionCheck(env))
308 return NULL;
309 }
310
311 return jarr;
312 }
313
314 JNIEXPORT jobject JNICALL
315 FUN(DOM_find)(JNIEnv *env, jobject self, jstring jtag, jstring jatt, jstring jval)
316 {
317 fz_context *ctx = get_context(env);
318 fz_xml *me = from_DOM_safe(env, self);
319 const char *tag = NULL;
320 const char *att = NULL;
321 const char *val = NULL;
322 fz_xml *xml;
323
324 if (jtag)
325 {
326 tag = (*env)->GetStringUTFChars(env, jtag, NULL);
327 if (!tag) jni_throw_run(env, "cannot get characters in tag");
328 }
329 if (jatt)
330 {
331 att = (*env)->GetStringUTFChars(env, jatt, NULL);
332 if (!att)
333 {
334 if (jtag)
335 (*env)->ReleaseStringUTFChars(env, jtag, tag);
336 jni_throw_run(env, "cannot get characters in attribute name");
337 }
338 }
339 if (jval)
340 {
341 val = (*env)->GetStringUTFChars(env, jval, NULL);
342 if (!val)
343 {
344 if (jatt)
345 (*env)->ReleaseStringUTFChars(env, jatt, att);
346 if (jtag)
347 (*env)->ReleaseStringUTFChars(env, jtag, tag);
348 jni_throw_run(env, "cannot get characters in attribute value");
349 }
350 }
351
352 fz_try(ctx)
353 xml = fz_dom_find(ctx, me, tag, att, val);
354 fz_always(ctx)
355 {
356 if (jval)
357 (*env)->ReleaseStringUTFChars(env, jval, val);
358 if (jatt)
359 (*env)->ReleaseStringUTFChars(env, jatt, att);
360 if (jtag)
361 (*env)->ReleaseStringUTFChars(env, jtag, tag);
362 }
363 fz_catch(ctx)
364 jni_rethrow(env, ctx);
365
366 return to_DOM_safe(ctx, env, xml);
367 }
368
369 JNIEXPORT jobject JNICALL
370 FUN(DOM_findNext)(JNIEnv *env, jobject self, jstring jtag, jstring jatt, jstring jval)
371 {
372 fz_context *ctx = get_context(env);
373 fz_xml *me = from_DOM_safe(env, self);
374 const char *tag = NULL;
375 const char *att = NULL;
376 const char *val = NULL;
377 fz_xml *xml;
378
379 if (jtag)
380 {
381 tag = (*env)->GetStringUTFChars(env, jtag, NULL);
382 if (!tag) jni_throw_run(env, "cannot get characters in tag");
383 }
384 if (jatt)
385 {
386 att = (*env)->GetStringUTFChars(env, jatt, NULL);
387 if (!att)
388 {
389 if (jtag)
390 (*env)->ReleaseStringUTFChars(env, jtag, tag);
391 jni_throw_run(env, "cannot get characters in attribute name");
392 }
393 }
394 if (jval)
395 {
396 val = (*env)->GetStringUTFChars(env, jval, NULL);
397 if (!val)
398 {
399 if (jatt)
400 (*env)->ReleaseStringUTFChars(env, jatt, att);
401 if (jtag)
402 (*env)->ReleaseStringUTFChars(env, jtag, tag);
403 jni_throw_run(env, "cannot get characters in attribute value");
404 }
405 }
406
407 fz_try(ctx)
408 xml = fz_dom_find_next(ctx, me, tag, att, val);
409 fz_always(ctx)
410 {
411 if (jval)
412 (*env)->ReleaseStringUTFChars(env, jval, val);
413 if (jatt)
414 (*env)->ReleaseStringUTFChars(env, jatt, att);
415 if (jtag)
416 (*env)->ReleaseStringUTFChars(env, jtag, tag);
417 }
418 fz_catch(ctx)
419 jni_rethrow(env, ctx);
420
421 return to_DOM_safe(ctx, env, xml);
422 }
423
424 JNIEXPORT jobject JNICALL
425 FUN(DOM_body)(JNIEnv *env, jobject self)
426 {
427 fz_context *ctx = get_context(env);
428 fz_xml *dom = from_DOM_safe(env, self);
429
430 return to_DOM_safe(ctx, env, fz_dom_body(ctx, dom));
431 }
432
433 JNIEXPORT jobject JNICALL
434 FUN(DOM_document)(JNIEnv *env, jobject self)
435 {
436 fz_context *ctx = get_context(env);
437 fz_xml *dom = from_DOM_safe(env, self);
438
439 return to_DOM_safe(ctx, env, fz_dom_document_element(ctx, dom));
440 }
441
442 JNIEXPORT jobject JNICALL
443 FUN(DOM_createTextNode)(JNIEnv *env, jobject self, jstring jtext)
444 {
445 fz_context *ctx = get_context(env);
446 fz_xml *dom = from_DOM_safe(env, self);
447 const char *text = NULL;
448 fz_xml *elt;
449
450 if (!jtext)
451 return NULL;
452
453 text = (*env)->GetStringUTFChars(env, jtext, NULL);
454 if (!text) jni_throw_run(env, "cannot get characters in text string");
455
456 fz_try(ctx)
457 elt = fz_dom_create_text_node(ctx, dom, text);
458 fz_always(ctx)
459 (*env)->ReleaseStringUTFChars(env, jtext, text);
460 fz_catch(ctx)
461 jni_rethrow(env, ctx);
462
463 return to_DOM_safe(ctx, env, elt);
464 }
465
466 JNIEXPORT jobject JNICALL
467 FUN(DOM_createElement)(JNIEnv *env, jobject self, jstring jtag)
468 {
469 fz_context *ctx = get_context(env);
470 fz_xml *dom = from_DOM_safe(env, self);
471 const char *tag = NULL;
472 fz_xml *elt;
473
474 if (!jtag)
475 return NULL;
476
477 tag = (*env)->GetStringUTFChars(env, jtag, NULL);
478 if (!tag) jni_throw_run(env, "cannot get characters in tag");
479
480 fz_try(ctx)
481 elt = fz_dom_create_element(ctx, dom, tag);
482 fz_always(ctx)
483 (*env)->ReleaseStringUTFChars(env, jtag, tag);
484 fz_catch(ctx)
485 jni_rethrow(env, ctx);
486
487 return to_DOM_safe(ctx, env, elt);
488 }