secur32: Update ntlm_auth version detection to detect new samba4 version numbers.
[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 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 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         return E_NOINTERFACE;
201     }
202
203     IXMLDOMNodeList_AddRef( iface );
204
205     return S_OK;
206 }
207
208 static ULONG WINAPI xmlnodelist_AddRef(
209     IXMLDOMNodeList *iface )
210 {
211     xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
212     return InterlockedIncrement( &This->ref );
213 }
214
215 static ULONG WINAPI xmlnodelist_Release(
216     IXMLDOMNodeList *iface )
217 {
218     xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
219     ULONG ref;
220
221     ref = InterlockedDecrement( &This->ref );
222     if ( ref == 0 )
223     {
224         free_xslt_info( &This->xinfo );
225         if(This->node) xmldoc_release( This->node->doc );
226         HeapFree( GetProcessHeap(), 0, This );
227     }
228
229     return ref;
230 }
231
232 static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
233     IXMLDOMNodeList *iface,
234     UINT* pctinfo )
235 {
236     FIXME("\n");
237     return E_NOTIMPL;
238 }
239
240 static HRESULT WINAPI xmlnodelist_GetTypeInfo(
241     IXMLDOMNodeList *iface,
242     UINT iTInfo,
243     LCID lcid,
244     ITypeInfo** ppTInfo )
245 {
246     FIXME("\n");
247     return E_NOTIMPL;
248 }
249
250 static HRESULT WINAPI xmlnodelist_GetIDsOfNames(
251     IXMLDOMNodeList *iface,
252     REFIID riid,
253     LPOLESTR* rgszNames,
254     UINT cNames,
255     LCID lcid,
256     DISPID* rgDispId )
257 {
258     FIXME("\n");
259     return E_NOTIMPL;
260 }
261
262 static HRESULT WINAPI xmlnodelist_Invoke(
263     IXMLDOMNodeList *iface,
264     DISPID dispIdMember,
265     REFIID riid,
266     LCID lcid,
267     WORD wFlags,
268     DISPPARAMS* pDispParams,
269     VARIANT* pVarResult,
270     EXCEPINFO* pExcepInfo,
271     UINT* puArgErr )
272 {
273     FIXME("\n");
274     return E_NOTIMPL;
275 }
276
277 static HRESULT WINAPI xmlnodelist_get_item(
278         IXMLDOMNodeList* iface,
279         long index,
280         IXMLDOMNode** listItem)
281 {
282     xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
283     xmlNodePtr curr, tmp;
284     xmlNodePtr *top_level_node = NULL;
285     long nodeIndex = 0;
286     HRESULT r;
287
288     TRACE("%p %ld\n", This, index);
289  
290     *listItem = NULL;
291
292     if (index < 0)
293         return S_FALSE;
294
295     curr = This->node;
296
297     if(This->enum_children)
298     {
299         tmp = curr;
300         top_level_node = &tmp;
301     }
302
303     while(curr)
304     {
305         r = xslt_next_match( &This->xinfo, &curr, top_level_node);
306         if(FAILED(r) || !curr) return S_FALSE;
307         if(nodeIndex++ == index) break;
308         curr = get_next_node(&This->xinfo, curr, top_level_node);
309     }
310     if(!curr) return S_FALSE;
311
312     *listItem = create_node( curr );
313
314     return S_OK;
315 }
316
317 static HRESULT WINAPI xmlnodelist_get_length(
318         IXMLDOMNodeList* iface,
319         long* listLength)
320 {
321
322     xmlNodePtr curr, tmp;
323     xmlNodePtr *top_level_node = NULL;
324     long nodeCount = 0;
325     HRESULT r;
326
327     xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
328
329     TRACE("%p\n", This);
330
331     if (This->node == NULL) {
332         *listLength = 0;
333         return S_OK;
334     }
335         
336     if(This->enum_children)
337     {
338         tmp = curr;
339         top_level_node = &tmp;
340     }
341
342     for(curr = This->node; curr; curr = get_next_node(&This->xinfo, curr, top_level_node))
343     {
344         r = xslt_next_match( &This->xinfo, &curr, top_level_node );
345         if(FAILED(r) || !curr) break;
346         nodeCount++;
347     }
348
349     *listLength = nodeCount;
350     return S_OK;
351 }
352
353 static HRESULT WINAPI xmlnodelist_nextNode(
354         IXMLDOMNodeList* iface,
355         IXMLDOMNode** nextItem)
356 {
357     xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
358     HRESULT r;
359     xmlNodePtr *top_level_node = NULL;
360
361     TRACE("%p %p\n", This, nextItem );
362
363     if(This->enum_children)
364         top_level_node = &This->top_level_node;
365
366     r = xslt_next_match( &This->xinfo, &This->current, top_level_node );
367     if (FAILED(r) )
368         return r;
369
370     if (!This->current)
371         return S_FALSE;
372
373     *nextItem = create_node( This->current );
374     This->current = get_next_node(&This->xinfo, This->current, top_level_node);
375     return S_OK;
376 }
377
378 static HRESULT WINAPI xmlnodelist_reset(
379         IXMLDOMNodeList* iface)
380 {
381     xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
382
383     TRACE("%p\n", This);
384     This->current = This->node;
385     return S_OK;
386 }
387
388 static HRESULT WINAPI xmlnodelist__newEnum(
389         IXMLDOMNodeList* iface,
390         IUnknown** ppUnk)
391 {
392     FIXME("\n");
393     return E_NOTIMPL;
394 }
395
396
397 static const struct IXMLDOMNodeListVtbl xmlnodelist_vtbl =
398 {
399     xmlnodelist_QueryInterface,
400     xmlnodelist_AddRef,
401     xmlnodelist_Release,
402     xmlnodelist_GetTypeInfoCount,
403     xmlnodelist_GetTypeInfo,
404     xmlnodelist_GetIDsOfNames,
405     xmlnodelist_Invoke,
406     xmlnodelist_get_item,
407     xmlnodelist_get_length,
408     xmlnodelist_nextNode,
409     xmlnodelist_reset,
410     xmlnodelist__newEnum,
411 };
412
413 static xmlnodelist *new_nodelist( xmlNodePtr node )
414 {
415     xmlnodelist *nodelist;
416
417     nodelist = HeapAlloc( GetProcessHeap(), 0, sizeof *nodelist );
418     if ( !nodelist )
419         return NULL;
420
421     nodelist->lpVtbl = &xmlnodelist_vtbl;
422     nodelist->ref = 1;
423     nodelist->node = node;
424     nodelist->current = node; 
425     nodelist->top_level_node = node;
426     nodelist->enum_children = FALSE;
427     xslt_info_init( &nodelist->xinfo );
428
429     if(node) xmldoc_add_ref( node->doc );
430
431     return nodelist;
432 }
433
434 IXMLDOMNodeList* create_nodelist( xmlNodePtr node )
435 {
436     xmlnodelist *nodelist = new_nodelist( node );
437     return (IXMLDOMNodeList*) &nodelist->lpVtbl;
438 }
439
440 IXMLDOMNodeList* create_filtered_nodelist( xmlNodePtr node, const xmlChar *str, BOOL enum_children )
441 {
442     xmlnodelist *This = new_nodelist( node );
443     if (create_xslt_parser( &This->xinfo, node, str ))
444     {
445         This->enum_children = enum_children;
446         return (IXMLDOMNodeList*) &This->lpVtbl;
447     }
448
449     IXMLDOMNodeList_Release( (IXMLDOMNodeList*) &This->lpVtbl );
450     return NULL;
451 }
452
453 #endif