2 * Node list implementation
4 * Copyright 2005 Mike McCormack
6 * iface 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 * iface 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
36 #include "msxml_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
46 #ifdef HAVE_LIBXSLT_PATTERN_H
47 #include <libxslt/pattern.h>
49 #ifdef HAVE_LIBXSLT_TRANSFORM_H
50 #include <libxslt/transform.h>
54 xsltTransformContextPtr ctxt;
55 xsltCompMatchPtr pattern;
56 xsltStylesheetPtr sheet;
59 static void xlst_info_init( struct xslt_info *info )
66 static int create_xslt_parser( struct xslt_info *info, xmlNodePtr node, const xmlChar *str )
68 info->sheet = xsltNewStylesheet();
72 info->ctxt = xsltNewTransformContext( info->sheet, node->doc );
76 info->pattern = xsltCompilePattern( str, node->doc,
77 node, info->sheet, info->ctxt );
83 void free_xslt_info( struct xslt_info *info )
86 xsltFreeCompMatchList( info->pattern );
88 xsltFreeStylesheet( info->sheet );
90 xsltFreeTransformContext( info->ctxt );
93 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node )
98 /* make sure that the current element matches the pattern */
103 r = xsltTestCompMatchList( info->ctxt, *node, info->pattern );
106 TRACE("Matched %p (%s)\n", *node, (*node)->name );
111 ERR("Pattern match failed\n");
114 *node = (*node)->next;
125 static void xlst_info_init( struct xslt_info *info )
129 void free_xslt_info( struct xslt_info *info )
133 static int create_xslt_parser( struct xslt_info *info, xmlNodePtr node, const xmlChar *str )
135 MESSAGE("libxslt was missing at compile time\n");
139 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node )
146 typedef struct _xmlnodelist
148 const struct IXMLDOMNodeListVtbl *lpVtbl;
152 struct xslt_info xinfo;
155 static inline xmlnodelist *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
157 return (xmlnodelist *)((char*)iface - FIELD_OFFSET(xmlnodelist, lpVtbl));
160 static HRESULT WINAPI xmlnodelist_QueryInterface(
161 IXMLDOMNodeList *iface,
165 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
167 if ( IsEqualGUID( riid, &IID_IUnknown ) ||
168 IsEqualGUID( riid, &IID_IDispatch ) ||
169 IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
174 return E_NOINTERFACE;
176 IXMLDOMNodeList_AddRef( iface );
181 static ULONG WINAPI xmlnodelist_AddRef(
182 IXMLDOMNodeList *iface )
184 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
185 return InterlockedIncrement( &This->ref );
188 static ULONG WINAPI xmlnodelist_Release(
189 IXMLDOMNodeList *iface )
191 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
194 ref = InterlockedDecrement( &This->ref );
197 free_xslt_info( &This->xinfo );
198 HeapFree( GetProcessHeap(), 0, This );
204 static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
205 IXMLDOMNodeList *iface,
212 static HRESULT WINAPI xmlnodelist_GetTypeInfo(
213 IXMLDOMNodeList *iface,
216 ITypeInfo** ppTInfo )
222 static HRESULT WINAPI xmlnodelist_GetIDsOfNames(
223 IXMLDOMNodeList *iface,
234 static HRESULT WINAPI xmlnodelist_Invoke(
235 IXMLDOMNodeList *iface,
240 DISPPARAMS* pDispParams,
242 EXCEPINFO* pExcepInfo,
249 static HRESULT WINAPI xmlnodelist_get_item(
250 IXMLDOMNodeList* iface,
252 IXMLDOMNode** listItem)
254 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
259 TRACE("%p %ld\n", This, index);
270 r = xslt_next_match( &This->xinfo, &curr );
271 if(FAILED(r) || !curr) return S_FALSE;
272 if(nodeIndex++ == index) break;
275 if(!curr) return S_FALSE;
277 *listItem = create_node( curr );
282 static HRESULT WINAPI xmlnodelist_get_length(
283 IXMLDOMNodeList* iface,
291 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
295 if (This->node == NULL) {
300 for(curr = This->node; curr; curr = curr->next)
302 r = xslt_next_match( &This->xinfo, &curr );
303 if(FAILED(r) || !curr) break;
307 *listLength = nodeCount;
311 static HRESULT WINAPI xmlnodelist_nextNode(
312 IXMLDOMNodeList* iface,
313 IXMLDOMNode** nextItem)
315 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
318 TRACE("%p %p\n", This, nextItem );
320 r = xslt_next_match( &This->xinfo, &This->current );
327 *nextItem = create_node( This->current );
328 This->current = This->current->next;
332 static HRESULT WINAPI xmlnodelist_reset(
333 IXMLDOMNodeList* iface)
335 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
338 This->current = This->node;
342 static HRESULT WINAPI xmlnodelist__newEnum(
343 IXMLDOMNodeList* iface,
351 static const struct IXMLDOMNodeListVtbl xmlnodelist_vtbl =
353 xmlnodelist_QueryInterface,
356 xmlnodelist_GetTypeInfoCount,
357 xmlnodelist_GetTypeInfo,
358 xmlnodelist_GetIDsOfNames,
360 xmlnodelist_get_item,
361 xmlnodelist_get_length,
362 xmlnodelist_nextNode,
364 xmlnodelist__newEnum,
367 static xmlnodelist *new_nodelist( xmlNodePtr node )
369 xmlnodelist *nodelist;
371 nodelist = HeapAlloc( GetProcessHeap(), 0, sizeof *nodelist );
375 nodelist->lpVtbl = &xmlnodelist_vtbl;
377 nodelist->node = node;
378 nodelist->current = node;
379 xlst_info_init( &nodelist->xinfo );
384 IXMLDOMNodeList* create_nodelist( xmlNodePtr node )
386 xmlnodelist *nodelist = new_nodelist( node );
389 return (IXMLDOMNodeList*) &nodelist->lpVtbl;
392 IXMLDOMNodeList* create_filtered_nodelist( xmlNodePtr node, const xmlChar *str )
394 xmlnodelist *This = new_nodelist( node );
396 if (create_xslt_parser( &This->xinfo, node, str ))
397 return (IXMLDOMNodeList*) &This->lpVtbl;
399 IXMLDOMNodeList_Release( (IXMLDOMNodeList*) &This->lpVtbl );