kernel32: FindFirstChangeNotification needs a static IO_STATUS_BLOCK.
[wine] / dlls / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
37
38 #ifdef HAVE_LIBXML2
39
40 #ifdef HAVE_LIBXSLT
41
42 #ifdef HAVE_LIBXSLT_PATTERN_H
43 #include <libxslt/pattern.h>
44 #endif
45 #ifdef HAVE_LIBXSLT_TRANSFORM_H
46 #include <libxslt/transform.h>
47 #endif
48
49 struct xslt_info {
50     xsltTransformContextPtr ctxt;
51     xsltCompMatchPtr pattern;
52     xsltStylesheetPtr sheet;
53 };
54
55 static void xlst_info_init( struct xslt_info *info )
56 {
57     info->ctxt = NULL;
58     info->pattern = NULL;
59     info->sheet = NULL;
60 }
61
62 static int create_xslt_parser( struct xslt_info *info, xmlNodePtr node, const xmlChar *str )
63 {
64     info->sheet = xsltNewStylesheet();
65     if (!info->sheet)
66         return 0;
67
68     info->ctxt = xsltNewTransformContext( info->sheet, node->doc );
69     if (!info->ctxt)
70         return 0;
71
72     info->pattern = xsltCompilePattern( str, node->doc,
73                                         node, info->sheet, info->ctxt );
74     if (!info->pattern)
75         return 0;
76     return 1;
77 }
78  
79 void free_xslt_info( struct xslt_info *info )
80 {
81     if (info->pattern)
82         xsltFreeCompMatchList( info->pattern );
83     if (info->sheet)
84         xsltFreeStylesheet( info->sheet );
85     if (info->ctxt)
86         xsltFreeTransformContext( info->ctxt );
87 }
88
89 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node )
90 {
91     if (!info->ctxt)
92         return S_FALSE;
93  
94     /* make sure that the current element matches the pattern */
95     while ( *node )
96     {
97         int r;
98
99         r = xsltTestCompMatchList( info->ctxt, *node, info->pattern );
100         if ( 1 == r )
101         {
102             TRACE("Matched %p (%s)\n", *node, (*node)->name );
103             return S_OK;
104         }
105         if (r != 0)
106         {
107             ERR("Pattern match failed\n");
108             return E_FAIL;
109         }
110         *node = (*node)->next;
111     }
112     return S_OK;
113 }
114
115 #else
116
117 struct xslt_info {
118     /* empty */
119 };
120
121 static void xlst_info_init( struct xslt_info *info )
122 {
123 }
124
125 void free_xslt_info( struct xslt_info *info )
126 {
127 }
128
129 static int create_xslt_parser( struct xslt_info *info, xmlNodePtr node, const xmlChar *str )
130 {
131     MESSAGE("libxslt was missing at compile time\n");
132     return 0;
133 }
134
135 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node )
136 {
137     return S_FALSE;
138 }
139
140 #endif
141
142 typedef struct _xmlnodelist
143 {
144     const struct IXMLDOMNodeListVtbl *lpVtbl;
145     LONG ref;
146     xmlNodePtr node;
147     xmlNodePtr current;
148     struct xslt_info xinfo;
149 } xmlnodelist;
150
151 static inline xmlnodelist *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
152 {
153     return (xmlnodelist *)((char*)iface - FIELD_OFFSET(xmlnodelist, lpVtbl));
154 }
155
156 static HRESULT WINAPI xmlnodelist_QueryInterface(
157     IXMLDOMNodeList *iface,
158     REFIID riid,
159     void** ppvObject )
160 {
161     TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
162
163     if ( IsEqualGUID( riid, &IID_IUnknown ) ||
164          IsEqualGUID( riid, &IID_IDispatch ) ||
165          IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
166     {
167         *ppvObject = iface;
168     }
169     else
170     {
171         FIXME("interface %s not implemented\n", debugstr_guid(riid));
172         return E_NOINTERFACE;
173     }
174
175     IXMLDOMNodeList_AddRef( iface );
176
177     return S_OK;
178 }
179
180 static ULONG WINAPI xmlnodelist_AddRef(
181     IXMLDOMNodeList *iface )
182 {
183     xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
184     return InterlockedIncrement( &This->ref );
185 }
186
187 static ULONG WINAPI xmlnodelist_Release(
188     IXMLDOMNodeList *iface )
189 {
190     xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
191     ULONG ref;
192
193     ref = InterlockedDecrement( &This->ref );
194     if ( ref == 0 )
195     {
196         free_xslt_info( &This->xinfo );
197         xmldoc_release( This->node->doc );
198         HeapFree( GetProcessHeap(), 0, This );
199     }
200
201     return ref;
202 }
203
204 static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
205     IXMLDOMNodeList *iface,
206     UINT* pctinfo )
207 {
208     FIXME("\n");
209     return E_NOTIMPL;
210 }
211
212 static HRESULT WINAPI xmlnodelist_GetTypeInfo(
213     IXMLDOMNodeList *iface,
214     UINT iTInfo,
215     LCID lcid,
216     ITypeInfo** ppTInfo )
217 {
218     FIXME("\n");
219     return E_NOTIMPL;
220 }
221
222 static HRESULT WINAPI xmlnodelist_GetIDsOfNames(
223     IXMLDOMNodeList *iface,
224     REFIID riid,
225     LPOLESTR* rgszNames,
226     UINT cNames,
227     LCID lcid,
228     DISPID* rgDispId )
229 {
230     FIXME("\n");
231     return E_NOTIMPL;
232 }
233
234 static HRESULT WINAPI xmlnodelist_Invoke(
235     IXMLDOMNodeList *iface,
236     DISPID dispIdMember,
237     REFIID riid,
238     LCID lcid,
239     WORD wFlags,
240     DISPPARAMS* pDispParams,
241     VARIANT* pVarResult,
242     EXCEPINFO* pExcepInfo,
243     UINT* puArgErr )
244 {
245     FIXME("\n");
246     return E_NOTIMPL;
247 }
248
249 static HRESULT WINAPI xmlnodelist_get_item(
250         IXMLDOMNodeList* iface,
251         long index,
252         IXMLDOMNode** listItem)
253 {
254     xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
255     xmlNodePtr curr;
256     long nodeIndex = 0;
257     HRESULT r;
258
259     TRACE("%p %ld\n", This, index);
260  
261     *listItem = NULL;
262
263     if (index < 0)
264         return S_FALSE;
265
266     curr = This->node;
267
268     while(curr)
269     {
270         r = xslt_next_match( &This->xinfo, &curr );
271         if(FAILED(r) || !curr) return S_FALSE;
272         if(nodeIndex++ == index) break;
273         curr = curr->next;
274     }
275     if(!curr) return S_FALSE;
276
277     *listItem = create_node( curr );
278
279     return S_OK;
280 }
281
282 static HRESULT WINAPI xmlnodelist_get_length(
283         IXMLDOMNodeList* iface,
284         long* listLength)
285 {
286
287     xmlNodePtr curr;
288     long nodeCount = 0;
289     HRESULT r;
290
291     xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
292
293     TRACE("%p\n", This);
294
295     if (This->node == NULL) {
296         *listLength = 0;
297         return S_OK;
298     }
299         
300     for(curr = This->node; curr; curr = curr->next)
301     {
302         r = xslt_next_match( &This->xinfo, &curr );
303         if(FAILED(r) || !curr) break;
304         nodeCount++;
305     }
306
307     *listLength = nodeCount;
308     return S_OK;
309 }
310
311 static HRESULT WINAPI xmlnodelist_nextNode(
312         IXMLDOMNodeList* iface,
313         IXMLDOMNode** nextItem)
314 {
315     xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
316     HRESULT r;
317
318     TRACE("%p %p\n", This, nextItem );
319
320     r = xslt_next_match( &This->xinfo, &This->current );
321     if (FAILED(r) )
322         return r;
323
324     if (!This->current)
325         return S_FALSE;
326
327     *nextItem = create_node( This->current );
328     This->current = This->current->next;
329     return S_OK;
330 }
331
332 static HRESULT WINAPI xmlnodelist_reset(
333         IXMLDOMNodeList* iface)
334 {
335     xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
336
337     TRACE("%p\n", This);
338     This->current = This->node;
339     return S_OK;
340 }
341
342 static HRESULT WINAPI xmlnodelist__newEnum(
343         IXMLDOMNodeList* iface,
344         IUnknown** ppUnk)
345 {
346     FIXME("\n");
347     return E_NOTIMPL;
348 }
349
350
351 static const struct IXMLDOMNodeListVtbl xmlnodelist_vtbl =
352 {
353     xmlnodelist_QueryInterface,
354     xmlnodelist_AddRef,
355     xmlnodelist_Release,
356     xmlnodelist_GetTypeInfoCount,
357     xmlnodelist_GetTypeInfo,
358     xmlnodelist_GetIDsOfNames,
359     xmlnodelist_Invoke,
360     xmlnodelist_get_item,
361     xmlnodelist_get_length,
362     xmlnodelist_nextNode,
363     xmlnodelist_reset,
364     xmlnodelist__newEnum,
365 };
366
367 static xmlnodelist *new_nodelist( xmlNodePtr node )
368 {
369     xmlnodelist *nodelist;
370
371     nodelist = HeapAlloc( GetProcessHeap(), 0, sizeof *nodelist );
372     if ( !nodelist )
373         return NULL;
374
375     nodelist->lpVtbl = &xmlnodelist_vtbl;
376     nodelist->ref = 1;
377     nodelist->node = node;
378     nodelist->current = node;
379     xlst_info_init( &nodelist->xinfo );
380
381     xmldoc_add_ref( node->doc );
382
383     return nodelist;
384 }
385
386 IXMLDOMNodeList* create_nodelist( xmlNodePtr node )
387 {
388     xmlnodelist *nodelist = new_nodelist( node );
389     if (!node)
390         return NULL;
391     return (IXMLDOMNodeList*) &nodelist->lpVtbl;
392 }
393
394 IXMLDOMNodeList* create_filtered_nodelist( xmlNodePtr node, const xmlChar *str )
395 {
396     xmlnodelist *This = new_nodelist( node );
397
398     if (create_xslt_parser( &This->xinfo, node, str ))
399         return (IXMLDOMNodeList*) &This->lpVtbl;
400
401     IXMLDOMNodeList_Release( (IXMLDOMNodeList*) &This->lpVtbl );
402     return NULL;
403 }
404
405 #endif