Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / msxml3 / queryresult.c
blob50e8b79a83f322340a5c77829f0aef1c4e000f8b
1 /*
2 * XPath query result node list implementation (TODO: XSLPattern support)
4 * Copyright 2005 Mike McCormack
5 * Copyright 2007 Mikolaj Zalewski
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
24 #include "config.h"
26 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "ole2.h"
31 #include "msxml2.h"
33 #include "msxml_private.h"
35 #include "wine/debug.h"
37 /* This file implements the object returned by a XPath query. Note that this is
38 * not the IXMLDOMNodeList returned by childNodes - it's implemented in nodelist.c.
39 * They are different because the list returned by XPath queries:
40 * - is static - gives the results for the XML tree as it existed during the
41 * execution of the query
42 * - supports IXMLDOMSelection (TODO)
44 * TODO: XSLPattern support
47 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
49 #ifdef HAVE_LIBXML2
51 #include <libxml/xpath.h>
53 typedef struct _queryresult
55 DispatchEx dispex;
56 const struct IXMLDOMNodeListVtbl *lpVtbl;
57 LONG ref;
58 xmlNodePtr node;
59 xmlXPathObjectPtr result;
60 int resultPos;
61 } queryresult;
63 static inline queryresult *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
65 return (queryresult *)((char*)iface - FIELD_OFFSET(queryresult, lpVtbl));
68 #define XMLQUERYRES(x) ((IXMLDOMNodeList*)&(x)->lpVtbl)
70 static HRESULT WINAPI queryresult_QueryInterface(
71 IXMLDOMNodeList *iface,
72 REFIID riid,
73 void** ppvObject )
75 queryresult *This = impl_from_IXMLDOMNodeList( iface );
77 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
79 if(!ppvObject)
80 return E_INVALIDARG;
82 if ( IsEqualGUID( riid, &IID_IUnknown ) ||
83 IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
85 *ppvObject = iface;
87 else if(dispex_query_interface(&This->dispex, riid, ppvObject))
89 return *ppvObject ? S_OK : E_NOINTERFACE;
91 else
93 FIXME("interface %s not implemented\n", debugstr_guid(riid));
94 *ppvObject = NULL;
95 return E_NOINTERFACE;
98 IXMLDOMNodeList_AddRef( iface );
100 return S_OK;
103 static ULONG WINAPI queryresult_AddRef(
104 IXMLDOMNodeList *iface )
106 queryresult *This = impl_from_IXMLDOMNodeList( iface );
107 return InterlockedIncrement( &This->ref );
110 static ULONG WINAPI queryresult_Release(
111 IXMLDOMNodeList *iface )
113 queryresult *This = impl_from_IXMLDOMNodeList( iface );
114 ULONG ref;
116 ref = InterlockedDecrement(&This->ref);
117 if ( ref == 0 )
119 xmlXPathFreeObject(This->result);
120 xmldoc_release(This->node->doc);
121 heap_free(This);
124 return ref;
127 static HRESULT WINAPI queryresult_GetTypeInfoCount(
128 IXMLDOMNodeList *iface,
129 UINT* pctinfo )
131 queryresult *This = impl_from_IXMLDOMNodeList( iface );
133 TRACE("(%p)->(%p)\n", This, pctinfo);
135 *pctinfo = 1;
137 return S_OK;
140 static HRESULT WINAPI queryresult_GetTypeInfo(
141 IXMLDOMNodeList *iface,
142 UINT iTInfo,
143 LCID lcid,
144 ITypeInfo** ppTInfo )
146 queryresult *This = impl_from_IXMLDOMNodeList( iface );
147 HRESULT hr;
149 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
151 hr = get_typeinfo(IXMLDOMNodeList_tid, ppTInfo);
153 return hr;
156 static HRESULT WINAPI queryresult_GetIDsOfNames(
157 IXMLDOMNodeList *iface,
158 REFIID riid,
159 LPOLESTR* rgszNames,
160 UINT cNames,
161 LCID lcid,
162 DISPID* rgDispId )
164 queryresult *This = impl_from_IXMLDOMNodeList( iface );
165 ITypeInfo *typeinfo;
166 HRESULT hr;
168 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
169 lcid, rgDispId);
171 if(!rgszNames || cNames == 0 || !rgDispId)
172 return E_INVALIDARG;
174 hr = get_typeinfo(IXMLDOMNodeList_tid, &typeinfo);
175 if(SUCCEEDED(hr))
177 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
178 ITypeInfo_Release(typeinfo);
181 return hr;
184 static HRESULT WINAPI queryresult_Invoke(
185 IXMLDOMNodeList *iface,
186 DISPID dispIdMember,
187 REFIID riid,
188 LCID lcid,
189 WORD wFlags,
190 DISPPARAMS* pDispParams,
191 VARIANT* pVarResult,
192 EXCEPINFO* pExcepInfo,
193 UINT* puArgErr )
195 queryresult *This = impl_from_IXMLDOMNodeList( iface );
196 ITypeInfo *typeinfo;
197 HRESULT hr;
199 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
200 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
202 hr = get_typeinfo(IXMLDOMNodeList_tid, &typeinfo);
203 if(SUCCEEDED(hr))
205 hr = ITypeInfo_Invoke(typeinfo, &(This->lpVtbl), dispIdMember, wFlags, pDispParams,
206 pVarResult, pExcepInfo, puArgErr);
207 ITypeInfo_Release(typeinfo);
210 return hr;
213 static HRESULT WINAPI queryresult_get_item(
214 IXMLDOMNodeList* iface,
215 LONG index,
216 IXMLDOMNode** listItem)
218 queryresult *This = impl_from_IXMLDOMNodeList( iface );
220 TRACE("%p %d\n", This, index);
222 if(!listItem)
223 return E_INVALIDARG;
225 *listItem = NULL;
227 if (index < 0 || index >= xmlXPathNodeSetGetLength(This->result->nodesetval))
228 return S_FALSE;
230 *listItem = create_node(This->result->nodesetval->nodeTab[index]);
231 This->resultPos = index + 1;
233 return S_OK;
236 static HRESULT WINAPI queryresult_get_length(
237 IXMLDOMNodeList* iface,
238 LONG* listLength)
240 queryresult *This = impl_from_IXMLDOMNodeList( iface );
242 TRACE("%p\n", This);
244 if(!listLength)
245 return E_INVALIDARG;
247 *listLength = xmlXPathNodeSetGetLength(This->result->nodesetval);
248 return S_OK;
251 static HRESULT WINAPI queryresult_nextNode(
252 IXMLDOMNodeList* iface,
253 IXMLDOMNode** nextItem)
255 queryresult *This = impl_from_IXMLDOMNodeList( iface );
257 TRACE("%p %p\n", This, nextItem );
259 if(!nextItem)
260 return E_INVALIDARG;
262 *nextItem = NULL;
264 if (This->resultPos >= xmlXPathNodeSetGetLength(This->result->nodesetval))
265 return S_FALSE;
267 *nextItem = create_node(This->result->nodesetval->nodeTab[This->resultPos]);
268 This->resultPos++;
269 return S_OK;
272 static HRESULT WINAPI queryresult_reset(
273 IXMLDOMNodeList* iface)
275 queryresult *This = impl_from_IXMLDOMNodeList( iface );
277 TRACE("%p\n", This);
278 This->resultPos = 0;
279 return S_OK;
282 static HRESULT WINAPI queryresult__newEnum(
283 IXMLDOMNodeList* iface,
284 IUnknown** ppUnk)
286 FIXME("\n");
287 return E_NOTIMPL;
291 static const struct IXMLDOMNodeListVtbl queryresult_vtbl =
293 queryresult_QueryInterface,
294 queryresult_AddRef,
295 queryresult_Release,
296 queryresult_GetTypeInfoCount,
297 queryresult_GetTypeInfo,
298 queryresult_GetIDsOfNames,
299 queryresult_Invoke,
300 queryresult_get_item,
301 queryresult_get_length,
302 queryresult_nextNode,
303 queryresult_reset,
304 queryresult__newEnum,
307 static HRESULT queryresult_get_dispid(IUnknown *iface, BSTR name, DWORD flags, DISPID *dispid)
309 queryresult *This = impl_from_IXMLDOMNodeList( (IXMLDOMNodeList*)iface );
310 WCHAR *ptr;
311 int idx = 0;
313 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
314 idx = idx*10 + (*ptr-'0');
315 if(*ptr)
316 return DISP_E_UNKNOWNNAME;
318 if(idx >= xmlXPathNodeSetGetLength(This->result->nodesetval))
319 return DISP_E_UNKNOWNNAME;
321 *dispid = MSXML_DISPID_CUSTOM_MIN + idx;
322 TRACE("ret %x\n", *dispid);
323 return S_OK;
326 static HRESULT queryresult_invoke(IUnknown *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
327 VARIANT *res, EXCEPINFO *ei)
329 queryresult *This = impl_from_IXMLDOMNodeList( (IXMLDOMNodeList*)iface );
331 TRACE("(%p)->(%x %x %x %p %p %p)\n", This, id, lcid, flags, params, res, ei);
333 V_VT(res) = VT_DISPATCH;
334 V_DISPATCH(res) = NULL;
336 switch(flags)
338 case INVOKE_PROPERTYGET:
340 IXMLDOMNode *disp = NULL;
342 queryresult_get_item(XMLQUERYRES(This), id - MSXML_DISPID_CUSTOM_MIN, &disp);
343 V_DISPATCH(res) = (IDispatch*)disp;
344 break;
346 default:
348 FIXME("unimplemented flags %x\n", flags);
349 break;
353 TRACE("ret %p\n", V_DISPATCH(res));
355 return S_OK;
358 static const dispex_static_data_vtbl_t queryresult_dispex_vtbl = {
359 queryresult_get_dispid,
360 queryresult_invoke
363 static const tid_t queryresult_iface_tids[] = {
364 IXMLDOMNodeList_tid,
367 static dispex_static_data_t queryresult_dispex = {
368 &queryresult_dispex_vtbl,
369 IXMLDOMSelection_tid,
370 NULL,
371 queryresult_iface_tids
374 HRESULT queryresult_create(xmlNodePtr node, LPWSTR szQuery, IXMLDOMNodeList **out)
376 queryresult *This = heap_alloc_zero(sizeof(queryresult));
377 xmlXPathContextPtr ctxt = xmlXPathNewContext(node->doc);
378 xmlChar *str = xmlChar_from_wchar(szQuery);
379 HRESULT hr;
382 TRACE("(%p, %s, %p)\n", node, wine_dbgstr_w(szQuery), out);
384 *out = NULL;
385 if (This == NULL || ctxt == NULL || str == NULL)
387 hr = E_OUTOFMEMORY;
388 goto cleanup;
391 This->lpVtbl = &queryresult_vtbl;
392 This->ref = 1;
393 This->resultPos = 0;
394 This->node = node;
395 xmldoc_add_ref(This->node->doc);
397 ctxt->node = node;
398 This->result = xmlXPathEval(str, ctxt);
399 if (!This->result || This->result->type != XPATH_NODESET)
401 hr = E_FAIL;
402 goto cleanup;
405 init_dispex(&This->dispex, (IUnknown*)&This->lpVtbl, &queryresult_dispex);
407 *out = (IXMLDOMNodeList *) &This->lpVtbl;
408 hr = S_OK;
409 TRACE("found %d matches\n", xmlXPathNodeSetGetLength(This->result->nodesetval));
411 cleanup:
412 if (This != NULL && FAILED(hr))
413 IXMLDOMNodeList_Release( (IXMLDOMNodeList*) &This->lpVtbl );
414 if (ctxt != NULL)
415 xmlXPathFreeContext(ctxt);
416 HeapFree(GetProcessHeap(), 0, str);
417 return hr;
420 #endif