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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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)
451 static HRESULT WINAPI xmlnode_appendChild(
453 IXMLDOMNode* newChild,
454 IXMLDOMNode** outNewChild)
456 xmlnode *This = impl_from_IXMLDOMNode( iface );
459 TRACE("(%p)->(%p,%p)\n", This, newChild, outNewChild);
461 return IXMLDOMNode_insertBefore(iface, newChild, var, outNewChild);
464 static HRESULT WINAPI xmlnode_hasChildNodes(
466 VARIANT_BOOL* hasChild)
468 xmlnode *This = impl_from_IXMLDOMNode( iface );
474 if (!This->node->children)
476 *hasChild = VARIANT_FALSE;
480 *hasChild = VARIANT_TRUE;
484 static HRESULT WINAPI xmlnode_get_ownerDocument(
486 IXMLDOMDocument** DOMDocument)
492 static HRESULT WINAPI xmlnode_cloneNode(
495 IXMLDOMNode** cloneRoot)
501 static HRESULT WINAPI xmlnode_get_nodeTypeString(
509 static HRESULT WINAPI xmlnode_get_text(
513 xmlnode *This = impl_from_IXMLDOMNode( iface );
521 switch(This->node->type)
523 case XML_ELEMENT_NODE:
524 case XML_ATTRIBUTE_NODE:
526 xmlNodePtr child = This->node->children;
527 if ( child && child->type == XML_TEXT_NODE )
528 str = bstr_from_xmlChar( child->content );
533 str = bstr_from_xmlChar( This->node->content );
537 FIXME("Unhandled node type %d\n", This->node->type);
540 TRACE("%p %s\n", This, debugstr_w(str) );
546 static HRESULT WINAPI xmlnode_put_text(
554 static HRESULT WINAPI xmlnode_get_specified(
556 VARIANT_BOOL* isSpecified)
562 static HRESULT WINAPI xmlnode_get_definition(
564 IXMLDOMNode** definitionNode)
570 static HRESULT WINAPI xmlnode_get_nodeTypedValue(
574 FIXME("ignoring data type\n");
575 return xmlnode_get_nodeValue(iface, typedValue);
578 static HRESULT WINAPI xmlnode_put_nodeTypedValue(
586 static HRESULT WINAPI xmlnode_get_dataType(
588 VARIANT* dataTypeName)
594 static HRESULT WINAPI xmlnode_put_dataType(
602 static HRESULT WINAPI xmlnode_get_xml(
610 static HRESULT WINAPI xmlnode_transformNode(
612 IXMLDOMNode* styleSheet,
619 static HRESULT WINAPI xmlnode_selectNodes(
622 IXMLDOMNodeList** resultList)
624 xmlnode *This = impl_from_IXMLDOMNode( iface );
628 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultList );
630 str = xmlChar_from_wchar( queryString );
634 if( !This->node->children )
637 *resultList = create_filtered_nodelist( This->node->children, str, FALSE );
638 HeapFree( GetProcessHeap(), 0, str );
642 static HRESULT WINAPI xmlnode_selectSingleNode(
645 IXMLDOMNode** resultNode)
647 xmlnode *This = impl_from_IXMLDOMNode( iface );
648 IXMLDOMNodeList *list;
651 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultNode );
653 r = IXMLDOMNode_selectNodes(iface, queryString, &list);
656 r = IXMLDOMNodeList_nextNode(list, resultNode);
657 IXMLDOMNodeList_Release(list);
662 static HRESULT WINAPI xmlnode_get_parsed(
664 VARIANT_BOOL* isParsed)
670 static HRESULT WINAPI xmlnode_get_namespaceURI(
678 static HRESULT WINAPI xmlnode_get_prefix(
686 static HRESULT WINAPI xmlnode_get_baseName(
690 xmlnode *This = impl_from_IXMLDOMNode( iface );
694 TRACE("%p %p\n", This, nameString );
699 switch ( This->node->type )
701 case XML_ELEMENT_NODE:
702 case XML_ATTRIBUTE_NODE:
703 str = bstr_from_xmlChar( This->node->name );
709 ERR("Unhandled type %d\n", This->node->type );
713 TRACE("returning %08lx str = %s\n", r, debugstr_w( str ) );
719 static HRESULT WINAPI xmlnode_transformNodeToObject(
721 IXMLDOMNode* stylesheet,
722 VARIANT outputObject)
728 static const struct IXMLDOMNodeVtbl xmlnode_vtbl =
730 xmlnode_QueryInterface,
733 xmlnode_GetTypeInfoCount,
735 xmlnode_GetIDsOfNames,
737 xmlnode_get_nodeName,
738 xmlnode_get_nodeValue,
739 xmlnode_put_nodeValue,
740 xmlnode_get_nodeType,
741 xmlnode_get_parentNode,
742 xmlnode_get_childNodes,
743 xmlnode_get_firstChild,
744 xmlnode_get_lastChild,
745 xmlnode_get_previousSibling,
746 xmlnode_get_nextSibling,
747 xmlnode_get_attributes,
748 xmlnode_insertBefore,
749 xmlnode_replaceChild,
752 xmlnode_hasChildNodes,
753 xmlnode_get_ownerDocument,
755 xmlnode_get_nodeTypeString,
758 xmlnode_get_specified,
759 xmlnode_get_definition,
760 xmlnode_get_nodeTypedValue,
761 xmlnode_put_nodeTypedValue,
762 xmlnode_get_dataType,
763 xmlnode_put_dataType,
765 xmlnode_transformNode,
767 xmlnode_selectSingleNode,
769 xmlnode_get_namespaceURI,
771 xmlnode_get_baseName,
772 xmlnode_transformNodeToObject,
775 static HRESULT WINAPI Internal_QueryInterface(
780 xmlnode *This = impl_from_InternalUnknown( iface );
782 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
785 if ( IsEqualGUID( riid, &IID_IUnknown ))
787 else if ( IsEqualGUID( riid, &IID_IDispatch ) ||
788 IsEqualGUID( riid, &IID_IXMLDOMNode ) )
789 *ppvObject = &This->lpVtbl;
792 FIXME("interface %s not implemented\n", debugstr_guid(riid));
794 return E_NOINTERFACE;
797 IUnknown_AddRef( (IUnknown*)*ppvObject );
802 static ULONG WINAPI Internal_AddRef(
805 xmlnode *This = impl_from_InternalUnknown( iface );
806 return InterlockedIncrement( &This->ref );
809 static ULONG WINAPI Internal_Release(
812 xmlnode *This = impl_from_InternalUnknown( iface );
815 ref = InterlockedDecrement( &This->ref );
819 xmldoc_release( This->node->doc );
820 HeapFree( GetProcessHeap(), 0, This );
826 static const struct IUnknownVtbl internal_unk_vtbl =
828 Internal_QueryInterface,
833 IUnknown *create_basic_node( xmlNodePtr node, IUnknown *pUnkOuter )
837 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
842 xmldoc_add_ref( node->doc );
844 This->lpVtbl = &xmlnode_vtbl;
845 This->lpInternalUnkVtbl = &internal_unk_vtbl;
848 This->pUnkOuter = pUnkOuter; /* Don't take a ref on outer Unknown */
850 This->pUnkOuter = (IUnknown *)&This->lpInternalUnkVtbl;
855 return (IUnknown*)&This->lpInternalUnkVtbl;
858 IXMLDOMNode *create_node( xmlNodePtr node )
867 TRACE("type %d\n", node->type);
870 case XML_ELEMENT_NODE:
871 pUnk = create_element( node );
873 case XML_ATTRIBUTE_NODE:
874 pUnk = create_attribute( node );
877 pUnk = create_text( node );
879 case XML_COMMENT_NODE:
880 pUnk = create_comment( node );
882 case XML_DOCUMENT_NODE:
883 ERR("shouldn't be here!\n");
886 FIXME("only creating basic node for type %d\n", node->type);
887 pUnk = create_basic_node( node, NULL );
890 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
891 IUnknown_Release(pUnk);
892 if(FAILED(hr)) return NULL;