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( IUnknown *pUnkOuter, 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 *p = bstr_from_xmlChar(This->node->name);
193 CharUpperBuffW(*p, SysStringLen(*p));
195 TRACE("returning %s\n", debugstr_w(*p));
200 static HRESULT WINAPI xmlelem_put_tagName(IXMLElement *iface, BSTR p)
202 xmlelem *This = impl_from_IXMLElement(iface);
204 FIXME("(%p)->(%s): stub\n", This, debugstr_w(p));
212 static HRESULT WINAPI xmlelem_get_parent(IXMLElement *iface, IXMLElement **parent)
214 xmlelem *This = impl_from_IXMLElement(iface);
216 TRACE("(%p)->(%p)\n", This, parent);
223 if (!This->node->parent)
226 return XMLElement_create((IUnknown *)iface, This->node->parent, (LPVOID *)parent, FALSE);
229 static HRESULT WINAPI xmlelem_setAttribute(IXMLElement *iface, BSTR strPropertyName,
230 VARIANT PropertyValue)
232 xmlelem *This = impl_from_IXMLElement(iface);
233 xmlChar *name, *value;
236 TRACE("(%p)->(%s %s)\n", This, debugstr_w(strPropertyName), debugstr_variant(&PropertyValue));
238 if (!strPropertyName || V_VT(&PropertyValue) != VT_BSTR)
241 name = xmlchar_from_wchar(strPropertyName);
242 value = xmlchar_from_wchar(V_BSTR(&PropertyValue));
243 attr = xmlSetProp(This->node, name, value);
247 return (attr) ? S_OK : S_FALSE;
250 static HRESULT WINAPI xmlelem_getAttribute(IXMLElement *iface, BSTR name,
253 static const WCHAR xmllangW[] = { 'x','m','l',':','l','a','n','g',0 };
254 xmlelem *This = impl_from_IXMLElement(iface);
257 TRACE("(%p)->(%s, %p)\n", This, debugstr_w(name), value);
263 V_BSTR(value) = NULL;
268 /* case for xml:lang attribute */
269 if (!lstrcmpiW(name, xmllangW))
272 ns = xmlSearchNs(This->node->doc, This->node, (xmlChar*)"xml");
273 val = xmlGetNsProp(This->node, (xmlChar*)"lang", ns->href);
280 xml_name = xmlchar_from_wchar(name);
281 attr = This->node->properties;
286 attr_name = bstr_from_xmlChar(attr->name);
287 if (!lstrcmpiW(name, attr_name))
289 val = xmlNodeListGetString(attr->doc, attr->children, 1);
290 SysFreeString(attr_name);
295 SysFreeString(attr_name);
303 V_VT(value) = VT_BSTR;
304 V_BSTR(value) = bstr_from_xmlChar(val);
308 TRACE("returning %s\n", debugstr_w(V_BSTR(value)));
309 return (val) ? S_OK : S_FALSE;
312 static HRESULT WINAPI xmlelem_removeAttribute(IXMLElement *iface, BSTR strPropertyName)
314 xmlelem *This = impl_from_IXMLElement(iface);
318 HRESULT hr = S_FALSE;
320 TRACE("(%p)->(%s)\n", This, debugstr_w(strPropertyName));
322 if (!strPropertyName)
325 name = xmlchar_from_wchar(strPropertyName);
326 attr = xmlHasProp(This->node, name);
330 res = xmlRemoveProp(attr);
340 static HRESULT WINAPI xmlelem_get_children(IXMLElement *iface, IXMLElementCollection **p)
342 xmlelem *This = impl_from_IXMLElement(iface);
344 TRACE("(%p)->(%p)\n", This, p);
349 return XMLElementCollection_create((IUnknown *)iface, This->node, (LPVOID *)p);
352 static LONG type_libxml_to_msxml(xmlElementType type)
356 case XML_ELEMENT_NODE:
357 return XMLELEMTYPE_ELEMENT;
359 return XMLELEMTYPE_TEXT;
360 case XML_COMMENT_NODE:
361 return XMLELEMTYPE_COMMENT;
362 case XML_DOCUMENT_NODE:
363 return XMLELEMTYPE_DOCUMENT;
365 return XMLELEMTYPE_DTD;
367 return XMLELEMTYPE_PI;
372 return XMLELEMTYPE_OTHER;
375 static HRESULT WINAPI xmlelem_get_type(IXMLElement *iface, LONG *p)
377 xmlelem *This = impl_from_IXMLElement(iface);
379 TRACE("(%p)->(%p)\n", This, p);
384 *p = type_libxml_to_msxml(This->node->type);
385 TRACE("returning %d\n", *p);
389 static HRESULT WINAPI xmlelem_get_text(IXMLElement *iface, BSTR *p)
391 xmlelem *This = impl_from_IXMLElement(iface);
394 TRACE("(%p)->(%p)\n", This, p);
399 content = xmlNodeGetContent(This->node);
400 *p = bstr_from_xmlChar(content);
401 TRACE("returning %s\n", debugstr_w(*p));
407 static HRESULT WINAPI xmlelem_put_text(IXMLElement *iface, BSTR p)
409 xmlelem *This = impl_from_IXMLElement(iface);
412 TRACE("(%p)->(%s)\n", This, debugstr_w(p));
414 /* FIXME: test which types can be used */
415 if (This->node->type == XML_ELEMENT_NODE)
418 content = xmlchar_from_wchar(p);
419 xmlNodeSetContent(This->node, content);
426 static HRESULT WINAPI xmlelem_addChild(IXMLElement *iface, IXMLElement *pChildElem,
427 LONG lIndex, LONG lreserved)
429 xmlelem *This = impl_from_IXMLElement(iface);
430 xmlelem *childElem = impl_from_IXMLElement(pChildElem);
433 TRACE("(%p)->(%p %d %d)\n", This, pChildElem, lIndex, lreserved);
436 child = xmlAddChild(This->node, childElem->node);
438 child = xmlAddNextSibling(This->node, childElem->node->last);
440 /* parent is responsible for child data */
441 if (child) childElem->own = FALSE;
443 return (child) ? S_OK : S_FALSE;
446 static HRESULT WINAPI xmlelem_removeChild(IXMLElement *iface, IXMLElement *pChildElem)
448 xmlelem *This = impl_from_IXMLElement(iface);
449 xmlelem *childElem = impl_from_IXMLElement(pChildElem);
451 TRACE("(%p)->(%p)\n", This, childElem);
456 /* only supported for This is childElem parent case */
457 if (This->node != childElem->node->parent)
460 xmlUnlinkNode(childElem->node);
461 /* standalone element now */
462 childElem->own = TRUE;
467 static const struct IXMLElementVtbl xmlelem_vtbl =
469 xmlelem_QueryInterface,
472 xmlelem_GetTypeInfoCount,
474 xmlelem_GetIDsOfNames,
479 xmlelem_setAttribute,
480 xmlelem_getAttribute,
481 xmlelem_removeAttribute,
482 xmlelem_get_children,
490 HRESULT XMLElement_create(IUnknown *pUnkOuter, xmlNodePtr node, LPVOID *ppObj, BOOL own)
494 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
501 elem = heap_alloc(sizeof (*elem));
503 return E_OUTOFMEMORY;
505 elem->IXMLElement_iface.lpVtbl = &xmlelem_vtbl;
510 *ppObj = &elem->IXMLElement_iface;
512 TRACE("returning iface %p\n", *ppObj);
516 /************************************************************************
517 * IXMLElementCollection
519 typedef struct _xmlelem_collection
521 IXMLElementCollection IXMLElementCollection_iface;
522 IEnumVARIANT IEnumVARIANT_iface;
527 /* IEnumVARIANT members */
529 } xmlelem_collection;
531 static inline LONG xmlelem_collection_updatelength(xmlelem_collection *collection)
533 xmlNodePtr ptr = collection->node->children;
535 collection->length = 0;
538 collection->length++;
541 return collection->length;
544 static inline xmlelem_collection *impl_from_IXMLElementCollection(IXMLElementCollection *iface)
546 return CONTAINING_RECORD(iface, xmlelem_collection, IXMLElementCollection_iface);
549 static inline xmlelem_collection *impl_from_IEnumVARIANT(IEnumVARIANT *iface)
551 return CONTAINING_RECORD(iface, xmlelem_collection, IEnumVARIANT_iface);
554 static HRESULT WINAPI xmlelem_collection_QueryInterface(IXMLElementCollection *iface, REFIID riid, void** ppvObject)
556 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
558 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
560 if (IsEqualGUID(riid, &IID_IUnknown) ||
561 IsEqualGUID(riid, &IID_IXMLElementCollection))
565 else if (IsEqualGUID(riid, &IID_IEnumVARIANT))
567 *ppvObject = &This->IEnumVARIANT_iface;
571 FIXME("interface %s not implemented\n", debugstr_guid(riid));
572 return E_NOINTERFACE;
575 IXMLElementCollection_AddRef(iface);
580 static ULONG WINAPI xmlelem_collection_AddRef(IXMLElementCollection *iface)
582 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
583 TRACE("(%p)\n", This);
584 return InterlockedIncrement(&This->ref);
587 static ULONG WINAPI xmlelem_collection_Release(IXMLElementCollection *iface)
589 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
592 TRACE("(%p)\n", This);
594 ref = InterlockedDecrement(&This->ref);
603 static HRESULT WINAPI xmlelem_collection_GetTypeInfoCount(IXMLElementCollection *iface, UINT* pctinfo)
609 static HRESULT WINAPI xmlelem_collection_GetTypeInfo(IXMLElementCollection *iface, UINT iTInfo,
610 LCID lcid, ITypeInfo** ppTInfo)
616 static HRESULT WINAPI xmlelem_collection_GetIDsOfNames(IXMLElementCollection *iface, REFIID riid,
617 LPOLESTR* rgszNames, UINT cNames,
618 LCID lcid, DISPID* rgDispId)
624 static HRESULT WINAPI xmlelem_collection_Invoke(IXMLElementCollection *iface, DISPID dispIdMember,
625 REFIID riid, LCID lcid, WORD wFlags,
626 DISPPARAMS* pDispParams, VARIANT* pVarResult,
627 EXCEPINFO* pExcepInfo, UINT* puArgErr)
633 static HRESULT WINAPI xmlelem_collection_put_length(IXMLElementCollection *iface, LONG v)
635 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
636 TRACE("(%p)->(%d)\n", This, v);
640 static HRESULT WINAPI xmlelem_collection_get_length(IXMLElementCollection *iface, LONG *p)
642 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
644 TRACE("(%p)->(%p)\n", This, p);
649 *p = xmlelem_collection_updatelength(This);
653 static HRESULT WINAPI xmlelem_collection_get__newEnum(IXMLElementCollection *iface, IUnknown **ppUnk)
655 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
657 TRACE("(%p)->(%p)\n", This, ppUnk);
662 *ppUnk = (IUnknown *)This;
663 IUnknown_AddRef(*ppUnk);
667 static HRESULT WINAPI xmlelem_collection_item(IXMLElementCollection *iface, VARIANT var1,
668 VARIANT var2, IDispatch **ppDisp)
670 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
671 xmlNodePtr ptr = This->node->children;
674 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&var1), debugstr_variant(&var2), ppDisp);
685 xmlelem_collection_updatelength(This);
686 if (index >= This->length)
689 for (i = 0; i < index; i++)
692 return XMLElement_create((IUnknown *)iface, ptr, (LPVOID *)ppDisp, FALSE);
695 static const struct IXMLElementCollectionVtbl xmlelem_collection_vtbl =
697 xmlelem_collection_QueryInterface,
698 xmlelem_collection_AddRef,
699 xmlelem_collection_Release,
700 xmlelem_collection_GetTypeInfoCount,
701 xmlelem_collection_GetTypeInfo,
702 xmlelem_collection_GetIDsOfNames,
703 xmlelem_collection_Invoke,
704 xmlelem_collection_put_length,
705 xmlelem_collection_get_length,
706 xmlelem_collection_get__newEnum,
707 xmlelem_collection_item
710 /************************************************************************
711 * xmlelem_collection implementation of IEnumVARIANT.
713 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_QueryInterface(
714 IEnumVARIANT *iface, REFIID riid, LPVOID *ppvObj)
716 xmlelem_collection *this = impl_from_IEnumVARIANT(iface);
717 return IXMLDocument_QueryInterface((IXMLDocument *)this, riid, ppvObj);
720 static ULONG WINAPI xmlelem_collection_IEnumVARIANT_AddRef(
723 xmlelem_collection *this = impl_from_IEnumVARIANT(iface);
724 return IXMLDocument_AddRef((IXMLDocument *)this);
727 static ULONG WINAPI xmlelem_collection_IEnumVARIANT_Release(
730 xmlelem_collection *this = impl_from_IEnumVARIANT(iface);
731 return IXMLDocument_Release((IXMLDocument *)this);
734 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Next(
735 IEnumVARIANT *iface, ULONG celt, VARIANT *rgVar, ULONG *fetched)
737 xmlelem_collection *This = impl_from_IEnumVARIANT(iface);
738 xmlNodePtr ptr = This->current;
740 TRACE("(%p)->(%d %p %p)\n", This, celt, rgVar, fetched);
745 /* FIXME: handle celt */
750 This->current = This->current->next;
753 V_VT(rgVar) = VT_EMPTY;
754 if (fetched) *fetched = 0;
758 V_VT(rgVar) = VT_DISPATCH;
759 return XMLElement_create((IUnknown *)iface, ptr, (LPVOID *)&V_DISPATCH(rgVar), FALSE);
762 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Skip(
763 IEnumVARIANT *iface, ULONG celt)
765 xmlelem_collection *This = impl_from_IEnumVARIANT(iface);
766 FIXME("(%p)->(%d): stub\n", This, celt);
770 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Reset(
773 xmlelem_collection *This = impl_from_IEnumVARIANT(iface);
774 TRACE("(%p)\n", This);
775 This->current = This->node->children;
779 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Clone(
780 IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
782 xmlelem_collection *This = impl_from_IEnumVARIANT(iface);
783 FIXME("(%p)->(%p): stub\n", This, ppEnum);
787 static const struct IEnumVARIANTVtbl xmlelem_collection_IEnumVARIANTvtbl =
789 xmlelem_collection_IEnumVARIANT_QueryInterface,
790 xmlelem_collection_IEnumVARIANT_AddRef,
791 xmlelem_collection_IEnumVARIANT_Release,
792 xmlelem_collection_IEnumVARIANT_Next,
793 xmlelem_collection_IEnumVARIANT_Skip,
794 xmlelem_collection_IEnumVARIANT_Reset,
795 xmlelem_collection_IEnumVARIANT_Clone
798 static HRESULT XMLElementCollection_create(IUnknown *pUnkOuter, xmlNodePtr node, LPVOID *ppObj)
800 xmlelem_collection *collection;
802 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
809 collection = heap_alloc(sizeof (*collection));
811 return E_OUTOFMEMORY;
813 collection->IXMLElementCollection_iface.lpVtbl = &xmlelem_collection_vtbl;
814 collection->IEnumVARIANT_iface.lpVtbl = &xmlelem_collection_IEnumVARIANTvtbl;
816 collection->length = 0;
817 collection->node = node;
818 collection->current = node->children;
819 xmlelem_collection_updatelength(collection);
821 *ppObj = &collection->IXMLElementCollection_iface;
823 TRACE("returning iface %p\n", *ppObj);