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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "msxml_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
42 #ifdef HAVE_LIBXSLT_PATTERN_H
43 #include <libxslt/pattern.h>
45 #ifdef HAVE_LIBXSLT_TRANSFORM_H
46 #include <libxslt/transform.h>
50 xsltTransformContextPtr ctxt;
51 xsltCompMatchPtr pattern;
52 xsltStylesheetPtr sheet;
55 static void xslt_info_init( struct xslt_info *info )
62 static int create_xslt_parser( struct xslt_info *info, xmlNodePtr node, const xmlChar *str )
64 info->sheet = xsltNewStylesheet();
68 info->ctxt = xsltNewTransformContext( info->sheet, node->doc );
72 info->pattern = xsltCompilePattern( str, node->doc,
73 node, info->sheet, info->ctxt );
79 void free_xslt_info( struct xslt_info *info )
82 xsltFreeCompMatchList( info->pattern );
84 xsltFreeStylesheet( info->sheet );
86 xsltFreeTransformContext( info->ctxt );
90 static xmlNodePtr get_next_node( struct xslt_info *info, xmlNodePtr node, xmlNodePtr *top_level_node );
92 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node, xmlNodePtr *top_level_node )
97 /* make sure that the current element matches the pattern */
102 r = xsltTestCompMatchList( info->ctxt, *node, info->pattern );
105 TRACE("Matched %p (%s)\n", *node, (*node)->name );
110 ERR("Pattern match failed\n");
113 *node = get_next_node(info, *node, top_level_node);
124 static void xslt_info_init( struct xslt_info *info )
128 void free_xslt_info( struct xslt_info *info )
132 static int create_xslt_parser( struct xslt_info *info, xmlNodePtr node, const xmlChar *str )
134 MESSAGE("libxslt was missing at compile time\n");
138 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node, xmlNodePtr *top_level_node )
145 static xmlNodePtr get_next_node( struct xslt_info *info, xmlNodePtr node, xmlNodePtr *top_level_node )
147 if(!top_level_node) return node->next;
149 if(node->children) return node->children;
152 if(node == *top_level_node)
153 *top_level_node = node->next;
157 if(node != *top_level_node && node->parent)
159 if(node->parent == *top_level_node)
160 *top_level_node = node->parent->next;
161 return node->parent->next;
166 typedef struct _xmlnodelist
168 const struct IXMLDOMNodeListVtbl *lpVtbl;
172 xmlNodePtr top_level_node;
174 struct xslt_info xinfo;
177 static inline xmlnodelist *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
179 return (xmlnodelist *)((char*)iface - FIELD_OFFSET(xmlnodelist, lpVtbl));
182 static HRESULT WINAPI xmlnodelist_QueryInterface(
183 IXMLDOMNodeList *iface,
187 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
189 if ( IsEqualGUID( riid, &IID_IUnknown ) ||
190 IsEqualGUID( riid, &IID_IDispatch ) ||
191 IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
197 FIXME("interface %s not implemented\n", debugstr_guid(riid));
198 return E_NOINTERFACE;
201 IXMLDOMNodeList_AddRef( iface );
206 static ULONG WINAPI xmlnodelist_AddRef(
207 IXMLDOMNodeList *iface )
209 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
210 return InterlockedIncrement( &This->ref );
213 static ULONG WINAPI xmlnodelist_Release(
214 IXMLDOMNodeList *iface )
216 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
219 ref = InterlockedDecrement( &This->ref );
222 free_xslt_info( &This->xinfo );
223 xmldoc_release( This->node->doc );
224 HeapFree( GetProcessHeap(), 0, This );
230 static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
231 IXMLDOMNodeList *iface,
238 static HRESULT WINAPI xmlnodelist_GetTypeInfo(
239 IXMLDOMNodeList *iface,
242 ITypeInfo** ppTInfo )
248 static HRESULT WINAPI xmlnodelist_GetIDsOfNames(
249 IXMLDOMNodeList *iface,
260 static HRESULT WINAPI xmlnodelist_Invoke(
261 IXMLDOMNodeList *iface,
266 DISPPARAMS* pDispParams,
268 EXCEPINFO* pExcepInfo,
275 static HRESULT WINAPI xmlnodelist_get_item(
276 IXMLDOMNodeList* iface,
278 IXMLDOMNode** listItem)
280 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
281 xmlNodePtr curr, tmp;
282 xmlNodePtr *top_level_node = NULL;
286 TRACE("%p %ld\n", This, index);
295 if(This->enum_children)
298 top_level_node = &tmp;
303 r = xslt_next_match( &This->xinfo, &curr, top_level_node);
304 if(FAILED(r) || !curr) return S_FALSE;
305 if(nodeIndex++ == index) break;
306 curr = get_next_node(&This->xinfo, curr, top_level_node);
308 if(!curr) return S_FALSE;
310 *listItem = create_node( curr );
315 static HRESULT WINAPI xmlnodelist_get_length(
316 IXMLDOMNodeList* iface,
320 xmlNodePtr curr, tmp;
321 xmlNodePtr *top_level_node = NULL;
325 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
329 if (This->node == NULL) {
334 if(This->enum_children)
337 top_level_node = &tmp;
340 for(curr = This->node; curr; curr = get_next_node(&This->xinfo, curr, top_level_node))
342 r = xslt_next_match( &This->xinfo, &curr, top_level_node );
343 if(FAILED(r) || !curr) break;
347 *listLength = nodeCount;
351 static HRESULT WINAPI xmlnodelist_nextNode(
352 IXMLDOMNodeList* iface,
353 IXMLDOMNode** nextItem)
355 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
357 xmlNodePtr *top_level_node = NULL;
359 TRACE("%p %p\n", This, nextItem );
361 if(This->enum_children)
362 top_level_node = &This->top_level_node;
364 r = xslt_next_match( &This->xinfo, &This->current, top_level_node );
371 *nextItem = create_node( This->current );
372 This->current = get_next_node(&This->xinfo, This->current, top_level_node);
376 static HRESULT WINAPI xmlnodelist_reset(
377 IXMLDOMNodeList* iface)
379 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
382 This->current = This->node;
386 static HRESULT WINAPI xmlnodelist__newEnum(
387 IXMLDOMNodeList* iface,
395 static const struct IXMLDOMNodeListVtbl xmlnodelist_vtbl =
397 xmlnodelist_QueryInterface,
400 xmlnodelist_GetTypeInfoCount,
401 xmlnodelist_GetTypeInfo,
402 xmlnodelist_GetIDsOfNames,
404 xmlnodelist_get_item,
405 xmlnodelist_get_length,
406 xmlnodelist_nextNode,
408 xmlnodelist__newEnum,
411 static xmlnodelist *new_nodelist( xmlNodePtr node )
413 xmlnodelist *nodelist;
415 nodelist = HeapAlloc( GetProcessHeap(), 0, sizeof *nodelist );
419 nodelist->lpVtbl = &xmlnodelist_vtbl;
421 nodelist->node = node;
422 nodelist->current = node;
423 nodelist->top_level_node = node;
424 nodelist->enum_children = FALSE;
425 xslt_info_init( &nodelist->xinfo );
427 xmldoc_add_ref( node->doc );
432 IXMLDOMNodeList* create_nodelist( xmlNodePtr node )
434 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 );
444 if (create_xslt_parser( &This->xinfo, node, str ))
446 This->enum_children = enum_children;
447 return (IXMLDOMNodeList*) &This->lpVtbl;
450 IXMLDOMNodeList_Release( (IXMLDOMNodeList*) &This->lpVtbl );