kernel32/tests: Get the test to run on Windows 95.
[wine] / dlls / msxml3 / queryresult.c
1 /*
2  *    XPath query result node list implementation (TODO: XSLPattern support)
3  *
4  * Copyright 2005 Mike McCormack
5  * Copyright 2007 Mikolaj Zalewski
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #define COBJMACROS
23
24 #include "config.h"
25
26 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "ole2.h"
31 #include "msxml2.h"
32
33 #include "msxml_private.h"
34
35 #include "wine/debug.h"
36
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)
43  *
44  * TODO: XSLPattern support
45  */
46
47 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
48
49 #ifdef HAVE_LIBXML2
50
51 #include <libxml/xpath.h>
52
53 static const struct IXMLDOMNodeListVtbl queryresult_vtbl;
54
55 typedef struct _queryresult
56 {
57     const struct IXMLDOMNodeListVtbl *lpVtbl;
58     LONG ref;
59     xmlNodePtr node;
60     xmlXPathObjectPtr result;
61     int resultPos;
62 } queryresult;
63
64 static inline queryresult *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
65 {
66     return (queryresult *)((char*)iface - FIELD_OFFSET(queryresult, lpVtbl));
67 }
68
69 HRESULT queryresult_create(xmlNodePtr node, LPWSTR szQuery, IXMLDOMNodeList **out)
70 {
71     queryresult *This = CoTaskMemAlloc(sizeof(queryresult));
72     xmlXPathContextPtr ctxt = xmlXPathNewContext(node->doc);
73     xmlChar *str = xmlChar_from_wchar(szQuery);
74     HRESULT hr;
75
76
77     TRACE("(%p, %s, %p)\n", node, wine_dbgstr_w(szQuery), out);
78
79     *out = NULL;
80     if (This == NULL || ctxt == NULL || str == NULL)
81     {
82         hr = E_OUTOFMEMORY;
83         goto cleanup;
84     }
85
86     This->lpVtbl = &queryresult_vtbl;
87     This->ref = 1;
88     This->resultPos = 0;
89     This->node = node;
90     xmldoc_add_ref(This->node->doc);
91
92     ctxt->node = node;
93     This->result = xmlXPathEval(str, ctxt);
94     if (!This->result || This->result->type != XPATH_NODESET)
95     {
96         hr = E_FAIL;
97         goto cleanup;
98     }
99
100     *out = (IXMLDOMNodeList *)This;
101     hr = S_OK;
102     TRACE("found %d matches\n", This->result->nodesetval->nodeNr);
103
104 cleanup:
105     if (This != NULL && FAILED(hr))
106         IXMLDOMNodeList_Release( (IXMLDOMNodeList*) &This->lpVtbl );
107     if (ctxt != NULL)
108         xmlXPathFreeContext(ctxt);
109     HeapFree(GetProcessHeap(), 0, str);
110     return hr;
111 }
112
113
114 static HRESULT WINAPI queryresult_QueryInterface(
115     IXMLDOMNodeList *iface,
116     REFIID riid,
117     void** ppvObject )
118 {
119     TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
120
121     if ( IsEqualGUID( riid, &IID_IUnknown ) ||
122          IsEqualGUID( riid, &IID_IDispatch ) ||
123          IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
124     {
125         *ppvObject = iface;
126     }
127     else
128     {
129         FIXME("interface %s not implemented\n", debugstr_guid(riid));
130         *ppvObject = NULL;
131         return E_NOINTERFACE;
132     }
133
134     IXMLDOMNodeList_AddRef( iface );
135
136     return S_OK;
137 }
138
139 static ULONG WINAPI queryresult_AddRef(
140     IXMLDOMNodeList *iface )
141 {
142     queryresult *This = impl_from_IXMLDOMNodeList( iface );
143     return InterlockedIncrement( &This->ref );
144 }
145
146 static ULONG WINAPI queryresult_Release(
147     IXMLDOMNodeList *iface )
148 {
149     queryresult *This = impl_from_IXMLDOMNodeList( iface );
150     ULONG ref;
151
152     ref = InterlockedDecrement(&This->ref);
153     if ( ref == 0 )
154     {
155         xmlXPathFreeObject(This->result);
156         xmldoc_release(This->node->doc);
157         CoTaskMemFree(This);
158     }
159
160     return ref;
161 }
162
163 static HRESULT WINAPI queryresult_GetTypeInfoCount(
164     IXMLDOMNodeList *iface,
165     UINT* pctinfo )
166 {
167     FIXME("\n");
168     return E_NOTIMPL;
169 }
170
171 static HRESULT WINAPI queryresult_GetTypeInfo(
172     IXMLDOMNodeList *iface,
173     UINT iTInfo,
174     LCID lcid,
175     ITypeInfo** ppTInfo )
176 {
177     FIXME("\n");
178     return E_NOTIMPL;
179 }
180
181 static HRESULT WINAPI queryresult_GetIDsOfNames(
182     IXMLDOMNodeList *iface,
183     REFIID riid,
184     LPOLESTR* rgszNames,
185     UINT cNames,
186     LCID lcid,
187     DISPID* rgDispId )
188 {
189     FIXME("\n");
190     return E_NOTIMPL;
191 }
192
193 static HRESULT WINAPI queryresult_Invoke(
194     IXMLDOMNodeList *iface,
195     DISPID dispIdMember,
196     REFIID riid,
197     LCID lcid,
198     WORD wFlags,
199     DISPPARAMS* pDispParams,
200     VARIANT* pVarResult,
201     EXCEPINFO* pExcepInfo,
202     UINT* puArgErr )
203 {
204     FIXME("\n");
205     return E_NOTIMPL;
206 }
207
208 static HRESULT WINAPI queryresult_get_item(
209         IXMLDOMNodeList* iface,
210         long index,
211         IXMLDOMNode** listItem)
212 {
213     queryresult *This = impl_from_IXMLDOMNodeList( iface );
214
215     TRACE("%p %ld\n", This, index);
216
217     *listItem = NULL;
218
219     if (index < 0 || index >= This->result->nodesetval->nodeNr)
220         return S_FALSE;
221
222     *listItem = create_node(This->result->nodesetval->nodeTab[index]);
223     This->resultPos = index + 1;
224
225     return S_OK;
226 }
227
228 static HRESULT WINAPI queryresult_get_length(
229         IXMLDOMNodeList* iface,
230         long* listLength)
231 {
232     queryresult *This = impl_from_IXMLDOMNodeList( iface );
233
234     TRACE("%p\n", This);
235
236     *listLength = This->result->nodesetval->nodeNr;
237     return S_OK;
238 }
239
240 static HRESULT WINAPI queryresult_nextNode(
241         IXMLDOMNodeList* iface,
242         IXMLDOMNode** nextItem)
243 {
244     queryresult *This = impl_from_IXMLDOMNodeList( iface );
245
246     TRACE("%p %p\n", This, nextItem );
247
248     *nextItem = NULL;
249
250     if (This->resultPos >= This->result->nodesetval->nodeNr)
251         return S_FALSE;
252
253     *nextItem = create_node(This->result->nodesetval->nodeTab[This->resultPos]);
254     This->resultPos++;
255     return S_OK;
256 }
257
258 static HRESULT WINAPI queryresult_reset(
259         IXMLDOMNodeList* iface)
260 {
261     queryresult *This = impl_from_IXMLDOMNodeList( iface );
262
263     TRACE("%p\n", This);
264     This->resultPos = 0;
265     return S_OK;
266 }
267
268 static HRESULT WINAPI queryresult__newEnum(
269         IXMLDOMNodeList* iface,
270         IUnknown** ppUnk)
271 {
272     FIXME("\n");
273     return E_NOTIMPL;
274 }
275
276
277 static const struct IXMLDOMNodeListVtbl queryresult_vtbl =
278 {
279     queryresult_QueryInterface,
280     queryresult_AddRef,
281     queryresult_Release,
282     queryresult_GetTypeInfoCount,
283     queryresult_GetTypeInfo,
284     queryresult_GetIDsOfNames,
285     queryresult_Invoke,
286     queryresult_get_item,
287     queryresult_get_length,
288     queryresult_nextNode,
289     queryresult_reset,
290     queryresult__newEnum,
291 };
292
293 #endif