nodelist.c 7.88 KB
Newer Older
1 2 3 4 5
/*
 *    Node list implementation
 *
 * Copyright 2005 Mike McCormack
 *
6
 * This library is free software; you can redistribute it and/or
7 8 9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * 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 13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
19 20 21 22 23 24 25
 */

#define COBJMACROS

#include "config.h"

#include <stdarg.h>
26 27 28 29 30
#ifdef HAVE_LIBXML2
# include <libxml/parser.h>
# include <libxml/xmlerror.h>
#endif

31 32 33 34
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"
35
#include "msxml6.h"
36 37 38 39 40

#include "msxml_private.h"

#include "wine/debug.h"

41 42 43 44 45 46 47 48 49
/* This file implements the object returned by childNodes property. Note that this is
 * not the IXMLDOMNodeList returned by XPath querites - it's implemented in queryresult.c.
 * They are different because the list returned by childNodes:
 *  - is "live" - changes to the XML tree are automatically reflected in the list
 *  - doesn't supports IXMLDOMSelection
 *  - note that an attribute node have a text child in DOM but not in the XPath data model
 *    thus the child is inaccessible by an XPath query
 */

50 51
WINE_DEFAULT_DEBUG_CHANNEL(msxml);

52 53 54 55
#ifdef HAVE_LIBXML2

typedef struct _xmlnodelist
{
56
    IXMLDOMNodeList IXMLDOMNodeList_iface;
57
    LONG ref;
58
    xmlNodePtr parent;
59 60 61 62 63
    xmlNodePtr current;
} xmlnodelist;

static inline xmlnodelist *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
{
64
    return CONTAINING_RECORD(iface, xmlnodelist, IXMLDOMNodeList_iface);
65 66 67 68 69 70 71
}

static HRESULT WINAPI xmlnodelist_QueryInterface(
    IXMLDOMNodeList *iface,
    REFIID riid,
    void** ppvObject )
{
72
    TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
73 74 75 76 77 78 79 80

    if ( IsEqualGUID( riid, &IID_IUnknown ) ||
         IsEqualGUID( riid, &IID_IDispatch ) ||
         IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
    {
        *ppvObject = iface;
    }
    else
81
    {
82
        TRACE("interface %s not implemented\n", debugstr_guid(riid));
83
        *ppvObject = NULL;
84
        return E_NOINTERFACE;
85
    }
86 87 88 89 90 91 92 93 94 95

    IXMLDOMNodeList_AddRef( iface );

    return S_OK;
}

static ULONG WINAPI xmlnodelist_AddRef(
    IXMLDOMNodeList *iface )
{
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
96 97 98
    ULONG ref = InterlockedIncrement( &This->ref );
    TRACE("(%p)->(%d)\n", This, ref);
    return ref;
99 100 101 102 103 104
}

static ULONG WINAPI xmlnodelist_Release(
    IXMLDOMNodeList *iface )
{
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
105
    ULONG ref = InterlockedDecrement( &This->ref );
106

107
    TRACE("(%p)->(%d)\n", This, ref);
108 109
    if ( ref == 0 )
    {
110
        xmldoc_release( This->parent->doc );
111
        heap_free( This );
112 113 114 115 116 117 118 119 120
    }

    return ref;
}

static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
    IXMLDOMNodeList *iface,
    UINT* pctinfo )
{
121 122 123 124 125 126 127
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

    TRACE("(%p)->(%p)\n", This, pctinfo);

    *pctinfo = 1;

    return S_OK;
128 129 130 131 132 133 134 135
}

static HRESULT WINAPI xmlnodelist_GetTypeInfo(
    IXMLDOMNodeList *iface,
    UINT iTInfo,
    LCID lcid,
    ITypeInfo** ppTInfo )
{
136 137 138 139 140 141 142 143
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
    HRESULT hr;

    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);

    hr = get_typeinfo(IXMLDOMNodeList_tid, ppTInfo);

    return hr;
144 145 146 147 148 149 150 151 152 153
}

static HRESULT WINAPI xmlnodelist_GetIDsOfNames(
    IXMLDOMNodeList *iface,
    REFIID riid,
    LPOLESTR* rgszNames,
    UINT cNames,
    LCID lcid,
    DISPID* rgDispId )
{
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
          lcid, rgDispId);

    if(!rgszNames || cNames == 0 || !rgDispId)
        return E_INVALIDARG;

    hr = get_typeinfo(IXMLDOMNodeList_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
        hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
172 173 174 175 176 177 178 179 180 181 182 183 184
}

static HRESULT WINAPI xmlnodelist_Invoke(
    IXMLDOMNodeList *iface,
    DISPID dispIdMember,
    REFIID riid,
    LCID lcid,
    WORD wFlags,
    DISPPARAMS* pDispParams,
    VARIANT* pVarResult,
    EXCEPINFO* pExcepInfo,
    UINT* puArgErr )
{
185 186 187 188 189 190 191 192 193 194
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
          lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    hr = get_typeinfo(IXMLDOMNodeList_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
195 196
        hr = ITypeInfo_Invoke(typeinfo, &This->IXMLDOMNodeList_iface, dispIdMember, wFlags,
                pDispParams, pVarResult, pExcepInfo, puArgErr);
197 198 199 200
        ITypeInfo_Release(typeinfo);
    }

    return hr;
201 202 203 204
}

static HRESULT WINAPI xmlnodelist_get_item(
        IXMLDOMNodeList* iface,
205
        LONG index,
206 207
        IXMLDOMNode** listItem)
{
208
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
209
    xmlNodePtr curr;
210
    LONG nodeIndex = 0;
211

212
    TRACE("(%p)->(%d %p)\n", This, index, listItem);
213 214 215 216

    if(!listItem)
        return E_INVALIDARG;

217 218 219 220 221
    *listItem = NULL;

    if (index < 0)
        return S_FALSE;

222
    curr = This->parent->children;
223 224 225
    while(curr)
    {
        if(nodeIndex++ == index) break;
226
        curr = curr->next;
227
    }
228 229
    if(!curr) return S_FALSE;

230 231 232
    *listItem = create_node( curr );

    return S_OK;
233 234 235 236
}

static HRESULT WINAPI xmlnodelist_get_length(
        IXMLDOMNodeList* iface,
237
        LONG* listLength)
238
{
239

240
    xmlNodePtr curr;
241
    LONG nodeCount = 0;
242 243 244

    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

245
    TRACE("(%p)->(%p)\n", This, listLength);
246

247 248 249
    if(!listLength)
        return E_INVALIDARG;

250
    curr = This->parent->children;
251
    while (curr)
252
    {
253
        nodeCount++;
254
        curr = curr->next;
255
    }
256 257

    *listLength = nodeCount;
258
    return S_OK;
259 260 261 262 263 264 265 266
}

static HRESULT WINAPI xmlnodelist_nextNode(
        IXMLDOMNodeList* iface,
        IXMLDOMNode** nextItem)
{
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

267
    TRACE("(%p)->(%p)\n", This, nextItem );
268

269 270 271
    if(!nextItem)
        return E_INVALIDARG;

272 273
    *nextItem = NULL;

274 275 276 277
    if (!This->current)
        return S_FALSE;

    *nextItem = create_node( This->current );
278
    This->current = This->current->next;
279 280 281 282 283 284
    return S_OK;
}

static HRESULT WINAPI xmlnodelist_reset(
        IXMLDOMNodeList* iface)
{
285 286 287
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

    TRACE("%p\n", This);
288
    This->current = This->parent->children;
289
    return S_OK;
290 291 292 293 294 295
}

static HRESULT WINAPI xmlnodelist__newEnum(
        IXMLDOMNodeList* iface,
        IUnknown** ppUnk)
{
296 297
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
    FIXME("(%p)->(%p)\n", This, ppUnk);
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    return E_NOTIMPL;
}


static const struct IXMLDOMNodeListVtbl xmlnodelist_vtbl =
{
    xmlnodelist_QueryInterface,
    xmlnodelist_AddRef,
    xmlnodelist_Release,
    xmlnodelist_GetTypeInfoCount,
    xmlnodelist_GetTypeInfo,
    xmlnodelist_GetIDsOfNames,
    xmlnodelist_Invoke,
    xmlnodelist_get_item,
    xmlnodelist_get_length,
    xmlnodelist_nextNode,
    xmlnodelist_reset,
    xmlnodelist__newEnum,
};

318
IXMLDOMNodeList* create_children_nodelist( xmlNodePtr node )
319 320 321
{
    xmlnodelist *nodelist;

322
    nodelist = heap_alloc( sizeof *nodelist );
323 324 325
    if ( !nodelist )
        return NULL;

326
    nodelist->IXMLDOMNodeList_iface.lpVtbl = &xmlnodelist_vtbl;
327
    nodelist->ref = 1;
328 329
    nodelist->parent = node;
    nodelist->current = node->children;
330

331
    xmldoc_add_ref( node->doc );
Huw Davies's avatar
Huw Davies committed
332

333
    return &nodelist->IXMLDOMNodeList_iface;
334 335 336
}

#endif