Debian Codegraph @

libgdome/gdomecore/gdome-xml-document.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2 /* gdome-xml-document.c
3 *
4 * Copyright (C) 1999 Raph Levien <[email protected]>
5 * Copyright (C) 2000 Mathieu Lacage <[email protected]>
6 * Copyright (C) 2001 Paolo Casarini <[email protected]>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include "gdome.h"
26 #include "gdome-events.h"
27 #include "gdome-treegc.h"
28 #include <libxml/tree.h>
29 #include <libxml/parser.h>
30 #include <libxml/xmlmemory.h>
31 #include <libxml/hash.h>
32 #include "gdome-xml-domimpl.h"
33 #include "gdome-xml-util.h"
34 #include "gdome-xml-str.h"
35 #include "gdome-xml-node.h"
36 #include "gdome-xml-nodel.h"
37 #include "gdome-xml-documentt.h"
38 #include "gdome-evt-event.h"
39 #include "gdome-evt-mevent.h"
40 #include "gdome-xml-document.h"
41 #include "gdome-xml-xmlutil.h"
42 #include "gdome-xml-element.h"
43
44
45 typedef struct _idsIterator idsIterator;
46 struct _idsIterator {
47 xmlChar *elementId;
48 xmlNode *element;
49 };
50
51 /* ****************************************************************/
52 /* **************** The Document Virtual Table **************** */
53 /* ****************************************************************/
54
55 const GdomeDocumentVtab gdome_xml_doc_vtab =
56 {
57 {
58 gdome_xml_n_ref,
59 gdome_xml_doc_unref,
60 gdome_xml_doc_query_interface,
61 gdome_xml_n_nodeName,
62 gdome_xml_n_nodeValue,
63 gdome_xml_n_set_nodeValue,
64 gdome_xml_n_nodeType,
65 gdome_xml_n_parentNode,
66 gdome_xml_n_childNodes,
67 gdome_xml_n_firstChild,
68 gdome_xml_n_lastChild,
69 gdome_xml_n_previousSibling,
70 gdome_xml_n_nextSibling,
71 gdome_xml_n_attributes,
72 gdome_xml_n_ownerDocument,
73 gdome_xml_n_insertBefore,
74 gdome_xml_n_replaceChild,
75 gdome_xml_n_removeChild,
76 gdome_xml_n_appendChild,
77 gdome_xml_n_hasChildNodes,
78 gdome_xml_n_cloneNode,
79 gdome_xml_n_normalize,
80 gdome_xml_n_isSupported,
81 gdome_xml_n_namespaceURI,
82 gdome_xml_n_prefix,
83 gdome_xml_n_set_prefix,
84 gdome_xml_n_localName,
85 gdome_xml_n_hasAttributes,
86 gdome_xml_n_addEventListener,
87 gdome_xml_n_removeEventListener,
88 gdome_xml_n_dispatchEvent,
89 gdome_xml_n_subTreeDispatchEvent
90 },
91 gdome_xml_doc_doctype,
92 gdome_xml_doc_implementation,
93 gdome_xml_doc_documentElement,
94 gdome_xml_doc_createElement,
95 gdome_xml_doc_createDocumentFragment,
96 gdome_xml_doc_createTextNode,
97 gdome_xml_doc_createComment,
98 gdome_xml_doc_createCDATASection,
99 gdome_xml_doc_createProcessingInstruction,
100 gdome_xml_doc_createAttribute,
101 gdome_xml_doc_createEntityReference,
102 gdome_xml_doc_getElementsByTagName,
103 gdome_xml_doc_importNode,
104 gdome_xml_doc_createElementNS,
105 gdome_xml_doc_createAttributeNS,
106 gdome_xml_doc_getElementsByTagNameNS,
107 gdome_xml_doc_getElementById,
108 gdome_xml_doc_createEvent
109 };
110
111 /**
112 * gdome_xml_doc_mkref:
113 * @n: The libxml document node of which a Gdome reference is wanted
114 *
115 * Constructor of the Document structure.
116 * Returns: the reference to the Gdome Document wanted.
117 */
118 GdomeDocument *
119 gdome_xml_doc_mkref (xmlDoc *n) {
120 Gdome_xml_Document *result = NULL;
121
122 if (n == NULL)
123 return NULL;
124 if (n->_private) {
125 result = (Gdome_xml_Document *)n->_private;
126
127 /* workaround since doc_unref decreases livenodes if refcnt==0*/
128 if (result->refcnt == 0)
129 result->livenodes++;
130
131 result->refcnt++;
132 } else if (n->type == XML_DOCUMENT_NODE ||
133 n->type == XML_HTML_DOCUMENT_NODE ) {
134 result = g_new (Gdome_xml_Document, 1);
135 result->refcnt = 1;
136 result->vtab = &gdome_xml_doc_vtab;
137 result->super.user_data = NULL;
138 result->n = n;
139 result->accessType = GDOME_READWRITE_NODE;
140 result->ll = NULL;
141 result->livenodes = 0;
142 /* By default all kind of events can be fired */
143 result->event_mask = DOM_ALL_EVENT_TYPES;
144 /* result->event_mask = 0; */
145 n->_private = result;
146
147 #ifdef DEBUG_REFCNT
148 gdome_refdbg_addRef ((void *)result, GDOME_REFDBG_NODE);
149 #endif
150 gdome_treegc_addNode ((GdomeNode *)result);
151 } else {
152 g_warning ("gdome_xml_doc_mkref: invalid node type");
153 result = NULL;
154 }
155
156 return (GdomeDocument *)result;
157 }
158
159 /**
160 * gdome_xml_doc_unref:
161 * @self: Node Object ref
162 * @exc: Exception Object ref
163 *
164 * Decrease the reference count of the specified Document Node. Free the Node
165 * structure if the Node will have zero reference.
166 */
167 void
168 gdome_xml_doc_unref (GdomeNode *self, GdomeException *exc) {
169 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
170
171 g_return_if_fail (priv != NULL);
172 g_return_if_fail (GDOME_XML_IS_DOC (priv));
173 g_return_if_fail (exc != NULL);
174
175 if (priv->refcnt > 0)
176 priv->refcnt--;
177
178 if (priv->refcnt == 0 && priv->ll == NULL)
179 gdome_treegc_delNode (self);
180 }
181
182 /***************************
183 * Implementation of the
184 * Document Interface
185 ***************************/
186
187 /**
188 * gdome_xml_doc_query_interface:
189 * @self: Node Object ref
190 * @interface: interface needed
191 * @exc: Exception Object ref
192 *
193 * Returns: a reference to this object that implements the @interface needed,
194 * or %NULL if the @interface is not supported by this Object.
195 */
196 gpointer
197 gdome_xml_doc_query_interface (GdomeNode *self, const char *interface, GdomeException *exc)
198 {
199 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
200
201 g_return_val_if_fail (priv != NULL, NULL);
202 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
203 g_return_val_if_fail (interface != NULL, NULL);
204 g_return_val_if_fail (exc != NULL, NULL);
205
206 if (!strcmp (interface, "Node") ||
207 !strcmp (interface, "Document") ||
208 !strcmp (interface, "DocumentEvent") ||
209 !strcmp (interface, "EventTarget")) {
210 priv->refcnt++;
211 return self;
212 }
213 else
214 return NULL;
215 }
216
217 /**
218 * gdome_xml_doc_doctype:
219 * @self: Document Object ref
220 * @exc: Exception Object ref
221 *
222 * Returns: The Document Type Declaration associated with this document. The
223 * DOM Level 2 does not support editing the Document Type Declaration. docType
224 * cannot be altered in any way, including through the use of methods inherited
225 * from the Node interface, such as insertNode or removeNode.
226 */
227 GdomeDocumentType *
228 gdome_xml_doc_doctype (GdomeDocument *self, GdomeException *exc)
229 {
230 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
231 xmlDoc *doc = NULL;
232
233 g_return_val_if_fail (priv != NULL, NULL);
234 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
235 g_return_val_if_fail (exc != NULL, NULL);
236
237 doc = priv->n;
238 if (doc->intSubset != NULL)
239 return (GdomeDocumentType *) gdome_xml_dt_mkref (doc->intSubset);
240 else
241 return NULL;
242 }
243
244 /**
245 * gdome_xml_doc_implementation:
246 * @self: Document Object ref
247 * @exc: Exception Object ref
248 *
249 * Returns: the DOMImplementation object that handles this document.
250 */
251 GdomeDOMImplementation *
252 gdome_xml_doc_implementation (GdomeDocument *self, GdomeException *exc)
253 {
254 g_return_val_if_fail (self != NULL, NULL);
255 g_return_val_if_fail (GDOME_XML_IS_DOC (self), NULL);
256 g_return_val_if_fail (exc != NULL, NULL);
257
258 /* Always return the global DOMImplementation we have */
259 return gdome_xml_di_mkref ();
260 }
261
262 /**
263 * gdome_xml_doc_documentElement:
264 * @self: Document Object ref
265 * @exc: Exception Object ref
266 *
267 * Returns: the root Element of the Document.
268 */
269 GdomeElement *
270 gdome_xml_doc_documentElement (GdomeDocument *self, GdomeException *exc)
271 {
272 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
273 xmlNode *result;
274
275 g_return_val_if_fail (priv != NULL, NULL);
276 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
277 g_return_val_if_fail (exc != NULL, NULL);
278
279 result = xmlDocGetRootElement (priv->n);
280
281 return (GdomeElement *)gdome_xml_n_mkref (result);
282 }
283
284 /**
285 * gdome_xml_doc_createElement:
286 * @self: Document Object ref
287 * @tagName: The name of the element type to instantiate.
288 * @exc: Exception Object ref
289 *
290 * Creates an element of the type specified. Note that the instance returned
291 * implements the Element interface, so attributes can be specified directly on
292 * the returned object.
293 * To create an element with a qualified name and namespace URI, use the
294 * gdome_doc_createElementNS() function.
295 * Returns: a new Element object with the nodeName attribute set to tagName,
296 * and localName, prefix, and namespaceURI set to %NULL.
297 */
298 GdomeElement *
299 gdome_xml_doc_createElement (GdomeDocument *self, GdomeDOMString *tagName, GdomeException *exc)
300 {
301 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
302 xmlNode *result = NULL;
303
304 g_return_val_if_fail (priv != NULL, NULL);
305 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
306 g_return_val_if_fail (tagName != NULL, NULL);
307 g_return_val_if_fail (exc != NULL, NULL);
308
309 result = xmlNewDocNode (priv->n, NULL, tagName->str, NULL);
310
311 return (GdomeElement *)gdome_xml_n_mkref (result);
312 }
313
314 /**
315 * gdome_xml_doc_createElementNS:
316 * @self: Document Object ref
317 * @namespaceURI: The namespace URI of the element to create
318 * @qualifiedName: The qualified name of the element to create
319 * @exc: Exception Object ref
320 *
321 * Creates an element of the given qualified name and namespace URI.
322 *
323 * %GDOME_NAMESPACE_ERR: Raised if the @qualifiedName is malformed, if the
324 * @qualifiedName has a prefix and the @namespaceURI is %NULL, or if the
325 * @qualifiedName has a prefix that is "xml" and the @namespaceURI is different
326 * from "http://www.w3.org/XML/1998/namespace".
327 * Returns: a new Element object with the following attributes:
328 * %Node.nodeName = @qualifiedName,
329 * %Node.namespaceURI = @namespaceURI,
330 * %Node.prefix = prefix, extracted from @qualifiedName,
331 * %Node.localName = localName, extracted from @qualifiedName,
332 * %Element.tagName = @qualifiedName.
333 */
334 GdomeElement *
335 gdome_xml_doc_createElementNS (GdomeDocument *self, GdomeDOMString *namespaceURI,
336 GdomeDOMString *qualifiedName, GdomeException *exc) {
337 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
338 gchar **strs;
339 gchar *prefix = NULL;
340 gchar *localName = NULL;
341 xmlNs *ns = NULL;
342 xmlNode *result = NULL;
343
344 g_return_val_if_fail (priv != NULL, NULL);
345 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
346 g_return_val_if_fail (qualifiedName != NULL, NULL);
347 g_return_val_if_fail (exc != NULL, NULL);
348
349 /* test malformed qualifiedName */
350 strs = g_strsplit((gchar *)qualifiedName->str, ":", 0);
351 if (strs[0] && strs[1] && !strs[2]) { /* there's one ':' */
352 if (namespaceURI == NULL) {
353 *exc = GDOME_NAMESPACE_ERR;
354 } else {
355 prefix = g_strdup(strs[0]);
356 localName = g_strdup(strs[1]);
357 if ((!strcmp (prefix, "xml") && strcmp(namespaceURI->str, "http://www.w3.org/XML/1998/namespace"))) {
358 *exc = GDOME_NAMESPACE_ERR;
359 }
360 }
361 } else if (!strs[1] && namespaceURI != NULL) { /* there's no ':' */
362 localName = g_strdup(strs[0]);
363 } else if (strs[0] && strs[1] && strs[2]) { /* there are more than one ':' */
364 *exc = GDOME_NAMESPACE_ERR;
365 }
366 g_strfreev(strs);
367
368 if (!*exc) {
369 if (localName != NULL) {
370 ns = gdome_xmlNewNs (priv->n, namespaceURI->str, prefix);
371 result = xmlNewDocNode (priv->n, ns, (xmlChar *)localName, NULL);
372 if (prefix != NULL)
373 g_free (prefix);
374 g_free (localName);
375 } else
376 result = xmlNewDocNode (priv->n, NULL, (xmlChar *)qualifiedName->str, NULL);
377
378 return (GdomeElement *)gdome_xml_n_mkref (result);
379 } else {
380 if (localName != NULL) {
381 g_free (prefix);
382 g_free (localName);
383 }
384
385 return NULL;
386 }
387 }
388
389 /**
390 * gdome_xml_doc_createDocumentFragment:
391 * @self: Document Object ref
392 * @exc: Exception Object ref
393 *
394 * Creates an empty DocumentFragment object.
395 * Returns: the new DocumentFragment object.
396 */
397 GdomeDocumentFragment *
398 gdome_xml_doc_createDocumentFragment (GdomeDocument *self, GdomeException *exc)
399 {
400 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
401 xmlNode *result = NULL;
402
403 g_return_val_if_fail (priv != NULL, NULL);
404 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
405 g_return_val_if_fail (exc != NULL, NULL);
406
407 result = xmlNewDocFragment (priv->n);
408
409 return (GdomeDocumentFragment *)gdome_xml_n_mkref (result);
410 }
411
412 /**
413 * gdome_xml_doc_createTextNode:
414 * @self: Document Object ref
415 * @data: The data for the node
416 * @exc: Exception Object ref
417 *
418 * Creates a Text node given the specified string.
419 * Returns: The new TextNode object.
420 */
421 GdomeText *
422 gdome_xml_doc_createTextNode (GdomeDocument *self, GdomeDOMString *data, GdomeException *exc)
423 {
424 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
425 xmlNode *result = NULL;
426
427 g_return_val_if_fail (priv != NULL, NULL);
428 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
429 g_return_val_if_fail (data != NULL, NULL);
430 g_return_val_if_fail (exc != NULL, NULL);
431
432 result = xmlNewDocText (priv->n, data->str);
433
434 return (GdomeText *)gdome_xml_n_mkref (result);
435 }
436
437 /**
438 * gdome_xml_doc_createComment:
439 * @self: Document Object ref
440 * @data: The data for the comment contents
441 * @exc: Exception Object ref
442 *
443 * Creates a Comment node whose value is the specified string.
444 * Returns: the new Comment object.
445 */
446 GdomeComment *
447 gdome_xml_doc_createComment (GdomeDocument *self, GdomeDOMString *data, GdomeException *exc)
448 {
449 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
450 xmlNode *result = NULL;
451
452 g_return_val_if_fail (priv != NULL, NULL);
453 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
454 g_return_val_if_fail (data != NULL, NULL);
455 g_return_val_if_fail (exc != NULL, NULL);
456
457 result = xmlNewComment (data->str);
458 gdome_xmlSetOwner (result, priv->n);
459
460 return (GdomeComment *)gdome_xml_n_mkref (result);
461 }
462
463 /**
464 * gdome_xml_doc_createCDATASection:
465 * @self: Document Object ref
466 * @data: The data for the CDATASection contents
467 * @exc: Exception Object ref
468 *
469 * Creates a CDATASection node whose value is the specified string.
470 * Returns: the new CDATASection object.
471 */
472 GdomeCDATASection *
473 gdome_xml_doc_createCDATASection (GdomeDocument *self, GdomeDOMString *data, GdomeException *exc)
474 {
475 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
476 xmlNode *result = NULL;
477
478 g_return_val_if_fail (priv != NULL, NULL);
479 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
480 g_return_val_if_fail (data != NULL, NULL);
481 g_return_val_if_fail (exc != NULL, NULL);
482
483 result = xmlNewCDataBlock (priv->n, data->str, strlen (data->str));
484 gdome_xmlSetOwner (result, priv->n);
485
486 return (GdomeCDATASection *)gdome_xml_n_mkref (result);
487 }
488
489 /**
490 * gdome_xml_doc_createProcessingInstruction:
491 * @self: Document Object ref
492 * @target: The target part of the processing instruction
493 * @data: The data for the node
494 * @exc: Exception Object ref
495 *
496 * Creates a ProcessingInstruction node given the specified name and data
497 * strings.
498 * Returns: The new ProcessingInstruction object.
499 */
500 GdomeProcessingInstruction *
501 gdome_xml_doc_createProcessingInstruction (GdomeDocument *self, GdomeDOMString *target, GdomeDOMString *data, GdomeException *exc)
502 {
503 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
504 xmlNode *result;
505
506 g_return_val_if_fail (priv != NULL, NULL);
507 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
508 g_return_val_if_fail (target != NULL, NULL);
509 g_return_val_if_fail (data != NULL, NULL);
510 g_return_val_if_fail (exc != NULL, NULL);
511
512 result = xmlNewPI (target->str, data->str);
513 gdome_xmlSetOwner (result, priv->n);
514
515 return (GdomeProcessingInstruction *)gdome_xml_n_mkref (result);
516 }
517
518 /**
519 * gdome_xml_doc_createAttribute:
520 * @self: Document Object ref
521 * @name: The name of the attribute
522 * @exc: Exception Object ref
523 *
524 * Creates an Attr of the given @name. Note that the Attr instance can then be
525 * set on an Element using the setAttributeNode method. To create an attribute
526 * with a qualified name and namespace URI, use the
527 * gdome_doc_createAttributeNS() method.
528 * Returns: a new Attr object with the nodeName attribute set to @name, and
529 * localName, prefix, and namespaceURI set to %NULL. The value of the attribute
530 * is the empty string.
531 */
532 GdomeAttr *
533 gdome_xml_doc_createAttribute (GdomeDocument *self, GdomeDOMString *name, GdomeException *exc)
534 {
535 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
536 xmlAttr *result = NULL;
537
538 g_return_val_if_fail (priv != NULL, NULL);
539 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
540 g_return_val_if_fail (name != NULL, NULL);
541 g_return_val_if_fail (exc != NULL, NULL);
542
543 result = xmlNewDocProp (priv->n, name->str, NULL);
544
545 return (GdomeAttr *)gdome_xml_n_mkref ((xmlNode *)result);
546 }
547
548 /**
549 * gdome_xml_doc_createAttributeNS:
550 * @self: Document Object ref
551 * @namespaceURI: The namespace URI of the attribute to create
552 * @qualifiedName: The qualified name of the attribute to create
553 * @exc: Exception Object ref
554 *
555 * Creates an attribute of the given qualified name and namespace URI.
556 *
557 * %GDOME_NAMESPACE_ERR: Raised if the @qualifiedName is malformed, if the
558 * @qualifiedName has a prefix and the @namespaceURI is %NUKK, if the
559 * @qualifiedName has a prefix that is "xml" and the @namespaceURI is different
560 * from "http://www.w3.org/XML/1998/namespace", or if the @qualifiedName is
561 * "xmlns" and the @namespaceURI is different from "http://www.w3.org/2000/xmlns/".
562 * Returns: a new Attr object with the following attributes:
563 * %Node.nodeName = @qualifiedName,
564 * %Node.namespaceURI = @namespaceURI,
565 * %Node.prefix = prefix, extracted from @qualifiedName,
566 * %Node.localName = localName, extracted from @qualifiedName,
567 * %Attr.name = @qualifiedName,
568 * %Node.nodeValue = the empty string.
569 */
570 GdomeAttr *
571 gdome_xml_doc_createAttributeNS (GdomeDocument *self, GdomeDOMString *namespaceURI,
572 GdomeDOMString *qualifiedName, GdomeException *exc) {
573 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
574 gchar **strs;
575 gchar *prefix = NULL;
576 gchar *localName = NULL;
577 xmlNs *ns = NULL;
578 xmlAttr *result = NULL;
579
580 g_return_val_if_fail (priv != NULL, NULL);
581 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
582 g_return_val_if_fail (qualifiedName != NULL, NULL);
583 g_return_val_if_fail (exc != NULL, NULL);
584
585 /* test malformed qualifiedName */
586 strs = g_strsplit((gchar *)qualifiedName->str, ":", 0);
587 if (strs[0] && strs[1] && !strs[2]) { /* there's only one ':' */
588 if (namespaceURI == NULL) {
589 *exc = GDOME_NAMESPACE_ERR;
590 } else {
591 prefix = g_strdup(strs[0]);
592 localName = g_strdup(strs[1]);
593 if ((!strcmp (prefix, "xml") && strcmp(namespaceURI->str, "http://www.w3.org/XML/1998/namespace"))) {
594 *exc = GDOME_NAMESPACE_ERR;
595 }
596 }
597 } else if (!strs[1] && namespaceURI != NULL) { /* there's no ':' */
598 prefix = g_strdup("");
599 localName = g_strdup(strs[0]);
600 if (strcmp (localName, "xmlns"))
601 *exc = GDOME_NAMESPACE_ERR;
602 } else if (strs[0] && strs[1] && strs[2]) { /* there are more than one ':' */
603 *exc = GDOME_NAMESPACE_ERR;
604 }
605 g_strfreev(strs);
606
607 if (!*exc) {
608 if (localName != NULL) {
609 result = xmlNewDocProp (priv->n, (xmlChar *)localName, NULL);
610 ns = gdome_xmlNewNs (priv->n, namespaceURI->str, prefix);
611 xmlSetNs((xmlNode *)result, ns);
612 g_free (prefix);
613 g_free (localName);
614 } else
615 result = xmlNewDocProp (priv->n, (xmlChar *)qualifiedName->str, NULL);
616
617 return (GdomeAttr *)gdome_xml_n_mkref ((xmlNode *)result);
618 } else {
619 if (localName != NULL) {
620 g_free (prefix);
621 g_free (localName);
622 }
623
624 return NULL;
625 }
626 }
627
628 /**
629 * gdome_xml_doc_createEntityReference:
630 * @self: Document Object ref
631 * @name: The name of the entity to reference
632 * @exc: Exception Object ref
633 *
634 * Creates an EntityReference object. In addition, if the referenced entity is
635 * known, the child list of the EntityReference node is made the same as that
636 * of the corresponding Entity node.
637 * Returns: the new EntityReference object.
638 */
639 GdomeEntityReference *
640 gdome_xml_doc_createEntityReference (GdomeDocument *self, GdomeDOMString *name, GdomeException *exc)
641 {
642 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
643 xmlNode *result;
644
645 g_return_val_if_fail (priv != NULL, NULL);
646 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
647 g_return_val_if_fail (name != NULL, NULL);
648 g_return_val_if_fail (exc != NULL, NULL);
649
650 result = xmlNewReference (priv->n, name->str);
651
652 return (GdomeEntityReference *)gdome_xml_n_mkref (result);
653 }
654
655 /* Hash Scanner function for gdome_xml_doc_getElementById */
656 void
657 idsHashScanner (void *payload, void *data, xmlChar *name) {
658 idsIterator *priv = (idsIterator *)data;
659
660 if (priv->element == NULL && xmlStrEqual (name, priv->elementId))
661 priv->element = (xmlNode *)gdome_xmlGetParent ((xmlNode *)((xmlID *)payload)->attr);
662 }
663
664 /**
665 * gdome_xml_doc_getElementById:
666 * @self: Document Object ref
667 * @elementId: The unique id value for an element
668 * @exc: Exception Object ref
669 *
670 * Returns: the Element whose %ID is given by @elementId. If no such element
671 * exists, returns %NULL. Behavior is not defined if more than one element has
672 * this %ID.
673 */
674 GdomeElement *
675 gdome_xml_doc_getElementById (GdomeDocument *self, GdomeDOMString *elementId, GdomeException *exc) {
676 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
677 xmlHashTable *ids = NULL;
678 idsIterator iter;
679
680 g_return_val_if_fail (priv != NULL, NULL);
681 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
682 g_return_val_if_fail (elementId != NULL, NULL);
683 g_return_val_if_fail (exc != NULL, NULL);
684
685 ids = (xmlHashTable *)priv->n->ids;
686
687 iter.elementId = (xmlChar *)elementId->str;
688 iter.element = NULL;
689 xmlHashScan(ids, idsHashScanner, &iter);
690
691 return (GdomeElement *)gdome_xml_n_mkref (iter.element);
692 }
693
694 /**
695 * gdome_xml_doc_getElementsByTagName:
696 * @self: Document Object ref
697 * @tagName: The name of the tag to match on. The special value * matches
698 * all tags.
699 * @exc: Exception Object ref
700 *
701 * Returns: a NodeList of all the elements with a given tag name in the order
702 * in which they are encountered in a preorder traversal of the Document tree.
703 */
704 GdomeNodeList *
705 gdome_xml_doc_getElementsByTagName (GdomeDocument *self, GdomeDOMString *tagname, GdomeException *exc) {
706 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
707
708 g_return_val_if_fail (priv != NULL, NULL);
709 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
710 g_return_val_if_fail (tagname != NULL, NULL);
711 g_return_val_if_fail (exc != NULL, NULL);
712
713 return gdome_xml_nl_mkref ((GdomeNode*)self, tagname, NULL, GDOME_READWRITE_NODE);
714 }
715
716 /**
717 * gdome_xml_doc_getElementsByTagNameNS:
718 * @self: Document Objects ref
719 * @namespaceURI: The namespace URI of the elements to match on. The special
720 * value * matches all namespaces.
721 * @localName: The local name of the elements to match on. The special value
722 * * matches all local names.
723 * @exc: Document Object ref
724 *
725 * Returns: a NodeList of all the descendant elements with a given local name
726 * and namespace URI in the order in which they are encountered in a preorder
727 * traversal of this Document tree.
728 */
729 GdomeNodeList *
730 gdome_xml_doc_getElementsByTagNameNS (GdomeDocument *self, GdomeDOMString *namespaceURI,
731 GdomeDOMString *localName, GdomeException *exc) {
732 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
733
734 g_return_val_if_fail (priv != NULL, NULL);
735 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
736 g_return_val_if_fail (namespaceURI != NULL, NULL);
737 g_return_val_if_fail (localName != NULL, NULL);
738 g_return_val_if_fail (exc != NULL, NULL);
739
740 return gdome_xml_nl_mkref ((GdomeNode*)self, localName, namespaceURI, GDOME_READWRITE_NODE);
741 }
742
743 /**
744 * gdome_xml_doc_importNode:
745 * @self: Document Objects ref
746 * @importedNode: The node to import.
747 * @deep: If %TRUE, recursively import the subtree under the specified node;
748 * if %FALSE, import only the node itself. This has no effect on Attr,
749 * EntityReference, and Notation nodes.
750 * @exc: Exception Object ref
751 *
752 *
753 * Imports a node from another document to this document. The returned node has
754 * no parent; (parentNode is %NULL). The source node is not altered or removed
755 * from the original document; this method creates a new copy of the source
756 * node. %GDOME_DOCUMENT_NODE, %GDOME_DOCUMENT_TYPE_NODE, %GDOME_NOTATION_NODE
757 * and %GDOME_ENTITY_NODE nodes are not supported.
758 *
759 * %GDOME_NOT_SUPPORTED_ERR: Raised if the type of node being imported is not
760 * supported.
761 * Returns: the imported node that belongs to this Document.
762 */
763 GdomeNode *
764 gdome_xml_doc_importNode (GdomeDocument *self, GdomeNode *importedNode, GdomeBoolean deep, GdomeException *exc) {
765 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
766 Gdome_xml_Node *priv_node = (Gdome_xml_Node *)importedNode;
767 xmlNode *ret = NULL;
768
769 g_return_val_if_fail (priv != NULL, NULL);
770 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
771 g_return_val_if_fail (importedNode != NULL, NULL);
772 g_return_val_if_fail (exc != NULL, NULL);
773
774 switch (gdome_xml_n_nodeType (importedNode, exc)) {
775 case XML_ATTRIBUTE_NODE:
776 g_assert (gdome_xmlGetOwner ((xmlNode *)priv->n) == priv->n);
777 ret = (xmlNode *)xmlCopyProp ((xmlNode *)priv->n, (xmlAttr *)priv_node->n);
778 gdome_xmlSetParent (ret, NULL);
779 break;
780 case XML_DOCUMENT_FRAG_NODE:
781 case XML_ELEMENT_NODE:
782 case XML_ENTITY_REF_NODE:
783 case XML_PI_NODE:
784 case XML_TEXT_NODE:
785 case XML_CDATA_SECTION_NODE:
786 case XML_COMMENT_NODE:
787 ret = xmlCopyNode (priv_node->n, deep);
788 xmlSetTreeDoc (ret, priv->n);
789 break;
790 default:
791 *exc = GDOME_NOT_SUPPORTED_ERR;
792 }
793
794 return gdome_xml_n_mkref (ret);
795 }
796
797 /**
798 * gdome_xml_doc_createEvent:
799 * @self: Document Objects ref
800 * @eventType: The @eventType parameter specifies the type of Event interface
801 * to be created.
802 * @exc: Exception Object ref
803 *
804 * If the Event interface specified is supported by the implementation this
805 * method will return a new Event of the interface type requested. If the Event
806 * is to be dispatched via the dispatchEvent method the appropriate event init
807 * method must be called after creation in order to initialize the Event's
808 * values. As an example, a user wishing to synthesize some kind of
809 * MutationEvents would call createEvent with the parameter "MutationEvents".
810 * The gdome_mevnt_initMuatationEvent() method could then be called on the
811 * newly created MutationEvent to set the specific type of MutationEvent to be
812 * dispatched and set its context information.
813 *
814 * GDOME_NOT_SUPPORTED_ERR: Raised if the implementation does not support the
815 * type of Event interface requested
816 * Returns: the newly created Event.
817 */
818 GdomeEvent *
819 gdome_xml_doc_createEvent (GdomeDocument *self, GdomeDOMString *eventType, GdomeException *exc) {
820 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
821
822 g_return_val_if_fail (priv != NULL, NULL);
823 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), NULL);
824 g_return_val_if_fail (eventType != NULL, NULL);
825 g_return_val_if_fail (exc != NULL, NULL);
826
827 if (!strcmp (eventType->str, "MutationEvents"))
828 return (GdomeEvent *)gdome_evt_mevnt_mkref ();
829 else if (!strcmp (eventType->str, "Events"))
830 return gdome_evt_evnt_mkref ();
831 else {
832 *exc = GDOME_NOT_SUPPORTED_ERR;
833 return NULL;
834 }
835 }
836
837 void
838 gdome_xml_doc_enableEventByName (GdomeDocument *self, const char *name)
839 {
840 gdome_xml_doc_enableEventByCode (self, gdome_evt_evnt_codeOfName (name));
841 }
842
843 void
844 gdome_xml_doc_enableEventByCode (GdomeDocument *self, guint32 code)
845 {
846 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
847
848 g_return_if_fail (priv != NULL);
849 g_return_if_fail (GDOME_XML_IS_DOC (priv));
850
851 priv->event_mask |= code;
852 }
853
854 void
855 gdome_xml_doc_disableEventByName (GdomeDocument *self, const char *name)
856 {
857 gdome_xml_doc_disableEventByCode (self, gdome_evt_evnt_codeOfName (name));
858 }
859
860 void
861 gdome_xml_doc_disableEventByCode (GdomeDocument *self, guint32 code)
862 {
863 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
864
865 g_return_if_fail (priv != NULL);
866 g_return_if_fail (GDOME_XML_IS_DOC (priv));
867
868 priv->event_mask &= ~code;
869 }
870
871 /**
872 * gdome_xml_doc_event_is_enabled:
873 * @self: Document Object ref
874 * @mask: The event type to check
875 *
876 * Returns: TRUE if the specified event type is enabled
877 */
878 GdomeBoolean
879 gdome_xml_doc_eventEnabledByName (GdomeDocument *self, const char *name)
880 {
881 return gdome_xml_doc_eventEnabledByCode (self, gdome_evt_evnt_codeOfName (name));
882 }
883
884 GdomeBoolean
885 gdome_xml_doc_eventEnabledByCode (GdomeDocument *self, guint32 code)
886 {
887 Gdome_xml_Document *priv = (Gdome_xml_Document *)self;
888
889 g_return_val_if_fail (priv != NULL, FALSE);
890 g_return_val_if_fail (GDOME_XML_IS_DOC (priv), FALSE);
891
892 return (priv->event_mask & code) == code;
893 }