Sync with trunk head
[reactos.git] / dll / win32 / msxml3 / nodelist.c
1 /*
2 * Node list implementation
3 *
4 * Copyright 2005 Mike McCormack
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define COBJMACROS
22
23 #include "config.h"
24
25 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30 #include "msxml2.h"
31
32 #include "msxml_private.h"
33
34 #include "wine/debug.h"
35
36 /* This file implements the object returned by childNodes property. Note that this is
37 * not the IXMLDOMNodeList returned by XPath querites - it's implemented in queryresult.c.
38 * They are different because the list returned by childNodes:
39 * - is "live" - changes to the XML tree are automatically reflected in the list
40 * - doesn't supports IXMLDOMSelection
41 * - note that an attribute node have a text child in DOM but not in the XPath data model
42 * thus the child is inaccessible by an XPath query
43 */
44
45 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
46
47 #ifdef HAVE_LIBXML2
48
49 typedef struct _xmlnodelist
50 {
51 const struct IXMLDOMNodeListVtbl *lpVtbl;
52 LONG ref;
53 xmlNodePtr parent;
54 xmlNodePtr current;
55 } xmlnodelist;
56
57 static inline xmlnodelist *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
58 {
59 return (xmlnodelist *)((char*)iface - FIELD_OFFSET(xmlnodelist, lpVtbl));
60 }
61
62 static HRESULT WINAPI xmlnodelist_QueryInterface(
63 IXMLDOMNodeList *iface,
64 REFIID riid,
65 void** ppvObject )
66 {
67 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
68
69 if(!ppvObject)
70 return E_INVALIDARG;
71
72 if ( IsEqualGUID( riid, &IID_IUnknown ) ||
73 IsEqualGUID( riid, &IID_IDispatch ) ||
74 IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
75 {
76 *ppvObject = iface;
77 }
78 else
79 {
80 FIXME("interface %s not implemented\n", debugstr_guid(riid));
81 *ppvObject = NULL;
82 return E_NOINTERFACE;
83 }
84
85 IXMLDOMNodeList_AddRef( iface );
86
87 return S_OK;
88 }
89
90 static ULONG WINAPI xmlnodelist_AddRef(
91 IXMLDOMNodeList *iface )
92 {
93 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
94 return InterlockedIncrement( &This->ref );
95 }
96
97 static ULONG WINAPI xmlnodelist_Release(
98 IXMLDOMNodeList *iface )
99 {
100 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
101 ULONG ref;
102
103 ref = InterlockedDecrement( &This->ref );
104 if ( ref == 0 )
105 {
106 xmldoc_release( This->parent->doc );
107 heap_free( This );
108 }
109
110 return ref;
111 }
112
113 static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
114 IXMLDOMNodeList *iface,
115 UINT* pctinfo )
116 {
117 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
118
119 TRACE("(%p)->(%p)\n", This, pctinfo);
120
121 *pctinfo = 1;
122
123 return S_OK;
124 }
125
126 static HRESULT WINAPI xmlnodelist_GetTypeInfo(
127 IXMLDOMNodeList *iface,
128 UINT iTInfo,
129 LCID lcid,
130 ITypeInfo** ppTInfo )
131 {
132 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
133 HRESULT hr;
134
135 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
136
137 hr = get_typeinfo(IXMLDOMNodeList_tid, ppTInfo);
138
139 return hr;
140 }
141
142 static HRESULT WINAPI xmlnodelist_GetIDsOfNames(
143 IXMLDOMNodeList *iface,
144 REFIID riid,
145 LPOLESTR* rgszNames,
146 UINT cNames,
147 LCID lcid,
148 DISPID* rgDispId )
149 {
150 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
151 ITypeInfo *typeinfo;
152 HRESULT hr;
153
154 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
155 lcid, rgDispId);
156
157 if(!rgszNames || cNames == 0 || !rgDispId)
158 return E_INVALIDARG;
159
160 hr = get_typeinfo(IXMLDOMNodeList_tid, &typeinfo);
161 if(SUCCEEDED(hr))
162 {
163 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
164 ITypeInfo_Release(typeinfo);
165 }
166
167 return hr;
168 }
169
170 static HRESULT WINAPI xmlnodelist_Invoke(
171 IXMLDOMNodeList *iface,
172 DISPID dispIdMember,
173 REFIID riid,
174 LCID lcid,
175 WORD wFlags,
176 DISPPARAMS* pDispParams,
177 VARIANT* pVarResult,
178 EXCEPINFO* pExcepInfo,
179 UINT* puArgErr )
180 {
181 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
182 ITypeInfo *typeinfo;
183 HRESULT hr;
184
185 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
186 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
187
188 hr = get_typeinfo(IXMLDOMNodeList_tid, &typeinfo);
189 if(SUCCEEDED(hr))
190 {
191 hr = ITypeInfo_Invoke(typeinfo, &(This->lpVtbl), dispIdMember, wFlags, pDispParams,
192 pVarResult, pExcepInfo, puArgErr);
193 ITypeInfo_Release(typeinfo);
194 }
195
196 return hr;
197 }
198
199 static HRESULT WINAPI xmlnodelist_get_item(
200 IXMLDOMNodeList* iface,
201 LONG index,
202 IXMLDOMNode** listItem)
203 {
204 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
205 xmlNodePtr curr;
206 LONG nodeIndex = 0;
207
208 TRACE("(%p)->(%d %p)\n", This, index, listItem);
209
210 if(!listItem)
211 return E_INVALIDARG;
212
213 *listItem = NULL;
214
215 if (index < 0)
216 return S_FALSE;
217
218 curr = This->parent->children;
219 while(curr)
220 {
221 if(nodeIndex++ == index) break;
222 curr = curr->next;
223 }
224 if(!curr) return S_FALSE;
225
226 *listItem = create_node( curr );
227
228 return S_OK;
229 }
230
231 static HRESULT WINAPI xmlnodelist_get_length(
232 IXMLDOMNodeList* iface,
233 LONG* listLength)
234 {
235
236 xmlNodePtr curr;
237 LONG nodeCount = 0;
238
239 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
240
241 TRACE("(%p)->(%p)\n", This, listLength);
242
243 if(!listLength)
244 return E_INVALIDARG;
245
246 curr = This->parent->children;
247 while (curr)
248 {
249 nodeCount++;
250 curr = curr->next;
251 }
252
253 *listLength = nodeCount;
254 return S_OK;
255 }
256
257 static HRESULT WINAPI xmlnodelist_nextNode(
258 IXMLDOMNodeList* iface,
259 IXMLDOMNode** nextItem)
260 {
261 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
262
263 TRACE("(%p)->(%p)\n", This, nextItem );
264
265 if(!nextItem)
266 return E_INVALIDARG;
267
268 *nextItem = NULL;
269
270 if (!This->current)
271 return S_FALSE;
272
273 *nextItem = create_node( This->current );
274 This->current = This->current->next;
275 return S_OK;
276 }
277
278 static HRESULT WINAPI xmlnodelist_reset(
279 IXMLDOMNodeList* iface)
280 {
281 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
282
283 TRACE("%p\n", This);
284 This->current = This->parent->children;
285 return S_OK;
286 }
287
288 static HRESULT WINAPI xmlnodelist__newEnum(
289 IXMLDOMNodeList* iface,
290 IUnknown** ppUnk)
291 {
292 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
293 FIXME("(%p)->(%p)\n", This, ppUnk);
294 return E_NOTIMPL;
295 }
296
297
298 static const struct IXMLDOMNodeListVtbl xmlnodelist_vtbl =
299 {
300 xmlnodelist_QueryInterface,
301 xmlnodelist_AddRef,
302 xmlnodelist_Release,
303 xmlnodelist_GetTypeInfoCount,
304 xmlnodelist_GetTypeInfo,
305 xmlnodelist_GetIDsOfNames,
306 xmlnodelist_Invoke,
307 xmlnodelist_get_item,
308 xmlnodelist_get_length,
309 xmlnodelist_nextNode,
310 xmlnodelist_reset,
311 xmlnodelist__newEnum,
312 };
313
314 IXMLDOMNodeList* create_children_nodelist( xmlNodePtr node )
315 {
316 xmlnodelist *nodelist;
317
318 nodelist = heap_alloc( sizeof *nodelist );
319 if ( !nodelist )
320 return NULL;
321
322 nodelist->lpVtbl = &xmlnodelist_vtbl;
323 nodelist->ref = 1;
324 nodelist->parent = node;
325 nodelist->current = node->children;
326
327 xmldoc_add_ref( node->doc );
328
329 return (IXMLDOMNodeList*) &nodelist->lpVtbl;
330 }
331
332 #endif