nodelist.c 7.65 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 26 27 28 29
 */

#define COBJMACROS

#include "config.h"

#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"
30
#include "msxml2.h"
31 32 33 34 35

#include "msxml_private.h"

#include "wine/debug.h"

36 37 38 39 40 41 42 43 44
/* 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
 */

45 46
WINE_DEFAULT_DEBUG_CHANNEL(msxml);

47 48 49 50 51 52
#ifdef HAVE_LIBXML2

typedef struct _xmlnodelist
{
    const struct IXMLDOMNodeListVtbl *lpVtbl;
    LONG ref;
53
    xmlNodePtr parent;
54 55 56 57 58 59 60 61 62 63 64 65 66
    xmlNodePtr current;
} xmlnodelist;

static inline xmlnodelist *impl_from_IXMLDOMNodeList( IXMLDOMNodeList *iface )
{
    return (xmlnodelist *)((char*)iface - FIELD_OFFSET(xmlnodelist, lpVtbl));
}

static HRESULT WINAPI xmlnodelist_QueryInterface(
    IXMLDOMNodeList *iface,
    REFIID riid,
    void** ppvObject )
{
Huw Davies's avatar
Huw Davies committed
67
    TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
68

69 70 71
    if(!ppvObject)
        return E_INVALIDARG;

72 73 74 75 76 77 78
    if ( IsEqualGUID( riid, &IID_IUnknown ) ||
         IsEqualGUID( riid, &IID_IDispatch ) ||
         IsEqualGUID( riid, &IID_IXMLDOMNodeList ) )
    {
        *ppvObject = iface;
    }
    else
79 80
    {
        FIXME("interface %s not implemented\n", debugstr_guid(riid));
81
        *ppvObject = NULL;
82
        return E_NOINTERFACE;
83
    }
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    IXMLDOMNodeList_AddRef( iface );

    return S_OK;
}

static ULONG WINAPI xmlnodelist_AddRef(
    IXMLDOMNodeList *iface )
{
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
    return InterlockedIncrement( &This->ref );
}

static ULONG WINAPI xmlnodelist_Release(
    IXMLDOMNodeList *iface )
{
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );
    ULONG ref;

    ref = InterlockedDecrement( &This->ref );
    if ( ref == 0 )
    {
106
        xmldoc_release( This->parent->doc );
107 108 109 110 111 112 113 114 115 116
        HeapFree( GetProcessHeap(), 0, This );
    }

    return ref;
}

static HRESULT WINAPI xmlnodelist_GetTypeInfoCount(
    IXMLDOMNodeList *iface,
    UINT* pctinfo )
{
117 118 119 120 121 122 123
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

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

    *pctinfo = 1;

    return S_OK;
124 125 126 127 128 129 130 131
}

static HRESULT WINAPI xmlnodelist_GetTypeInfo(
    IXMLDOMNodeList *iface,
    UINT iTInfo,
    LCID lcid,
    ITypeInfo** ppTInfo )
{
132 133 134 135 136 137 138 139
    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;
140 141 142 143 144 145 146 147 148 149
}

static HRESULT WINAPI xmlnodelist_GetIDsOfNames(
    IXMLDOMNodeList *iface,
    REFIID riid,
    LPOLESTR* rgszNames,
    UINT cNames,
    LCID lcid,
    DISPID* rgDispId )
{
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    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;
168 169 170 171 172 173 174 175 176 177 178 179 180
}

static HRESULT WINAPI xmlnodelist_Invoke(
    IXMLDOMNodeList *iface,
    DISPID dispIdMember,
    REFIID riid,
    LCID lcid,
    WORD wFlags,
    DISPPARAMS* pDispParams,
    VARIANT* pVarResult,
    EXCEPINFO* pExcepInfo,
    UINT* puArgErr )
{
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    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))
    {
        hr = ITypeInfo_Invoke(typeinfo, &(This->lpVtbl), dispIdMember, wFlags, pDispParams,
                pVarResult, pExcepInfo, puArgErr);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
197 198 199 200
}

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

208
    TRACE("%p %d\n", This, index);
209 210 211 212

    if(!listItem)
        return E_INVALIDARG;

213 214 215 216 217
    *listItem = NULL;

    if (index < 0)
        return S_FALSE;

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

226 227 228
    *listItem = create_node( curr );

    return S_OK;
229 230 231 232
}

static HRESULT WINAPI xmlnodelist_get_length(
        IXMLDOMNodeList* iface,
233
        LONG* listLength)
234
{
235

236
    xmlNodePtr curr;
237
    LONG nodeCount = 0;
238 239 240 241 242

    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

    TRACE("%p\n", This);

243 244 245
    if(!listLength)
        return E_INVALIDARG;

246
    curr = This->parent->children;
247
    while (curr)
248
    {
249
        nodeCount++;
250
        curr = curr->next;
251
    }
252 253

    *listLength = nodeCount;
254
    return S_OK;
255 256 257 258 259 260 261 262 263 264
}

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

    TRACE("%p %p\n", This, nextItem );

265 266 267
    if(!nextItem)
        return E_INVALIDARG;

268 269
    *nextItem = NULL;

270 271 272 273
    if (!This->current)
        return S_FALSE;

    *nextItem = create_node( This->current );
274
    This->current = This->current->next;
275 276 277 278 279 280
    return S_OK;
}

static HRESULT WINAPI xmlnodelist_reset(
        IXMLDOMNodeList* iface)
{
281 282 283
    xmlnodelist *This = impl_from_IXMLDOMNodeList( iface );

    TRACE("%p\n", This);
284
    This->current = This->parent->children;
285
    return S_OK;
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
}

static HRESULT WINAPI xmlnodelist__newEnum(
        IXMLDOMNodeList* iface,
        IUnknown** ppUnk)
{
    FIXME("\n");
    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,
};

313
IXMLDOMNodeList* create_children_nodelist( xmlNodePtr node )
314 315 316 317 318 319 320 321 322
{
    xmlnodelist *nodelist;

    nodelist = HeapAlloc( GetProcessHeap(), 0, sizeof *nodelist );
    if ( !nodelist )
        return NULL;

    nodelist->lpVtbl = &xmlnodelist_vtbl;
    nodelist->ref = 1;
323 324
    nodelist->parent = node;
    nodelist->current = node->children;
325

326
    xmldoc_add_ref( node->doc );
Huw Davies's avatar
Huw Davies committed
327

328 329 330 331
    return (IXMLDOMNodeList*) &nodelist->lpVtbl;
}

#endif