We should always allocate in NdrConformantStringUnmarshal if the
[wine.git] / dlls / msxml3 / nodelist.c
blobdb4e3701c98e4457c773517e86f9c5b42f28bf3d
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 xlst_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 info->sheet = xsltNewStylesheet();
65 if (!info->sheet)
66 return 0;
68 info->ctxt = xsltNewTransformContext( info->sheet, node->doc );
69 if (!info->ctxt)
70 return 0;
72 info->pattern = xsltCompilePattern( str, node->doc,
73 node, info->sheet, info->ctxt );
74 if (!info->pattern)
75 return 0;
76 return 1;
79 void free_xslt_info( struct xslt_info *info )
81 if (info->pattern)
82 xsltFreeCompMatchList( info->pattern );
83 if (info->sheet)
84 xsltFreeStylesheet( info->sheet );
85 if (info->ctxt)
86 xsltFreeTransformContext( info->ctxt );
89 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node )
91 if (!info->ctxt)
92 return S_FALSE;
94 /* make sure that the current element matches the pattern */
95 while ( *node )
97 int r;
99 r = xsltTestCompMatchList( info->ctxt, *node, info->pattern );
100 if ( 1 == r )
102 TRACE("Matched %p (%s)\n", *node, (*node)->name );
103 return S_OK;
105 if (r != 0)
107 ERR("Pattern match failed\n");
108 return E_FAIL;
110 *node = (*node)->next;
112 return S_OK;
115 #else
117 struct xslt_info {
118 /* empty */
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");
132 return 0;
135 static HRESULT xslt_next_match( struct xslt_info *info, xmlNodePtr *node )
137 return S_FALSE;
140 #endif
142 typedef struct _xmlnodelist
144 const struct IXMLDOMNodeListVtbl *lpVtbl;
145 LONG ref;
146 xmlNodePtr node;
147 xmlNodePtr current;
148 struct xslt_info xinfo;
149 } xmlnodelist;
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,
158 REFIID riid,
159 void** ppvObject )
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 ) )
167 *ppvObject = iface;
169 else
170 return E_NOINTERFACE;
172 IXMLDOMNodeList_AddRef( iface );
174 return S_OK;
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 );
188 ULONG ref;
190 ref = InterlockedDecrement( &This->ref );
191 if ( ref == 0 )
193 free_xslt_info( &This->xinfo );
194 xmldoc_release( This->node->doc );
195 HeapFree( GetProcessHeap(), 0, This );
198 return ref;
201 static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
202 IXMLDOMNodeList *iface,
203 UINT* pctinfo )
205 FIXME("\n");
206 return E_NOTIMPL;
209 static HRESULT WINAPI xmlnodelist_GetTypeInfo(
210 IXMLDOMNodeList *iface,
211 UINT iTInfo,
212 LCID lcid,
213 ITypeInfo** ppTInfo )
215 FIXME("\n");
216 return E_NOTIMPL;
219 static HRESULT WINAPI xmlnodelist_GetIDsOfNames(
220 IXMLDOMNodeList *iface,
221 REFIID riid,
222 LPOLESTR* rgszNames,
223 UINT cNames,
224 LCID lcid,
225 DISPID* rgDispId )
227 FIXME("\n");
228 return E_NOTIMPL;
231 static HRESULT WINAPI xmlnodelist_Invoke(
232 IXMLDOMNodeList *iface,
233 DISPID dispIdMember,
234 REFIID riid,
235 LCID lcid,
236 WORD wFlags,
237 DISPPARAMS* pDispParams,
238 VARIANT* pVarResult,
239 EXCEPINFO* pExcepInfo,
240 UINT* puArgErr )
242 FIXME("\n");
243 return E_NOTIMPL;
246 static HRESULT WINAPI xmlnodelist_get_item(
247 IXMLDOMNodeList* iface,
248 long index,
249 IXMLDOMNode** listItem)
251 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
252 xmlNodePtr curr;
253 long nodeIndex = 0;
254 HRESULT r;
256 TRACE("%p %ld\n", This, index);
258 *listItem = NULL;
260 if (index < 0)
261 return S_FALSE;
263 curr = This->node;
265 while(curr)
267 r = xslt_next_match( &This->xinfo, &curr );
268 if(FAILED(r) || !curr) return S_FALSE;
269 if(nodeIndex++ == index) break;
270 curr = curr->next;
272 if(!curr) return S_FALSE;
274 *listItem = create_node( curr );
276 return S_OK;
279 static HRESULT WINAPI xmlnodelist_get_length(
280 IXMLDOMNodeList* iface,
281 long* listLength)
284 xmlNodePtr curr;
285 long nodeCount = 0;
286 HRESULT r;
288 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
290 TRACE("%p\n", This);
292 if (This->node == NULL) {
293 *listLength = 0;
294 return S_OK;
297 for(curr = This->node; curr; curr = curr->next)
299 r = xslt_next_match( &This->xinfo, &curr );
300 if(FAILED(r) || !curr) break;
301 nodeCount++;
304 *listLength = nodeCount;
305 return S_OK;
308 static HRESULT WINAPI xmlnodelist_nextNode(
309 IXMLDOMNodeList* iface,
310 IXMLDOMNode** nextItem)
312 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
313 HRESULT r;
315 TRACE("%p %p\n", This, nextItem );
317 r = xslt_next_match( &This->xinfo, &This->current );
318 if (FAILED(r) )
319 return r;
321 if (!This->current)
322 return S_FALSE;
324 *nextItem = create_node( This->current );
325 This->current = This->current->next;
326 return S_OK;
329 static HRESULT WINAPI xmlnodelist_reset(
330 IXMLDOMNodeList* iface)
332 xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
334 TRACE("%p\n", This);
335 This->current = This->node;
336 return S_OK;
339 static HRESULT WINAPI xmlnodelist__newEnum(
340 IXMLDOMNodeList* iface,
341 IUnknown** ppUnk)
343 FIXME("\n");
344 return E_NOTIMPL;
348 static const struct IXMLDOMNodeListVtbl xmlnodelist_vtbl =
350 xmlnodelist_QueryInterface,
351 xmlnodelist_AddRef,
352 xmlnodelist_Release,
353 xmlnodelist_GetTypeInfoCount,
354 xmlnodelist_GetTypeInfo,
355 xmlnodelist_GetIDsOfNames,
356 xmlnodelist_Invoke,
357 xmlnodelist_get_item,
358 xmlnodelist_get_length,
359 xmlnodelist_nextNode,
360 xmlnodelist_reset,
361 xmlnodelist__newEnum,
364 static xmlnodelist *new_nodelist( xmlNodePtr node )
366 xmlnodelist *nodelist;
368 nodelist = HeapAlloc( GetProcessHeap(), 0, sizeof *nodelist );
369 if ( !nodelist )
370 return NULL;
372 nodelist->lpVtbl = &xmlnodelist_vtbl;
373 nodelist->ref = 1;
374 nodelist->node = node;
375 nodelist->current = node;
376 xlst_info_init( &nodelist->xinfo );
378 xmldoc_add_ref( node->doc );
380 return nodelist;
383 IXMLDOMNodeList* create_nodelist( xmlNodePtr node )
385 xmlnodelist *nodelist = new_nodelist( node );
386 if (!node)
387 return NULL;
388 return (IXMLDOMNodeList*) &nodelist->lpVtbl;
391 IXMLDOMNodeList* create_filtered_nodelist( xmlNodePtr node, const xmlChar *str )
393 xmlnodelist *This = new_nodelist( node );
395 if (create_xslt_parser( &This->xinfo, node, str ))
396 return (IXMLDOMNodeList*) &This->lpVtbl;
398 IXMLDOMNodeList_Release( (IXMLDOMNodeList*) &This->lpVtbl );
399 return NULL;
402 #endif