Debian Codegraph @

libgdome/gdomecore/gdome-xml-pi.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2 /* gdome-xml-pi.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 <glib.h>
26 #include "gdome.h"
27 #include "gdome-events.h"
28 #include <libxml/tree.h>
29 #include <libxml/parser.h>
30 #include <libxml/xmlmemory.h>
31 #include "gdome-xml-util.h"
32 #include "gdome-xml-str.h"
33 #include "gdome-xml-node.h"
34 #include "gdome-xml-xmlutil.h"
35 #include "gdome-evt-event.h"
36 #include "gdome-evt-mevent.h"
37 #include "gdome-evt-propagation.h"
38 #include "gdome-xml-pi.h"
39 #include "gdome-xml-document.h"
40
41 const GdomeProcessingInstructionVtab gdome_xml_pi_vtab = {
42 {
43 gdome_xml_n_ref,
44 gdome_xml_n_unref,
45 gdome_xml_pi_query_interface,
46 gdome_xml_n_nodeName,
47 gdome_xml_n_nodeValue,
48 gdome_xml_n_set_nodeValue,
49 gdome_xml_n_nodeType,
50 gdome_xml_n_parentNode,
51 gdome_xml_n_childNodes,
52 gdome_xml_n_firstChild,
53 gdome_xml_n_lastChild,
54 gdome_xml_n_previousSibling,
55 gdome_xml_n_nextSibling,
56 gdome_xml_n_attributes,
57 gdome_xml_n_ownerDocument,
58 gdome_xml_n_insertBefore,
59 gdome_xml_n_replaceChild,
60 gdome_xml_n_removeChild,
61 gdome_xml_n_appendChild,
62 gdome_xml_n_hasChildNodes,
63 gdome_xml_n_cloneNode,
64 gdome_xml_n_normalize,
65 gdome_xml_n_isSupported,
66 gdome_xml_n_namespaceURI,
67 gdome_xml_n_prefix,
68 gdome_xml_n_set_prefix,
69 gdome_xml_n_localName,
70 gdome_xml_n_hasAttributes,
71 gdome_xml_n_addEventListener,
72 gdome_xml_n_removeEventListener,
73 gdome_xml_n_dispatchEvent,
74 gdome_xml_n_subTreeDispatchEvent
75 },
76 gdome_xml_pi_target,
77 gdome_xml_pi_data,
78 gdome_xml_pi_set_data
79 };
80
81 /**
82 * gdome_xml_pi_query_interface:
83 * @self: Node Object ref
84 * @interface: interface needed
85 * @exc: Exception Object ref
86 *
87 * Returns: a reference to this object that implements the @interface needed,
88 * or %NULL if the @interface is not supported by this Object.
89 */
90 gpointer
91 gdome_xml_pi_query_interface (GdomeNode *self, const char *interface, GdomeException *exc)
92 {
93 Gdome_xml_ProcessingInstruction *priv = (Gdome_xml_ProcessingInstruction *)self;
94
95 g_return_val_if_fail (priv != NULL, NULL);
96 g_return_val_if_fail (GDOME_XML_IS_PI (priv), NULL);
97 g_return_val_if_fail (interface != NULL, NULL);
98 g_return_val_if_fail (exc != NULL, NULL);
99
100 if (!strcmp (interface, "Node") ||
101 !strcmp (interface, "ProcessingInstruction") ||
102 !strcmp (interface, "EventTarget")) {
103 priv->refcnt++;
104 return self;
105 }
106 else
107 return NULL;
108 }
109
110 /**
111 * gdome_xml_pi_target:
112 * @self: ProcessingInstruction Object ref
113 * @exc: Exception Object ref
114 *
115 * Returns: the target of this processing instruction. XML defines this as being
116 * the first token following the markup that begins the processing instruction.
117 */
118 GdomeDOMString *
119 gdome_xml_pi_target (GdomeProcessingInstruction *self, GdomeException *exc)
120 {
121 Gdome_xml_ProcessingInstruction *priv = (Gdome_xml_ProcessingInstruction *)self;
122
123 g_return_val_if_fail (priv != NULL, NULL);
124 g_return_val_if_fail (GDOME_XML_IS_PI (priv), NULL);
125 g_return_val_if_fail (exc != NULL, NULL);
126
127 return gdome_xml_str_mkref_dup((gchar *)gdome_xmlGetName (priv->n));
128 }
129
130 /**
131 * gdome_xml_pi_data:
132 * @self: ProcessingInstruction Object ref
133 * @exc: Exception Object ref
134 *
135 * Returns: the content of this processing instruction. This is from the first
136 * non white space character after the target to the character immediately
137 * preceding the %?>.
138 */
139 GdomeDOMString *
140 gdome_xml_pi_data (GdomeProcessingInstruction *self, GdomeException *exc)
141 {
142 Gdome_xml_ProcessingInstruction *priv = (Gdome_xml_ProcessingInstruction *)self;
143
144 g_return_val_if_fail (priv != NULL, NULL);
145 g_return_val_if_fail (GDOME_XML_IS_PI (priv), NULL);
146 g_return_val_if_fail (exc != NULL, NULL);
147
148 return gdome_xml_str_mkref_own (xmlNodeGetContent (priv->n));
149 }
150
151 /**
152 * gdome_xml_pi_set_data:
153 * @self: ProcessingInstruction Object ref
154 * @data: The string of data to be set
155 * @exc: Exception Object ref
156 *
157 * Set the content of this processing instruction.
158 *
159 * %GDOME_NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
160 */
161 void
162 gdome_xml_pi_set_data (GdomeProcessingInstruction *self, GdomeDOMString *data, GdomeException *exc)
163 {
164 Gdome_xml_ProcessingInstruction *priv = (Gdome_xml_ProcessingInstruction *)self;
165 GdomeMutationEvent *mev;
166 GdomeDOMString *prevValue;
167 GdomeNode *parent;
168
169 g_return_if_fail (priv != NULL);
170 g_return_if_fail (GDOME_XML_IS_PI (priv));
171 g_return_if_fail (data != NULL);
172 g_return_if_fail (exc != NULL);
173
174 if (GDOME_ISREADONLY (priv)) {
175 *exc = GDOME_NO_MODIFICATION_ALLOWED_ERR;
176 return;
177 }
178
179 xmlNodeSetContent (priv->n, (xmlChar *)data->str);
180
181 if (gdome_xml_n_eventEnabledByCode ((GdomeNode *) self, DOM_CHARACTER_DATA_MODIFIED_EVENT_TYPE)) {
182 /* Fire DOMCharacterDataModified */
183 mev = gdome_evt_mevnt_mkref ();
184 prevValue = gdome_xml_pi_data (self, exc);
185 gdome_str_ref (data);
186 gdome_evt_mevnt_initMutationEventByCode (mev, DOM_CHARACTER_DATA_MODIFIED_EVENT_TYPE,
187 TRUE, FALSE, NULL,
188 prevValue, data, NULL, 0, exc);
189 gdome_xml_n_dispatchEvent ((GdomeNode *)self, (GdomeEvent *)mev, exc);
190 gdome_xml_str_unref (data);
191 gdome_xml_str_unref (prevValue);
192 gdome_evt_mevnt_unref ((GdomeEvent *)mev, exc);
193 }
194
195 if (gdome_xml_n_eventEnabledByCode ((GdomeNode *) self, DOM_SUBTREE_MODIFIED_EVENT_TYPE)) {
196 /* Fire DOMSubtreeModified */
197 parent = gdome_xml_n_mkref (gdome_xmlGetParent (priv->n));
198 if (parent != NULL) {
199 mev = gdome_evt_mevnt_mkref ();
200 gdome_evt_mevnt_initMutationEventByCode (mev, DOM_SUBTREE_MODIFIED_EVENT_TYPE,
201 TRUE, FALSE, NULL,
202 NULL, NULL, NULL, 0, exc);
203 gdome_xml_n_dispatchEvent (parent, (GdomeEvent *)mev, exc);
204 gdome_xml_n_unref (parent, exc);
205 gdome_evt_mevnt_unref ((GdomeEvent *)mev, exc);
206 }
207 }
208 }