2 * XPath/XSLPattern query result node list implementation
4 * Copyright 2005 Mike McCormack
5 * Copyright 2007 Mikolaj Zalewski
6 * Copyright 2010 Adam Martinson for CodeWeavers
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #include "msxml_private.h"
36 #include "wine/debug.h"
38 /* This file implements the object returned by a XPath query. Note that this is
39 * not the IXMLDOMNodeList returned by childNodes - it's implemented in nodelist.c.
40 * They are different because the list returned by XPath queries:
41 * - is static - gives the results for the XML tree as it existed during the
42 * execution of the query
43 * - supports IXMLDOMSelection (TODO)
47 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
51 #include <libxml/xpath.h>
52 #include <libxml/xpathInternals.h>
54 int registerNamespaces(xmlXPathContextPtr ctxt);
55 xmlChar* XSLPattern_to_XPath(xmlXPathContextPtr ctxt, xmlChar const* xslpat_str);
57 typedef struct _queryresult
60 const struct IXMLDOMNodeListVtbl *lpVtbl;
63 xmlXPathObjectPtr result;
67 static inline queryresult *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
69 return (queryresult *)((char*)iface - FIELD_OFFSET(queryresult, lpVtbl));
72 #define XMLQUERYRES(x) ((IXMLDOMNodeList*)&(x)->lpVtbl)
74 static HRESULT WINAPI queryresult_QueryInterface(
75 IXMLDOMNodeList *iface,
79 queryresult *This = impl_from_IXMLDOMNodeList( iface );
81 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
86 if ( IsEqualGUID( riid, &IID_IUnknown ) ||
87 IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
91 else if(dispex_query_interface(&This->dispex, riid, ppvObject))
93 return *ppvObject ? S_OK : E_NOINTERFACE;
97 FIXME("interface %s not implemented\n", debugstr_guid(riid));
102 IXMLDOMNodeList_AddRef( iface );
107 static ULONG WINAPI queryresult_AddRef(
108 IXMLDOMNodeList *iface )
110 queryresult *This = impl_from_IXMLDOMNodeList( iface );
111 return InterlockedIncrement( &This->ref );
114 static ULONG WINAPI queryresult_Release(
115 IXMLDOMNodeList *iface )
117 queryresult *This = impl_from_IXMLDOMNodeList( iface );
120 ref = InterlockedDecrement(&This->ref);
123 xmlXPathFreeObject(This->result);
124 xmldoc_release(This->node->doc);
131 static HRESULT WINAPI queryresult_GetTypeInfoCount(
132 IXMLDOMNodeList *iface,
135 queryresult *This = impl_from_IXMLDOMNodeList( iface );
137 TRACE("(%p)->(%p)\n", This, pctinfo);
144 static HRESULT WINAPI queryresult_GetTypeInfo(
145 IXMLDOMNodeList *iface,
148 ITypeInfo** ppTInfo )
150 queryresult *This = impl_from_IXMLDOMNodeList( iface );
153 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
155 hr = get_typeinfo(IXMLDOMNodeList_tid, ppTInfo);
160 static HRESULT WINAPI queryresult_GetIDsOfNames(
161 IXMLDOMNodeList *iface,
168 queryresult *This = impl_from_IXMLDOMNodeList( iface );
172 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
175 if(!rgszNames || cNames == 0 || !rgDispId)
178 hr = get_typeinfo(IXMLDOMNodeList_tid, &typeinfo);
181 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
182 ITypeInfo_Release(typeinfo);
188 static HRESULT WINAPI queryresult_Invoke(
189 IXMLDOMNodeList *iface,
194 DISPPARAMS* pDispParams,
196 EXCEPINFO* pExcepInfo,
199 queryresult *This = impl_from_IXMLDOMNodeList( iface );
203 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
204 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
206 hr = get_typeinfo(IXMLDOMNodeList_tid, &typeinfo);
209 hr = ITypeInfo_Invoke(typeinfo, &(This->lpVtbl), dispIdMember, wFlags, pDispParams,
210 pVarResult, pExcepInfo, puArgErr);
211 ITypeInfo_Release(typeinfo);
217 static HRESULT WINAPI queryresult_get_item(
218 IXMLDOMNodeList* iface,
220 IXMLDOMNode** listItem)
222 queryresult *This = impl_from_IXMLDOMNodeList( iface );
224 TRACE("(%p)->(%d %p)\n", This, index, listItem);
231 if (index < 0 || index >= xmlXPathNodeSetGetLength(This->result->nodesetval))
234 *listItem = create_node(xmlXPathNodeSetItem(This->result->nodesetval, index));
235 This->resultPos = index + 1;
240 static HRESULT WINAPI queryresult_get_length(
241 IXMLDOMNodeList* iface,
244 queryresult *This = impl_from_IXMLDOMNodeList( iface );
246 TRACE("(%p)->(%p)\n", This, listLength);
251 *listLength = xmlXPathNodeSetGetLength(This->result->nodesetval);
255 static HRESULT WINAPI queryresult_nextNode(
256 IXMLDOMNodeList* iface,
257 IXMLDOMNode** nextItem)
259 queryresult *This = impl_from_IXMLDOMNodeList( iface );
261 TRACE("(%p)->(%p)\n", This, nextItem );
268 if (This->resultPos >= xmlXPathNodeSetGetLength(This->result->nodesetval))
271 *nextItem = create_node(xmlXPathNodeSetItem(This->result->nodesetval, This->resultPos));
276 static HRESULT WINAPI queryresult_reset(
277 IXMLDOMNodeList* iface)
279 queryresult *This = impl_from_IXMLDOMNodeList( iface );
286 static HRESULT WINAPI queryresult__newEnum(
287 IXMLDOMNodeList* iface,
290 queryresult *This = impl_from_IXMLDOMNodeList( iface );
291 FIXME("(%p)->(%p)\n", This, ppUnk);
296 static const struct IXMLDOMNodeListVtbl queryresult_vtbl =
298 queryresult_QueryInterface,
301 queryresult_GetTypeInfoCount,
302 queryresult_GetTypeInfo,
303 queryresult_GetIDsOfNames,
305 queryresult_get_item,
306 queryresult_get_length,
307 queryresult_nextNode,
309 queryresult__newEnum,
312 static HRESULT queryresult_get_dispid(IUnknown *iface, BSTR name, DWORD flags, DISPID *dispid)
314 queryresult *This = impl_from_IXMLDOMNodeList( (IXMLDOMNodeList*)iface );
318 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
319 idx = idx*10 + (*ptr-'0');
321 return DISP_E_UNKNOWNNAME;
323 if(idx >= xmlXPathNodeSetGetLength(This->result->nodesetval))
324 return DISP_E_UNKNOWNNAME;
326 *dispid = MSXML_DISPID_CUSTOM_MIN + idx;
327 TRACE("ret %x\n", *dispid);
331 static HRESULT queryresult_invoke(IUnknown *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
332 VARIANT *res, EXCEPINFO *ei)
334 queryresult *This = impl_from_IXMLDOMNodeList( (IXMLDOMNodeList*)iface );
336 TRACE("(%p)->(%x %x %x %p %p %p)\n", This, id, lcid, flags, params, res, ei);
338 V_VT(res) = VT_DISPATCH;
339 V_DISPATCH(res) = NULL;
343 case INVOKE_PROPERTYGET:
345 IXMLDOMNode *disp = NULL;
347 queryresult_get_item(XMLQUERYRES(This), id - MSXML_DISPID_CUSTOM_MIN, &disp);
348 V_DISPATCH(res) = (IDispatch*)disp;
353 FIXME("unimplemented flags %x\n", flags);
358 TRACE("ret %p\n", V_DISPATCH(res));
363 static const dispex_static_data_vtbl_t queryresult_dispex_vtbl = {
364 queryresult_get_dispid,
368 static const tid_t queryresult_iface_tids[] = {
372 static dispex_static_data_t queryresult_dispex = {
373 &queryresult_dispex_vtbl,
374 IXMLDOMSelection_tid,
376 queryresult_iface_tids
379 #define XSLPATTERN_CHECK_ARGS(n) \
381 FIXME("XSLPattern syntax error: Expected %i arguments, got %i\n", n, nargs); \
382 xmlXPathSetArityError(pctx); \
387 void XSLPattern_index(xmlXPathParserContextPtr pctx, int nargs)
389 XSLPATTERN_CHECK_ARGS(0);
391 xmlXPathPositionFunction(pctx, 0);
392 xmlXPathReturnNumber(pctx, xmlXPathPopNumber(pctx) - 1.0);
395 void XSLPattern_end(xmlXPathParserContextPtr pctx, int nargs)
398 XSLPATTERN_CHECK_ARGS(0);
400 xmlXPathPositionFunction(pctx, 0);
401 pos = xmlXPathPopNumber(pctx);
402 xmlXPathLastFunction(pctx, 0);
403 last = xmlXPathPopNumber(pctx);
404 xmlXPathReturnBoolean(pctx, pos == last);
407 void XSLPattern_nodeType(xmlXPathParserContextPtr pctx, int nargs)
409 XSLPATTERN_CHECK_ARGS(0);
410 xmlXPathReturnNumber(pctx, pctx->context->node->type);
413 void XSLPattern_OP_IEq(xmlXPathParserContextPtr pctx, int nargs)
415 xmlChar *arg1, *arg2;
416 XSLPATTERN_CHECK_ARGS(2);
418 arg2 = xmlXPathPopString(pctx);
419 arg1 = xmlXPathPopString(pctx);
420 xmlXPathReturnBoolean(pctx, xmlStrcasecmp(arg1, arg2) == 0);
425 void XSLPattern_OP_INEq(xmlXPathParserContextPtr pctx, int nargs)
427 xmlChar *arg1, *arg2;
428 XSLPATTERN_CHECK_ARGS(2);
430 arg2 = xmlXPathPopString(pctx);
431 arg1 = xmlXPathPopString(pctx);
432 xmlXPathReturnBoolean(pctx, xmlStrcasecmp(arg1, arg2) != 0);
437 void XSLPattern_OP_ILt(xmlXPathParserContextPtr pctx, int nargs)
439 xmlChar *arg1, *arg2;
440 XSLPATTERN_CHECK_ARGS(2);
442 arg2 = xmlXPathPopString(pctx);
443 arg1 = xmlXPathPopString(pctx);
444 xmlXPathReturnBoolean(pctx, xmlStrcasecmp(arg1, arg2) < 0);
449 void XSLPattern_OP_ILEq(xmlXPathParserContextPtr pctx, int nargs)
451 xmlChar *arg1, *arg2;
452 XSLPATTERN_CHECK_ARGS(2);
454 arg2 = xmlXPathPopString(pctx);
455 arg1 = xmlXPathPopString(pctx);
456 xmlXPathReturnBoolean(pctx, xmlStrcasecmp(arg1, arg2) <= 0);
461 void XSLPattern_OP_IGt(xmlXPathParserContextPtr pctx, int nargs)
463 xmlChar *arg1, *arg2;
464 XSLPATTERN_CHECK_ARGS(2);
466 arg2 = xmlXPathPopString(pctx);
467 arg1 = xmlXPathPopString(pctx);
468 xmlXPathReturnBoolean(pctx, xmlStrcasecmp(arg1, arg2) > 0);
473 void XSLPattern_OP_IGEq(xmlXPathParserContextPtr pctx, int nargs)
475 xmlChar *arg1, *arg2;
476 XSLPATTERN_CHECK_ARGS(2);
478 arg2 = xmlXPathPopString(pctx);
479 arg1 = xmlXPathPopString(pctx);
480 xmlXPathReturnBoolean(pctx, xmlStrcasecmp(arg1, arg2) >= 0);
485 static void query_serror(void* ctx, xmlErrorPtr err)
487 LIBXML2_CALLBACK_SERROR(queryresult_create, err);
490 HRESULT queryresult_create(xmlNodePtr node, xmlChar* szQuery, IXMLDOMNodeList **out)
492 queryresult *This = heap_alloc_zero(sizeof(queryresult));
493 xmlXPathContextPtr ctxt = xmlXPathNewContext(node->doc);
496 TRACE("(%p, %s, %p)\n", node, wine_dbgstr_a((char const*)szQuery), out);
499 if (This == NULL || ctxt == NULL || szQuery == NULL)
505 This->lpVtbl = &queryresult_vtbl;
509 xmldoc_add_ref(This->node->doc);
511 ctxt->error = query_serror;
513 registerNamespaces(ctxt);
515 if (is_xpathmode(This->node->doc))
517 xmlXPathRegisterAllFunctions(ctxt);
518 This->result = xmlXPathEvalExpression(szQuery, ctxt);
522 xmlChar* xslpQuery = XSLPattern_to_XPath(ctxt, szQuery);
524 xmlXPathRegisterFunc(ctxt, (xmlChar const*)"not", xmlXPathNotFunction);
525 xmlXPathRegisterFunc(ctxt, (xmlChar const*)"boolean", xmlXPathBooleanFunction);
527 xmlXPathRegisterFunc(ctxt, (xmlChar const*)"index", XSLPattern_index);
528 xmlXPathRegisterFunc(ctxt, (xmlChar const*)"end", XSLPattern_end);
529 xmlXPathRegisterFunc(ctxt, (xmlChar const*)"nodeType", XSLPattern_nodeType);
531 xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_IEq", XSLPattern_OP_IEq);
532 xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_INEq", XSLPattern_OP_INEq);
533 xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_ILt", XSLPattern_OP_ILt);
534 xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_ILEq", XSLPattern_OP_ILEq);
535 xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_IGt", XSLPattern_OP_IGt);
536 xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_IGEq", XSLPattern_OP_IGEq);
538 This->result = xmlXPathEvalExpression(xslpQuery, ctxt);
542 if (!This->result || This->result->type != XPATH_NODESET)
548 init_dispex(&This->dispex, (IUnknown*)&This->lpVtbl, &queryresult_dispex);
550 *out = (IXMLDOMNodeList *) &This->lpVtbl;
552 TRACE("found %d matches\n", xmlXPathNodeSetGetLength(This->result->nodesetval));
555 if (This != NULL && FAILED(hr))
556 IXMLDOMNodeList_Release( (IXMLDOMNodeList*) &This->lpVtbl );
557 xmlXPathFreeContext(ctxt);