2 * XML Element implementation
4 * Copyright 2007 James Hawkins
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
27 # include <libxml/parser.h>
28 # include <libxml/xmlerror.h>
38 #include "wine/debug.h"
40 #include "msxml_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msxml
);
46 static HRESULT
XMLElementCollection_create( xmlNodePtr node
, LPVOID
*ppObj
);
48 /**********************************************************************
51 typedef struct _xmlelem
53 IXMLElement IXMLElement_iface
;
59 static inline xmlelem
*impl_from_IXMLElement(IXMLElement
*iface
)
61 return CONTAINING_RECORD(iface
, xmlelem
, IXMLElement_iface
);
64 static HRESULT WINAPI
xmlelem_QueryInterface(IXMLElement
*iface
, REFIID riid
, void** ppvObject
)
66 xmlelem
*This
= impl_from_IXMLElement(iface
);
68 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppvObject
);
70 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
71 IsEqualGUID(riid
, &IID_IDispatch
) ||
72 IsEqualGUID(riid
, &IID_IXMLElement
))
78 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
83 IXMLElement_AddRef(iface
);
88 static ULONG WINAPI
xmlelem_AddRef(IXMLElement
*iface
)
90 xmlelem
*This
= impl_from_IXMLElement(iface
);
92 return InterlockedIncrement(&This
->ref
);
95 static ULONG WINAPI
xmlelem_Release(IXMLElement
*iface
)
97 xmlelem
*This
= impl_from_IXMLElement(iface
);
102 ref
= InterlockedDecrement(&This
->ref
);
105 if (This
->own
) xmlFreeNode(This
->node
);
112 static HRESULT WINAPI
xmlelem_GetTypeInfoCount(IXMLElement
*iface
, UINT
* pctinfo
)
114 xmlelem
*This
= impl_from_IXMLElement(iface
);
116 TRACE("(%p)->(%p)\n", This
, pctinfo
);
123 static HRESULT WINAPI
xmlelem_GetTypeInfo(IXMLElement
*iface
, UINT iTInfo
,
124 LCID lcid
, ITypeInfo
** ppTInfo
)
126 xmlelem
*This
= impl_from_IXMLElement(iface
);
129 TRACE("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
131 hr
= get_typeinfo(IXMLElement_tid
, ppTInfo
);
136 static HRESULT WINAPI
xmlelem_GetIDsOfNames(IXMLElement
*iface
, REFIID riid
,
137 LPOLESTR
* rgszNames
, UINT cNames
,
138 LCID lcid
, DISPID
* rgDispId
)
140 xmlelem
*This
= impl_from_IXMLElement(iface
);
144 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
147 if(!rgszNames
|| cNames
== 0 || !rgDispId
)
150 hr
= get_typeinfo(IXMLElement_tid
, &typeinfo
);
153 hr
= ITypeInfo_GetIDsOfNames(typeinfo
, rgszNames
, cNames
, rgDispId
);
154 ITypeInfo_Release(typeinfo
);
160 static HRESULT WINAPI
xmlelem_Invoke(IXMLElement
*iface
, DISPID dispIdMember
,
161 REFIID riid
, LCID lcid
, WORD wFlags
,
162 DISPPARAMS
* pDispParams
, VARIANT
* pVarResult
,
163 EXCEPINFO
* pExcepInfo
, UINT
* puArgErr
)
165 xmlelem
*This
= impl_from_IXMLElement(iface
);
169 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
170 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
172 hr
= get_typeinfo(IXMLElement_tid
, &typeinfo
);
175 hr
= ITypeInfo_Invoke(typeinfo
, &This
->IXMLElement_iface
, dispIdMember
, wFlags
, pDispParams
,
176 pVarResult
, pExcepInfo
, puArgErr
);
177 ITypeInfo_Release(typeinfo
);
183 static HRESULT WINAPI
xmlelem_get_tagName(IXMLElement
*iface
, BSTR
*p
)
185 xmlelem
*This
= impl_from_IXMLElement(iface
);
187 TRACE("(%p)->(%p)\n", This
, p
);
192 if (*This
->node
->name
) {
193 *p
= bstr_from_xmlChar(This
->node
->name
);
194 CharUpperBuffW(*p
, SysStringLen(*p
));
199 TRACE("returning %s\n", debugstr_w(*p
));
204 static HRESULT WINAPI
xmlelem_put_tagName(IXMLElement
*iface
, BSTR p
)
206 xmlelem
*This
= impl_from_IXMLElement(iface
);
208 FIXME("(%p)->(%s): stub\n", This
, debugstr_w(p
));
216 static HRESULT WINAPI
xmlelem_get_parent(IXMLElement
*iface
, IXMLElement
**parent
)
218 xmlelem
*This
= impl_from_IXMLElement(iface
);
220 TRACE("(%p)->(%p)\n", This
, parent
);
227 if (!This
->node
->parent
)
230 return XMLElement_create(This
->node
->parent
, (LPVOID
*)parent
, FALSE
);
233 static HRESULT WINAPI
xmlelem_setAttribute(IXMLElement
*iface
, BSTR strPropertyName
,
234 VARIANT PropertyValue
)
236 xmlelem
*This
= impl_from_IXMLElement(iface
);
237 xmlChar
*name
, *value
;
240 TRACE("(%p)->(%s %s)\n", This
, debugstr_w(strPropertyName
), debugstr_variant(&PropertyValue
));
242 if (!strPropertyName
|| V_VT(&PropertyValue
) != VT_BSTR
)
245 name
= xmlchar_from_wchar(strPropertyName
);
246 value
= xmlchar_from_wchar(V_BSTR(&PropertyValue
));
247 attr
= xmlSetProp(This
->node
, name
, value
);
251 return (attr
) ? S_OK
: S_FALSE
;
254 static HRESULT WINAPI
xmlelem_getAttribute(IXMLElement
*iface
, BSTR name
,
257 static const WCHAR xmllangW
[] = { 'x','m','l',':','l','a','n','g',0 };
258 xmlelem
*This
= impl_from_IXMLElement(iface
);
261 TRACE("(%p)->(%s, %p)\n", This
, debugstr_w(name
), value
);
267 V_BSTR(value
) = NULL
;
272 /* case for xml:lang attribute */
273 if (!lstrcmpiW(name
, xmllangW
))
276 ns
= xmlSearchNs(This
->node
->doc
, This
->node
, (xmlChar
*)"xml");
277 val
= xmlGetNsProp(This
->node
, (xmlChar
*)"lang", ns
->href
);
284 xml_name
= xmlchar_from_wchar(name
);
285 attr
= This
->node
->properties
;
290 attr_name
= bstr_from_xmlChar(attr
->name
);
291 if (!lstrcmpiW(name
, attr_name
))
293 val
= xmlNodeListGetString(attr
->doc
, attr
->children
, 1);
294 SysFreeString(attr_name
);
299 SysFreeString(attr_name
);
307 V_VT(value
) = VT_BSTR
;
308 V_BSTR(value
) = bstr_from_xmlChar(val
);
312 TRACE("returning %s\n", debugstr_w(V_BSTR(value
)));
313 return (val
) ? S_OK
: S_FALSE
;
316 static HRESULT WINAPI
xmlelem_removeAttribute(IXMLElement
*iface
, BSTR strPropertyName
)
318 xmlelem
*This
= impl_from_IXMLElement(iface
);
322 HRESULT hr
= S_FALSE
;
324 TRACE("(%p)->(%s)\n", This
, debugstr_w(strPropertyName
));
326 if (!strPropertyName
)
329 name
= xmlchar_from_wchar(strPropertyName
);
330 attr
= xmlHasProp(This
->node
, name
);
334 res
= xmlRemoveProp(attr
);
344 static HRESULT WINAPI
xmlelem_get_children(IXMLElement
*iface
, IXMLElementCollection
**p
)
346 xmlelem
*This
= impl_from_IXMLElement(iface
);
348 TRACE("(%p)->(%p)\n", This
, p
);
353 return XMLElementCollection_create(This
->node
, (LPVOID
*)p
);
356 static LONG
type_libxml_to_msxml(xmlElementType type
)
360 case XML_ELEMENT_NODE
:
361 return XMLELEMTYPE_ELEMENT
;
363 return XMLELEMTYPE_TEXT
;
364 case XML_COMMENT_NODE
:
365 return XMLELEMTYPE_COMMENT
;
366 case XML_DOCUMENT_NODE
:
367 return XMLELEMTYPE_DOCUMENT
;
369 return XMLELEMTYPE_DTD
;
371 return XMLELEMTYPE_PI
;
376 return XMLELEMTYPE_OTHER
;
379 static HRESULT WINAPI
xmlelem_get_type(IXMLElement
*iface
, LONG
*p
)
381 xmlelem
*This
= impl_from_IXMLElement(iface
);
383 TRACE("(%p)->(%p)\n", This
, p
);
388 *p
= type_libxml_to_msxml(This
->node
->type
);
389 TRACE("returning %d\n", *p
);
393 static HRESULT WINAPI
xmlelem_get_text(IXMLElement
*iface
, BSTR
*p
)
395 xmlelem
*This
= impl_from_IXMLElement(iface
);
398 TRACE("(%p)->(%p)\n", This
, p
);
403 content
= xmlNodeGetContent(This
->node
);
404 *p
= bstr_from_xmlChar(content
);
405 TRACE("returning %s\n", debugstr_w(*p
));
411 static HRESULT WINAPI
xmlelem_put_text(IXMLElement
*iface
, BSTR p
)
413 xmlelem
*This
= impl_from_IXMLElement(iface
);
416 TRACE("(%p)->(%s)\n", This
, debugstr_w(p
));
418 /* FIXME: test which types can be used */
419 if (This
->node
->type
== XML_ELEMENT_NODE
)
422 content
= xmlchar_from_wchar(p
);
423 xmlNodeSetContent(This
->node
, content
);
430 static HRESULT WINAPI
xmlelem_addChild(IXMLElement
*iface
, IXMLElement
*pChildElem
,
431 LONG lIndex
, LONG lreserved
)
433 xmlelem
*This
= impl_from_IXMLElement(iface
);
434 xmlelem
*childElem
= impl_from_IXMLElement(pChildElem
);
437 TRACE("(%p)->(%p %d %d)\n", This
, pChildElem
, lIndex
, lreserved
);
440 child
= xmlAddChild(This
->node
, childElem
->node
);
442 child
= xmlAddNextSibling(This
->node
, childElem
->node
->last
);
444 /* parent is responsible for child data */
445 if (child
) childElem
->own
= FALSE
;
447 return (child
) ? S_OK
: S_FALSE
;
450 static HRESULT WINAPI
xmlelem_removeChild(IXMLElement
*iface
, IXMLElement
*pChildElem
)
452 xmlelem
*This
= impl_from_IXMLElement(iface
);
453 xmlelem
*childElem
= impl_from_IXMLElement(pChildElem
);
455 TRACE("(%p)->(%p)\n", This
, childElem
);
460 /* only supported for This is childElem parent case */
461 if (This
->node
!= childElem
->node
->parent
)
464 xmlUnlinkNode(childElem
->node
);
465 /* standalone element now */
466 childElem
->own
= TRUE
;
471 static const struct IXMLElementVtbl xmlelem_vtbl
=
473 xmlelem_QueryInterface
,
476 xmlelem_GetTypeInfoCount
,
478 xmlelem_GetIDsOfNames
,
483 xmlelem_setAttribute
,
484 xmlelem_getAttribute
,
485 xmlelem_removeAttribute
,
486 xmlelem_get_children
,
494 HRESULT
XMLElement_create(xmlNodePtr node
, LPVOID
*ppObj
, BOOL own
)
498 TRACE("(%p)\n", ppObj
);
505 elem
= heap_alloc(sizeof (*elem
));
507 return E_OUTOFMEMORY
;
509 elem
->IXMLElement_iface
.lpVtbl
= &xmlelem_vtbl
;
514 *ppObj
= &elem
->IXMLElement_iface
;
516 TRACE("returning iface %p\n", *ppObj
);
520 /************************************************************************
521 * IXMLElementCollection
523 typedef struct _xmlelem_collection
525 IXMLElementCollection IXMLElementCollection_iface
;
526 IEnumVARIANT IEnumVARIANT_iface
;
531 /* IEnumVARIANT members */
533 } xmlelem_collection
;
535 static inline LONG
xmlelem_collection_updatelength(xmlelem_collection
*collection
)
537 xmlNodePtr ptr
= collection
->node
->children
;
539 collection
->length
= 0;
542 collection
->length
++;
545 return collection
->length
;
548 static inline xmlelem_collection
*impl_from_IXMLElementCollection(IXMLElementCollection
*iface
)
550 return CONTAINING_RECORD(iface
, xmlelem_collection
, IXMLElementCollection_iface
);
553 static inline xmlelem_collection
*impl_from_IEnumVARIANT(IEnumVARIANT
*iface
)
555 return CONTAINING_RECORD(iface
, xmlelem_collection
, IEnumVARIANT_iface
);
558 static HRESULT WINAPI
xmlelem_collection_QueryInterface(IXMLElementCollection
*iface
, REFIID riid
, void** ppvObject
)
560 xmlelem_collection
*This
= impl_from_IXMLElementCollection(iface
);
562 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppvObject
);
564 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
565 IsEqualGUID(riid
, &IID_IXMLElementCollection
))
569 else if (IsEqualGUID(riid
, &IID_IEnumVARIANT
))
571 *ppvObject
= &This
->IEnumVARIANT_iface
;
575 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
577 return E_NOINTERFACE
;
580 IXMLElementCollection_AddRef(iface
);
585 static ULONG WINAPI
xmlelem_collection_AddRef(IXMLElementCollection
*iface
)
587 xmlelem_collection
*This
= impl_from_IXMLElementCollection(iface
);
588 TRACE("(%p)\n", This
);
589 return InterlockedIncrement(&This
->ref
);
592 static ULONG WINAPI
xmlelem_collection_Release(IXMLElementCollection
*iface
)
594 xmlelem_collection
*This
= impl_from_IXMLElementCollection(iface
);
597 TRACE("(%p)\n", This
);
599 ref
= InterlockedDecrement(&This
->ref
);
608 static HRESULT WINAPI
xmlelem_collection_GetTypeInfoCount(IXMLElementCollection
*iface
, UINT
* pctinfo
)
614 static HRESULT WINAPI
xmlelem_collection_GetTypeInfo(IXMLElementCollection
*iface
, UINT iTInfo
,
615 LCID lcid
, ITypeInfo
** ppTInfo
)
621 static HRESULT WINAPI
xmlelem_collection_GetIDsOfNames(IXMLElementCollection
*iface
, REFIID riid
,
622 LPOLESTR
* rgszNames
, UINT cNames
,
623 LCID lcid
, DISPID
* rgDispId
)
629 static HRESULT WINAPI
xmlelem_collection_Invoke(IXMLElementCollection
*iface
, DISPID dispIdMember
,
630 REFIID riid
, LCID lcid
, WORD wFlags
,
631 DISPPARAMS
* pDispParams
, VARIANT
* pVarResult
,
632 EXCEPINFO
* pExcepInfo
, UINT
* puArgErr
)
638 static HRESULT WINAPI
xmlelem_collection_put_length(IXMLElementCollection
*iface
, LONG v
)
640 xmlelem_collection
*This
= impl_from_IXMLElementCollection(iface
);
641 TRACE("(%p)->(%d)\n", This
, v
);
645 static HRESULT WINAPI
xmlelem_collection_get_length(IXMLElementCollection
*iface
, LONG
*p
)
647 xmlelem_collection
*This
= impl_from_IXMLElementCollection(iface
);
649 TRACE("(%p)->(%p)\n", This
, p
);
654 *p
= xmlelem_collection_updatelength(This
);
658 static HRESULT WINAPI
xmlelem_collection_get__newEnum(IXMLElementCollection
*iface
, IUnknown
**ppUnk
)
660 xmlelem_collection
*This
= impl_from_IXMLElementCollection(iface
);
662 TRACE("(%p)->(%p)\n", This
, ppUnk
);
667 IXMLElementCollection_AddRef(iface
);
668 *ppUnk
= (IUnknown
*)&This
->IEnumVARIANT_iface
;
672 static HRESULT WINAPI
xmlelem_collection_item(IXMLElementCollection
*iface
, VARIANT var1
,
673 VARIANT var2
, IDispatch
**ppDisp
)
675 xmlelem_collection
*This
= impl_from_IXMLElementCollection(iface
);
676 xmlNodePtr ptr
= This
->node
->children
;
679 TRACE("(%p)->(%s %s %p)\n", This
, debugstr_variant(&var1
), debugstr_variant(&var2
), ppDisp
);
690 xmlelem_collection_updatelength(This
);
691 if (index
>= This
->length
)
694 for (i
= 0; i
< index
; i
++)
697 return XMLElement_create(ptr
, (LPVOID
*)ppDisp
, FALSE
);
700 static const struct IXMLElementCollectionVtbl xmlelem_collection_vtbl
=
702 xmlelem_collection_QueryInterface
,
703 xmlelem_collection_AddRef
,
704 xmlelem_collection_Release
,
705 xmlelem_collection_GetTypeInfoCount
,
706 xmlelem_collection_GetTypeInfo
,
707 xmlelem_collection_GetIDsOfNames
,
708 xmlelem_collection_Invoke
,
709 xmlelem_collection_put_length
,
710 xmlelem_collection_get_length
,
711 xmlelem_collection_get__newEnum
,
712 xmlelem_collection_item
715 /************************************************************************
716 * xmlelem_collection implementation of IEnumVARIANT.
718 static HRESULT WINAPI
xmlelem_collection_IEnumVARIANT_QueryInterface(
719 IEnumVARIANT
*iface
, REFIID riid
, LPVOID
*ppvObj
)
721 xmlelem_collection
*this = impl_from_IEnumVARIANT(iface
);
723 TRACE("(%p)->(%s %p)\n", this, debugstr_guid(riid
), ppvObj
);
725 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
726 IsEqualGUID(riid
, &IID_IEnumVARIANT
))
729 IEnumVARIANT_AddRef(iface
);
733 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
735 return E_NOINTERFACE
;
738 static ULONG WINAPI
xmlelem_collection_IEnumVARIANT_AddRef(
741 xmlelem_collection
*this = impl_from_IEnumVARIANT(iface
);
742 return IXMLElementCollection_AddRef(&this->IXMLElementCollection_iface
);
745 static ULONG WINAPI
xmlelem_collection_IEnumVARIANT_Release(
748 xmlelem_collection
*this = impl_from_IEnumVARIANT(iface
);
749 return IXMLElementCollection_Release(&this->IXMLElementCollection_iface
);
752 static HRESULT WINAPI
xmlelem_collection_IEnumVARIANT_Next(
753 IEnumVARIANT
*iface
, ULONG celt
, VARIANT
*rgVar
, ULONG
*fetched
)
755 xmlelem_collection
*This
= impl_from_IEnumVARIANT(iface
);
756 xmlNodePtr ptr
= This
->current
;
758 TRACE("(%p)->(%d %p %p)\n", This
, celt
, rgVar
, fetched
);
763 /* FIXME: handle celt */
768 This
->current
= This
->current
->next
;
771 V_VT(rgVar
) = VT_EMPTY
;
772 if (fetched
) *fetched
= 0;
776 V_VT(rgVar
) = VT_DISPATCH
;
777 return XMLElement_create(ptr
, (LPVOID
*)&V_DISPATCH(rgVar
), FALSE
);
780 static HRESULT WINAPI
xmlelem_collection_IEnumVARIANT_Skip(
781 IEnumVARIANT
*iface
, ULONG celt
)
783 xmlelem_collection
*This
= impl_from_IEnumVARIANT(iface
);
784 FIXME("(%p)->(%d): stub\n", This
, celt
);
788 static HRESULT WINAPI
xmlelem_collection_IEnumVARIANT_Reset(
791 xmlelem_collection
*This
= impl_from_IEnumVARIANT(iface
);
792 TRACE("(%p)\n", This
);
793 This
->current
= This
->node
->children
;
797 static HRESULT WINAPI
xmlelem_collection_IEnumVARIANT_Clone(
798 IEnumVARIANT
*iface
, IEnumVARIANT
**ppEnum
)
800 xmlelem_collection
*This
= impl_from_IEnumVARIANT(iface
);
801 FIXME("(%p)->(%p): stub\n", This
, ppEnum
);
805 static const struct IEnumVARIANTVtbl xmlelem_collection_IEnumVARIANTvtbl
=
807 xmlelem_collection_IEnumVARIANT_QueryInterface
,
808 xmlelem_collection_IEnumVARIANT_AddRef
,
809 xmlelem_collection_IEnumVARIANT_Release
,
810 xmlelem_collection_IEnumVARIANT_Next
,
811 xmlelem_collection_IEnumVARIANT_Skip
,
812 xmlelem_collection_IEnumVARIANT_Reset
,
813 xmlelem_collection_IEnumVARIANT_Clone
816 static HRESULT
XMLElementCollection_create(xmlNodePtr node
, LPVOID
*ppObj
)
818 xmlelem_collection
*collection
;
820 TRACE("(%p)\n", ppObj
);
827 collection
= heap_alloc(sizeof (*collection
));
829 return E_OUTOFMEMORY
;
831 collection
->IXMLElementCollection_iface
.lpVtbl
= &xmlelem_collection_vtbl
;
832 collection
->IEnumVARIANT_iface
.lpVtbl
= &xmlelem_collection_IEnumVARIANTvtbl
;
834 collection
->length
= 0;
835 collection
->node
= node
;
836 collection
->current
= node
->children
;
837 xmlelem_collection_updatelength(collection
);
839 *ppObj
= &collection
->IXMLElementCollection_iface
;
841 TRACE("returning iface %p\n", *ppObj
);