nodelist.c 11.1 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
#include "msxml2did.h"
37 38 39 40 41

#include "msxml_private.h"

#include "wine/debug.h"

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

51 52
#ifdef HAVE_LIBXML2

53 54
WINE_DEFAULT_DEBUG_CHANNEL(msxml);

55
typedef struct
56
{
57
    DispatchEx dispex;
58
    IXMLDOMNodeList IXMLDOMNodeList_iface;
59
    LONG ref;
60
    xmlNodePtr parent;
61
    xmlNodePtr current;
62
    IEnumVARIANT *enumvariant;
63 64
} xmlnodelist;

65
static HRESULT nodelist_get_item(IUnknown *iface, LONG index, VARIANT *item)
66
{
67 68
    V_VT(item) = VT_DISPATCH;
    return IXMLDOMNodeList_get_item((IXMLDOMNodeList*)iface, index, (IXMLDOMNode**)&V_DISPATCH(item));
69 70 71 72 73 74 75
}

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

76 77
static inline xmlnodelist *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
{
78
    return CONTAINING_RECORD(iface, xmlnodelist, IXMLDOMNodeList_iface);
79 80 81 82 83 84 85
}

static HRESULT WINAPI xmlnodelist_QueryInterface(
    IXMLDOMNodeList *iface,
    REFIID riid,
    void** ppvObject )
{
86 87 88
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
89 90 91 92 93 94 95

    if ( IsEqualGUID( riid, &IID_IUnknown ) ||
         IsEqualGUID( riid, &IID_IDispatch ) ||
         IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
    {
        *ppvObject = iface;
    }
96 97 98 99 100 101 102 103 104 105
    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);
    }
106 107 108 109
    else if (dispex_query_interface(&This->dispex, riid, ppvObject))
    {
        return *ppvObject ? S_OK : E_NOINTERFACE;
    }
110
    else
111
    {
112
        TRACE("interface %s not implemented\n", debugstr_guid(riid));
113
        *ppvObject = NULL;
114
        return E_NOINTERFACE;
115
    }
116 117 118 119 120 121 122 123 124 125

    IXMLDOMNodeList_AddRef( iface );

    return S_OK;
}

static ULONG WINAPI xmlnodelist_AddRef(
    IXMLDOMNodeList *iface )
{
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
126 127 128
    ULONG ref = InterlockedIncrement( &This->ref );
    TRACE("(%p)->(%d)\n", This, ref);
    return ref;
129 130 131 132 133 134
}

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

137
    TRACE("(%p)->(%d)\n", This, ref);
138 139
    if ( ref == 0 )
    {
140
        xmldoc_release( This->parent->doc );
141
        if (This->enumvariant) IEnumVARIANT_Release(This->enumvariant);
142
        heap_free( This );
143 144 145 146 147 148 149 150 151
    }

    return ref;
}

static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
    IXMLDOMNodeList *iface,
    UINT* pctinfo )
{
152
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
153
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
154 155 156 157 158 159 160 161
}

static HRESULT WINAPI xmlnodelist_GetTypeInfo(
    IXMLDOMNodeList *iface,
    UINT iTInfo,
    LCID lcid,
    ITypeInfo** ppTInfo )
{
162
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
163 164
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface,
        iTInfo, lcid, ppTInfo);
165 166 167 168 169 170 171 172 173 174
}

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

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

static HRESULT WINAPI xmlnodelist_get_item(
        IXMLDOMNodeList* iface,
198
        LONG index,
199 200
        IXMLDOMNode** listItem)
{
201
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
202
    xmlNodePtr curr;
203
    LONG nodeIndex = 0;
204

205
    TRACE("(%p)->(%d %p)\n", This, index, listItem);
206 207 208 209

    if(!listItem)
        return E_INVALIDARG;

210 211 212 213 214
    *listItem = NULL;

    if (index < 0)
        return S_FALSE;

215
    curr = This->parent->children;
216 217 218
    while(curr)
    {
        if(nodeIndex++ == index) break;
219
        curr = curr->next;
220
    }
221 222
    if(!curr) return S_FALSE;

223 224 225
    *listItem = create_node( curr );

    return S_OK;
226 227 228 229
}

static HRESULT WINAPI xmlnodelist_get_length(
        IXMLDOMNodeList* iface,
230
        LONG* listLength)
231
{
232

233
    xmlNodePtr curr;
234
    LONG nodeCount = 0;
235 236 237

    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

238
    TRACE("(%p)->(%p)\n", This, listLength);
239

240 241 242
    if(!listLength)
        return E_INVALIDARG;

243
    curr = This->parent->children;
244
    while (curr)
245
    {
246
        nodeCount++;
247
        curr = curr->next;
248
    }
249 250

    *listLength = nodeCount;
251
    return S_OK;
252 253 254 255 256 257 258 259
}

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

260
    TRACE("(%p)->(%p)\n", This, nextItem );
261

262 263 264
    if(!nextItem)
        return E_INVALIDARG;

265 266
    *nextItem = NULL;

267 268 269 270
    if (!This->current)
        return S_FALSE;

    *nextItem = create_node( This->current );
271
    This->current = This->current->next;
272 273 274 275 276 277
    return S_OK;
}

static HRESULT WINAPI xmlnodelist_reset(
        IXMLDOMNodeList* iface)
{
278 279 280
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

    TRACE("%p\n", This);
281
    This->current = This->parent->children;
282
    return S_OK;
283 284 285 286
}

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

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

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
static HRESULT xmlnodelist_get_dispid(IUnknown *iface, BSTR name, DWORD flags, DISPID *dispid)
{
    WCHAR *ptr;
    int idx = 0;

    for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
        idx = idx*10 + (*ptr-'0');
    if(*ptr)
        return DISP_E_UNKNOWNNAME;

    *dispid = DISPID_DOM_COLLECTION_BASE + idx;
    TRACE("ret %x\n", *dispid);
    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 );

    TRACE("(%p)->(%x %x %x %p %p %p)\n", This, id, lcid, flags, params, res, ei);

332
    if (id >= DISPID_DOM_COLLECTION_BASE && id <= DISPID_DOM_COLLECTION_MAX)
333
    {
334
        switch(flags)
335
        {
336 337 338 339 340 341 342 343 344 345 346 347 348 349
            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;
            }
350
        }
351 352 353 354
    }
    else if (id == DISPID_VALUE)
    {
        switch(flags)
355
        {
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
            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;
            }
384 385
        }
    }
386 387
    else
        return DISP_E_UNKNOWNNAME;
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

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

410
IXMLDOMNodeList* create_children_nodelist( xmlNodePtr node )
411
{
412
    xmlnodelist *This;
413

414 415
    This = heap_alloc( sizeof *This );
    if ( !This )
416 417
        return NULL;

418 419 420 421
    This->IXMLDOMNodeList_iface.lpVtbl = &xmlnodelist_vtbl;
    This->ref = 1;
    This->parent = node;
    This->current = node->children;
422
    This->enumvariant = NULL;
423
    xmldoc_add_ref( node->doc );
Huw Davies's avatar
Huw Davies committed
424

425 426 427
    init_dispex(&This->dispex, (IUnknown*)&This->IXMLDOMNodeList_iface, &xmlnodelist_dispex);

    return &This->IXMLDOMNodeList_iface;
428 429 430
}

#endif