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
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 )
66 info->sheet = xsltNewStylesheet();
70 info->ctxt = xsltNewTransformContext( info->sheet, node->doc );
74 info->pattern = xsltCompilePattern( str, node->doc,
75 node, info->sheet, info->ctxt );
81 static void free_xslt_info( struct xslt_info *info )
84 xsltFreeCompMatchList( info->pattern );
86 xsltFreeStylesheet( info->sheet );
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 )
99 /* make sure that the current element matches the pattern */
104 r = xsltTestCompMatchList( info->ctxt, *node, info->pattern );
107 TRACE("Matched %p (%s)\n", *node, (*node)->name );
112 ERR("Pattern match failed\n");
115 *node = get_next_node(info, *node, top_level_node);
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");
140 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node, xmlNodePtr *top_level_node )
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;
154 if(node == *top_level_node)
155 *top_level_node = 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;
168 typedef struct _xmlnodelist
170 const struct IXMLDOMNodeListVtbl *lpVtbl;
174 xmlNodePtr top_level_node;
176 struct xslt_info xinfo;
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,
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 ) )
199 FIXME("interface %s not implemented\n", debugstr_guid(riid));
201 return E_NOINTERFACE;
204 IXMLDOMNodeList_AddRef( iface );
209 static ULONG WINAPI xmlnodelist_AddRef(
210 IXMLDOMNodeList *iface )
212 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
213 return InterlockedIncrement( &This->ref );
216 static ULONG WINAPI xmlnodelist_Release(
217 IXMLDOMNodeList *iface )
219 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
222 ref = InterlockedDecrement( &This->ref );
225 free_xslt_info( &This->xinfo );
226 if(This->node) xmldoc_release( This->node->doc );
227 HeapFree( GetProcessHeap(), 0, This );
233 static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
234 IXMLDOMNodeList *iface,
241 static HRESULT WINAPI xmlnodelist_GetTypeInfo(
242 IXMLDOMNodeList *iface,
245 ITypeInfo** ppTInfo )
251 static HRESULT WINAPI xmlnodelist_GetIDsOfNames(
252 IXMLDOMNodeList *iface,
263 static HRESULT WINAPI xmlnodelist_Invoke(
264 IXMLDOMNodeList *iface,
269 DISPPARAMS* pDispParams,
271 EXCEPINFO* pExcepInfo,
278 static HRESULT WINAPI xmlnodelist_get_item(
279 IXMLDOMNodeList* iface,
281 IXMLDOMNode** listItem)
283 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
284 xmlNodePtr curr, tmp;
285 xmlNodePtr *top_level_node = NULL;
289 TRACE("%p %ld\n", This, index);
298 if(This->enum_children)
301 top_level_node = &tmp;
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);
311 if(!curr) return S_FALSE;
313 *listItem = create_node( curr );
318 static HRESULT WINAPI xmlnodelist_get_length(
319 IXMLDOMNodeList* iface,
323 xmlNodePtr curr, tmp;
324 xmlNodePtr *top_level_node = NULL;
328 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
332 if (This->node == NULL) {
337 if(This->enum_children)
340 top_level_node = &tmp;
343 for(curr = This->node; curr; curr = get_next_node(&This->xinfo, curr, top_level_node))
345 r = xslt_next_match( &This->xinfo, &curr, top_level_node );
346 if(FAILED(r) || !curr) break;
350 *listLength = nodeCount;
354 static HRESULT WINAPI xmlnodelist_nextNode(
355 IXMLDOMNodeList* iface,
356 IXMLDOMNode** nextItem)
358 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
360 xmlNodePtr *top_level_node = NULL;
362 TRACE("%p %p\n", This, nextItem );
366 if(This->enum_children)
367 top_level_node = &This->top_level_node;
369 r = xslt_next_match( &This->xinfo, &This->current, top_level_node );
376 *nextItem = create_node( This->current );
377 This->current = get_next_node(&This->xinfo, This->current, top_level_node);
381 static HRESULT WINAPI xmlnodelist_reset(
382 IXMLDOMNodeList* iface)
384 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
387 This->current = This->node;
391 static HRESULT WINAPI xmlnodelist__newEnum(
392 IXMLDOMNodeList* iface,
400 static const struct IXMLDOMNodeListVtbl xmlnodelist_vtbl =
402 xmlnodelist_QueryInterface,
405 xmlnodelist_GetTypeInfoCount,
406 xmlnodelist_GetTypeInfo,
407 xmlnodelist_GetIDsOfNames,
409 xmlnodelist_get_item,
410 xmlnodelist_get_length,
411 xmlnodelist_nextNode,
413 xmlnodelist__newEnum,
416 static xmlnodelist *new_nodelist( xmlNodePtr node )
418 xmlnodelist *nodelist;
420 nodelist = HeapAlloc( GetProcessHeap(), 0, sizeof *nodelist );
424 nodelist->lpVtbl = &xmlnodelist_vtbl;
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 );
432 if(node) xmldoc_add_ref( node->doc );
437 IXMLDOMNodeList* create_nodelist( xmlNodePtr node )
439 xmlnodelist *nodelist = new_nodelist( node );
440 return (IXMLDOMNodeList*) &nodelist->lpVtbl;
443 IXMLDOMNodeList* create_filtered_nodelist( xmlNodePtr node, const xmlChar *str, BOOL enum_children )
445 xmlnodelist *This = new_nodelist( node );
446 if (create_xslt_parser( &This->xinfo, node, str ))
448 This->enum_children = enum_children;
449 return (IXMLDOMNodeList*) &This->lpVtbl;
452 IXMLDOMNodeList_Release( (IXMLDOMNodeList*) &This->lpVtbl );