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
*)"*" );
320 case XML_ATTRIBUTE_NODE
:
321 *childList
= create_filtered_nodelist( This
->node
->children
, (const xmlChar
*)"node()" );
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
);
522 child
= This
->node
->children
;
523 if ( child
&& child
->type
== XML_TEXT_NODE
)
524 str
= bstr_from_xmlChar( child
->content
);
526 TRACE("%p %s\n", This
, debugstr_w(str
) );
532 static HRESULT WINAPI
xmlnode_put_text(
540 static HRESULT WINAPI
xmlnode_get_specified(
542 VARIANT_BOOL
* isSpecified
)
548 static HRESULT WINAPI
xmlnode_get_definition(
550 IXMLDOMNode
** definitionNode
)
556 static HRESULT WINAPI
xmlnode_get_nodeTypedValue(
564 static HRESULT WINAPI
xmlnode_put_nodeTypedValue(
572 static HRESULT WINAPI
xmlnode_get_dataType(
574 VARIANT
* dataTypeName
)
580 static HRESULT WINAPI
xmlnode_put_dataType(
588 static HRESULT WINAPI
xmlnode_get_xml(
596 static HRESULT WINAPI
xmlnode_transformNode(
598 IXMLDOMNode
* styleSheet
,
605 static HRESULT WINAPI
xmlnode_selectNodes(
608 IXMLDOMNodeList
** resultList
)
610 xmlnode
*This
= impl_from_IXMLDOMNode( iface
);
614 TRACE("%p %s %p\n", This
, debugstr_w(queryString
), resultList
);
616 str
= xmlChar_from_wchar( queryString
);
620 if( !This
->node
->children
)
623 *resultList
= create_filtered_nodelist( This
->node
->children
, str
);
624 HeapFree( GetProcessHeap(), 0, str
);
628 static HRESULT WINAPI
xmlnode_selectSingleNode(
631 IXMLDOMNode
** resultNode
)
633 xmlnode
*This
= impl_from_IXMLDOMNode( iface
);
634 IXMLDOMNodeList
*list
;
637 TRACE("%p %s %p\n", This
, debugstr_w(queryString
), resultNode
);
639 r
= IXMLDOMNode_selectNodes(iface
, queryString
, &list
);
642 r
= IXMLDOMNodeList_nextNode(list
, resultNode
);
643 IXMLDOMNodeList_Release(list
);
648 static HRESULT WINAPI
xmlnode_get_parsed(
650 VARIANT_BOOL
* isParsed
)
656 static HRESULT WINAPI
xmlnode_get_namespaceURI(
664 static HRESULT WINAPI
xmlnode_get_prefix(
672 static HRESULT WINAPI
xmlnode_get_baseName(
676 xmlnode
*This
= impl_from_IXMLDOMNode( iface
);
680 TRACE("%p %p\n", This
, nameString
);
685 switch ( This
->node
->type
)
687 case XML_ELEMENT_NODE
:
688 case XML_ATTRIBUTE_NODE
:
689 str
= bstr_from_xmlChar( This
->node
->name
);
695 ERR("Unhandled type %d\n", This
->node
->type
);
699 TRACE("returning %08lx str = %s\n", r
, debugstr_w( str
) );
705 static HRESULT WINAPI
xmlnode_transformNodeToObject(
707 IXMLDOMNode
* stylesheet
,
708 VARIANT outputObject
)
714 static const struct IXMLDOMNodeVtbl xmlnode_vtbl
=
716 xmlnode_QueryInterface
,
719 xmlnode_GetTypeInfoCount
,
721 xmlnode_GetIDsOfNames
,
723 xmlnode_get_nodeName
,
724 xmlnode_get_nodeValue
,
725 xmlnode_put_nodeValue
,
726 xmlnode_get_nodeType
,
727 xmlnode_get_parentNode
,
728 xmlnode_get_childNodes
,
729 xmlnode_get_firstChild
,
730 xmlnode_get_lastChild
,
731 xmlnode_get_previousSibling
,
732 xmlnode_get_nextSibling
,
733 xmlnode_get_attributes
,
734 xmlnode_insertBefore
,
735 xmlnode_replaceChild
,
738 xmlnode_hasChildNodes
,
739 xmlnode_get_ownerDocument
,
741 xmlnode_get_nodeTypeString
,
744 xmlnode_get_specified
,
745 xmlnode_get_definition
,
746 xmlnode_get_nodeTypedValue
,
747 xmlnode_put_nodeTypedValue
,
748 xmlnode_get_dataType
,
749 xmlnode_put_dataType
,
751 xmlnode_transformNode
,
753 xmlnode_selectSingleNode
,
755 xmlnode_get_namespaceURI
,
757 xmlnode_get_baseName
,
758 xmlnode_transformNodeToObject
,
761 static HRESULT WINAPI
Internal_QueryInterface(
766 xmlnode
*This
= impl_from_InternalUnknown( iface
);
768 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
771 if ( IsEqualGUID( riid
, &IID_IUnknown
))
773 else if ( IsEqualGUID( riid
, &IID_IDispatch
) ||
774 IsEqualGUID( riid
, &IID_IXMLDOMNode
) )
775 *ppvObject
= &This
->lpVtbl
;
779 return E_NOINTERFACE
;
782 IUnknown_AddRef( (IUnknown
*)*ppvObject
);
787 static ULONG WINAPI
Internal_AddRef(
790 xmlnode
*This
= impl_from_InternalUnknown( iface
);
791 return InterlockedIncrement( &This
->ref
);
794 static ULONG WINAPI
Internal_Release(
797 xmlnode
*This
= impl_from_InternalUnknown( iface
);
800 ref
= InterlockedDecrement( &This
->ref
);
804 xmldoc_release( This
->node
->doc
);
805 HeapFree( GetProcessHeap(), 0, This
);
811 static const struct IUnknownVtbl internal_unk_vtbl
=
813 Internal_QueryInterface
,
818 IUnknown
*create_basic_node( xmlNodePtr node
, IUnknown
*pUnkOuter
)
822 This
= HeapAlloc( GetProcessHeap(), 0, sizeof *This
);
827 xmldoc_add_ref( node
->doc
);
829 This
->lpVtbl
= &xmlnode_vtbl
;
830 This
->lpInternalUnkVtbl
= &internal_unk_vtbl
;
833 This
->pUnkOuter
= pUnkOuter
; /* Don't take a ref on outer Unknown */
835 This
->pUnkOuter
= (IUnknown
*)&This
->lpInternalUnkVtbl
;
840 return (IUnknown
*)&This
->lpInternalUnkVtbl
;
843 IXMLDOMNode
*create_node( xmlNodePtr node
)
852 TRACE("type %d\n", node
->type
);
855 case XML_ATTRIBUTE_NODE
:
856 pUnk
= create_attribute( node
);
858 case XML_ELEMENT_NODE
:
859 pUnk
= create_element( node
);
861 case XML_DOCUMENT_NODE
:
862 ERR("shouldn't be here!\n");
865 FIXME("only creating basic node for type %d\n", node
->type
);
866 pUnk
= create_basic_node( node
, NULL
);
869 hr
= IUnknown_QueryInterface(pUnk
, &IID_IXMLDOMNode
, (LPVOID
*)&ret
);
870 IUnknown_Release(pUnk
);
871 if(FAILED(hr
)) return NULL
;