OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [native/] [jni/] [xmlj/] [xmlj_dom.c] - Blame information for rev 774

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 774 jeremybenn
/* xmlj_dom.c -
2
   Copyright (C) 2004 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
#include "xmlj_dom.h"
39
#include "xmlj_error.h"
40
#include "xmlj_io.h"
41
#include "xmlj_node.h"
42
#include "xmlj_sax.h"
43
#include "xmlj_util.h"
44
 
45
#include <sys/types.h>
46
#include <sys/stat.h>
47
#include <fcntl.h>
48
#include <unistd.h>
49
 
50
JNIEnv *dom_cb_env;
51
jobject dom_cb_obj;
52
 
53
typedef struct
54
{
55
  int index;
56
  int count;
57
  xmlNodePtr node;
58
}
59
xmljHashScanData;
60
 
61
/* Prototypes for local functions */
62
 
63
void
64
xmljAddAttribute (xmlNodePtr node, xmlAttrPtr attr);
65
 
66
void
67
xmljHashScanner (void *payload, void *vdata, xmlChar *name);
68
 
69
xmlChar *
70
xmljGetNodeValue (xmlNodePtr node);
71
 
72
/*
73
 * Determines whether a child node is suitable for insertion in the list of
74
 * children for a given parent node.
75
 * Returns 0 on success, a DOMException code otherwise.
76
 */
77
void
78
xmljValidateChildNode (JNIEnv *env, xmlNodePtr parent, xmlNodePtr child)
79
{
80
  xmlNodePtr cur;
81
 
82
  if (child == NULL || parent == NULL)
83
    {
84
      xmljThrowDOMException (env, 8, NULL); /* NOT_FOUND_ERR */
85
      return;
86
    }
87
  if (child->doc != parent->doc)
88
    {
89
      xmljThrowDOMException (env, 4, NULL); /* WRONG_DOCUMENT_ERR */
90
      return;
91
    }
92
  /* Check that new parent is of an allowed type */
93
  switch (parent->type)
94
    {
95
    case XML_CDATA_SECTION_NODE:
96
    case XML_COMMENT_NODE:
97
    case XML_TEXT_NODE:
98
    case XML_ENTITY_NODE:
99
    case XML_ENTITY_REF_NODE:
100
    case XML_NOTATION_NODE:
101
    case XML_PI_NODE:
102
      /* these can't have any children */
103
      /* HIERARCHY_REQUEST_ERR */
104
      xmljThrowDOMException (env, 3, "parent type does not allow children");
105
      return;
106
    case XML_ATTRIBUTE_NODE:
107
      if (child->type != XML_TEXT_NODE &&
108
          child->type != XML_ENTITY_REF_NODE)
109
        {
110
          /* HIERARCHY_REQUEST_ERR */
111
          xmljThrowDOMException (env, 3, "attributes may only contain text or entity reference nodes");
112
          return;
113
        }
114
      break;
115
    case XML_DOCUMENT_FRAG_NODE:
116
    case XML_ELEMENT_NODE:
117
      if (child->type == XML_DTD_NODE ||
118
          child->type == XML_DOCUMENT_TYPE_NODE ||
119
          child->type == XML_ENTITY_NODE ||
120
          child->type == XML_NOTATION_NODE ||
121
          child->type == XML_PI_NODE)
122
        {
123
          /* HIERARCHY_REQUEST_ERR */
124
          xmljThrowDOMException (env, 3, "parent type does not allow child of this type");
125
          return;
126
        }
127
      /* fall through */
128
    default:
129
      if (child->type == XML_ATTRIBUTE_NODE ||
130
          child->type == XML_DOCUMENT_NODE ||
131
          child->type == XML_DOCUMENT_FRAG_NODE)
132
        {
133
          /* HIERARCHY_REQUEST_ERR */
134
          xmljThrowDOMException (env, 3, "node type may not be a child");
135
          return;
136
        }
137
      /* TODO others? */
138
    }
139
  /* Check that new parent is not self or an ancestor */
140
  for (cur = parent; cur != NULL; cur = cur->parent)
141
    {
142
      if (cur == child)
143
        {
144
          /* HIERARCHY_REQUEST_ERR */
145
          xmljThrowDOMException (env, 3, "child cannot be an ancestor of itself");
146
          return;
147
        }
148
    }
149
  /* Check that new parent does not add a second doctype or root element
150
   * to a document parent */
151
  if (parent->type == XML_DOCUMENT_NODE)
152
    {
153
      cur = parent->children;
154
      while (cur != NULL)
155
        {
156
          if (cur->type == XML_DTD_NODE ||
157
              cur->type == XML_DOCUMENT_TYPE_NODE ||
158
              (cur->type == XML_ELEMENT_NODE &&
159
               parent->type == XML_DOCUMENT_NODE))
160
            {
161
              if (child->type == cur->type && child != cur)
162
                {
163
                  /* HIERARCHY_REQUEST_ERR */
164
                  xmljThrowDOMException (env, 3, "cannot add a second doctype or root element");
165
                  return;
166
                }
167
            }
168
          cur = cur->next;
169
        }
170
    }
171
}
172
 
173
/*
174
 * Adds the specified attribute node to the list of attributes for the given
175
 * element.
176
 */
177
void
178
xmljAddAttribute (xmlNodePtr node, xmlAttrPtr attr)
179
{
180
  xmlAttrPtr cur = node->properties;
181
 
182
  if (cur == NULL)
183
    {
184
      node->properties = attr;
185
      attr->prev = NULL;
186
      attr->next = NULL;
187
      attr->parent = node;
188
      attr->doc = node->doc;
189
    }
190
  else
191
    {
192
      while (cur->next != NULL)
193
        {
194
          cur = cur->next;
195
        }
196
      cur->next = attr;
197
      attr->prev = cur;
198
      attr->next = NULL;
199
      attr->parent = node;
200
      attr->doc = node->doc;
201
    }
202
}
203
 
204
/* -- GnomeAttr -- */
205
 
206
JNIEXPORT jboolean JNICALL
207
Java_gnu_xml_libxmlj_dom_GnomeAttr_getSpecified (JNIEnv * env, jobject self)
208
{
209
  xmlAttrPtr attr;
210
 
211
  attr = (xmlAttrPtr) xmljGetNodeID (env, self);
212
  return (attr->atype != 0);
213
}
214
 
215
JNIEXPORT jstring JNICALL
216
Java_gnu_xml_libxmlj_dom_GnomeAttr_getValue (JNIEnv * env, jobject self)
217
{
218
  xmlNodePtr node;
219
  xmlChar *text;
220
  jstring ret;
221
 
222
  node = xmljGetNodeID (env, self);
223
  text = xmlNodeGetContent (node);
224
  ret = xmljNewString (env, (const xmlChar *) text);
225
  if (text != NULL)
226
    {
227
      xmlFree (text);
228
    }
229
  return ret;
230
}
231
 
232
JNIEXPORT void JNICALL
233
Java_gnu_xml_libxmlj_dom_GnomeAttr_setValue (JNIEnv * env,
234
                                             jobject self, jstring value)
235
{
236
  xmlNodePtr node;
237
  const xmlChar *s_value;
238
 
239
  node = xmljGetNodeID (env, self);
240
  s_value = xmljGetStringChars (env, value);
241
  xmlNodeSetContent (node, s_value);
242
}
243
 
244
JNIEXPORT jboolean JNICALL
245
Java_gnu_xml_libxmlj_dom_GnomeAttr_xmljIsId (JNIEnv * env, jobject self)
246
{
247
  xmlAttrPtr attr;
248
 
249
  attr = (xmlAttrPtr) xmljGetNodeID (env, self);
250
  return (attr->atype == XML_ATTRIBUTE_ID);
251
}
252
 
253
/* -- GnomeDocument -- */
254
 
255
JNIEXPORT void JNICALL
256
Java_gnu_xml_libxmlj_dom_GnomeDocument_free (JNIEnv * env,
257
                                             jobject self
258
                                             __attribute__ ((__unused__)),
259
                                             jobject id)
260
{
261
  xmlDocPtr doc;
262
 
263
  doc = (xmlDocPtr) xmljAsPointer (env, id);
264
  xmljFreeDoc (env, doc);
265
  xmlFree (doc);
266
}
267
 
268
JNIEXPORT jobject JNICALL
269
Java_gnu_xml_libxmlj_dom_GnomeDocument_getDoctype (JNIEnv * env, jobject self)
270
{
271
  xmlDocPtr doc;
272
  xmlDtdPtr dtd;
273
 
274
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
275
  dtd = doc->extSubset;
276
  if (dtd == NULL)
277
    {
278
      dtd = doc->intSubset;
279
    }
280
  return xmljGetNodeInstance (env, (xmlNodePtr) dtd);
281
}
282
 
283
JNIEXPORT jobject JNICALL
284
Java_gnu_xml_libxmlj_dom_GnomeDocument_getDocumentElement (JNIEnv * env,
285
                                                           jobject self)
286
{
287
  xmlDocPtr doc;
288
 
289
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
290
  return xmljGetNodeInstance (env, xmlDocGetRootElement (doc));
291
}
292
 
293
JNIEXPORT jobject JNICALL
294
Java_gnu_xml_libxmlj_dom_GnomeDocument_createDocumentType (JNIEnv * env,
295
                                                           jobject self,
296
                                                           jstring name,
297
                                                           jstring publicId,
298
                                                           jstring systemId)
299
{
300
  xmlDocPtr doc;
301
  xmlDtdPtr dtd;
302
 
303
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
304
  dtd = xmlNewDtd (doc,
305
                   xmljGetStringChars (env, name),
306
                   xmljGetStringChars (env, publicId),
307
                   xmljGetStringChars (env, systemId));
308
  return xmljGetNodeInstance (env, (xmlNodePtr) dtd);
309
}
310
 
311
JNIEXPORT jobject JNICALL
312
Java_gnu_xml_libxmlj_dom_GnomeDocument_createDocumentFragment (JNIEnv * env,
313
                                                               jobject self)
314
{
315
  xmlDocPtr doc;
316
 
317
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
318
  return xmljGetNodeInstance (env, xmlNewDocFragment (doc));
319
}
320
 
321
JNIEXPORT jobject JNICALL
322
Java_gnu_xml_libxmlj_dom_GnomeDocument_createTextNode (JNIEnv * env,
323
                                                       jobject self,
324
                                                       jstring data)
325
{
326
  xmlDocPtr doc;
327
  xmlNodePtr text;
328
  const xmlChar *s_data;
329
 
330
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
331
  s_data = xmljGetStringChars (env, data);
332
  text = xmlNewDocText (doc, s_data);
333
  return xmljGetNodeInstance (env, text);
334
}
335
 
336
JNIEXPORT jobject JNICALL
337
Java_gnu_xml_libxmlj_dom_GnomeDocument_createComment (JNIEnv * env,
338
                                                      jobject self,
339
                                                      jstring data)
340
{
341
  xmlDocPtr doc;
342
  xmlNodePtr comment;
343
  const xmlChar *s_data;
344
 
345
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
346
  s_data = xmljGetStringChars (env, data);
347
  comment = xmlNewDocComment (doc, s_data);
348
  return xmljGetNodeInstance (env, comment);
349
}
350
 
351
JNIEXPORT jobject JNICALL
352
Java_gnu_xml_libxmlj_dom_GnomeDocument_createCDATASection (JNIEnv * env,
353
                                                           jobject self,
354
                                                           jstring data)
355
{
356
  xmlDocPtr doc;
357
  xmlNodePtr cdata;
358
  const xmlChar *s_data;
359
  int len;
360
 
361
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
362
  s_data = xmljGetStringChars (env, data);
363
  len = xmlStrlen (s_data);
364
  cdata = xmlNewCDataBlock (doc, s_data, len);
365
  return xmljGetNodeInstance (env, cdata);
366
}
367
 
368
JNIEXPORT jobject JNICALL
369
Java_gnu_xml_libxmlj_dom_GnomeDocument_createProcessingInstruction (JNIEnv *
370
                                                                    env,
371
                                                                    jobject
372
                                                                    self,
373
                                                                    jstring
374
                                                                    target,
375
                                                                    jstring
376
                                                                    data)
377
{
378
  xmlDocPtr doc;
379
  xmlNodePtr pi;
380
  const xmlChar *s_target;
381
  const xmlChar *s_data;
382
 
383
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
384
  s_target = xmljGetStringChars (env, target);
385
  s_data = xmljGetStringChars (env, data);
386
  pi = xmlNewPI (s_target, s_data);
387
  pi->doc = doc;
388
  return xmljGetNodeInstance (env, pi);
389
}
390
 
391
JNIEXPORT jobject JNICALL
392
Java_gnu_xml_libxmlj_dom_GnomeDocument_createEntityReference (JNIEnv * env,
393
                                                              jobject self,
394
                                                              jstring name)
395
{
396
  xmlDocPtr doc;
397
  xmlNodePtr ref;
398
  const xmlChar *s_name;
399
 
400
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
401
  s_name = xmljGetStringChars (env, name);
402
  ref = xmlNewReference (doc, s_name);
403
  return xmljGetNodeInstance (env, ref);
404
}
405
 
406
JNIEXPORT jobject JNICALL
407
Java_gnu_xml_libxmlj_dom_GnomeDocument_xmljImportNode (JNIEnv * env,
408
                                                       jobject self,
409
                                                       jobject importedNode,
410
                                                       jboolean deep)
411
{
412
  xmlDocPtr doc;
413
  xmlNodePtr node;
414
 
415
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
416
  node = xmljGetNodeID (env, importedNode);
417
  if (node == NULL)
418
    {
419
      xmljThrowDOMException (env, 8, NULL); /* NOT_FOUND_ERR */
420
      return NULL;
421
    }
422
  if (node->type == XML_DOCUMENT_NODE ||
423
      node->type == XML_DOCUMENT_TYPE_NODE)
424
    {
425
      xmljThrowDOMException (env, 9, NULL); /* NOT_SUPPORTED_ERR */
426
      return NULL;
427
    }
428
  node = xmlDocCopyNode (node, doc, deep);
429
  return xmljGetNodeInstance (env, node);
430
}
431
 
432
JNIEXPORT jobject JNICALL
433
Java_gnu_xml_libxmlj_dom_GnomeDocument_createElementNS (JNIEnv * env,
434
                                                        jobject self,
435
                                                        jstring uri,
436
                                                        jstring qName)
437
{
438
  xmlDocPtr doc;
439
  xmlNodePtr element;
440
  xmlNsPtr ns = NULL;
441
  const xmlChar *s_uri;
442
  const xmlChar *s_qName;
443
  const xmlChar *s_prefix;
444
  const xmlChar *s_localName;
445
 
446
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
447
  s_qName = xmljGetStringChars (env, qName);
448
  if (xmlValidateQName (s_qName, 0))
449
    {
450
      xmljThrowDOMException (env, 5, NULL); /* INVALID_CHARACTER_ERR */
451
      return NULL;
452
    }
453
  if (uri != NULL)
454
    {
455
      s_uri = xmljGetStringChars (env, uri);
456
      s_prefix = xmljGetPrefix (s_qName);
457
      s_localName = xmljGetLocalName (s_qName);
458
      ns = xmlNewNs ((xmlNodePtr) doc, s_uri, s_prefix);
459
    }
460
  element = xmlNewDocNode (doc, ns, s_qName, NULL);
461
  return xmljGetNodeInstance (env, element);
462
}
463
 
464
JNIEXPORT jobject JNICALL
465
Java_gnu_xml_libxmlj_dom_GnomeDocument_createAttributeNS (JNIEnv * env,
466
                                                          jobject self,
467
                                                          jstring uri,
468
                                                          jstring qName)
469
{
470
  xmlDocPtr doc;
471
  xmlNodePtr attr;
472
  xmlNsPtr ns = NULL;
473
  const xmlChar *s_uri;
474
  const xmlChar *s_qName;
475
  const xmlChar *s_prefix;
476
  const xmlChar *s_localName;
477
 
478
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
479
  s_qName = xmljGetStringChars (env, qName);
480
  if (xmlValidateQName (s_qName, 0))
481
    {
482
      xmljThrowDOMException (env, 5, NULL); /* INVALID_CHARACTER_ERR */
483
      return NULL;
484
    }
485
  if (uri != NULL)
486
    {
487
      s_uri = xmljGetStringChars (env, uri);
488
      s_prefix = xmljGetPrefix (s_qName);
489
      s_localName = xmljGetLocalName (s_qName);
490
      ns = xmlNewNs ((xmlNodePtr) doc, s_uri, s_prefix);
491
    }
492
  attr = (xmlNodePtr) xmlNewNsProp ((xmlNodePtr) doc, ns, s_qName, NULL);
493
  attr->parent = NULL;
494
  return xmljGetNodeInstance (env, attr);
495
}
496
 
497
JNIEXPORT jobject JNICALL
498
Java_gnu_xml_libxmlj_dom_GnomeDocument_xmljGetElementById (JNIEnv * env,
499
                                                           jobject self,
500
                                                           jstring elementId)
501
{
502
  xmlDocPtr doc;
503
  xmlNodePtr ctx, tmp;
504
  xmlAttrPtr attr;
505
  const xmlChar *id;
506
  const xmlChar *val;
507
 
508
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
509
  id = xmljGetStringChars (env, elementId);
510
 
511
  ctx = xmlDocGetRootElement (doc);
512
  while (ctx && ctx != (xmlNodePtr) doc)
513
    {
514
      if (ctx->type == XML_ELEMENT_NODE)
515
        {
516
          for (attr = ctx->properties; attr;
517
               attr = (xmlAttrPtr) attr->next)
518
            {
519
              if (xmlIsID (doc, ctx, attr))
520
                {
521
                  val = xmlGetProp (ctx, attr->name);
522
                  if (val && xmlStrEqual (id, val))
523
                    {
524
                      return xmljGetNodeInstance (env, ctx);
525
                    }
526
                }
527
            }
528
        }
529
      if (ctx->children)
530
        {
531
          ctx = ctx->children;
532
        }
533
      else
534
        {
535
          tmp = ctx->next;
536
          if (tmp)
537
            {
538
              ctx = tmp;
539
            }
540
          else
541
            {
542
              do
543
                {
544
                  tmp = ctx->parent;
545
                  if (!tmp)
546
                    {
547
                      return NULL;
548
                    }
549
                  ctx = tmp;
550
                  tmp = ctx->next;
551
                }
552
              while (!tmp);
553
              ctx = tmp;
554
            }
555
        }
556
    }
557
  return NULL;
558
}
559
 
560
JNIEXPORT jstring JNICALL
561
Java_gnu_xml_libxmlj_dom_GnomeDocument_getInputEncoding (JNIEnv * env,
562
                                                         jobject self)
563
{
564
  xmlDocPtr doc;
565
 
566
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
567
  if (doc->encoding)
568
    {
569
      return xmljNewString (env, doc->encoding);
570
    }
571
  switch (doc->charset)
572
    {
573
    case XML_CHAR_ENCODING_ASCII:
574
      return xmljNewString (env, BAD_CAST "US-ASCII");
575
    case XML_CHAR_ENCODING_UTF16LE:
576
      return xmljNewString (env, BAD_CAST "UTF-16LE");
577
    case XML_CHAR_ENCODING_UTF16BE:
578
      return xmljNewString (env, BAD_CAST "UTF-16BE");
579
    case XML_CHAR_ENCODING_8859_1:
580
      return xmljNewString (env, BAD_CAST "ISO-8859-1");
581
      /* TODO others */
582
    default:
583
      return xmljNewString (env, BAD_CAST "UTF-8");
584
    }
585
}
586
 
587
JNIEXPORT jstring JNICALL
588
Java_gnu_xml_libxmlj_dom_GnomeDocument_getXmlEncoding (JNIEnv * env,
589
                                                       jobject self)
590
{
591
  xmlDocPtr doc;
592
 
593
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
594
  return (doc->encoding == NULL) ?
595
    xmljNewString (env, BAD_CAST "UTF-8") :
596
    xmljNewString (env, doc->encoding);
597
}
598
 
599
JNIEXPORT jboolean JNICALL
600
Java_gnu_xml_libxmlj_dom_GnomeDocument_getXmlStandalone (JNIEnv * env,
601
                                                         jobject self)
602
{
603
  xmlDocPtr doc;
604
 
605
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
606
  return doc->standalone;
607
}
608
 
609
JNIEXPORT void JNICALL
610
Java_gnu_xml_libxmlj_dom_GnomeDocument_setXmlStandalone (JNIEnv * env,
611
                                                         jobject self,
612
                                                         jboolean xmlStandalone)
613
{
614
  xmlDocPtr doc;
615
 
616
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
617
  doc->standalone = xmlStandalone;
618
}
619
 
620
JNIEXPORT jstring JNICALL
621
Java_gnu_xml_libxmlj_dom_GnomeDocument_getXmlVersion (JNIEnv * env,
622
                                                      jobject self)
623
{
624
  xmlDocPtr doc;
625
 
626
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
627
  return (doc->version == NULL) ?
628
    xmljNewString (env, BAD_CAST "1.0") :
629
    xmljNewString (env, doc->version);
630
}
631
 
632
JNIEXPORT void JNICALL
633
Java_gnu_xml_libxmlj_dom_GnomeDocument_setXmlVersion (JNIEnv * env,
634
                                                      jobject self,
635
                                                      jstring xmlVersion)
636
{
637
  xmlDocPtr doc;
638
 
639
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
640
  if (xmlVersion == NULL)
641
    {
642
      doc->version = NULL;
643
    }
644
  else
645
    {
646
      const xmlChar *version = xmljGetStringChars (env, xmlVersion);
647
      if (!xmlStrEqual (version, BAD_CAST "1.0") &&
648
          !xmlStrEqual (version, BAD_CAST "1.1"))
649
        {
650
          xmljThrowDOMException (env, 9, NULL); /* NOT_SUPPORTED_ERR */
651
          return;
652
        }
653
      doc->version = version;
654
    }
655
}
656
 
657
JNIEXPORT jstring JNICALL
658
Java_gnu_xml_libxmlj_dom_GnomeDocument_getDocumentURI (JNIEnv * env,
659
                                                       jobject self)
660
{
661
  xmlDocPtr doc;
662
 
663
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
664
  return (doc->name == NULL) ? NULL :
665
    xmljNewString (env, (const xmlChar *) doc->URL);
666
}
667
 
668
JNIEXPORT void JNICALL
669
Java_gnu_xml_libxmlj_dom_GnomeDocument_setDocumentURI (JNIEnv * env,
670
                                                       jobject self,
671
                                                       jstring documentURI)
672
{
673
  xmlDocPtr doc;
674
 
675
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
676
  if (documentURI == NULL)
677
    {
678
      doc->URL = NULL;
679
    }
680
  else
681
    {
682
      doc->URL = xmljGetStringChars (env, documentURI);
683
    }
684
}
685
 
686
JNIEXPORT jobject JNICALL
687
Java_gnu_xml_libxmlj_dom_GnomeDocument_xmljAdoptNode (JNIEnv *env,
688
                                                      jobject self,
689
                                                      jobject jnode)
690
{
691
  xmlDocPtr doc;
692
  xmlNodePtr node;
693
 
694
  doc = (xmlDocPtr) xmljGetNodeID (env, self);
695
  node = xmljGetNodeID (env, jnode);
696
 
697
  if (node == NULL)
698
    {
699
      xmljThrowDOMException (env, 8, NULL); /* NOT_FOUND_ERR */
700
      return NULL;
701
    }
702
  if (node->type == XML_DOCUMENT_NODE ||
703
      node->type == XML_DOCUMENT_TYPE_NODE ||
704
      node->type == XML_ENTITY_NODE ||
705
      node->type == XML_NOTATION_NODE)
706
    {
707
      xmljThrowDOMException (env, 9, NULL); /* NOT_SUPPORTED_ERR */
708
      return NULL;
709
    }
710
  xmlUnlinkNode (node);
711
  node = xmlDocCopyNode (node, doc, 1);
712
  return xmljGetNodeInstance (env, node);
713
}
714
 
715
JNIEXPORT jobject JNICALL
716
Java_gnu_xml_libxmlj_dom_GnomeDocument_renameNode (JNIEnv * env,
717
                                                   jobject self
718
                                                   __attribute__ ((__unused__)),
719
                                                   jobject n
720
                                                   __attribute__ ((__unused__)),
721
                                                   jstring namespaceURI
722
                                                   __attribute__ ((__unused__)),
723
                                                   jstring qName
724
                                                   __attribute__ ((__unused__)))
725
{
726
  xmlNodePtr node;
727
  xmlNsPtr ns;
728
  const xmlChar *s_qName;
729
  const xmlChar *href;
730
  const xmlChar *prefix;
731
  int *len;
732
 
733
  node = xmljGetNodeID (env, n);
734
  if (node == NULL)
735
    {
736
      xmljThrowDOMException (env, 8, NULL); /* NOT_FOUND_ERR */
737
      return NULL;
738
    }
739
  s_qName = xmljGetStringChars (env, qName);
740
  if (xmlValidateQName (s_qName, 0))
741
    {
742
      xmljThrowDOMException (env, 5, NULL); /* INVALID_CHARACTER_ERR */
743
      return NULL;
744
    }
745
  xmlNodeSetName (node, s_qName);
746
 
747
  href = xmljGetStringChars (env, namespaceURI);
748
  len = (int *) malloc (sizeof (int));
749
  prefix = xmlSplitQName3 (s_qName, len);
750
  ns = node->ns;
751
  if (ns == NULL)
752
    {
753
      if (href != NULL)
754
        {
755
          ns = xmlNewNs (node, href, prefix);
756
          xmlSetNs (node, ns);
757
        }
758
    }
759
  else
760
    {
761
      node->ns = NULL;
762
      /*xmlFreeNs (ns); FIXME this can segfault (?) */
763
      if (href != NULL)
764
        {
765
          ns = xmlNewNs (node, href, prefix);
766
          xmlSetNs (node, ns);
767
        }
768
    }
769
  free (len);
770
  return n;
771
}
772
 
773
/* -- GnomeDocumentBuilder -- */
774
 
775
JNIEXPORT jobject JNICALL
776
Java_gnu_xml_libxmlj_dom_GnomeDocumentBuilder_parseStream (JNIEnv * env,
777
                                                           jobject self,
778
                                                           jobject in,
779
                                                           jbyteArray
780
                                                           detectBuffer,
781
                                                           jstring publicId,
782
                                                           jstring systemId,
783
                                                           jstring base,
784
                                                           jboolean validate,
785
                                                           jboolean coalesce,
786
                                                           jboolean
787
                                                           expandEntities,
788
                                                           jboolean
789
                                                           entityResolver,
790
                                                           jboolean
791
                                                           errorHandler)
792
{
793
  xmlDocPtr doc;
794
 
795
  doc = xmljParseDocument(env,
796
                          self,
797
                          in,
798
                          detectBuffer,
799
                          publicId,
800
                          systemId,
801
                          base,
802
                          validate,
803
                          coalesce,
804
                          expandEntities,
805
                          0,
806
                          0,
807
                          entityResolver,
808
                          errorHandler,
809
                          0,
810
                          0,
811
                          1);
812
  return xmljCreateDocument (env, self, doc);
813
}
814
 
815
JNIEXPORT jobject JNICALL
816
Java_gnu_xml_libxmlj_dom_GnomeDocumentBuilder_createDocument
817
(JNIEnv * env,
818
 jobject self,
819
 jstring namespaceURI,
820
 jstring qualifiedName,
821
 jobject doctype)
822
{
823
  xmlDocPtr doc;
824
  xmlNodePtr root;
825
  xmlNsPtr ns;
826
  const xmlChar *href;
827
  const xmlChar *prefix;
828
  const xmlChar *qName;
829
 
830
  qName = xmljGetStringChars (env, qualifiedName);
831
  href = xmljGetStringChars (env, namespaceURI);
832
  if (qName == NULL)
833
    {
834
      prefix = NULL;
835
    }
836
  else
837
    {
838
      int *len;
839
 
840
      len = (int *) malloc (sizeof (int));
841
      prefix = xmlSplitQName3 (qName, len);
842
      free (len);
843
    }
844
 
845
  /* Create the document node */
846
  doc = xmlNewDoc (BAD_CAST "1.0");
847
 
848
  /* doctype */
849
  if (doctype != NULL)
850
    {
851
      jclass cls;
852
      jmethodID method;
853
      jstring ret;
854
      const xmlChar *name;
855
      const xmlChar *publicId;
856
      const xmlChar *systemId;
857
      const xmlChar *internalSubset;
858
      xmlDtdPtr dtd;
859
 
860
      cls = (*env)->FindClass (env, "org/w3c/dom/DocumentType");
861
      if (cls == NULL)
862
        {
863
          return NULL;
864
        }
865
      /* name */
866
      method = (*env)->GetMethodID (env, cls, "getName",
867
                                    "()Ljava/lang/String;");
868
      if (method == NULL)
869
        {
870
          return NULL;
871
        }
872
      ret = (jstring) (*env)->CallObjectMethod (env, doctype, method);
873
      name = xmljGetStringChars (env, ret);
874
 
875
      /* publicId */
876
      method = (*env)->GetMethodID (env, cls, "getPublicId",
877
                                    "()Ljava/lang/String;");
878
      if (method == NULL)
879
        {
880
          return NULL;
881
        }
882
      ret = (jstring) (*env)->CallObjectMethod (env, doctype, method);
883
      publicId = xmljGetStringChars (env, ret);
884
 
885
      /* systemId */
886
      method = (*env)->GetMethodID (env, cls, "getSystemId",
887
                                    "()Ljava/lang/String;");
888
      if (method == NULL)
889
        {
890
          return NULL;
891
        }
892
      ret = (jstring) (*env)->CallObjectMethod (env, doctype, method);
893
      systemId = xmljGetStringChars (env, ret);
894
 
895
      /* internalSubset */
896
      method = (*env)->GetMethodID (env, cls, "getInternalSubset",
897
                                    "()Ljava/lang/String;");
898
      if (method == NULL)
899
        {
900
          return NULL;
901
        }
902
      ret = (jstring) (*env)->CallObjectMethod (env, doctype, method);
903
      internalSubset = xmljGetStringChars (env, ret);
904
 
905
      /* TODO notations */
906
      /* TODO entities */
907
      if (internalSubset == NULL)
908
        {
909
          dtd = xmlNewDtd (doc, name, publicId, systemId);
910
        }
911
      else
912
        {
913
          dtd = xmlCreateIntSubset (doc, name, publicId, systemId);
914
          /* TODO parse internal subset? */
915
          xmljThrowDOMException (env, 9, NULL); /* NOT_SUPPORTED_ERR */
916
          return NULL;
917
        }
918
    }
919
 
920
  /* Create the root element */
921
  root = xmlNewNode (NULL, qName);
922
  xmlDocSetRootElement (doc, root);
923
  ns = xmlNewNs (root, href, prefix);
924
  xmlSetNs (root, ns);
925
 
926
  return xmljCreateDocument (env, self, doc);
927
}
928
 
929
/* -- GnomeDocumentType -- */
930
 
931
JNIEXPORT jstring JNICALL
932
Java_gnu_xml_libxmlj_dom_GnomeDocumentType_getPublicId (JNIEnv * env,
933
                                                        jobject self)
934
{
935
  xmlDtdPtr dtd;
936
 
937
  dtd = (xmlDtdPtr) xmljGetNodeID (env, self);
938
  return xmljNewString (env, dtd->ExternalID);
939
}
940
 
941
JNIEXPORT jstring JNICALL
942
Java_gnu_xml_libxmlj_dom_GnomeDocumentType_getSystemId (JNIEnv * env,
943
                                                        jobject self)
944
{
945
  xmlDtdPtr dtd;
946
 
947
  dtd = (xmlDtdPtr) xmljGetNodeID (env, self);
948
  return xmljNewString (env, dtd->SystemID);
949
}
950
 
951
JNIEXPORT jstring JNICALL
952
Java_gnu_xml_libxmlj_dom_GnomeDocumentType_getInternalSubset (JNIEnv * env,
953
                                                             jobject self
954
                                                             __attribute__ ((__unused__)))
955
{
956
  /* TODO */
957
  xmljThrowDOMException (env, 9, NULL); /* NOT_SUPPORTED_ERR */
958
  return NULL;
959
}
960
 
961
/* -- GnomeElement -- */
962
 
963
JNIEXPORT jstring JNICALL
964
Java_gnu_xml_libxmlj_dom_GnomeElement_getAttribute (JNIEnv * env,
965
                                                    jobject self,
966
                                                    jstring name)
967
{
968
  xmlNodePtr node;
969
  const xmlChar *s_name;
970
  const xmlChar *s_value;
971
 
972
  node = xmljGetNodeID (env, self);
973
  s_name = xmljGetStringChars (env, name);
974
  s_value = xmlGetProp (node, s_name);
975
  xmlFree ((xmlChar *) s_name);
976
  return (s_value == NULL) ?
977
    xmljNewString (env, BAD_CAST "") :
978
    xmljNewString (env, s_value);
979
}
980
 
981
JNIEXPORT void JNICALL
982
Java_gnu_xml_libxmlj_dom_GnomeElement_setAttribute (JNIEnv * env,
983
                                                    jobject self,
984
                                                    jstring name,
985
                                                    jstring value)
986
{
987
  xmlNodePtr node;
988
  const xmlChar *s_name;
989
  const xmlChar *s_value;
990
 
991
  node = xmljGetNodeID (env, self);
992
  s_name = xmljGetStringChars (env, name);
993
  if (xmlValidateName (s_name, 0))
994
    {
995
      xmljThrowDOMException (env, 5, NULL); /* INVALID_CHARACTER_ERR */
996
      return;
997
    }
998
  s_value = xmljGetStringChars (env, value);
999
  xmlSetProp (node, s_name, s_value);
1000
}
1001
 
1002
JNIEXPORT jobject JNICALL
1003
Java_gnu_xml_libxmlj_dom_GnomeElement_getAttributeNode (JNIEnv * env,
1004
                                                        jobject self,
1005
                                                        jstring name)
1006
{
1007
  xmlNodePtr node;
1008
  const xmlChar *s_name;
1009
  xmlAttrPtr attr;
1010
 
1011
  node = xmljGetNodeID (env, self);
1012
  s_name = xmljGetStringChars (env, name);
1013
  attr = xmlHasProp (node, s_name);
1014
  if (attr == NULL)
1015
    {
1016
      return NULL;
1017
    }
1018
  xmlFree ((xmlChar *) s_name);
1019
  return xmljGetNodeInstance (env, (xmlNodePtr) attr);
1020
}
1021
 
1022
JNIEXPORT jobject JNICALL
1023
Java_gnu_xml_libxmlj_dom_GnomeElement_setAttributeNode (JNIEnv * env,
1024
                                                        jobject self,
1025
                                                        jobject newAttr)
1026
{
1027
  xmlNodePtr node;
1028
  xmlAttrPtr new_attr;
1029
  xmlAttrPtr old_attr;
1030
 
1031
  node = xmljGetNodeID (env, self);
1032
  new_attr = (xmlAttrPtr) xmljGetNodeID (env, newAttr);
1033
  if (new_attr->parent != NULL)
1034
    {
1035
      xmljThrowDOMException (env, 10, NULL); /* INUSE_ATTRIBUTE_ERR */
1036
      return NULL;
1037
    }
1038
  if (new_attr->doc != node->doc)
1039
    {
1040
      xmljThrowDOMException (env, 4, NULL); /* WRONG_DOCUMENT_ERR */
1041
      return NULL;
1042
    }
1043
  old_attr = xmlHasProp (node, new_attr->name);
1044
  if (old_attr)
1045
    {
1046
      xmlUnlinkNode ((xmlNodePtr) old_attr);
1047
    }
1048
  xmljAddAttribute (node, new_attr);
1049
  return xmljGetNodeInstance (env, (xmlNodePtr) old_attr);
1050
}
1051
 
1052
JNIEXPORT jobject JNICALL
1053
Java_gnu_xml_libxmlj_dom_GnomeElement_removeAttributeNode (JNIEnv * env,
1054
                                                           jobject self
1055
                                                           __attribute__ ((__unused__)),
1056
                                                           jobject oldAttr)
1057
{
1058
  xmlNodePtr attr;
1059
 
1060
  attr = xmljGetNodeID (env, oldAttr);
1061
  xmlUnlinkNode (attr);
1062
  return oldAttr;
1063
}
1064
 
1065
JNIEXPORT jstring JNICALL
1066
Java_gnu_xml_libxmlj_dom_GnomeElement_getAttributeNS (JNIEnv * env,
1067
                                                      jobject self,
1068
                                                      jstring uri,
1069
                                                      jstring localName)
1070
{
1071
  xmlNodePtr node;
1072
  const xmlChar *s_uri;
1073
  const xmlChar *s_localName;
1074
  const xmlChar *s_value;
1075
 
1076
  node = xmljGetNodeID (env, self);
1077
  s_localName = xmljGetStringChars (env, localName);
1078
  if (uri == NULL)
1079
    {
1080
      s_value = xmlGetNoNsProp (node, s_localName);
1081
    }
1082
  else
1083
    {
1084
      s_uri = xmljGetStringChars (env, uri);
1085
      s_value = xmlGetNsProp (node, s_localName, s_uri);
1086
      xmlFree ((xmlChar *) s_uri);
1087
    }
1088
  xmlFree ((xmlChar *) s_localName);
1089
  return (s_value == NULL) ?
1090
    xmljNewString (env, BAD_CAST "") :
1091
    xmljNewString (env, s_value);
1092
}
1093
 
1094
JNIEXPORT void JNICALL
1095
Java_gnu_xml_libxmlj_dom_GnomeElement_setAttributeNS (JNIEnv * env,
1096
                                                      jobject self,
1097
                                                      jstring uri,
1098
                                                      jstring qName,
1099
                                                      jstring value)
1100
{
1101
  xmlNodePtr node;
1102
  xmlNsPtr ns;
1103
  const xmlChar *s_uri;
1104
  const xmlChar *s_qName;
1105
  const xmlChar *s_prefix;
1106
  const xmlChar *s_localName;
1107
  const xmlChar *s_value;
1108
 
1109
  node = xmljGetNodeID (env, self);
1110
  s_qName = xmljGetStringChars (env, qName);
1111
  if (xmlValidateQName (s_qName, 0))
1112
    {
1113
      xmljThrowDOMException (env, 5, NULL); /* INVALID_CHARACTER_ERR */
1114
      return;
1115
    }
1116
  s_value = xmljGetStringChars (env, value);
1117
  if (uri == NULL)
1118
    {
1119
      xmlSetProp (node, s_qName, s_value);
1120
    }
1121
  else
1122
    {
1123
      s_prefix = xmljGetPrefix (s_qName);
1124
      s_localName = xmljGetLocalName (s_qName);
1125
      s_uri = xmljGetStringChars (env, uri);
1126
      ns = xmlNewNs (node, s_uri, s_prefix);
1127
      xmlSetNsProp (node, ns, s_localName, s_value);
1128
    }
1129
}
1130
 
1131
JNIEXPORT jobject JNICALL
1132
Java_gnu_xml_libxmlj_dom_GnomeElement_getAttributeNodeNS (JNIEnv * env,
1133
                                                          jobject self,
1134
                                                          jstring uri,
1135
                                                          jstring localName)
1136
{
1137
  xmlNodePtr node;
1138
  xmlAttrPtr attr;
1139
  const xmlChar *s_uri;
1140
  const xmlChar *s_localName;
1141
 
1142
  node = xmljGetNodeID (env, self);
1143
  attr = node->properties;
1144
  s_uri = xmljGetStringChars (env, uri);
1145
  s_localName = xmljGetStringChars (env, localName);
1146
  while (attr != NULL)
1147
    {
1148
      if (uri == NULL)
1149
        {
1150
          if (xmljMatch (s_localName, (xmlNodePtr) attr))
1151
            break;
1152
        }
1153
      else
1154
        {
1155
          if (xmljMatchNS (s_uri, s_localName, (xmlNodePtr) attr))
1156
            break;
1157
        }
1158
      attr = attr->next;
1159
    }
1160
  xmlFree ((xmlChar *) s_uri);
1161
  xmlFree ((xmlChar *) s_localName);
1162
  return xmljGetNodeInstance (env, (xmlNodePtr) attr);
1163
}
1164
 
1165
JNIEXPORT jobject JNICALL
1166
Java_gnu_xml_libxmlj_dom_GnomeElement_setAttributeNodeNS (JNIEnv * env,
1167
                                                          jobject self,
1168
                                                          jobject newAttr)
1169
{
1170
  xmlNodePtr node;
1171
  xmlAttrPtr new_attr;
1172
  xmlAttrPtr old_attr;
1173
  const xmlChar *uri;
1174
 
1175
  node = xmljGetNodeID (env, self);
1176
  new_attr = (xmlAttrPtr) xmljGetNodeID (env, newAttr);
1177
  if (new_attr->parent != NULL)
1178
    {
1179
      xmljThrowDOMException (env, 10, NULL); /* INUSE_ATTRIBUTE_ERR */
1180
      return NULL;
1181
    }
1182
  if (new_attr->doc != node->doc)
1183
    {
1184
      xmljThrowDOMException (env, 4, NULL); /* WRONG_DOCUMENT_ERR */
1185
      return NULL;
1186
    }
1187
  uri = (new_attr->ns != NULL) ? new_attr->ns->href : NULL;
1188
  old_attr = xmlHasNsProp (node, new_attr->name, uri);
1189
  if (old_attr)
1190
    {
1191
      xmlUnlinkNode ((xmlNodePtr) old_attr);
1192
    }
1193
  xmljAddAttribute (node, new_attr);
1194
  return xmljGetNodeInstance (env, (xmlNodePtr) old_attr);
1195
}
1196
 
1197
JNIEXPORT jboolean JNICALL
1198
Java_gnu_xml_libxmlj_dom_GnomeElement_hasAttribute (JNIEnv * env,
1199
                                                    jobject self,
1200
                                                    jstring name)
1201
{
1202
  xmlNodePtr node;
1203
  const xmlChar *s_name;
1204
  const xmlChar *s_value;
1205
 
1206
  node = xmljGetNodeID (env, self);
1207
  s_name = xmljGetStringChars (env, name);
1208
  s_value = xmlGetProp (node, s_name);
1209
  xmlFree ((xmlChar *) s_name);
1210
  return (s_value != NULL);
1211
}
1212
 
1213
JNIEXPORT jboolean JNICALL
1214
Java_gnu_xml_libxmlj_dom_GnomeElement_hasAttributeNS (JNIEnv * env,
1215
                                                      jobject self,
1216
                                                      jstring uri,
1217
                                                      jstring localName)
1218
{
1219
  xmlNodePtr node;
1220
  const xmlChar *s_uri;
1221
  const xmlChar *s_localName;
1222
  const xmlChar *s_value;
1223
 
1224
  node = xmljGetNodeID (env, self);
1225
  s_localName = xmljGetStringChars (env, localName);
1226
  if (uri == NULL)
1227
    {
1228
      s_value = xmlGetNoNsProp (node, s_localName);
1229
    }
1230
  else
1231
    {
1232
      s_uri = xmljGetStringChars (env, uri);
1233
      s_value = xmlGetNsProp (node, s_localName, s_uri);
1234
      xmlFree ((xmlChar *) s_uri);
1235
    }
1236
  xmlFree ((xmlChar *) s_localName);
1237
  return (s_value != NULL);
1238
}
1239
 
1240
/* -- GnomeEntity -- */
1241
 
1242
JNIEXPORT jstring JNICALL
1243
Java_gnu_xml_libxmlj_dom_GnomeEntity_getPublicId (JNIEnv * env, jobject self)
1244
{
1245
  xmlEntityPtr entity;
1246
 
1247
  entity = (xmlEntityPtr) xmljGetNodeID (env, self);
1248
  return xmljNewString (env, entity->ExternalID);
1249
}
1250
 
1251
JNIEXPORT jstring JNICALL
1252
Java_gnu_xml_libxmlj_dom_GnomeEntity_getSystemId (JNIEnv * env, jobject self)
1253
{
1254
  xmlEntityPtr entity;
1255
 
1256
  entity = (xmlEntityPtr) xmljGetNodeID (env, self);
1257
  return xmljNewString (env, entity->SystemID);
1258
}
1259
 
1260
JNIEXPORT jstring JNICALL
1261
Java_gnu_xml_libxmlj_dom_GnomeEntity_getNotationName (JNIEnv * env,
1262
                                                      jobject self
1263
                                                      __attribute__ ((__unused__)))
1264
{
1265
  /* TODO */
1266
  xmljThrowDOMException (env, 9, NULL); /* NOT_SUPPORTED_ERR */
1267
  return NULL;
1268
}
1269
 
1270
/* -- GnomeNamedNodeMap -- */
1271
 
1272
JNIEXPORT jobject JNICALL
1273
Java_gnu_xml_libxmlj_dom_GnomeNamedNodeMap_getNamedItem (JNIEnv * env,
1274
                                                         jobject self,
1275
                                                         jstring name)
1276
{
1277
  jclass cls;
1278
  jfieldID field;
1279
  jint type;
1280
 
1281
  cls = (*env)->GetObjectClass (env, self);
1282
  field = (*env)->GetFieldID (env, cls, "type", "I");
1283
  type = (*env)->GetIntField (env, self, field);
1284
 
1285
  if (type == 0)
1286
    {
1287
      xmlAttrPtr attr;
1288
 
1289
      attr = xmljGetNamedItem (env, self, name);
1290
      return xmljGetNodeInstance (env, (xmlNodePtr) attr);
1291
    }
1292
  else
1293
    {
1294
      xmlDtdPtr dtd;
1295
      xmlHashTablePtr hash;
1296
      const xmlChar *s_name;
1297
      xmlNodePtr ret;
1298
 
1299
      dtd = (xmlDtdPtr) xmljGetNodeID (env, self);
1300
      hash = (xmlHashTablePtr) ((type == 1) ? dtd->entities : dtd->notations);
1301
      if (hash == NULL)
1302
        {
1303
          return NULL;
1304
        }
1305
      s_name = xmljGetStringChars (env, name);
1306
      ret = (xmlNodePtr) xmlHashLookup (hash, s_name);
1307
      xmlFree ((xmlChar *) s_name);
1308
      return xmljGetNodeInstance (env, ret);
1309
    }
1310
}
1311
 
1312
JNIEXPORT jobject JNICALL
1313
Java_gnu_xml_libxmlj_dom_GnomeNamedNodeMap_setNamedItem (JNIEnv * env,
1314
                                                         jobject self,
1315
                                                         jobject arg)
1316
{
1317
  jclass cls;
1318
  jfieldID field;
1319
  jint type;
1320
  xmlNodePtr node;
1321
  xmlNodePtr argNode;
1322
 
1323
  cls = (*env)->GetObjectClass (env, self);
1324
  field = (*env)->GetFieldID (env, cls, "type", "I");
1325
  type = (*env)->GetIntField (env, self, field);
1326
 
1327
  node = xmljGetNodeID (env, self);
1328
  argNode = xmljGetNodeID (env, arg);
1329
 
1330
  if (argNode->doc != node->doc)
1331
    {
1332
      xmljThrowDOMException (env, 4, NULL);     /* WRONG_DOCUMENT_ERR */
1333
    }
1334
  xmljValidateChildNode (env, node, argNode);
1335
  if ((*env)->ExceptionOccurred (env))
1336
    {
1337
      return NULL;
1338
    }
1339
  if (type == 0)
1340
    {
1341
      if (argNode->parent != NULL)
1342
        {
1343
          xmljThrowDOMException (env, 10, NULL);  /* INUSE_ATTRIBUTE_ERR */
1344
          return NULL;
1345
        }
1346
      xmlAddChild (node, argNode);
1347
    }
1348
  else
1349
    {
1350
      xmlDtdPtr dtd;
1351
      xmlHashTablePtr hash;
1352
 
1353
      dtd = (xmlDtdPtr) xmljGetNodeID (env, self);
1354
      hash = (xmlHashTablePtr) ((type == 1) ? dtd->entities : dtd->notations);
1355
      if (hash == NULL)
1356
        {
1357
          hash = xmlHashCreate (10);
1358
          if (type == 1)
1359
            {
1360
              dtd->entities = hash;
1361
            }
1362
          else
1363
            {
1364
              dtd->notations = hash;
1365
            }
1366
        }
1367
      xmlHashAddEntry (hash, argNode->name, argNode);
1368
    }
1369
  return arg;
1370
}
1371
 
1372
JNIEXPORT jobject JNICALL
1373
Java_gnu_xml_libxmlj_dom_GnomeNamedNodeMap_removeNamedItem (JNIEnv * env,
1374
                                                            jobject self,
1375
                                                            jstring name)
1376
{
1377
  jclass cls;
1378
  jfieldID field;
1379
  jint type;
1380
 
1381
  cls = (*env)->GetObjectClass (env, self);
1382
  field = (*env)->GetFieldID (env, cls, "type", "I");
1383
  type = (*env)->GetIntField (env, self, field);
1384
 
1385
  if (type == 0)
1386
    {
1387
      xmlAttrPtr attr;
1388
 
1389
      attr = xmljGetNamedItem (env, self, name);
1390
      if (attr == NULL)
1391
        {
1392
          xmljThrowDOMException (env, 8, NULL); /* NOT_FOUND_ERR */
1393
          return NULL;
1394
        }
1395
      xmlUnlinkNode ((xmlNodePtr) attr);
1396
      return xmljGetNodeInstance (env, (xmlNodePtr) attr);
1397
    }
1398
  else
1399
    {
1400
      xmlDtdPtr dtd;
1401
      xmlHashTablePtr hash;
1402
      const xmlChar *s_name;
1403
      xmlNodePtr ret;
1404
 
1405
      dtd = (xmlDtdPtr) xmljGetNodeID (env, self);
1406
      hash = (xmlHashTablePtr) ((type == 1) ? dtd->entities : dtd->notations);
1407
      if (hash == NULL)
1408
        {
1409
          return NULL;
1410
        }
1411
      s_name = xmljGetStringChars (env, name);
1412
      ret = (xmlNodePtr) xmlHashLookup (hash, s_name);
1413
      if (ret != NULL)
1414
        {
1415
          xmlHashRemoveEntry (hash, s_name, NULL);
1416
        }
1417
      xmlFree ((xmlChar *) s_name);
1418
      return xmljGetNodeInstance (env, ret);
1419
    }
1420
}
1421
 
1422
void
1423
xmljHashScanner (void *payload, void *vdata, xmlChar *name)
1424
{
1425
  xmljHashScanData *data;
1426
 
1427
  data = (xmljHashScanData *) vdata;
1428
  if (data->count <= data->index)
1429
    {
1430
      data->node = (xmlNodePtr) payload;
1431
    }
1432
  data->count++;
1433
}
1434
 
1435
JNIEXPORT jobject JNICALL
1436
Java_gnu_xml_libxmlj_dom_GnomeNamedNodeMap_item (JNIEnv * env,
1437
                                                 jobject self, jint index)
1438
{
1439
  jclass cls;
1440
  jfieldID field;
1441
  jint type;
1442
 
1443
  cls = (*env)->GetObjectClass (env, self);
1444
  field = (*env)->GetFieldID (env, cls, "type", "I");
1445
  type = (*env)->GetIntField (env, self, field);
1446
 
1447
  if (type == 0)
1448
    {
1449
      xmlNodePtr node;
1450
      xmlAttrPtr attr;
1451
      jint count;
1452
 
1453
      node = xmljGetNodeID (env, self);
1454
      switch (node->type)
1455
        {
1456
        case XML_ELEMENT_NODE:
1457
          attr = node->properties;
1458
          for (count = 0; attr != NULL && count < index; count++)
1459
            {
1460
              attr = attr->next;
1461
            }
1462
          if (attr == NULL)
1463
            {
1464
              char msg[1024];
1465
              sprintf (msg, "No attribute at index %d\n", (int) index);
1466
              xmljThrowException (env, "java/lang/NullPointerException", msg);
1467
              return NULL;
1468
            }
1469
          return xmljGetNodeInstance (env, (xmlNodePtr) attr);
1470
        default:
1471
          return NULL;
1472
        }
1473
    }
1474
  else
1475
    {
1476
      xmlDtdPtr dtd;
1477
      xmlHashTablePtr hash;
1478
      xmljHashScanData *data;
1479
      xmlNodePtr ret;
1480
 
1481
      dtd = (xmlDtdPtr) xmljGetNodeID (env, self);
1482
      hash = (xmlHashTablePtr) ((type == 1) ? dtd->entities : dtd->notations);
1483
      if (hash == NULL)
1484
        {
1485
          return NULL;
1486
        }
1487
      data = (xmljHashScanData *) malloc (sizeof (xmljHashScanData));
1488
      if (data == NULL)
1489
        {
1490
          return NULL;
1491
        }
1492
      data->index = index;
1493
      data->count = 0;
1494
      data->node = NULL;
1495
      xmlHashScan (hash, xmljHashScanner, data);
1496
      ret = data->node;
1497
      free (data);
1498
      return xmljGetNodeInstance (env, ret);
1499
    }
1500
}
1501
 
1502
JNIEXPORT jint JNICALL
1503
Java_gnu_xml_libxmlj_dom_GnomeNamedNodeMap_getLength (JNIEnv * env,
1504
                                                      jobject self)
1505
{
1506
  jclass cls;
1507
  jfieldID field;
1508
  jint type;
1509
 
1510
  cls = (*env)->GetObjectClass (env, self);
1511
  field = (*env)->GetFieldID (env, cls, "type", "I");
1512
  type = (*env)->GetIntField (env, self, field);
1513
 
1514
  if (type == 0)
1515
    {
1516
      xmlNodePtr node;
1517
      xmlAttrPtr attr;
1518
      jint count;
1519
 
1520
      node = xmljGetNodeID (env, self);
1521
      switch (node->type)
1522
        {
1523
        case XML_ELEMENT_NODE:
1524
          count = 0;
1525
          attr = node->properties;
1526
          while (attr != NULL)
1527
            {
1528
              count++;
1529
              attr = attr->next;
1530
            }
1531
          return count;
1532
        default:
1533
          return -1;
1534
        }
1535
    }
1536
  else
1537
    {
1538
      xmlDtdPtr dtd;
1539
      xmlHashTablePtr hash;
1540
      xmljHashScanData *data;
1541
      jint ret;
1542
 
1543
      dtd = (xmlDtdPtr) xmljGetNodeID (env, self);
1544
      hash = (xmlHashTablePtr) ((type == 1) ? dtd->entities : dtd->notations);
1545
      if (hash == NULL)
1546
        {
1547
          return 0;
1548
        }
1549
      data = (xmljHashScanData *) malloc (sizeof (xmljHashScanData));
1550
      if (data == NULL)
1551
        {
1552
          return 0;
1553
        }
1554
      data->index = -1;
1555
      data->count = 0;
1556
      data->node = NULL;
1557
      xmlHashScan (hash, xmljHashScanner, data);
1558
      ret = data->count;
1559
      free (data);
1560
      return ret;
1561
    }
1562
}
1563
 
1564
JNIEXPORT jobject JNICALL
1565
Java_gnu_xml_libxmlj_dom_GnomeNamedNodeMap_getNamedItemNS (JNIEnv * env,
1566
                                                           jobject self,
1567
                                                           jstring uri,
1568
                                                           jstring localName)
1569
{
1570
  jclass cls;
1571
  jfieldID field;
1572
  jint type;
1573
 
1574
  cls = (*env)->GetObjectClass (env, self);
1575
  field = (*env)->GetFieldID (env, cls, "type", "I");
1576
  type = (*env)->GetIntField (env, self, field);
1577
 
1578
  if (type == 0)
1579
    {
1580
      xmlAttrPtr attr;
1581
 
1582
      attr = xmljGetNamedItemNS (env, self, uri, localName);
1583
      return xmljGetNodeInstance (env, (xmlNodePtr) attr);
1584
    }
1585
  else
1586
    {
1587
      return NULL;
1588
    }
1589
}
1590
 
1591
JNIEXPORT jobject JNICALL
1592
Java_gnu_xml_libxmlj_dom_GnomeNamedNodeMap_setNamedItemNS (JNIEnv * env,
1593
                                                           jobject self,
1594
                                                           jobject arg)
1595
{
1596
  return Java_gnu_xml_libxmlj_dom_GnomeNamedNodeMap_setNamedItem (env, self,
1597
                                                                  arg);
1598
}
1599
 
1600
JNIEXPORT jobject JNICALL
1601
Java_gnu_xml_libxmlj_dom_GnomeNamedNodeMap_removeNamedItemNS (JNIEnv * env,
1602
                                                              jobject self,
1603
                                                              jstring uri,
1604
                                                              jstring
1605
                                                              localName)
1606
{
1607
  jclass cls;
1608
  jfieldID field;
1609
  jint type;
1610
 
1611
  cls = (*env)->GetObjectClass (env, self);
1612
  field = (*env)->GetFieldID (env, cls, "type", "I");
1613
  type = (*env)->GetIntField (env, self, field);
1614
 
1615
  if (type == 0)
1616
    {
1617
      xmlAttrPtr attr;
1618
 
1619
      attr = xmljGetNamedItemNS (env, self, uri, localName);
1620
      if (attr == NULL)
1621
        {
1622
          xmljThrowDOMException (env, 8, NULL); /* NOT_FOUND_ERR */
1623
          return NULL;
1624
        }
1625
      else
1626
        {
1627
          xmlUnlinkNode ((xmlNodePtr) attr);
1628
          return xmljGetNodeInstance (env, (xmlNodePtr) attr);
1629
        }
1630
    }
1631
  else
1632
    {
1633
      return NULL;
1634
    }
1635
}
1636
 
1637
/* -- GnomeNode -- */
1638
 
1639
JNIEXPORT jstring JNICALL
1640
Java_gnu_xml_libxmlj_dom_GnomeNode_getNodeName (JNIEnv * env, jobject self)
1641
{
1642
  xmlNodePtr node;
1643
 
1644
  node = xmljGetNodeID (env, self);
1645
  if (node == NULL)
1646
    {
1647
      return NULL;
1648
    }
1649
  return xmljNewString (env, node->name);
1650
}
1651
 
1652
xmlChar *
1653
xmljGetNodeValue (xmlNodePtr node)
1654
{
1655
  /* If not character data, return null */
1656
  switch (node->type)
1657
    {
1658
    case XML_TEXT_NODE:
1659
    case XML_CDATA_SECTION_NODE:
1660
    case XML_COMMENT_NODE:
1661
    case XML_ATTRIBUTE_NODE:
1662
      return xmlNodeGetContent (node);
1663
    default:
1664
      return NULL;
1665
    }
1666
}
1667
 
1668
JNIEXPORT jstring JNICALL
1669
Java_gnu_xml_libxmlj_dom_GnomeNode_getNodeValue (JNIEnv * env, jobject self)
1670
{
1671
  xmlNodePtr node;
1672
  xmlChar *text;
1673
  jstring ret;
1674
 
1675
  node = xmljGetNodeID (env, self);
1676
  text = xmljGetNodeValue (node);
1677
  ret = xmljNewString (env, (const xmlChar *) text);
1678
  if (text != NULL)
1679
    {
1680
      xmlFree (text);
1681
    }
1682
  return ret;
1683
}
1684
 
1685
JNIEXPORT void JNICALL
1686
Java_gnu_xml_libxmlj_dom_GnomeNode_setNodeValue (JNIEnv * env,
1687
                                                 jobject self,
1688
                                                 jstring nodeValue)
1689
{
1690
  xmlNodePtr node;
1691
  const xmlChar *s_nodeValue;
1692
 
1693
  node = xmljGetNodeID (env, self);
1694
 
1695
  /* If not character data, return */
1696
  if (node->type != XML_TEXT_NODE &&
1697
      node->type != XML_CDATA_SECTION_NODE && node->type != XML_COMMENT_NODE)
1698
    return;
1699
 
1700
  s_nodeValue = xmljGetStringChars (env, nodeValue);
1701
  xmlNodeSetContent (node, s_nodeValue);
1702
}
1703
 
1704
JNIEXPORT jshort JNICALL
1705
Java_gnu_xml_libxmlj_dom_GnomeNode_getNodeType (JNIEnv * env, jobject self)
1706
{
1707
  xmlNodePtr node;
1708
 
1709
  node = xmljGetNodeID (env, self);
1710
  switch (node->type)
1711
    {
1712
    case XML_DTD_NODE:
1713
      return XML_DOCUMENT_TYPE_NODE;
1714
    case XML_ATTRIBUTE_DECL:
1715
      return XML_ATTRIBUTE_NODE;
1716
    case XML_ENTITY_DECL:
1717
      return XML_ENTITY_NODE;
1718
    default:
1719
      return node->type;
1720
    }
1721
}
1722
 
1723
JNIEXPORT jobject JNICALL
1724
Java_gnu_xml_libxmlj_dom_GnomeNode_getParentNode (JNIEnv * env, jobject self)
1725
{
1726
  xmlNodePtr node;
1727
 
1728
  node = xmljGetNodeID (env, self);
1729
  return xmljGetNodeInstance (env, node->parent);
1730
}
1731
 
1732
JNIEXPORT jobject JNICALL
1733
Java_gnu_xml_libxmlj_dom_GnomeNode_getFirstChild (JNIEnv * env, jobject self)
1734
{
1735
  xmlNodePtr node;
1736
 
1737
  node = xmljGetNodeID (env, self);
1738
  return xmljGetNodeInstance (env, node->children);
1739
}
1740
 
1741
JNIEXPORT jobject JNICALL
1742
Java_gnu_xml_libxmlj_dom_GnomeNode_getLastChild (JNIEnv * env, jobject self)
1743
{
1744
  xmlNodePtr node;
1745
 
1746
  node = xmljGetNodeID (env, self);
1747
  return xmljGetNodeInstance (env, node->last);
1748
}
1749
 
1750
JNIEXPORT jobject JNICALL
1751
Java_gnu_xml_libxmlj_dom_GnomeNode_getPreviousSibling (JNIEnv * env,
1752
                                                       jobject self)
1753
{
1754
  xmlNodePtr node;
1755
 
1756
  node = xmljGetNodeID (env, self);
1757
  return xmljGetNodeInstance (env, node->prev);
1758
}
1759
 
1760
JNIEXPORT jobject JNICALL
1761
Java_gnu_xml_libxmlj_dom_GnomeNode_getNextSibling (JNIEnv * env, jobject self)
1762
{
1763
  xmlNodePtr node;
1764
 
1765
  node = xmljGetNodeID (env, self);
1766
  return xmljGetNodeInstance (env, node->next);
1767
}
1768
 
1769
JNIEXPORT jobject JNICALL
1770
Java_gnu_xml_libxmlj_dom_GnomeNode_getOwnerDocument (JNIEnv * env,
1771
                                                     jobject self)
1772
{
1773
  xmlNodePtr node;
1774
 
1775
  node = xmljGetNodeID (env, self);
1776
  return xmljGetNodeInstance (env, (xmlNodePtr) node->doc);
1777
}
1778
 
1779
JNIEXPORT jobject JNICALL
1780
Java_gnu_xml_libxmlj_dom_GnomeNode_xmljInsertBefore (JNIEnv * env,
1781
                                                     jobject self,
1782
                                                     jobject newChild,
1783
                                                     jobject refChild)
1784
{
1785
  xmlNodePtr node;
1786
  xmlNodePtr newChildNode;
1787
  xmlNodePtr refChildNode;
1788
 
1789
  node = xmljGetNodeID (env, self);
1790
  newChildNode = xmljGetNodeID (env, newChild);
1791
  refChildNode = xmljGetNodeID (env, refChild);
1792
 
1793
  /* Is refChildNode a child of this node? */
1794
  if (refChildNode == NULL ||
1795
      refChildNode->parent == NULL ||
1796
      refChildNode->parent != node)
1797
    {
1798
      xmljThrowDOMException (env, 8, NULL);     /* NOT_FOUND_ERR */
1799
      return NULL;
1800
    }
1801
  /* Check new child */
1802
  xmljValidateChildNode (env, node, newChildNode);
1803
  if ((*env)->ExceptionOccurred (env))
1804
    {
1805
      return NULL;
1806
    }
1807
 
1808
  newChildNode = xmlAddPrevSibling (refChildNode, newChildNode);
1809
  return xmljGetNodeInstance (env, newChildNode);
1810
}
1811
 
1812
JNIEXPORT jobject JNICALL
1813
Java_gnu_xml_libxmlj_dom_GnomeNode_xmljReplaceChild (JNIEnv * env,
1814
                                                     jobject self,
1815
                                                     jobject newChild,
1816
                                                     jobject oldChild)
1817
{
1818
  xmlNodePtr node;
1819
  xmlNodePtr newChildNode;
1820
  xmlNodePtr oldChildNode;
1821
 
1822
  node = xmljGetNodeID (env, self);
1823
  newChildNode = xmljGetNodeID (env, newChild);
1824
  oldChildNode = xmljGetNodeID (env, oldChild);
1825
 
1826
  /* Is oldChildNode a child of this node? */
1827
  if (oldChildNode == NULL ||
1828
      oldChildNode->parent == NULL ||
1829
      oldChildNode->parent != node)
1830
    {
1831
      xmljThrowDOMException (env, 8, NULL);     /* NOT_FOUND_ERR */
1832
      return NULL;
1833
    }
1834
  /* Check new child */
1835
  xmljValidateChildNode (env, node, newChildNode);
1836
  if ((*env)->ExceptionOccurred (env))
1837
    {
1838
      return NULL;
1839
    }
1840
 
1841
  newChildNode = xmlReplaceNode (oldChildNode, newChildNode);
1842
  return xmljGetNodeInstance (env, newChildNode);
1843
}
1844
 
1845
JNIEXPORT jobject JNICALL
1846
Java_gnu_xml_libxmlj_dom_GnomeNode_xmljRemoveChild (JNIEnv * env,
1847
                                                    jobject self,
1848
                                                    jobject oldChild)
1849
{
1850
  xmlNodePtr node;
1851
  xmlNodePtr oldChildNode;
1852
 
1853
  node = xmljGetNodeID (env, self);
1854
  oldChildNode = xmljGetNodeID (env, oldChild);
1855
 
1856
  if (oldChildNode == NULL ||
1857
      oldChildNode->parent == NULL ||
1858
      oldChildNode->parent != node)
1859
    {
1860
      xmljThrowDOMException (env, 8, NULL);     /* NOT_FOUND_ERR */
1861
      return NULL;
1862
    }
1863
  xmlUnlinkNode (oldChildNode);
1864
  return oldChild;
1865
}
1866
 
1867
JNIEXPORT jobject JNICALL
1868
Java_gnu_xml_libxmlj_dom_GnomeNode_xmljAppendChild (JNIEnv * env,
1869
                                                    jobject self,
1870
                                                    jobject newChild)
1871
{
1872
  xmlNodePtr node;
1873
  xmlNodePtr newChildNode;
1874
 
1875
  node = xmljGetNodeID (env, self);
1876
  newChildNode = xmljGetNodeID (env, newChild);
1877
 
1878
  /* Check new child */
1879
  xmljValidateChildNode (env, node, newChildNode);
1880
  if ((*env)->ExceptionOccurred (env))
1881
    {
1882
      return NULL;
1883
    }
1884
 
1885
  newChildNode = xmlAddChild (node, newChildNode);
1886
  return xmljGetNodeInstance (env, newChildNode);
1887
}
1888
 
1889
JNIEXPORT jboolean JNICALL
1890
Java_gnu_xml_libxmlj_dom_GnomeNode_hasChildNodes (JNIEnv * env, jobject self)
1891
{
1892
  xmlNodePtr node;
1893
 
1894
  node = xmljGetNodeID (env, self);
1895
  return (node->children != NULL);
1896
}
1897
 
1898
JNIEXPORT jobject JNICALL
1899
Java_gnu_xml_libxmlj_dom_GnomeNode_xmljCloneNode (JNIEnv * env,
1900
                                                  jobject self, jboolean deep)
1901
{
1902
  xmlNodePtr node;
1903
  xmlNodePtr clone;
1904
 
1905
  node = xmljGetNodeID (env, self);
1906
  clone = xmlCopyNode (node, deep);
1907
  clone->parent = NULL;
1908
  clone->doc = node->doc;
1909
  return xmljGetNodeInstance (env, clone);
1910
}
1911
 
1912
JNIEXPORT void JNICALL
1913
Java_gnu_xml_libxmlj_dom_GnomeNode_normalize (JNIEnv * env, jobject self)
1914
{
1915
  xmlNodePtr node;
1916
 
1917
  node = xmljGetNodeID (env, self);
1918
  xmljNormalizeNode (node);
1919
}
1920
 
1921
void
1922
xmljNormalizeNode (xmlNodePtr node)
1923
{
1924
  xmlNodePtr cur;
1925
  xmlNodePtr last = NULL;
1926
 
1927
  cur = node->children;
1928
  while (cur != NULL)
1929
    {
1930
      switch (cur->type)
1931
        {
1932
        case XML_CDATA_SECTION_NODE:
1933
        case XML_TEXT_NODE:
1934
          if (xmlIsBlankNode (cur))
1935
            {
1936
              xmlNodePtr next = cur->next;
1937
              xmlUnlinkNode (cur);
1938
              xmlFreeNode (cur);
1939
              cur = next;
1940
              continue;
1941
            }
1942
          if (last != NULL)
1943
            {
1944
              last = xmlTextMerge (last, cur);
1945
              xmlUnlinkNode (cur);
1946
              xmlFreeNode (cur);
1947
              cur = last;
1948
            }
1949
          else
1950
            {
1951
              last = cur;
1952
            }
1953
          break;
1954
        default:
1955
          last = NULL;
1956
          xmljNormalizeNode (cur);
1957
        }
1958
      cur = cur->next;
1959
    }
1960
}
1961
 
1962
JNIEXPORT jstring JNICALL
1963
Java_gnu_xml_libxmlj_dom_GnomeNode_getNamespaceURI (JNIEnv * env,
1964
                                                    jobject self)
1965
{
1966
  xmlNodePtr node;
1967
 
1968
  node = xmljGetNodeID (env, self);
1969
  if (node->type != XML_ELEMENT_NODE &&
1970
      node->type != XML_ATTRIBUTE_NODE)
1971
    {
1972
      return NULL;
1973
    }
1974
  if (node->ns == NULL)
1975
    {
1976
      return NULL;
1977
    }
1978
  return xmljNewString (env, node->ns->href);
1979
}
1980
 
1981
JNIEXPORT jstring JNICALL
1982
Java_gnu_xml_libxmlj_dom_GnomeNode_getPrefix (JNIEnv * env, jobject self)
1983
{
1984
  xmlNodePtr node;
1985
 
1986
  node = xmljGetNodeID (env, self);
1987
  if (node->type != XML_ELEMENT_NODE &&
1988
      node->type != XML_ATTRIBUTE_NODE)
1989
    {
1990
      return NULL;
1991
    }
1992
  if (node->ns == NULL)
1993
    {
1994
      return NULL;
1995
    }
1996
  return xmljNewString (env, node->ns->prefix);
1997
}
1998
 
1999
JNIEXPORT void JNICALL
2000
Java_gnu_xml_libxmlj_dom_GnomeNode_setPrefix (JNIEnv * env,
2001
                                              jobject self, jstring prefix)
2002
{
2003
  xmlNodePtr node;
2004
  const xmlChar *s_prefix;
2005
 
2006
  s_prefix = xmljGetStringChars (env, prefix);
2007
  if (xmlValidateName (s_prefix, 0))
2008
    {
2009
      xmljThrowDOMException (env, 5, NULL);     /* INVALID_CHARACTER_ERR */
2010
    }
2011
  node = xmljGetNodeID (env, self);
2012
  if (node->type != XML_ELEMENT_NODE &&
2013
      node->type != XML_ATTRIBUTE_NODE)
2014
    {
2015
      xmljThrowDOMException (env, 3, NULL);     /* HIERARCHY_REQUEST_ERR */
2016
      return;
2017
    }
2018
  if (node->ns == NULL)
2019
    {
2020
      xmljThrowDOMException (env, 14, NULL);    /* NAMESPACE_ERR */
2021
      return;
2022
    }
2023
  node->ns->prefix = s_prefix;
2024
}
2025
 
2026
JNIEXPORT jstring JNICALL
2027
Java_gnu_xml_libxmlj_dom_GnomeNode_getLocalName (JNIEnv * env, jobject self)
2028
{
2029
  xmlNodePtr node;
2030
  int *len;
2031
  jstring ret;
2032
 
2033
  node = xmljGetNodeID (env, self);
2034
  if (node->name == NULL)
2035
    {
2036
      return NULL;
2037
    }
2038
  len = (int *) malloc (sizeof (int));
2039
  if (xmlSplitQName3 (node->name, len) != NULL)
2040
    {
2041
      ret = xmljNewString (env, node->name + (*len));
2042
    }
2043
  else
2044
    {
2045
      ret = xmljNewString (env, node->name);
2046
    }
2047
  free (len);
2048
  return ret;
2049
}
2050
 
2051
JNIEXPORT jboolean JNICALL
2052
Java_gnu_xml_libxmlj_dom_GnomeNode_hasAttributes (JNIEnv * env, jobject self)
2053
{
2054
  xmlNodePtr node;
2055
 
2056
  node = xmljGetNodeID (env, self);
2057
  return (node->properties != NULL);
2058
}
2059
 
2060
JNIEXPORT jstring JNICALL
2061
Java_gnu_xml_libxmlj_dom_GnomeNode_getBaseURI (JNIEnv * env, jobject self)
2062
{
2063
  xmlNodePtr node;
2064
  xmlChar *baseURI;
2065
  jstring ret;
2066
 
2067
  node = xmljGetNodeID (env, self);
2068
  baseURI = xmlNodeGetBase (node->doc, node);
2069
  ret = xmljNewString (env, (const xmlChar *) baseURI);
2070
  if (baseURI != NULL)
2071
    {
2072
      xmlFree (baseURI);
2073
    }
2074
  return ret;
2075
}
2076
 
2077
JNIEXPORT jstring JNICALL
2078
Java_gnu_xml_libxmlj_dom_GnomeNode_lookupPrefix (JNIEnv * env, jobject self,
2079
                                                 jstring namespaceURI)
2080
{
2081
  xmlNodePtr node;
2082
  xmlNsPtr ns;
2083
  xmlDocPtr doc;
2084
  const xmlChar *s_uri;
2085
 
2086
  node = xmljGetNodeID (env, self);
2087
  doc = node->doc;
2088
  /* If this is a document node, search from the root element */
2089
  if (node->type == XML_DOCUMENT_NODE)
2090
    {
2091
      doc = (xmlDocPtr) node;
2092
      node = xmlDocGetRootElement (doc);
2093
    }
2094
  s_uri = xmljGetStringChars (env, namespaceURI);
2095
  ns = xmlSearchNsByHref (doc, node, s_uri);
2096
  xmlFree ((xmlChar *) s_uri);
2097
  if (ns == NULL)
2098
    {
2099
      return NULL;
2100
    }
2101
  return xmljNewString (env, ns->prefix);
2102
}
2103
 
2104
JNIEXPORT jboolean JNICALL
2105
Java_gnu_xml_libxmlj_dom_GnomeNode_isDefaultNamespace (JNIEnv * env,
2106
                                                       jobject self,
2107
                                                       jstring namespaceURI)
2108
{
2109
  xmlNodePtr node;
2110
  xmlNsPtr ns;
2111
  const xmlChar *s_uri;
2112
 
2113
  node = xmljGetNodeID (env, self);
2114
  s_uri = xmljGetStringChars (env, namespaceURI);
2115
  ns = xmlSearchNsByHref (node->doc, node, s_uri);
2116
  xmlFree ((xmlChar *) s_uri);
2117
  if (ns == NULL)
2118
    {
2119
      return 0;
2120
    }
2121
  return (ns->prefix == NULL || xmlStrlen (ns->prefix) == 0);
2122
}
2123
 
2124
JNIEXPORT jstring JNICALL
2125
Java_gnu_xml_libxmlj_dom_GnomeNode_lookupNamespaceURI (JNIEnv * env,
2126
                                                       jobject self,
2127
                                                       jstring prefix)
2128
{
2129
  xmlNodePtr node;
2130
  xmlDocPtr doc;
2131
  xmlNsPtr ns;
2132
  const xmlChar *s_prefix;
2133
 
2134
  node = xmljGetNodeID (env, self);
2135
  doc = node->doc;
2136
  /* If this is a document node, search from the root element */
2137
  if (node->type == XML_DOCUMENT_NODE)
2138
    {
2139
      doc = (xmlDocPtr) node;
2140
      node = xmlDocGetRootElement (doc);
2141
    }
2142
  s_prefix = xmljGetStringChars (env, prefix);
2143
  ns = xmlSearchNs (doc, node, s_prefix);
2144
  xmlFree ((xmlChar *) s_prefix);
2145
  if (ns == NULL)
2146
    {
2147
      return NULL;
2148
    }
2149
  return xmljNewString (env, ns->href);
2150
}
2151
 
2152
JNIEXPORT jint JNICALL
2153
Java_gnu_xml_libxmlj_dom_GnomeNode_xmljCompareTo (JNIEnv * env,
2154
                                                  jobject self,
2155
                                                  jobject other)
2156
{
2157
  xmlNodePtr n1, n2, x;
2158
  int d1, d2, delta, c;
2159
 
2160
  n1 = xmljGetNodeID (env, self);
2161
  n2 = xmljGetNodeID (env, other);
2162
  if (n1->doc != n2->doc)
2163
    {
2164
      return 0;
2165
    }
2166
  if (n1->type == XML_ATTRIBUTE_NODE || n2->type == XML_ATTRIBUTE_NODE)
2167
    {
2168
      return 0;
2169
    }
2170
  d1 = 0;
2171
  for (x = n1->parent; x && x->type != XML_DOCUMENT_NODE; x = x->parent)
2172
    {
2173
      d1++;
2174
    }
2175
  d2 = 0;
2176
  for (x = n2->parent; x && x->type != XML_DOCUMENT_NODE; x = x->parent)
2177
    {
2178
      d2++;
2179
    }
2180
  delta = d1 - d2;
2181
  while (d1 > d2)
2182
    {
2183
      n1 = n1->parent;
2184
      d1--;
2185
    }
2186
  while (d2 > d1)
2187
    {
2188
      n2 = n2->parent;
2189
      d2--;
2190
    }
2191
  c = xmljCompare (n1, n2);
2192
  return (c != 0) ? c : delta;
2193
}
2194
 
2195
/* Compare at same level */
2196
int
2197
xmljCompare (xmlNodePtr n1, xmlNodePtr n2)
2198
{
2199
  int c, i1, i2;
2200
 
2201
  if (n1->parent == NULL || n1->type == XML_DOCUMENT_NODE ||
2202
      n2->parent == NULL || n2->type == XML_DOCUMENT_NODE ||
2203
      n1 == n2)
2204
    {
2205
      return 0;
2206
    }
2207
  c = xmljCompare (n1->parent, n2->parent);
2208
  if (c != 0)
2209
    {
2210
      return c;
2211
    }
2212
  i1 = 0;
2213
  for (n1 = n1->prev; n1; n1 = n1->prev)
2214
    {
2215
      i1++;
2216
    }
2217
  i2 = 0;
2218
  for (n2 = n2->prev; n2; n2 = n2->prev)
2219
    {
2220
      i2++;
2221
    }
2222
  return i1 - i2;
2223
}
2224
 
2225
int
2226
xmljIsEqualNodeList (xmlNodePtr node1, xmlNodePtr node2)
2227
{
2228
  while (node1 != NULL)
2229
    {
2230
      if (!xmljIsEqualNode (node1, node2))
2231
        {
2232
          return 0;
2233
        }
2234
      node1 = node1->next;
2235
      node2 = node2->next;
2236
    }
2237
  return 1;
2238
}
2239
 
2240
int
2241
xmljIsEqualNode (xmlNodePtr node1, xmlNodePtr node2)
2242
{
2243
  const xmlChar *val1;
2244
  const xmlChar *val2;
2245
 
2246
  if (node1 == node2)
2247
    {
2248
      return 1;
2249
    }
2250
  if (node1 == NULL || node2 == NULL)
2251
    {
2252
      return 0;
2253
    }
2254
  /* Check node type */
2255
  if (node1->type != node2->type)
2256
    {
2257
      return 0;
2258
    }
2259
  /* Check node name */
2260
  if (!xmlStrEqual (node1->name, node2->name))
2261
    {
2262
      return 0;
2263
    }
2264
  /* Check node namespace */
2265
  if (node1->type == XML_ELEMENT_NODE ||
2266
      node1->type == XML_ATTRIBUTE_NODE)
2267
    {
2268
      xmlNsPtr ns1, ns2;
2269
 
2270
      ns1 = node1->ns;
2271
      if (ns1 != NULL)
2272
        {
2273
          ns2 = node2->ns;
2274
          if (ns2 == NULL)
2275
            {
2276
              return 0;
2277
            }
2278
          val1 = ns1->href;
2279
          val2 = ns2->href;
2280
          if (!xmlStrEqual (val1, val2))
2281
            {
2282
              return 0;
2283
            }
2284
        }
2285
    }
2286
  /* Check node value */
2287
  val1 = xmljGetNodeValue (node1);
2288
  val2 = xmljGetNodeValue (node2);
2289
  if (!xmlStrEqual (val1, val2))
2290
    {
2291
      return 0;
2292
    }
2293
  /* Check attributes */
2294
  if (node1->type == XML_ELEMENT_NODE &&
2295
      !xmljIsEqualNodeList ((xmlNodePtr) node1->properties,
2296
                            (xmlNodePtr) node2->properties))
2297
    {
2298
      return 0;
2299
    }
2300
  /* Check doctype */
2301
  if (node1->type == XML_DOCUMENT_NODE)
2302
    {
2303
      xmlDocPtr doc1 = (xmlDocPtr) node1;
2304
      xmlDocPtr doc2 = (xmlDocPtr) node2;
2305
 
2306
      if (!xmljIsEqualNode ((xmlNodePtr) doc1->intSubset,
2307
                            (xmlNodePtr) doc2->intSubset) ||
2308
          !xmljIsEqualNode ((xmlNodePtr) doc1->extSubset,
2309
                            (xmlNodePtr) doc2->extSubset))
2310
        {
2311
          return 0;
2312
        }
2313
    }
2314
  /* Check child nodes */
2315
  if (!xmljIsEqualNodeList (node1->children, node2->children))
2316
    {
2317
      return 0;
2318
    }
2319
  return 1;
2320
}
2321
 
2322
JNIEXPORT jboolean JNICALL
2323
Java_gnu_xml_libxmlj_dom_GnomeNode_isEqualNode (JNIEnv * env,
2324
                                                jobject self,
2325
                                                jobject arg)
2326
{
2327
  xmlNodePtr node1;
2328
  xmlNodePtr node2;
2329
 
2330
  node1 = xmljGetNodeID (env, self);
2331
  node2 = xmljGetNodeID (env, arg);
2332
  return xmljIsEqualNode (node1, node2);
2333
}
2334
 
2335
/* -- GnomeNodeList -- */
2336
 
2337
JNIEXPORT jobject JNICALL
2338
Java_gnu_xml_libxmlj_dom_GnomeNodeList_item (JNIEnv * env,
2339
                                             jobject self, jint index)
2340
{
2341
  xmlNodePtr node;
2342
  jint count;
2343
 
2344
  node = xmljGetNodeID (env, self);
2345
  node = node->children;
2346
  count = 0;
2347
  for (count = 0; node != NULL && count < index; count++)
2348
    {
2349
      node = node->next;
2350
    }
2351
  return xmljGetNodeInstance (env, node);
2352
}
2353
 
2354
JNIEXPORT jint JNICALL
2355
Java_gnu_xml_libxmlj_dom_GnomeNodeList_getLength (JNIEnv * env, jobject self)
2356
{
2357
  xmlNodePtr node;
2358
  jint count;
2359
 
2360
  node = xmljGetNodeID (env, self);
2361
  count = 0;
2362
  node = node->children;
2363
  while (node != NULL)
2364
    {
2365
      count++;
2366
      node = node->next;
2367
    }
2368
  return count;
2369
}
2370
 
2371
/* -- GnomeNotation -- */
2372
 
2373
JNIEXPORT jstring JNICALL
2374
Java_gnu_xml_libxmlj_dom_GnomeNotation_getPublicId (JNIEnv * env,
2375
                                                    jobject self)
2376
{
2377
  xmlNotationPtr notation;
2378
 
2379
  notation = (xmlNotationPtr) xmljGetNodeID (env, self);
2380
  if (notation->PublicID == NULL)
2381
    {
2382
      return NULL;
2383
    }
2384
  return xmljNewString (env, notation->PublicID);
2385
}
2386
 
2387
JNIEXPORT jstring JNICALL
2388
Java_gnu_xml_libxmlj_dom_GnomeNotation_getSystemId (JNIEnv * env,
2389
                                                    jobject self)
2390
{
2391
  xmlNotationPtr notation;
2392
 
2393
  notation = (xmlNotationPtr) xmljGetNodeID (env, self);
2394
  if (notation->SystemID == NULL)
2395
    {
2396
      return NULL;
2397
    }
2398
  return xmljNewString (env, notation->SystemID);
2399
}
2400
 
2401
/* -- GnomeProcessingInstruction -- */
2402
 
2403
JNIEXPORT jstring JNICALL
2404
Java_gnu_xml_libxmlj_dom_GnomeProcessingInstruction_getData (JNIEnv * env,
2405
                                                             jobject self)
2406
{
2407
  xmlNodePtr node;
2408
  xmlChar *text;
2409
  jstring ret;
2410
 
2411
  node = xmljGetNodeID (env, self);
2412
  text = xmlNodeGetContent (node);
2413
  ret = xmljNewString (env, (const xmlChar *) text);
2414
  if (text != NULL)
2415
    {
2416
      xmlFree (text);
2417
    }
2418
  return ret;
2419
}
2420
 
2421
JNIEXPORT void JNICALL
2422
Java_gnu_xml_libxmlj_dom_GnomeProcessingInstruction_setData (JNIEnv * env,
2423
                                                             jobject self,
2424
                                                             jstring data)
2425
{
2426
  xmlNodePtr node;
2427
  const xmlChar *s_data;
2428
 
2429
  node = xmljGetNodeID (env, self);
2430
  s_data = xmljGetStringChars (env, data);
2431
  xmlNodeSetContent (node, s_data);
2432
}
2433
 
2434
/* -- GnomeTypeInfo -- */
2435
 
2436
xmlDtdPtr xmljGetDtd (xmlDocPtr doc)
2437
{
2438
  xmlNodePtr ctx;
2439
 
2440
  for (ctx = doc->children; ctx; ctx = ctx->next)
2441
    {
2442
      if (ctx->type == XML_DOCUMENT_TYPE_NODE)
2443
        {
2444
          return (xmlDtdPtr) ctx;
2445
        }
2446
    }
2447
  return NULL;
2448
}
2449
 
2450
JNIEXPORT jstring JNICALL
2451
Java_gnu_xml_libxmlj_dom_GnomeTypeInfo_getTypeName (JNIEnv *env, jobject self)
2452
{
2453
  xmlNodePtr node;
2454
  xmlDtdPtr dtd;
2455
  xmlAttributePtr attribute;
2456
 
2457
  node = xmljGetNodeID (env, self);
2458
  dtd = xmljGetDtd (node->doc);
2459
  if (dtd)
2460
    {
2461
      switch (node->type)
2462
        {
2463
        case XML_ATTRIBUTE_NODE:
2464
          attribute = xmlGetDtdAttrDesc (dtd, node->parent->name, node->name);
2465
          if (attribute)
2466
            {
2467
              switch (attribute->type)
2468
                {
2469
                case XML_ATTRIBUTE_CDATA:
2470
                  return xmljNewString (env, BAD_CAST "CDATA");
2471
                case XML_ATTRIBUTE_ID:
2472
                  return xmljNewString (env, BAD_CAST "ID");
2473
                case XML_ATTRIBUTE_IDREF:
2474
                  return xmljNewString (env, BAD_CAST "IDREF");
2475
                case XML_ATTRIBUTE_IDREFS:
2476
                  return xmljNewString (env, BAD_CAST "IDREFS");
2477
                case XML_ATTRIBUTE_ENTITY:
2478
                  return xmljNewString (env, BAD_CAST "ENTITY");
2479
                case XML_ATTRIBUTE_ENTITIES:
2480
                  return xmljNewString (env, BAD_CAST "ENTITIES");
2481
                case XML_ATTRIBUTE_NMTOKEN:
2482
                  return xmljNewString (env, BAD_CAST "NMTOKEN");
2483
                case XML_ATTRIBUTE_NMTOKENS:
2484
                  return xmljNewString (env, BAD_CAST "NMTOKENS");
2485
                default:
2486
                  return NULL;
2487
                }
2488
            }
2489
          return NULL;
2490
        default:
2491
          return NULL;
2492
        }
2493
    }
2494
  /* TODO when XML Schema support is available */
2495
  return NULL;
2496
}
2497
 
2498
JNIEXPORT jstring JNICALL
2499
Java_gnu_xml_libxmlj_dom_GnomeTypeInfo_getTypeNamespace (JNIEnv *env,
2500
                                                         jobject self)
2501
{
2502
  xmlNodePtr node;
2503
  xmlDtdPtr dtd;
2504
  xmlAttributePtr attribute;
2505
 
2506
  node = xmljGetNodeID (env, self);
2507
  dtd = xmljGetDtd (node->doc);
2508
  if (dtd)
2509
    {
2510
      switch (node->type)
2511
        {
2512
        case XML_ATTRIBUTE_NODE:
2513
          attribute = xmlGetDtdAttrDesc (dtd, node->parent->name, node->name);
2514
          if (attribute)
2515
            {
2516
              return xmljNewString (env,
2517
                                    BAD_CAST "http://www.w3.org/TR/REC-xml");
2518
            }
2519
          return NULL;
2520
        default:
2521
          return NULL;
2522
        }
2523
    }
2524
  /* TODO when XML Schema support is available */
2525
  return NULL;
2526
}
2527
 
2528
JNIEXPORT jboolean JNICALL
2529
Java_gnu_xml_libxmlj_dom_GnomeTypeInfo_isDerivedFrom (JNIEnv *env
2530
                                                      __attribute__ ((__unused__)),
2531
                                                      jobject self
2532
                                                      __attribute__ ((__unused__)),
2533
                                                      jstring typeNS
2534
                                                      __attribute__ ((__unused__)),
2535
                                                      jstring typeName
2536
                                                      __attribute__ ((__unused__)),
2537
                                                      jint method
2538
                                                      __attribute__ ((__unused__)))
2539
{
2540
  /* TODO when XML Schema support is available */
2541
  return 0;
2542
}
2543
 
2544
/* -- Utility -- */
2545
 
2546
/*
2547
 * Create GnomeDocument object from the given xmlDocPtr
2548
 */
2549
jobject
2550
xmljCreateDocument (JNIEnv * env, jobject self, xmlDocPtr doc)
2551
{
2552
  jclass cls;
2553
  jfieldID field;
2554
  jobject ret;
2555
 
2556
  if (!doc)
2557
    {
2558
      return NULL;
2559
    }
2560
 
2561
  /* Get document object */
2562
  ret = xmljGetNodeInstance (env, (xmlNodePtr) doc);
2563
 
2564
  /* Set DOM implementation field */
2565
  cls = (*env)->FindClass (env, "gnu/xml/libxmlj/dom/GnomeDocument");
2566
  field = (*env)->GetFieldID (env, cls, "dom",
2567
                              "Lorg/w3c/dom/DOMImplementation;");
2568
  (*env)->SetObjectField (env, ret, field, self);
2569
  return ret;
2570
}
2571
 
2572
xmlAttrPtr
2573
xmljGetNamedItem (JNIEnv * env, jobject self, jstring name)
2574
{
2575
  xmlNodePtr node;
2576
  xmlAttrPtr attr;
2577
  const xmlChar *s_name;
2578
 
2579
  s_name = xmljGetStringChars (env, name);
2580
 
2581
  node = xmljGetNodeID (env, self);
2582
  attr = node->properties;
2583
  while (attr != NULL)
2584
    {
2585
      if (xmljMatch (s_name, (xmlNodePtr) attr))
2586
        break;
2587
      attr = attr->next;
2588
    }
2589
  xmlFree ((xmlChar *) s_name);
2590
 
2591
  return attr;
2592
}
2593
 
2594
xmlAttrPtr
2595
xmljGetNamedItemNS (JNIEnv * env, jobject self, jstring uri, jstring localName)
2596
{
2597
  xmlNodePtr node;
2598
  xmlAttrPtr attr;
2599
  const xmlChar *s_uri;
2600
  const xmlChar *s_localName;
2601
 
2602
  s_uri = xmljGetStringChars (env, uri);
2603
  s_localName = xmljGetStringChars (env, localName);
2604
 
2605
  node = xmljGetNodeID (env, self);
2606
  attr = node->properties;
2607
  while (attr != NULL)
2608
    {
2609
      if (xmljMatchNS (s_uri, s_localName, (xmlNodePtr) attr))
2610
        break;
2611
      attr = attr->next;
2612
    }
2613
  xmlFree ((xmlChar *) s_uri);
2614
  xmlFree ((xmlChar *) s_localName);
2615
 
2616
  return attr;
2617
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.