2 * Node map 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "msxml_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
41 typedef struct _xmlnodemap
43 const struct IXMLDOMNamedNodeMapVtbl *lpVtbl;
44 const struct ISupportErrorInfoVtbl *lpSEIVtbl;
50 static inline xmlnodemap *impl_from_IXMLDOMNamedNodeMap( IXMLDOMNamedNodeMap *iface )
52 return (xmlnodemap *)((char*)iface - FIELD_OFFSET(xmlnodemap, lpVtbl));
55 static inline xmlnodemap *impl_from_ISupportErrorInfo( ISupportErrorInfo *iface )
57 return (xmlnodemap *)((char*)iface - FIELD_OFFSET(xmlnodemap, lpSEIVtbl));
60 static HRESULT WINAPI xmlnodemap_QueryInterface(
61 IXMLDOMNamedNodeMap *iface,
62 REFIID riid, void** ppvObject )
64 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
65 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
67 if( IsEqualGUID( riid, &IID_IUnknown ) ||
68 IsEqualGUID( riid, &IID_IDispatch ) ||
69 IsEqualGUID( riid, &IID_IXMLDOMNamedNodeMap ) )
73 else if( IsEqualGUID( riid, &IID_ISupportErrorInfo ))
75 *ppvObject = &This->lpSEIVtbl;
79 FIXME("interface %s not implemented\n", debugstr_guid(riid));
83 IXMLDOMElement_AddRef( iface );
88 static ULONG WINAPI xmlnodemap_AddRef(
89 IXMLDOMNamedNodeMap *iface )
91 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
92 return InterlockedIncrement( &This->ref );
95 static ULONG WINAPI xmlnodemap_Release(
96 IXMLDOMNamedNodeMap *iface )
98 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
101 ref = InterlockedDecrement( &This->ref );
104 IXMLDOMNode_Release( This->node );
105 HeapFree( GetProcessHeap(), 0, This );
111 static HRESULT WINAPI xmlnodemap_GetTypeInfoCount(
112 IXMLDOMNamedNodeMap *iface,
115 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
117 TRACE("(%p)->(%p)\n", This, pctinfo);
124 static HRESULT WINAPI xmlnodemap_GetTypeInfo(
125 IXMLDOMNamedNodeMap *iface,
126 UINT iTInfo, LCID lcid,
127 ITypeInfo** ppTInfo )
129 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
132 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
134 hr = get_typeinfo(IXMLDOMNamedNodeMap_tid, ppTInfo);
139 static HRESULT WINAPI xmlnodemap_GetIDsOfNames(
140 IXMLDOMNamedNodeMap *iface,
141 REFIID riid, LPOLESTR* rgszNames,
142 UINT cNames, LCID lcid, DISPID* rgDispId )
144 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
148 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
151 if(!rgszNames || cNames == 0 || !rgDispId)
154 hr = get_typeinfo(IXMLDOMNamedNodeMap_tid, &typeinfo);
157 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
158 ITypeInfo_Release(typeinfo);
164 static HRESULT WINAPI xmlnodemap_Invoke(
165 IXMLDOMNamedNodeMap *iface,
166 DISPID dispIdMember, REFIID riid, LCID lcid,
167 WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
168 EXCEPINFO* pExcepInfo, UINT* puArgErr )
170 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
174 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
175 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
177 hr = get_typeinfo(IXMLDOMNamedNodeMap_tid, &typeinfo);
180 hr = ITypeInfo_Invoke(typeinfo, &(This->lpVtbl), dispIdMember, wFlags, pDispParams,
181 pVarResult, pExcepInfo, puArgErr);
182 ITypeInfo_Release(typeinfo);
188 xmlChar *xmlChar_from_wchar( LPWSTR str )
193 len = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
194 xmlstr = HeapAlloc( GetProcessHeap(), 0, len );
196 WideCharToMultiByte( CP_UTF8, 0, str, -1, (LPSTR) xmlstr, len, NULL, NULL );
200 static HRESULT WINAPI xmlnodemap_getNamedItem(
201 IXMLDOMNamedNodeMap *iface,
203 IXMLDOMNode** namedItem)
205 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
206 xmlChar *element_name;
210 TRACE("%p %s %p\n", This, debugstr_w(name), namedItem );
215 node = xmlNodePtr_from_domnode( This->node, 0 );
219 element_name = xmlChar_from_wchar( name );
220 attr = xmlHasNsProp( node, element_name, NULL );
221 HeapFree( GetProcessHeap(), 0, element_name );
229 *namedItem = create_node( (xmlNodePtr) attr );
234 static HRESULT WINAPI xmlnodemap_setNamedItem(
235 IXMLDOMNamedNodeMap *iface,
236 IXMLDOMNode* newItem,
237 IXMLDOMNode** namedItem)
239 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
240 xmlnode *ThisNew = NULL;
242 IXMLDOMNode *pAttr = NULL;
245 TRACE("%p %p %p\n", This, newItem, namedItem );
250 if(namedItem) *namedItem = NULL;
252 node = xmlNodePtr_from_domnode( This->node, 0 );
256 /* Must be an Attribute */
257 IUnknown_QueryInterface(newItem, &IID_IXMLDOMNode, (LPVOID*)&pAttr);
260 ThisNew = impl_from_IXMLDOMNode( pAttr );
262 if(ThisNew->node->type != XML_ATTRIBUTE_NODE)
264 IUnknown_Release(pAttr);
268 if(!ThisNew->node->parent)
269 if(xmldoc_remove_orphan(ThisNew->node->doc, ThisNew->node) != S_OK)
270 WARN("%p is not an orphan of %p\n", ThisNew->node, ThisNew->node->doc);
272 nodeNew = xmlAddChild(node, ThisNew->node);
275 *namedItem = create_node( nodeNew );
277 IUnknown_Release(pAttr);
285 static HRESULT WINAPI xmlnodemap_removeNamedItem(
286 IXMLDOMNamedNodeMap *iface,
288 IXMLDOMNode** namedItem)
290 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
291 xmlChar *element_name;
295 TRACE("%p %s %p\n", This, debugstr_w(name), namedItem );
300 node = xmlNodePtr_from_domnode( This->node, 0 );
304 element_name = xmlChar_from_wchar( name );
305 attr = xmlHasNsProp( node, element_name, NULL );
306 HeapFree( GetProcessHeap(), 0, element_name );
317 xmlUnlinkNode( (xmlNodePtr) attr );
318 xmldoc_add_orphan( attr->doc, (xmlNodePtr) attr );
319 *namedItem = create_node( (xmlNodePtr) attr );
323 if( xmlRemoveProp( attr ) == -1 )
324 ERR("xmlRemoveProp failed\n");
330 static HRESULT WINAPI xmlnodemap_get_item(
331 IXMLDOMNamedNodeMap *iface,
333 IXMLDOMNode** listItem)
335 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
340 TRACE("%p %d\n", This, index);
347 node = xmlNodePtr_from_domnode( This->node, 0 );
348 curr = node->properties;
350 for (attrIndex = 0; attrIndex < index; attrIndex++) {
351 if (curr->next == NULL)
357 *listItem = create_node( (xmlNodePtr) curr );
362 static HRESULT WINAPI xmlnodemap_get_length(
363 IXMLDOMNamedNodeMap *iface,
371 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
378 node = xmlNodePtr_from_domnode( This->node, 0 );
382 first = node->properties;
390 while (curr->next != NULL) {
394 *listLength = attrCount;
399 static HRESULT WINAPI xmlnodemap_getQualifiedItem(
400 IXMLDOMNamedNodeMap *iface,
403 IXMLDOMNode** qualifiedItem)
409 static HRESULT WINAPI xmlnodemap_removeQualifiedItem(
410 IXMLDOMNamedNodeMap *iface,
413 IXMLDOMNode** qualifiedItem)
419 static HRESULT WINAPI xmlnodemap_nextNode(
420 IXMLDOMNamedNodeMap *iface,
421 IXMLDOMNode** nextItem)
423 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
428 TRACE("%p %ld\n", This, This->iterator);
432 node = xmlNodePtr_from_domnode( This->node, 0 );
433 curr = node->properties;
435 for (attrIndex = 0; attrIndex < This->iterator; attrIndex++) {
436 if (curr->next == NULL)
444 *nextItem = create_node( (xmlNodePtr) curr );
449 static HRESULT WINAPI xmlnodemap_reset(
450 IXMLDOMNamedNodeMap *iface )
452 xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
454 TRACE("%p %ld\n", This, This->iterator);
461 static HRESULT WINAPI xmlnodemap__newEnum(
462 IXMLDOMNamedNodeMap *iface,
469 static const struct IXMLDOMNamedNodeMapVtbl xmlnodemap_vtbl =
471 xmlnodemap_QueryInterface,
474 xmlnodemap_GetTypeInfoCount,
475 xmlnodemap_GetTypeInfo,
476 xmlnodemap_GetIDsOfNames,
478 xmlnodemap_getNamedItem,
479 xmlnodemap_setNamedItem,
480 xmlnodemap_removeNamedItem,
482 xmlnodemap_get_length,
483 xmlnodemap_getQualifiedItem,
484 xmlnodemap_removeQualifiedItem,
490 static HRESULT WINAPI support_error_QueryInterface(
491 ISupportErrorInfo *iface,
492 REFIID riid, void** ppvObject )
494 xmlnodemap *This = impl_from_ISupportErrorInfo( iface );
495 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
497 return IXMLDOMNamedNodeMap_QueryInterface((IXMLDOMNamedNodeMap*)&This->lpVtbl, riid, ppvObject);
500 static ULONG WINAPI support_error_AddRef(
501 ISupportErrorInfo *iface )
503 xmlnodemap *This = impl_from_ISupportErrorInfo( iface );
504 return IXMLDOMNamedNodeMap_AddRef((IXMLDOMNamedNodeMap*)&This->lpVtbl);
507 static ULONG WINAPI support_error_Release(
508 ISupportErrorInfo *iface )
510 xmlnodemap *This = impl_from_ISupportErrorInfo( iface );
511 return IXMLDOMNamedNodeMap_Release((IXMLDOMNamedNodeMap*)&This->lpVtbl);
514 static HRESULT WINAPI support_error_InterfaceSupportsErrorInfo(
515 ISupportErrorInfo *iface,
518 FIXME("(%p)->(%s)\n", iface, debugstr_guid(riid));
522 static const struct ISupportErrorInfoVtbl support_error_vtbl =
524 support_error_QueryInterface,
525 support_error_AddRef,
526 support_error_Release,
527 support_error_InterfaceSupportsErrorInfo
530 IXMLDOMNamedNodeMap *create_nodemap( IXMLDOMNode *node )
534 nodemap = HeapAlloc( GetProcessHeap(), 0, sizeof *nodemap );
538 nodemap->lpVtbl = &xmlnodemap_vtbl;
539 nodemap->lpSEIVtbl = &support_error_vtbl;
540 nodemap->node = node;
542 nodemap->iterator = 0;
544 IXMLDOMNode_AddRef( node );
545 /* Since we AddRef a node here, we don't need to call xmldoc_add_ref() */
547 return (IXMLDOMNamedNodeMap*) &nodemap->lpVtbl;