rpcrt4: Make UserMarshalFlags static.
[wine/hacks.git] / dlls / msxml3 / nodelist.c
blobb85ce36fc9b76da2e084660fdf7c75cdf69929fb
1 /*
2 * Node list implementation
4 * Copyright 2005 Mike McCormack
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.
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.
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
21 #define COBJMACROS
23 #include "config.h"
25 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30 #include "msxml2.h"
32 #include "msxml_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
38 #ifdef HAVE_LIBXML2
40 #ifdef HAVE_LIBXSLT
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
49 struct xslt_info {
50 xsltTransformContextPtr ctxt;
51 xsltCompMatchPtr pattern;
52 xsltStylesheetPtr sheet;
55 static void xslt_info_init( struct xslt_info *info )
57 info->ctxt = NULL;
58 info->pattern = NULL;
59 info->sheet = NULL;
62 static int create_xslt_parser( struct xslt_info *info, xmlNodePtr node, const xmlChar *str )
64 if(!node) return 1;
66 info->sheet = xsltNewStylesheet();
67 if (!info->sheet)
68 return 0;
70 info->ctxt = xsltNewTransformContext( info->sheet, node->doc );
71 if (!info->ctxt)
72 return 0;
74 info->pattern = xsltCompilePattern( str, node->doc,
75 node, info->sheet, info->ctxt );
76 if (!info->pattern)
77 return 0;
78 return 1;
81 void free_xslt_info( struct xslt_info *info )
83 if (info->pattern)
84 xsltFreeCompMatchList( info->pattern );
85 if (info->sheet)
86 xsltFreeStylesheet( info->sheet );
87 if (info->ctxt)
88 xsltFreeTransformContext( info->ctxt );
92 static xmlNodePtr get_next_node( struct xslt_info *info, xmlNodePtr node, xmlNodePtr *top_level_node );
94 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node, xmlNodePtr *top_level_node )
96 if (!info->ctxt)
97 return S_FALSE;
99 /* make sure that the current element matches the pattern */
100 while ( *node )
102 int r;
104 r = xsltTestCompMatchList( info->ctxt, *node, info->pattern );
105 if ( 1 == r )
107 TRACE("Matched %p (%s)\n", *node, (*node)->name );
108 return S_OK;
110 if (r != 0)
112 ERR("Pattern match failed\n");
113 return E_FAIL;
115 *node = get_next_node(info, *node, top_level_node);
117 return S_OK;
120 #else
122 struct xslt_info {
123 /* empty */
126 static void xslt_info_init( struct xslt_info *info )
130 void free_xslt_info( struct xslt_info *info )
134 static int create_xslt_parser( struct xslt_info *info, xmlNodePtr node, const xmlChar *str )
136 MESSAGE("libxslt was missing at compile time\n");
137 return 0;
140 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node, xmlNodePtr *top_level_node )
142 return S_FALSE;
145 #endif
147 static xmlNodePtr get_next_node( struct xslt_info *info, xmlNodePtr node, xmlNodePtr *top_level_node )
149 if(!top_level_node) return node->next;
151 if(node->children) return node->children;
152 if(node->next)
154 if(node == *top_level_node)
155 *top_level_node = node->next;
156 return node->next;
159 if(node != *top_level_node && node->parent)
161 if(node->parent == *top_level_node)
162 *top_level_node = node->parent->next;
163 return node->parent->next;
165 return NULL;
168 typedef struct _xmlnodelist
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;
179 static inline xmlnodelist *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
181 return (xmlnodelist *)((char*)iface - FIELD_OFFSET(xmlnodelist, lpVtbl));
184 static HRESULT WINAPI xmlnodelist_QueryInterface(
185 IXMLDOMNodeList *iface,
186 REFIID riid,
187 void** ppvObject )
189 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
191 if ( IsEqualGUID( riid, &IID_IUnknown ) ||
192 IsEqualGUID( riid, &IID_IDispatch ) ||
193 IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
195 *ppvObject = iface;
197 else
199 FIXME("interface %s not implemented\n", debugstr_guid(riid));
200 return E_NOINTERFACE;
203 IXMLDOMNodeList_AddRef( iface );
205 return S_OK;
208 static ULONG WINAPI xmlnodelist_AddRef(
209 IXMLDOMNodeList *iface )
211 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
212 return InterlockedIncrement( &This->ref );
215 static ULONG WINAPI xmlnodelist_Release(
216 IXMLDOMNodeList *iface )
218 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
219 ULONG ref;
221 ref = InterlockedDecrement( &This->ref );
222 if ( ref == 0 )
224 free_xslt_info( &This->xinfo );
225 if(This->node) xmldoc_release( This->node->doc );
226 HeapFree( GetProcessHeap(), 0, This );
229 return ref;
232 static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
233 IXMLDOMNodeList *iface,
234 UINT* pctinfo )
236 FIXME("\n");
237 return E_NOTIMPL;
240 static HRESULT WINAPI xmlnodelist_GetTypeInfo(
241 IXMLDOMNodeList *iface,
242 UINT iTInfo,
243 LCID lcid,
244 ITypeInfo** ppTInfo )
246 FIXME("\n");
247 return E_NOTIMPL;
250 static HRESULT WINAPI xmlnodelist_GetIDsOfNames(
251 IXMLDOMNodeList *iface,
252 REFIID riid,
253 LPOLESTR* rgszNames,
254 UINT cNames,
255 LCID lcid,
256 DISPID* rgDispId )
258 FIXME("\n");
259 return E_NOTIMPL;
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 )
273 FIXME("\n");
274 return E_NOTIMPL;
277 static HRESULT WINAPI xmlnodelist_get_item(
278 IXMLDOMNodeList* iface,
279 long index,
280 IXMLDOMNode** listItem)
282 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
283 xmlNodePtr curr, tmp;
284 xmlNodePtr *top_level_node = NULL;
285 long nodeIndex = 0;
286 HRESULT r;
288 TRACE("%p %ld\n", This, index);
290 *listItem = NULL;
292 if (index < 0)
293 return S_FALSE;
295 curr = This->node;
297 if(This->enum_children)
299 tmp = curr;
300 top_level_node = &tmp;
303 while(curr)
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);
310 if(!curr) return S_FALSE;
312 *listItem = create_node( curr );
314 return S_OK;
317 static HRESULT WINAPI xmlnodelist_get_length(
318 IXMLDOMNodeList* iface,
319 long* listLength)
322 xmlNodePtr curr, tmp;
323 xmlNodePtr *top_level_node = NULL;
324 long nodeCount = 0;
325 HRESULT r;
327 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
329 TRACE("%p\n", This);
331 if (This->node == NULL) {
332 *listLength = 0;
333 return S_OK;
336 if(This->enum_children)
338 tmp = curr;
339 top_level_node = &tmp;
342 for(curr = This->node; curr; curr = get_next_node(&This->xinfo, curr, top_level_node))
344 r = xslt_next_match( &This->xinfo, &curr, top_level_node );
345 if(FAILED(r) || !curr) break;
346 nodeCount++;
349 *listLength = nodeCount;
350 return S_OK;
353 static HRESULT WINAPI xmlnodelist_nextNode(
354 IXMLDOMNodeList* iface,
355 IXMLDOMNode** nextItem)
357 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
358 HRESULT r;
359 xmlNodePtr *top_level_node = NULL;
361 TRACE("%p %p\n", This, nextItem );
363 if(This->enum_children)
364 top_level_node = &This->top_level_node;
366 r = xslt_next_match( &This->xinfo, &This->current, top_level_node );
367 if (FAILED(r) )
368 return r;
370 if (!This->current)
371 return S_FALSE;
373 *nextItem = create_node( This->current );
374 This->current = get_next_node(&This->xinfo, This->current, top_level_node);
375 return S_OK;
378 static HRESULT WINAPI xmlnodelist_reset(
379 IXMLDOMNodeList* iface)
381 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
383 TRACE("%p\n", This);
384 This->current = This->node;
385 return S_OK;
388 static HRESULT WINAPI xmlnodelist__newEnum(
389 IXMLDOMNodeList* iface,
390 IUnknown** ppUnk)
392 FIXME("\n");
393 return E_NOTIMPL;
397 static const struct IXMLDOMNodeListVtbl xmlnodelist_vtbl =
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,
413 static xmlnodelist *new_nodelist( xmlNodePtr node )
415 xmlnodelist *nodelist;
417 nodelist = HeapAlloc( GetProcessHeap(), 0, sizeof *nodelist );
418 if ( !nodelist )
419 return NULL;
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 );
429 if(node) xmldoc_add_ref( node->doc );
431 return nodelist;
434 IXMLDOMNodeList* create_nodelist( xmlNodePtr node )
436 xmlnodelist *nodelist = new_nodelist( node );
437 return (IXMLDOMNodeList*) &nodelist->lpVtbl;
440 IXMLDOMNodeList* create_filtered_nodelist( xmlNodePtr node, const xmlChar *str, BOOL enum_children )
442 xmlnodelist *This = new_nodelist( node );
443 if (create_xslt_parser( &This->xinfo, node, str ))
445 This->enum_children = enum_children;
446 return (IXMLDOMNodeList*) &This->lpVtbl;
449 IXMLDOMNodeList_Release( (IXMLDOMNodeList*) &This->lpVtbl );
450 return NULL;
453 #endif