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