nodelist.c 11 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
 */

#define COBJMACROS

#include <stdarg.h>
24 25
#include <libxml/parser.h>
#include <libxml/xmlerror.h>
26

27 28 29 30
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"
31
#include "msxml6.h"
32
#include "msxml2did.h"
33 34 35 36 37

#include "msxml_private.h"

#include "wine/debug.h"

38
/* This file implements the object returned by childNodes property. Note that this is
39
 * not the IXMLDOMNodeList returned by XPath queries - it's implemented in selection.c.
40 41 42 43 44 45 46
 * 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
 */

47 48
WINE_DEFAULT_DEBUG_CHANNEL(msxml);

49
typedef struct
50
{
51
    DispatchEx dispex;
52
    IXMLDOMNodeList IXMLDOMNodeList_iface;
53
    LONG ref;
54
    xmlNodePtr parent;
55
    xmlNodePtr current;
56
    IEnumVARIANT *enumvariant;
57 58
} xmlnodelist;

59
static HRESULT nodelist_get_item(IUnknown *iface, LONG index, VARIANT *item)
60
{
61 62
    V_VT(item) = VT_DISPATCH;
    return IXMLDOMNodeList_get_item((IXMLDOMNodeList*)iface, index, (IXMLDOMNode**)&V_DISPATCH(item));
63 64 65 66 67 68 69
}

static const struct enumvariant_funcs nodelist_enumvariant = {
    nodelist_get_item,
    NULL
};

70 71
static inline xmlnodelist *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
{
72
    return CONTAINING_RECORD(iface, xmlnodelist, IXMLDOMNodeList_iface);
73 74 75 76 77 78 79
}

static HRESULT WINAPI xmlnodelist_QueryInterface(
    IXMLDOMNodeList *iface,
    REFIID riid,
    void** ppvObject )
{
80 81 82
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
83 84 85 86 87 88 89

    if ( IsEqualGUID( riid, &IID_IUnknown ) ||
         IsEqualGUID( riid, &IID_IDispatch ) ||
         IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
    {
        *ppvObject = iface;
    }
90 91 92 93 94 95 96 97 98 99
    else if (IsEqualGUID( riid, &IID_IEnumVARIANT ))
    {
        if (!This->enumvariant)
        {
            HRESULT hr = create_enumvariant((IUnknown*)iface, FALSE, &nodelist_enumvariant, &This->enumvariant);
            if (FAILED(hr)) return hr;
        }

        return IEnumVARIANT_QueryInterface(This->enumvariant, &IID_IEnumVARIANT, ppvObject);
    }
100 101 102 103
    else if (dispex_query_interface(&This->dispex, riid, ppvObject))
    {
        return *ppvObject ? S_OK : E_NOINTERFACE;
    }
104
    else
105
    {
106
        TRACE("interface %s not implemented\n", debugstr_guid(riid));
107
        *ppvObject = NULL;
108
        return E_NOINTERFACE;
109
    }
110 111 112 113 114 115 116 117 118 119

    IXMLDOMNodeList_AddRef( iface );

    return S_OK;
}

static ULONG WINAPI xmlnodelist_AddRef(
    IXMLDOMNodeList *iface )
{
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
120
    ULONG ref = InterlockedIncrement( &This->ref );
121
    TRACE("%p, refcount %lu.\n", iface, ref);
122
    return ref;
123 124 125 126 127 128
}

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

131 132 133
    TRACE("%p, refcount %lu.\n", iface, ref);

    if (!ref)
134
    {
135
        xmldoc_release( This->parent->doc );
136
        if (This->enumvariant) IEnumVARIANT_Release(This->enumvariant);
137
        heap_free( This );
138 139 140 141 142 143 144 145 146
    }

    return ref;
}

static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
    IXMLDOMNodeList *iface,
    UINT* pctinfo )
{
147
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
148
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
149 150 151 152 153 154 155 156
}

static HRESULT WINAPI xmlnodelist_GetTypeInfo(
    IXMLDOMNodeList *iface,
    UINT iTInfo,
    LCID lcid,
    ITypeInfo** ppTInfo )
{
157
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
158 159
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface,
        iTInfo, lcid, ppTInfo);
160 161 162 163 164 165 166 167 168 169
}

static HRESULT WINAPI xmlnodelist_GetIDsOfNames(
    IXMLDOMNodeList *iface,
    REFIID riid,
    LPOLESTR* rgszNames,
    UINT cNames,
    LCID lcid,
    DISPID* rgDispId )
{
170
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
171 172
    return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface,
        riid, rgszNames, cNames, lcid, rgDispId);
173 174 175 176 177 178 179 180 181 182 183 184 185
}

static HRESULT WINAPI xmlnodelist_Invoke(
    IXMLDOMNodeList *iface,
    DISPID dispIdMember,
    REFIID riid,
    LCID lcid,
    WORD wFlags,
    DISPPARAMS* pDispParams,
    VARIANT* pVarResult,
    EXCEPINFO* pExcepInfo,
    UINT* puArgErr )
{
186
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
187 188
    return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface,
        dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
189 190 191 192
}

static HRESULT WINAPI xmlnodelist_get_item(
        IXMLDOMNodeList* iface,
193
        LONG index,
194 195
        IXMLDOMNode** listItem)
{
196
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
197
    xmlNodePtr curr;
198
    LONG nodeIndex = 0;
199

200
    TRACE("%p, %ld, %p.\n", iface, index, listItem);
201 202 203 204

    if(!listItem)
        return E_INVALIDARG;

205 206 207 208 209
    *listItem = NULL;

    if (index < 0)
        return S_FALSE;

210
    curr = This->parent->children;
211 212 213
    while(curr)
    {
        if(nodeIndex++ == index) break;
214
        curr = curr->next;
215
    }
216 217
    if(!curr) return S_FALSE;

218 219 220
    *listItem = create_node( curr );

    return S_OK;
221 222 223 224
}

static HRESULT WINAPI xmlnodelist_get_length(
        IXMLDOMNodeList* iface,
225
        LONG* listLength)
226
{
227

228
    xmlNodePtr curr;
229
    LONG nodeCount = 0;
230 231 232

    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

233
    TRACE("(%p)->(%p)\n", This, listLength);
234

235 236 237
    if(!listLength)
        return E_INVALIDARG;

238
    curr = This->parent->children;
239
    while (curr)
240
    {
241
        nodeCount++;
242
        curr = curr->next;
243
    }
244 245

    *listLength = nodeCount;
246
    return S_OK;
247 248 249 250 251 252 253 254
}

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

255
    TRACE("(%p)->(%p)\n", This, nextItem );
256

257 258 259
    if(!nextItem)
        return E_INVALIDARG;

260 261
    *nextItem = NULL;

262 263 264 265
    if (!This->current)
        return S_FALSE;

    *nextItem = create_node( This->current );
266
    This->current = This->current->next;
267 268 269 270 271 272
    return S_OK;
}

static HRESULT WINAPI xmlnodelist_reset(
        IXMLDOMNodeList* iface)
{
273 274 275
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

    TRACE("%p\n", This);
276
    This->current = This->parent->children;
277
    return S_OK;
278 279 280 281
}

static HRESULT WINAPI xmlnodelist__newEnum(
        IXMLDOMNodeList* iface,
282
        IUnknown** enumv)
283
{
284
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
285 286
    TRACE("(%p)->(%p)\n", This, enumv);
    return create_enumvariant((IUnknown*)iface, TRUE, &nodelist_enumvariant, (IEnumVARIANT**)enumv);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
}

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,
};

305 306 307 308 309
static HRESULT xmlnodelist_get_dispid(IUnknown *iface, BSTR name, DWORD flags, DISPID *dispid)
{
    WCHAR *ptr;
    int idx = 0;

310
    for(ptr = name; *ptr >= '0' && *ptr <= '9'; ptr++)
311 312 313 314 315
        idx = idx*10 + (*ptr-'0');
    if(*ptr)
        return DISP_E_UNKNOWNNAME;

    *dispid = DISPID_DOM_COLLECTION_BASE + idx;
316
    TRACE("ret %lx\n", *dispid);
317 318 319 320 321 322 323 324
    return S_OK;
}

static HRESULT xmlnodelist_invoke(IUnknown *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
        VARIANT *res, EXCEPINFO *ei)
{
    xmlnodelist *This = impl_from_IXMLDOMNodeList( (IXMLDOMNodeList*)iface );

325
    TRACE("%p, %ld, %lx, %x, %p, %p, %p.\n", iface, id, lcid, flags, params, res, ei);
326

327
    if (id >= DISPID_DOM_COLLECTION_BASE && id <= DISPID_DOM_COLLECTION_MAX)
328
    {
329
        switch(flags)
330
        {
331 332 333 334 335 336 337 338 339 340 341 342 343 344
            case DISPATCH_PROPERTYGET:
            {
                IXMLDOMNode *disp = NULL;

                V_VT(res) = VT_DISPATCH;
                IXMLDOMNodeList_get_item(&This->IXMLDOMNodeList_iface, id - DISPID_DOM_COLLECTION_BASE, &disp);
                V_DISPATCH(res) = (IDispatch*)disp;
                break;
            }
            default:
            {
                FIXME("unimplemented flags %x\n", flags);
                break;
            }
345
        }
346 347 348 349
    }
    else if (id == DISPID_VALUE)
    {
        switch(flags)
350
        {
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
            case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
            case DISPATCH_PROPERTYGET:
            case DISPATCH_METHOD:
            {
                IXMLDOMNode *item;
                VARIANT index;
                HRESULT hr;

                if (params->cArgs - params->cNamedArgs != 1) return DISP_E_BADPARAMCOUNT;

                VariantInit(&index);
                hr = VariantChangeType(&index, params->rgvarg, 0, VT_I4);
                if(FAILED(hr))
                {
                    FIXME("failed to convert arg, %s\n", debugstr_variant(params->rgvarg));
                    return hr;
                }

                IXMLDOMNodeList_get_item(&This->IXMLDOMNodeList_iface, V_I4(&index), &item);
                V_VT(res) = VT_DISPATCH;
                V_DISPATCH(res) = (IDispatch*)item;
                break;
            }
            default:
            {
                FIXME("DISPID_VALUE: unimplemented flags %x\n", flags);
                break;
            }
379 380
        }
    }
381 382
    else
        return DISP_E_UNKNOWNNAME;
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

    TRACE("ret %p\n", V_DISPATCH(res));

    return S_OK;
}

static const dispex_static_data_vtbl_t xmlnodelist_dispex_vtbl = {
    xmlnodelist_get_dispid,
    xmlnodelist_invoke
};

static const tid_t xmlnodelist_iface_tids[] = {
    IXMLDOMNodeList_tid,
    0
};
static dispex_static_data_t xmlnodelist_dispex = {
    &xmlnodelist_dispex_vtbl,
    IXMLDOMNodeList_tid,
    NULL,
    xmlnodelist_iface_tids
};

405
IXMLDOMNodeList* create_children_nodelist( xmlNodePtr node )
406
{
407
    xmlnodelist *This;
408

409 410
    This = heap_alloc( sizeof *This );
    if ( !This )
411 412
        return NULL;

413 414 415 416
    This->IXMLDOMNodeList_iface.lpVtbl = &xmlnodelist_vtbl;
    This->ref = 1;
    This->parent = node;
    This->current = node->children;
417
    This->enumvariant = NULL;
418
    xmldoc_add_ref( node->doc );
Huw Davies's avatar
Huw Davies committed
419

420 421 422
    init_dispex(&This->dispex, (IUnknown*)&This->IXMLDOMNodeList_iface, &xmlnodelist_dispex);

    return &This->IXMLDOMNodeList_iface;
423
}