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 *childList = create_children_nodelist(This->node);
315 if (*childList == NULL)
316 return E_OUTOFMEMORY;
321 static HRESULT WINAPI xmlnode_get_firstChild(
323 IXMLDOMNode** firstChild)
325 xmlnode *This = impl_from_IXMLDOMNode( iface );
326 return get_node( This, "firstChild", This->node->children, firstChild );
329 static HRESULT WINAPI xmlnode_get_lastChild(
331 IXMLDOMNode** lastChild)
333 xmlnode *This = impl_from_IXMLDOMNode( iface );
334 return get_node( This, "lastChild", This->node->last, lastChild );
337 static HRESULT WINAPI xmlnode_get_previousSibling(
339 IXMLDOMNode** previousSibling)
341 xmlnode *This = impl_from_IXMLDOMNode( iface );
342 return get_node( This, "previous", This->node->prev, previousSibling );
345 static HRESULT WINAPI xmlnode_get_nextSibling(
347 IXMLDOMNode** nextSibling)
349 xmlnode *This = impl_from_IXMLDOMNode( iface );
350 return get_node( This, "next", This->node->next, nextSibling );
353 static HRESULT WINAPI xmlnode_get_attributes(
355 IXMLDOMNamedNodeMap** attributeMap)
357 xmlnode *This = impl_from_IXMLDOMNode( iface );
359 *attributeMap = create_nodemap( iface );
363 static HRESULT WINAPI xmlnode_insertBefore(
365 IXMLDOMNode* newChild,
367 IXMLDOMNode** outNewChild)
369 xmlnode *This = impl_from_IXMLDOMNode( iface );
370 xmlNodePtr before_node, new_child_node;
371 IXMLDOMNode *before = NULL, *new;
374 TRACE("(%p)->(%p,var,%p)\n",This,newChild,outNewChild);
376 if (!(newChild && outNewChild))
379 switch(V_VT(&refChild))
386 hr = IUnknown_QueryInterface(V_UNKNOWN(&refChild), &IID_IXMLDOMNode, (LPVOID)&before);
387 if(FAILED(hr)) return hr;
391 hr = IDispatch_QueryInterface(V_DISPATCH(&refChild), &IID_IXMLDOMNode, (LPVOID)&before);
392 if(FAILED(hr)) return hr;
396 FIXME("refChild var type %x\n", V_VT(&refChild));
400 IXMLDOMNode_QueryInterface(newChild, &IID_IXMLDOMNode, (LPVOID)&new);
401 new_child_node = impl_from_IXMLDOMNode(new)->node;
402 TRACE("new_child_node %p This->node %p\n", new_child_node, This->node);
406 before_node = impl_from_IXMLDOMNode(before)->node;
407 xmlAddPrevSibling(before_node, new_child_node);
408 IXMLDOMNode_Release(before);
412 xmlAddChild(This->node, new_child_node);
415 IXMLDOMNode_Release(new);
416 IXMLDOMNode_AddRef(newChild);
417 *outNewChild = newChild;
422 static HRESULT WINAPI xmlnode_replaceChild(
424 IXMLDOMNode* newChild,
425 IXMLDOMNode* oldChild,
426 IXMLDOMNode** outOldChild)
432 static HRESULT WINAPI xmlnode_removeChild(
434 IXMLDOMNode* childNode,
435 IXMLDOMNode** oldChild)
437 xmlnode *This = impl_from_IXMLDOMNode( iface );
438 xmlNode *ancestor, *child_node_ptr;
442 TRACE("%p->(%p, %p)\n", This, childNode, oldChild);
446 if(!childNode) return E_INVALIDARG;
448 hr = IXMLDOMNode_QueryInterface(childNode, &IID_IXMLDOMNode, (LPVOID)&child);
452 child_node_ptr = ancestor = impl_from_IXMLDOMNode(child)->node;
453 while(ancestor->parent)
455 if(ancestor->parent == This->node)
457 ancestor = ancestor->parent;
459 if(!ancestor->parent)
461 WARN("childNode %p is not a child of %p\n", childNode, iface);
462 IXMLDOMNode_Release(child);
466 xmlUnlinkNode(child_node_ptr);
468 IXMLDOMNode_Release(child);
469 IXMLDOMNode_AddRef(childNode);
470 *oldChild = childNode;
474 static HRESULT WINAPI xmlnode_appendChild(
476 IXMLDOMNode* newChild,
477 IXMLDOMNode** outNewChild)
479 xmlnode *This = impl_from_IXMLDOMNode( iface );
482 TRACE("(%p)->(%p,%p)\n", This, newChild, outNewChild);
484 return IXMLDOMNode_insertBefore(iface, newChild, var, outNewChild);
487 static HRESULT WINAPI xmlnode_hasChildNodes(
489 VARIANT_BOOL* hasChild)
491 xmlnode *This = impl_from_IXMLDOMNode( iface );
497 if (!This->node->children)
499 *hasChild = VARIANT_FALSE;
503 *hasChild = VARIANT_TRUE;
507 static HRESULT WINAPI xmlnode_get_ownerDocument(
509 IXMLDOMDocument** DOMDocument)
515 static HRESULT WINAPI xmlnode_cloneNode(
518 IXMLDOMNode** cloneRoot)
524 static HRESULT WINAPI xmlnode_get_nodeTypeString(
532 static HRESULT WINAPI xmlnode_get_text(
536 xmlnode *This = impl_from_IXMLDOMNode( iface );
544 switch(This->node->type)
546 case XML_ELEMENT_NODE:
547 case XML_ATTRIBUTE_NODE:
549 xmlNodePtr child = This->node->children;
550 if ( child && child->type == XML_TEXT_NODE )
551 str = bstr_from_xmlChar( child->content );
556 str = bstr_from_xmlChar( This->node->content );
560 FIXME("Unhandled node type %d\n", This->node->type);
563 TRACE("%p %s\n", This, debugstr_w(str) );
569 static HRESULT WINAPI xmlnode_put_text(
577 static HRESULT WINAPI xmlnode_get_specified(
579 VARIANT_BOOL* isSpecified)
585 static HRESULT WINAPI xmlnode_get_definition(
587 IXMLDOMNode** definitionNode)
593 static HRESULT WINAPI xmlnode_get_nodeTypedValue(
597 FIXME("ignoring data type\n");
598 return xmlnode_get_nodeValue(iface, typedValue);
601 static HRESULT WINAPI xmlnode_put_nodeTypedValue(
609 static HRESULT WINAPI xmlnode_get_dataType(
611 VARIANT* dataTypeName)
617 static HRESULT WINAPI xmlnode_put_dataType(
625 static HRESULT WINAPI xmlnode_get_xml(
633 static HRESULT WINAPI xmlnode_transformNode(
635 IXMLDOMNode* styleSheet,
642 static HRESULT WINAPI xmlnode_selectNodes(
645 IXMLDOMNodeList** resultList)
647 xmlnode *This = impl_from_IXMLDOMNode( iface );
649 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultList );
651 return queryresult_create( This->node, queryString, resultList );
654 static HRESULT WINAPI xmlnode_selectSingleNode(
657 IXMLDOMNode** resultNode)
659 xmlnode *This = impl_from_IXMLDOMNode( iface );
660 IXMLDOMNodeList *list;
663 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultNode );
666 r = IXMLDOMNode_selectNodes(iface, queryString, &list);
669 r = IXMLDOMNodeList_nextNode(list, resultNode);
670 IXMLDOMNodeList_Release(list);
675 static HRESULT WINAPI xmlnode_get_parsed(
677 VARIANT_BOOL* isParsed)
683 static HRESULT WINAPI xmlnode_get_namespaceURI(
691 static HRESULT WINAPI xmlnode_get_prefix(
699 static HRESULT WINAPI xmlnode_get_baseName(
703 xmlnode *This = impl_from_IXMLDOMNode( iface );
707 TRACE("%p %p\n", This, nameString );
712 switch ( This->node->type )
714 case XML_ELEMENT_NODE:
715 case XML_ATTRIBUTE_NODE:
716 str = bstr_from_xmlChar( This->node->name );
722 ERR("Unhandled type %d\n", This->node->type );
726 TRACE("returning %08x str = %s\n", r, debugstr_w( str ) );
732 static HRESULT WINAPI xmlnode_transformNodeToObject(
734 IXMLDOMNode* stylesheet,
735 VARIANT outputObject)
741 static const struct IXMLDOMNodeVtbl xmlnode_vtbl =
743 xmlnode_QueryInterface,
746 xmlnode_GetTypeInfoCount,
748 xmlnode_GetIDsOfNames,
750 xmlnode_get_nodeName,
751 xmlnode_get_nodeValue,
752 xmlnode_put_nodeValue,
753 xmlnode_get_nodeType,
754 xmlnode_get_parentNode,
755 xmlnode_get_childNodes,
756 xmlnode_get_firstChild,
757 xmlnode_get_lastChild,
758 xmlnode_get_previousSibling,
759 xmlnode_get_nextSibling,
760 xmlnode_get_attributes,
761 xmlnode_insertBefore,
762 xmlnode_replaceChild,
765 xmlnode_hasChildNodes,
766 xmlnode_get_ownerDocument,
768 xmlnode_get_nodeTypeString,
771 xmlnode_get_specified,
772 xmlnode_get_definition,
773 xmlnode_get_nodeTypedValue,
774 xmlnode_put_nodeTypedValue,
775 xmlnode_get_dataType,
776 xmlnode_put_dataType,
778 xmlnode_transformNode,
780 xmlnode_selectSingleNode,
782 xmlnode_get_namespaceURI,
784 xmlnode_get_baseName,
785 xmlnode_transformNodeToObject,
788 static HRESULT WINAPI Internal_QueryInterface(
793 xmlnode *This = impl_from_InternalUnknown( iface );
795 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
798 if ( IsEqualGUID( riid, &IID_IUnknown ))
800 else if ( IsEqualGUID( riid, &IID_IDispatch ) ||
801 IsEqualGUID( riid, &IID_IXMLDOMNode ) )
802 *ppvObject = &This->lpVtbl;
805 FIXME("interface %s not implemented\n", debugstr_guid(riid));
807 return E_NOINTERFACE;
810 IUnknown_AddRef( (IUnknown*)*ppvObject );
815 static ULONG WINAPI Internal_AddRef(
818 xmlnode *This = impl_from_InternalUnknown( iface );
819 return InterlockedIncrement( &This->ref );
822 static ULONG WINAPI Internal_Release(
825 xmlnode *This = impl_from_InternalUnknown( iface );
828 ref = InterlockedDecrement( &This->ref );
832 xmldoc_release( This->node->doc );
833 HeapFree( GetProcessHeap(), 0, This );
839 static const struct IUnknownVtbl internal_unk_vtbl =
841 Internal_QueryInterface,
846 IUnknown *create_basic_node( xmlNodePtr node, IUnknown *pUnkOuter )
850 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
855 xmldoc_add_ref( node->doc );
857 This->lpVtbl = &xmlnode_vtbl;
858 This->lpInternalUnkVtbl = &internal_unk_vtbl;
861 This->pUnkOuter = pUnkOuter; /* Don't take a ref on outer Unknown */
863 This->pUnkOuter = (IUnknown *)&This->lpInternalUnkVtbl;
868 return (IUnknown*)&This->lpInternalUnkVtbl;
871 IXMLDOMNode *create_node( xmlNodePtr node )
880 TRACE("type %d\n", node->type);
883 case XML_ELEMENT_NODE:
884 pUnk = create_element( node, NULL );
886 case XML_ATTRIBUTE_NODE:
887 pUnk = create_attribute( node );
890 pUnk = create_text( node );
892 case XML_COMMENT_NODE:
893 pUnk = create_comment( node );
895 case XML_DOCUMENT_NODE:
896 ERR("shouldn't be here!\n");
899 FIXME("only creating basic node for type %d\n", node->type);
900 pUnk = create_basic_node( node, NULL );
903 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
904 IUnknown_Release(pUnk);
905 if(FAILED(hr)) return NULL;