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
34 #include "msxml_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
42 typedef struct _xmlnode
44 const struct IXMLDOMNodeVtbl *lpVtbl;
45 const struct IUnknownVtbl *lpInternalUnkVtbl;
51 static inline xmlnode *impl_from_IXMLDOMNode( IXMLDOMNode *iface )
53 return (xmlnode *)((char*)iface - FIELD_OFFSET(xmlnode, lpVtbl));
56 static inline xmlnode *impl_from_InternalUnknown( IUnknown *iface )
58 return (xmlnode *)((char*)iface - FIELD_OFFSET(xmlnode, lpInternalUnkVtbl));
61 xmlNodePtr xmlNodePtr_from_domnode( IXMLDOMNode *iface, xmlElementType type )
67 This = impl_from_IXMLDOMNode( iface );
70 if ( type && This->node->type != type )
75 void attach_xmlnode( IXMLDOMNode *node, xmlNodePtr xml )
77 xmlnode *This = impl_from_IXMLDOMNode( node );
80 xmldoc_release(This->node->doc);
84 xmldoc_add_ref(This->node->doc);
89 static HRESULT WINAPI xmlnode_QueryInterface(
94 xmlnode *This = impl_from_IXMLDOMNode( iface );
95 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
97 return IUnknown_QueryInterface(This->pUnkOuter, riid, ppvObject);
100 static ULONG WINAPI xmlnode_AddRef(
103 xmlnode *This = impl_from_IXMLDOMNode( iface );
104 return IUnknown_AddRef(This->pUnkOuter);
107 static ULONG WINAPI xmlnode_Release(
110 xmlnode *This = impl_from_IXMLDOMNode( iface );
111 return IUnknown_Release(This->pUnkOuter);
114 static HRESULT WINAPI xmlnode_GetTypeInfoCount(
122 static HRESULT WINAPI xmlnode_GetTypeInfo(
126 ITypeInfo** ppTInfo )
132 static HRESULT WINAPI xmlnode_GetIDsOfNames(
144 static HRESULT WINAPI xmlnode_Invoke(
150 DISPPARAMS* pDispParams,
152 EXCEPINFO* pExcepInfo,
159 static HRESULT WINAPI xmlnode_get_nodeName(
163 xmlnode *This = impl_from_IXMLDOMNode( iface );
166 TRACE("%p\n", This );
174 switch( This->node->type )
177 str = (const xmlChar*) "#text";
179 case XML_DOCUMENT_NODE:
180 str = (const xmlChar*) "#document";
183 str = This->node->name;
187 *name = bstr_from_xmlChar( str );
194 BSTR bstr_from_xmlChar( const xmlChar *buf )
203 len = MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) buf, -1, NULL, 0 );
204 str = (LPWSTR) HeapAlloc( GetProcessHeap(), 0, len * sizeof (WCHAR) );
207 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) buf, -1, str, len );
208 bstr = SysAllocString( str );
209 HeapFree( GetProcessHeap(), 0, str );
213 static HRESULT WINAPI xmlnode_get_nodeValue(
217 xmlnode *This = impl_from_IXMLDOMNode( iface );
220 TRACE("%p %p\n", This, value);
222 V_BSTR(value) = NULL;
223 V_VT(value) = VT_NULL;
225 switch ( This->node->type )
227 case XML_ATTRIBUTE_NODE:
229 xmlChar *content = xmlNodeGetContent(This->node);
230 V_VT(value) = VT_BSTR;
231 V_BSTR(value) = bstr_from_xmlChar( content );
237 V_VT(value) = VT_BSTR;
238 V_BSTR(value) = bstr_from_xmlChar( This->node->content );
241 case XML_ELEMENT_NODE:
242 case XML_DOCUMENT_NODE:
243 /* these seem to return NULL */
247 FIXME("node %p type %d\n", This, This->node->type);
250 TRACE("%p returned %s\n", This, debugstr_w( V_BSTR(value) ) );
255 static HRESULT WINAPI xmlnode_put_nodeValue(
263 static HRESULT WINAPI xmlnode_get_nodeType(
267 xmlnode *This = impl_from_IXMLDOMNode( iface );
269 TRACE("%p %p\n", This, type);
271 assert( NODE_ELEMENT == XML_ELEMENT_NODE );
272 assert( NODE_NOTATION == XML_NOTATION_NODE );
274 *type = This->node->type;
279 static HRESULT get_node(
285 TRACE("%p->%s %p\n", This, name, node );
289 *out = create_node( node );
295 static HRESULT WINAPI xmlnode_get_parentNode(
297 IXMLDOMNode** parent)
299 xmlnode *This = impl_from_IXMLDOMNode( iface );
300 return get_node( This, "parent", This->node->parent, parent );
303 static HRESULT WINAPI xmlnode_get_childNodes(
305 IXMLDOMNodeList** childList)
307 xmlnode *This = impl_from_IXMLDOMNode( iface );
309 TRACE("%p %p\n", This, childList );
314 switch(This->node->type)
316 case XML_ELEMENT_NODE:
317 *childList = create_filtered_nodelist( This->node->children, (const xmlChar *)"*", FALSE );
320 case XML_ATTRIBUTE_NODE:
321 *childList = create_filtered_nodelist( This->node->children, (const xmlChar *)"node()", FALSE );
325 FIXME("unhandled node type %d\n", This->node->type);
334 static HRESULT WINAPI xmlnode_get_firstChild(
336 IXMLDOMNode** firstChild)
338 xmlnode *This = impl_from_IXMLDOMNode( iface );
339 return get_node( This, "firstChild", This->node->children, firstChild );
342 static HRESULT WINAPI xmlnode_get_lastChild(
344 IXMLDOMNode** lastChild)
346 xmlnode *This = impl_from_IXMLDOMNode( iface );
347 return get_node( This, "lastChild", This->node->last, lastChild );
350 static HRESULT WINAPI xmlnode_get_previousSibling(
352 IXMLDOMNode** previousSibling)
354 xmlnode *This = impl_from_IXMLDOMNode( iface );
355 return get_node( This, "previous", This->node->prev, previousSibling );
358 static HRESULT WINAPI xmlnode_get_nextSibling(
360 IXMLDOMNode** nextSibling)
362 xmlnode *This = impl_from_IXMLDOMNode( iface );
363 return get_node( This, "next", This->node->next, nextSibling );
366 static HRESULT WINAPI xmlnode_get_attributes(
368 IXMLDOMNamedNodeMap** attributeMap)
370 xmlnode *This = impl_from_IXMLDOMNode( iface );
372 *attributeMap = create_nodemap( iface );
376 static HRESULT WINAPI xmlnode_insertBefore(
378 IXMLDOMNode* newChild,
380 IXMLDOMNode** outNewChild)
382 xmlnode *This = impl_from_IXMLDOMNode( iface );
383 xmlNodePtr before_node, new_child_node;
384 IXMLDOMNode *before = NULL, *new;
387 TRACE("(%p)->(%p,var,%p)\n",This,newChild,outNewChild);
389 switch(V_VT(&refChild))
396 hr = IUnknown_QueryInterface(V_UNKNOWN(&refChild), &IID_IXMLDOMNode, (LPVOID)&before);
397 if(FAILED(hr)) return hr;
401 hr = IDispatch_QueryInterface(V_DISPATCH(&refChild), &IID_IXMLDOMNode, (LPVOID)&before);
402 if(FAILED(hr)) return hr;
406 FIXME("refChild var type %x\n", V_VT(&refChild));
410 IXMLDOMNode_QueryInterface(newChild, &IID_IXMLDOMNode, (LPVOID)&new);
411 new_child_node = impl_from_IXMLDOMNode(new)->node;
412 TRACE("new_child_node %p This->node %p\n", new_child_node, This->node);
416 before_node = impl_from_IXMLDOMNode(before)->node;
417 xmlAddPrevSibling(before_node, new_child_node);
418 IXMLDOMNode_Release(before);
422 xmlAddChild(This->node, new_child_node);
425 IXMLDOMNode_Release(new);
426 IXMLDOMNode_AddRef(newChild);
427 *outNewChild = newChild;
432 static HRESULT WINAPI xmlnode_replaceChild(
434 IXMLDOMNode* newChild,
435 IXMLDOMNode* oldChild,
436 IXMLDOMNode** outOldChild)
442 static HRESULT WINAPI xmlnode_removeChild(
444 IXMLDOMNode* childNode,
445 IXMLDOMNode** oldChild)
447 xmlnode *This = impl_from_IXMLDOMNode( iface );
448 xmlNode *ancestor, *child_node_ptr;
452 TRACE("%p->(%p, %p)\n", This, childNode, oldChild);
456 if(!childNode) return E_INVALIDARG;
458 hr = IXMLDOMNode_QueryInterface(childNode, &IID_IXMLDOMNode, (LPVOID)&child);
462 child_node_ptr = ancestor = impl_from_IXMLDOMNode(child)->node;
463 while(ancestor->parent)
465 if(ancestor->parent == This->node)
467 ancestor = ancestor->parent;
469 if(!ancestor->parent)
471 WARN("childNode %p is not a child of %p\n", childNode, iface);
472 IXMLDOMNode_Release(child);
476 xmlUnlinkNode(child_node_ptr);
478 IXMLDOMNode_Release(child);
479 IXMLDOMNode_AddRef(childNode);
480 *oldChild = childNode;
484 static HRESULT WINAPI xmlnode_appendChild(
486 IXMLDOMNode* newChild,
487 IXMLDOMNode** outNewChild)
489 xmlnode *This = impl_from_IXMLDOMNode( iface );
492 TRACE("(%p)->(%p,%p)\n", This, newChild, outNewChild);
494 return IXMLDOMNode_insertBefore(iface, newChild, var, outNewChild);
497 static HRESULT WINAPI xmlnode_hasChildNodes(
499 VARIANT_BOOL* hasChild)
501 xmlnode *This = impl_from_IXMLDOMNode( iface );
507 if (!This->node->children)
509 *hasChild = VARIANT_FALSE;
513 *hasChild = VARIANT_TRUE;
517 static HRESULT WINAPI xmlnode_get_ownerDocument(
519 IXMLDOMDocument** DOMDocument)
525 static HRESULT WINAPI xmlnode_cloneNode(
528 IXMLDOMNode** cloneRoot)
534 static HRESULT WINAPI xmlnode_get_nodeTypeString(
542 static HRESULT WINAPI xmlnode_get_text(
546 xmlnode *This = impl_from_IXMLDOMNode( iface );
554 switch(This->node->type)
556 case XML_ELEMENT_NODE:
557 case XML_ATTRIBUTE_NODE:
559 xmlNodePtr child = This->node->children;
560 if ( child && child->type == XML_TEXT_NODE )
561 str = bstr_from_xmlChar( child->content );
566 str = bstr_from_xmlChar( This->node->content );
570 FIXME("Unhandled node type %d\n", This->node->type);
573 TRACE("%p %s\n", This, debugstr_w(str) );
579 static HRESULT WINAPI xmlnode_put_text(
587 static HRESULT WINAPI xmlnode_get_specified(
589 VARIANT_BOOL* isSpecified)
595 static HRESULT WINAPI xmlnode_get_definition(
597 IXMLDOMNode** definitionNode)
603 static HRESULT WINAPI xmlnode_get_nodeTypedValue(
607 FIXME("ignoring data type\n");
608 return xmlnode_get_nodeValue(iface, typedValue);
611 static HRESULT WINAPI xmlnode_put_nodeTypedValue(
619 static HRESULT WINAPI xmlnode_get_dataType(
621 VARIANT* dataTypeName)
627 static HRESULT WINAPI xmlnode_put_dataType(
635 static HRESULT WINAPI xmlnode_get_xml(
643 static HRESULT WINAPI xmlnode_transformNode(
645 IXMLDOMNode* styleSheet,
652 static HRESULT WINAPI xmlnode_selectNodes(
655 IXMLDOMNodeList** resultList)
657 xmlnode *This = impl_from_IXMLDOMNode( iface );
661 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultList );
663 str = xmlChar_from_wchar( queryString );
667 if( !This->node->children )
670 *resultList = create_filtered_nodelist( This->node->children, str, FALSE );
671 HeapFree( GetProcessHeap(), 0, str );
675 static HRESULT WINAPI xmlnode_selectSingleNode(
678 IXMLDOMNode** resultNode)
680 xmlnode *This = impl_from_IXMLDOMNode( iface );
681 IXMLDOMNodeList *list;
684 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultNode );
686 r = IXMLDOMNode_selectNodes(iface, queryString, &list);
689 r = IXMLDOMNodeList_nextNode(list, resultNode);
690 IXMLDOMNodeList_Release(list);
695 static HRESULT WINAPI xmlnode_get_parsed(
697 VARIANT_BOOL* isParsed)
703 static HRESULT WINAPI xmlnode_get_namespaceURI(
711 static HRESULT WINAPI xmlnode_get_prefix(
719 static HRESULT WINAPI xmlnode_get_baseName(
723 xmlnode *This = impl_from_IXMLDOMNode( iface );
727 TRACE("%p %p\n", This, nameString );
732 switch ( This->node->type )
734 case XML_ELEMENT_NODE:
735 case XML_ATTRIBUTE_NODE:
736 str = bstr_from_xmlChar( This->node->name );
742 ERR("Unhandled type %d\n", This->node->type );
746 TRACE("returning %08lx str = %s\n", r, debugstr_w( str ) );
752 static HRESULT WINAPI xmlnode_transformNodeToObject(
754 IXMLDOMNode* stylesheet,
755 VARIANT outputObject)
761 static const struct IXMLDOMNodeVtbl xmlnode_vtbl =
763 xmlnode_QueryInterface,
766 xmlnode_GetTypeInfoCount,
768 xmlnode_GetIDsOfNames,
770 xmlnode_get_nodeName,
771 xmlnode_get_nodeValue,
772 xmlnode_put_nodeValue,
773 xmlnode_get_nodeType,
774 xmlnode_get_parentNode,
775 xmlnode_get_childNodes,
776 xmlnode_get_firstChild,
777 xmlnode_get_lastChild,
778 xmlnode_get_previousSibling,
779 xmlnode_get_nextSibling,
780 xmlnode_get_attributes,
781 xmlnode_insertBefore,
782 xmlnode_replaceChild,
785 xmlnode_hasChildNodes,
786 xmlnode_get_ownerDocument,
788 xmlnode_get_nodeTypeString,
791 xmlnode_get_specified,
792 xmlnode_get_definition,
793 xmlnode_get_nodeTypedValue,
794 xmlnode_put_nodeTypedValue,
795 xmlnode_get_dataType,
796 xmlnode_put_dataType,
798 xmlnode_transformNode,
800 xmlnode_selectSingleNode,
802 xmlnode_get_namespaceURI,
804 xmlnode_get_baseName,
805 xmlnode_transformNodeToObject,
808 static HRESULT WINAPI Internal_QueryInterface(
813 xmlnode *This = impl_from_InternalUnknown( iface );
815 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
818 if ( IsEqualGUID( riid, &IID_IUnknown ))
820 else if ( IsEqualGUID( riid, &IID_IDispatch ) ||
821 IsEqualGUID( riid, &IID_IXMLDOMNode ) )
822 *ppvObject = &This->lpVtbl;
825 FIXME("interface %s not implemented\n", debugstr_guid(riid));
827 return E_NOINTERFACE;
830 IUnknown_AddRef( (IUnknown*)*ppvObject );
835 static ULONG WINAPI Internal_AddRef(
838 xmlnode *This = impl_from_InternalUnknown( iface );
839 return InterlockedIncrement( &This->ref );
842 static ULONG WINAPI Internal_Release(
845 xmlnode *This = impl_from_InternalUnknown( iface );
848 ref = InterlockedDecrement( &This->ref );
852 xmldoc_release( This->node->doc );
853 HeapFree( GetProcessHeap(), 0, This );
859 static const struct IUnknownVtbl internal_unk_vtbl =
861 Internal_QueryInterface,
866 IUnknown *create_basic_node( xmlNodePtr node, IUnknown *pUnkOuter )
870 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
875 xmldoc_add_ref( node->doc );
877 This->lpVtbl = &xmlnode_vtbl;
878 This->lpInternalUnkVtbl = &internal_unk_vtbl;
881 This->pUnkOuter = pUnkOuter; /* Don't take a ref on outer Unknown */
883 This->pUnkOuter = (IUnknown *)&This->lpInternalUnkVtbl;
888 return (IUnknown*)&This->lpInternalUnkVtbl;
891 IXMLDOMNode *create_node( xmlNodePtr node )
900 TRACE("type %d\n", node->type);
903 case XML_ELEMENT_NODE:
904 pUnk = create_element( node, NULL );
906 case XML_ATTRIBUTE_NODE:
907 pUnk = create_attribute( node );
910 pUnk = create_text( node );
912 case XML_COMMENT_NODE:
913 pUnk = create_comment( node );
915 case XML_DOCUMENT_NODE:
916 ERR("shouldn't be here!\n");
919 FIXME("only creating basic node for type %d\n", node->type);
920 pUnk = create_basic_node( node, NULL );
923 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
924 IUnknown_Release(pUnk);
925 if(FAILED(hr)) return NULL;