include: Specify calling convention explicitly in idl files where needed.
[wine/hramrach.git] / dlls / msxml3 / queryresult.c
blob2c6f23ed3f6b98fb9c925eb25c9e399a8f01b192
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 %p)\n", This, index, listItem);
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(xmlXPathNodeSetItem(This->result->nodesetval, 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)->(%p)\n", This, listLength);
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(xmlXPathNodeSetItem(This->result->nodesetval, 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 queryresult *This = impl_from_IXMLDOMNodeList( iface );
287 FIXME("(%p)->(%p)\n", This, ppUnk);
288 return E_NOTIMPL;
292 static const struct IXMLDOMNodeListVtbl queryresult_vtbl =
294 queryresult_QueryInterface,
295 queryresult_AddRef,
296 queryresult_Release,
297 queryresult_GetTypeInfoCount,
298 queryresult_GetTypeInfo,
299 queryresult_GetIDsOfNames,
300 queryresult_Invoke,
301 queryresult_get_item,
302 queryresult_get_length,
303 queryresult_nextNode,
304 queryresult_reset,
305 queryresult__newEnum,
308 static HRESULT queryresult_get_dispid(IUnknown *iface, BSTR name, DWORD flags, DISPID *dispid)
310 queryresult *This = impl_from_IXMLDOMNodeList( (IXMLDOMNodeList*)iface );
311 WCHAR *ptr;
312 int idx = 0;
314 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
315 idx = idx*10 + (*ptr-'0');
316 if(*ptr)
317 return DISP_E_UNKNOWNNAME;
319 if(idx >= xmlXPathNodeSetGetLength(This->result->nodesetval))
320 return DISP_E_UNKNOWNNAME;
322 *dispid = MSXML_DISPID_CUSTOM_MIN + idx;
323 TRACE("ret %x\n", *dispid);
324 return S_OK;
327 static HRESULT queryresult_invoke(IUnknown *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
328 VARIANT *res, EXCEPINFO *ei)
330 queryresult *This = impl_from_IXMLDOMNodeList( (IXMLDOMNodeList*)iface );
332 TRACE("(%p)->(%x %x %x %p %p %p)\n", This, id, lcid, flags, params, res, ei);
334 V_VT(res) = VT_DISPATCH;
335 V_DISPATCH(res) = NULL;
337 switch(flags)
339 case INVOKE_PROPERTYGET:
341 IXMLDOMNode *disp = NULL;
343 queryresult_get_item(XMLQUERYRES(This), id - MSXML_DISPID_CUSTOM_MIN, &disp);
344 V_DISPATCH(res) = (IDispatch*)disp;
345 break;
347 default:
349 FIXME("unimplemented flags %x\n", flags);
350 break;
354 TRACE("ret %p\n", V_DISPATCH(res));
356 return S_OK;
359 static const dispex_static_data_vtbl_t queryresult_dispex_vtbl = {
360 queryresult_get_dispid,
361 queryresult_invoke
364 static const tid_t queryresult_iface_tids[] = {
365 IXMLDOMNodeList_tid,
368 static dispex_static_data_t queryresult_dispex = {
369 &queryresult_dispex_vtbl,
370 IXMLDOMSelection_tid,
371 NULL,
372 queryresult_iface_tids
375 HRESULT queryresult_create(xmlNodePtr node, LPWSTR szQuery, IXMLDOMNodeList **out)
377 queryresult *This = heap_alloc_zero(sizeof(queryresult));
378 xmlXPathContextPtr ctxt = xmlXPathNewContext(node->doc);
379 xmlChar *str = xmlChar_from_wchar(szQuery);
380 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 xmlXPathFreeContext(ctxt);
415 heap_free(str);
416 return hr;
419 #endif