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 case XML_CDATA_SECTION_NODE:
558 case XML_COMMENT_NODE:
559 str = bstr_from_xmlChar( This->node->content );
563 FIXME("Unhandled node type %d\n", This->node->type);
566 TRACE("%p %s\n", This, debugstr_w(str) );
572 static HRESULT WINAPI xmlnode_put_text(
580 static HRESULT WINAPI xmlnode_get_specified(
582 VARIANT_BOOL* isSpecified)
588 static HRESULT WINAPI xmlnode_get_definition(
590 IXMLDOMNode** definitionNode)
596 static HRESULT WINAPI xmlnode_get_nodeTypedValue(
600 FIXME("ignoring data type\n");
601 return xmlnode_get_nodeValue(iface, typedValue);
604 static HRESULT WINAPI xmlnode_put_nodeTypedValue(
612 static HRESULT WINAPI xmlnode_get_dataType(
614 VARIANT* dataTypeName)
620 static HRESULT WINAPI xmlnode_put_dataType(
628 static HRESULT WINAPI xmlnode_get_xml(
636 static HRESULT WINAPI xmlnode_transformNode(
638 IXMLDOMNode* styleSheet,
645 static HRESULT WINAPI xmlnode_selectNodes(
648 IXMLDOMNodeList** resultList)
650 xmlnode *This = impl_from_IXMLDOMNode( iface );
652 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultList );
654 return queryresult_create( This->node, queryString, resultList );
657 static HRESULT WINAPI xmlnode_selectSingleNode(
660 IXMLDOMNode** resultNode)
662 xmlnode *This = impl_from_IXMLDOMNode( iface );
663 IXMLDOMNodeList *list;
666 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultNode );
669 r = IXMLDOMNode_selectNodes(iface, queryString, &list);
672 r = IXMLDOMNodeList_nextNode(list, resultNode);
673 IXMLDOMNodeList_Release(list);
678 static HRESULT WINAPI xmlnode_get_parsed(
680 VARIANT_BOOL* isParsed)
686 static HRESULT WINAPI xmlnode_get_namespaceURI(
694 static HRESULT WINAPI xmlnode_get_prefix(
702 static HRESULT WINAPI xmlnode_get_baseName(
706 xmlnode *This = impl_from_IXMLDOMNode( iface );
710 TRACE("%p %p\n", This, nameString );
715 switch ( This->node->type )
717 case XML_ELEMENT_NODE:
718 case XML_ATTRIBUTE_NODE:
719 str = bstr_from_xmlChar( This->node->name );
725 ERR("Unhandled type %d\n", This->node->type );
729 TRACE("returning %08x str = %s\n", r, debugstr_w( str ) );
735 static HRESULT WINAPI xmlnode_transformNodeToObject(
737 IXMLDOMNode* stylesheet,
738 VARIANT outputObject)
744 static const struct IXMLDOMNodeVtbl xmlnode_vtbl =
746 xmlnode_QueryInterface,
749 xmlnode_GetTypeInfoCount,
751 xmlnode_GetIDsOfNames,
753 xmlnode_get_nodeName,
754 xmlnode_get_nodeValue,
755 xmlnode_put_nodeValue,
756 xmlnode_get_nodeType,
757 xmlnode_get_parentNode,
758 xmlnode_get_childNodes,
759 xmlnode_get_firstChild,
760 xmlnode_get_lastChild,
761 xmlnode_get_previousSibling,
762 xmlnode_get_nextSibling,
763 xmlnode_get_attributes,
764 xmlnode_insertBefore,
765 xmlnode_replaceChild,
768 xmlnode_hasChildNodes,
769 xmlnode_get_ownerDocument,
771 xmlnode_get_nodeTypeString,
774 xmlnode_get_specified,
775 xmlnode_get_definition,
776 xmlnode_get_nodeTypedValue,
777 xmlnode_put_nodeTypedValue,
778 xmlnode_get_dataType,
779 xmlnode_put_dataType,
781 xmlnode_transformNode,
783 xmlnode_selectSingleNode,
785 xmlnode_get_namespaceURI,
787 xmlnode_get_baseName,
788 xmlnode_transformNodeToObject,
791 static HRESULT WINAPI Internal_QueryInterface(
796 xmlnode *This = impl_from_InternalUnknown( iface );
798 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
801 if ( IsEqualGUID( riid, &IID_IUnknown ))
803 else if ( IsEqualGUID( riid, &IID_IDispatch ) ||
804 IsEqualGUID( riid, &IID_IXMLDOMNode ) )
805 *ppvObject = &This->lpVtbl;
808 FIXME("interface %s not implemented\n", debugstr_guid(riid));
810 return E_NOINTERFACE;
813 IUnknown_AddRef( (IUnknown*)*ppvObject );
818 static ULONG WINAPI Internal_AddRef(
821 xmlnode *This = impl_from_InternalUnknown( iface );
822 return InterlockedIncrement( &This->ref );
825 static ULONG WINAPI Internal_Release(
828 xmlnode *This = impl_from_InternalUnknown( iface );
831 ref = InterlockedDecrement( &This->ref );
835 xmldoc_release( This->node->doc );
836 HeapFree( GetProcessHeap(), 0, This );
842 static const struct IUnknownVtbl internal_unk_vtbl =
844 Internal_QueryInterface,
849 IUnknown *create_basic_node( xmlNodePtr node, IUnknown *pUnkOuter )
853 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
858 xmldoc_add_ref( node->doc );
860 This->lpVtbl = &xmlnode_vtbl;
861 This->lpInternalUnkVtbl = &internal_unk_vtbl;
864 This->pUnkOuter = pUnkOuter; /* Don't take a ref on outer Unknown */
866 This->pUnkOuter = (IUnknown *)&This->lpInternalUnkVtbl;
871 return (IUnknown*)&This->lpInternalUnkVtbl;
874 IXMLDOMNode *create_node( xmlNodePtr node )
883 TRACE("type %d\n", node->type);
886 case XML_ELEMENT_NODE:
887 pUnk = create_element( node, NULL );
889 case XML_ATTRIBUTE_NODE:
890 pUnk = create_attribute( node );
893 pUnk = create_text( node );
895 case XML_COMMENT_NODE:
896 pUnk = create_comment( node );
898 case XML_DOCUMENT_NODE:
899 ERR("shouldn't be here!\n");
902 FIXME("only creating basic node for type %d\n", node->type);
903 pUnk = create_basic_node( node, NULL );
906 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
907 IUnknown_Release(pUnk);
908 if(FAILED(hr)) return NULL;