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 xlst_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 );
89 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node )
94 /* make sure that the current element matches the pattern */
99 r = xsltTestCompMatchList( info->ctxt, *node, info->pattern );
102 TRACE("Matched %p (%s)\n", *node, (*node)->name );
107 ERR("Pattern match failed\n");
110 *node = (*node)->next;
121 static void xlst_info_init( struct xslt_info *info )
125 void free_xslt_info( struct xslt_info *info )
129 static int create_xslt_parser( struct xslt_info *info, xmlNodePtr node, const xmlChar *str )
131 MESSAGE("libxslt was missing at compile time\n");
135 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node )
142 typedef struct _xmlnodelist
144 const struct IXMLDOMNodeListVtbl *lpVtbl;
148 struct xslt_info xinfo;
151 static inline xmlnodelist *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
153 return (xmlnodelist *)((char*)iface - FIELD_OFFSET(xmlnodelist, lpVtbl));
156 static HRESULT WINAPI xmlnodelist_QueryInterface(
157 IXMLDOMNodeList *iface,
161 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
163 if ( IsEqualGUID( riid, &IID_IUnknown ) ||
164 IsEqualGUID( riid, &IID_IDispatch ) ||
165 IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
170 return E_NOINTERFACE;
172 IXMLDOMNodeList_AddRef( iface );
177 static ULONG WINAPI xmlnodelist_AddRef(
178 IXMLDOMNodeList *iface )
180 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
181 return InterlockedIncrement( &This->ref );
184 static ULONG WINAPI xmlnodelist_Release(
185 IXMLDOMNodeList *iface )
187 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
190 ref = InterlockedDecrement( &This->ref );
193 free_xslt_info( &This->xinfo );
194 HeapFree( GetProcessHeap(), 0, This );
200 static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
201 IXMLDOMNodeList *iface,
208 static HRESULT WINAPI xmlnodelist_GetTypeInfo(
209 IXMLDOMNodeList *iface,
212 ITypeInfo** ppTInfo )
218 static HRESULT WINAPI xmlnodelist_GetIDsOfNames(
219 IXMLDOMNodeList *iface,
230 static HRESULT WINAPI xmlnodelist_Invoke(
231 IXMLDOMNodeList *iface,
236 DISPPARAMS* pDispParams,
238 EXCEPINFO* pExcepInfo,
245 static HRESULT WINAPI xmlnodelist_get_item(
246 IXMLDOMNodeList* iface,
248 IXMLDOMNode** listItem)
250 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
255 TRACE("%p %ld\n", This, index);
266 r = xslt_next_match( &This->xinfo, &curr );
267 if(FAILED(r) || !curr) return S_FALSE;
268 if(nodeIndex++ == index) break;
271 if(!curr) return S_FALSE;
273 *listItem = create_node( curr );
278 static HRESULT WINAPI xmlnodelist_get_length(
279 IXMLDOMNodeList* iface,
287 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
291 if (This->node == NULL) {
296 for(curr = This->node; curr; curr = curr->next)
298 r = xslt_next_match( &This->xinfo, &curr );
299 if(FAILED(r) || !curr) break;
303 *listLength = nodeCount;
307 static HRESULT WINAPI xmlnodelist_nextNode(
308 IXMLDOMNodeList* iface,
309 IXMLDOMNode** nextItem)
311 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
314 TRACE("%p %p\n", This, nextItem );
316 r = xslt_next_match( &This->xinfo, &This->current );
323 *nextItem = create_node( This->current );
324 This->current = This->current->next;
328 static HRESULT WINAPI xmlnodelist_reset(
329 IXMLDOMNodeList* iface)
331 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
334 This->current = This->node;
338 static HRESULT WINAPI xmlnodelist__newEnum(
339 IXMLDOMNodeList* iface,
347 static const struct IXMLDOMNodeListVtbl xmlnodelist_vtbl =
349 xmlnodelist_QueryInterface,
352 xmlnodelist_GetTypeInfoCount,
353 xmlnodelist_GetTypeInfo,
354 xmlnodelist_GetIDsOfNames,
356 xmlnodelist_get_item,
357 xmlnodelist_get_length,
358 xmlnodelist_nextNode,
360 xmlnodelist__newEnum,
363 static xmlnodelist *new_nodelist( xmlNodePtr node )
365 xmlnodelist *nodelist;
367 nodelist = HeapAlloc( GetProcessHeap(), 0, sizeof *nodelist );
371 nodelist->lpVtbl = &xmlnodelist_vtbl;
373 nodelist->node = node;
374 nodelist->current = node;
375 xlst_info_init( &nodelist->xinfo );
380 IXMLDOMNodeList* create_nodelist( xmlNodePtr node )
382 xmlnodelist *nodelist = new_nodelist( node );
385 return (IXMLDOMNodeList*) &nodelist->lpVtbl;
388 IXMLDOMNodeList* create_filtered_nodelist( xmlNodePtr node, const xmlChar *str )
390 xmlnodelist *This = new_nodelist( node );
392 if (create_xslt_parser( &This->xinfo, node, str ))
393 return (IXMLDOMNodeList*) &This->lpVtbl;
395 IXMLDOMNodeList_Release( (IXMLDOMNodeList*) &This->lpVtbl );