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
33 #include "wine/debug.h"
35 #include "msxml_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
41 static HRESULT XMLElementCollection_create( IUnknown *pUnkOuter, xmlNodePtr node, LPVOID *ppObj );
43 /**********************************************************************
46 typedef struct _xmlelem
48 const IXMLElementVtbl *lpVtbl;
53 static inline xmlelem *impl_from_IXMLElement(IXMLElement *iface)
55 return (xmlelem *)((char*)iface - FIELD_OFFSET(xmlelem, lpVtbl));
58 static HRESULT WINAPI xmlelem_QueryInterface(IXMLElement *iface, REFIID riid, void** ppvObject)
60 xmlelem *This = impl_from_IXMLElement(iface);
62 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
64 if (IsEqualGUID(riid, &IID_IUnknown) ||
65 IsEqualGUID(riid, &IID_IXMLElement))
71 FIXME("interface %s not implemented\n", debugstr_guid(riid));
75 IXMLElement_AddRef(iface);
80 static ULONG WINAPI xmlelem_AddRef(IXMLElement *iface)
82 xmlelem *This = impl_from_IXMLElement(iface);
84 return InterlockedIncrement(&This->ref);
87 static ULONG WINAPI xmlelem_Release(IXMLElement *iface)
89 xmlelem *This = impl_from_IXMLElement(iface);
94 ref = InterlockedDecrement(&This->ref);
97 HeapFree(GetProcessHeap(), 0, This);
103 static HRESULT WINAPI xmlelem_GetTypeInfoCount(IXMLElement *iface, UINT* pctinfo)
105 xmlelem *This = impl_from_IXMLElement(iface);
107 TRACE("(%p)->(%p)\n", This, pctinfo);
114 static HRESULT WINAPI xmlelem_GetTypeInfo(IXMLElement *iface, UINT iTInfo,
115 LCID lcid, ITypeInfo** ppTInfo)
117 xmlelem *This = impl_from_IXMLElement(iface);
120 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
122 hr = get_typeinfo(IXMLElement_tid, ppTInfo);
127 static HRESULT WINAPI xmlelem_GetIDsOfNames(IXMLElement *iface, REFIID riid,
128 LPOLESTR* rgszNames, UINT cNames,
129 LCID lcid, DISPID* rgDispId)
131 xmlelem *This = impl_from_IXMLElement(iface);
135 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
138 if(!rgszNames || cNames == 0 || !rgDispId)
141 hr = get_typeinfo(IXMLElement_tid, &typeinfo);
144 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
145 ITypeInfo_Release(typeinfo);
151 static HRESULT WINAPI xmlelem_Invoke(IXMLElement *iface, DISPID dispIdMember,
152 REFIID riid, LCID lcid, WORD wFlags,
153 DISPPARAMS* pDispParams, VARIANT* pVarResult,
154 EXCEPINFO* pExcepInfo, UINT* puArgErr)
156 xmlelem *This = impl_from_IXMLElement(iface);
160 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
161 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
163 hr = get_typeinfo(IXMLElement_tid, &typeinfo);
166 hr = ITypeInfo_Invoke(typeinfo, &(This->lpVtbl), dispIdMember, wFlags, pDispParams,
167 pVarResult, pExcepInfo, puArgErr);
168 ITypeInfo_Release(typeinfo);
174 static HRESULT WINAPI xmlelem_get_tagName(IXMLElement *iface, BSTR *p)
176 xmlelem *This = impl_from_IXMLElement(iface);
178 TRACE("(%p, %p)\n", iface, p);
183 *p = bstr_from_xmlChar(This->node->name);
184 CharUpperBuffW(*p, SysStringLen(*p));
186 TRACE("returning %s\n", debugstr_w(*p));
191 static HRESULT WINAPI xmlelem_put_tagName(IXMLElement *iface, BSTR p)
193 FIXME("(%p, %p): stub\n", iface, p);
201 static HRESULT WINAPI xmlelem_get_parent(IXMLElement *iface, IXMLElement **parent)
203 xmlelem *This = impl_from_IXMLElement(iface);
205 TRACE("(%p, %p)\n", iface, parent);
212 if (!This->node->parent)
215 return XMLElement_create((IUnknown *)iface, This->node->parent, (LPVOID *)parent);
218 static HRESULT WINAPI xmlelem_setAttribute(IXMLElement *iface, BSTR strPropertyName,
219 VARIANT PropertyValue)
221 xmlelem *This = impl_from_IXMLElement(iface);
222 xmlChar *name, *value;
225 TRACE("(%p, %s)\n", iface, debugstr_w(strPropertyName));
227 if (!strPropertyName || V_VT(&PropertyValue) != VT_BSTR)
230 name = xmlChar_from_wchar(strPropertyName);
231 value = xmlChar_from_wchar(V_BSTR(&PropertyValue));
232 attr = xmlSetProp(This->node, name, value);
234 HeapFree(GetProcessHeap(), 0, name);
235 HeapFree(GetProcessHeap(), 0, value);
236 return (attr) ? S_OK : S_FALSE;
239 static HRESULT WINAPI xmlelem_getAttribute(IXMLElement *iface, BSTR strPropertyName,
240 VARIANT *PropertyValue)
242 xmlelem *This = impl_from_IXMLElement(iface);
243 xmlChar *val = NULL, *name;
246 TRACE("(%p, %s, %p)\n", iface, debugstr_w(strPropertyName), PropertyValue);
251 VariantInit(PropertyValue);
252 V_BSTR(PropertyValue) = NULL;
254 if (!strPropertyName)
257 name = xmlChar_from_wchar(strPropertyName);
258 ptr = This->node->properties;
261 if (!lstrcmpiA((LPSTR)name, (LPCSTR)ptr->name))
263 val = xmlNodeListGetString(ptr->doc, ptr->children, 1);
272 V_VT(PropertyValue) = VT_BSTR;
273 V_BSTR(PropertyValue) = bstr_from_xmlChar(val);
276 HeapFree(GetProcessHeap(), 0, name);
278 TRACE("returning %s\n", debugstr_w(V_BSTR(PropertyValue)));
279 return (val) ? S_OK : S_FALSE;
282 static HRESULT WINAPI xmlelem_removeAttribute(IXMLElement *iface, BSTR strPropertyName)
284 xmlelem *This = impl_from_IXMLElement(iface);
288 HRESULT hr = S_FALSE;
290 TRACE("(%p, %s)\n", iface, debugstr_w(strPropertyName));
292 if (!strPropertyName)
295 name = xmlChar_from_wchar(strPropertyName);
296 attr = xmlHasProp(This->node, name);
300 res = xmlRemoveProp(attr);
306 HeapFree(GetProcessHeap(), 0, name);
310 static HRESULT WINAPI xmlelem_get_children(IXMLElement *iface, IXMLElementCollection **p)
312 xmlelem *This = impl_from_IXMLElement(iface);
314 TRACE("(%p, %p)\n", iface, p);
319 return XMLElementCollection_create((IUnknown *)iface, This->node, (LPVOID *)p);
322 static LONG type_libxml_to_msxml(xmlElementType type)
326 case XML_ELEMENT_NODE:
327 return XMLELEMTYPE_ELEMENT;
329 return XMLELEMTYPE_TEXT;
330 case XML_COMMENT_NODE:
331 return XMLELEMTYPE_COMMENT;
332 case XML_DOCUMENT_NODE:
333 return XMLELEMTYPE_DOCUMENT;
335 return XMLELEMTYPE_DTD;
337 return XMLELEMTYPE_PI;
342 return XMLELEMTYPE_OTHER;
345 static HRESULT WINAPI xmlelem_get_type(IXMLElement *iface, LONG *p)
347 xmlelem *This = impl_from_IXMLElement(iface);
349 TRACE("(%p, %p)\n", This, p);
354 *p = type_libxml_to_msxml(This->node->type);
355 TRACE("returning %d\n", *p);
359 static HRESULT WINAPI xmlelem_get_text(IXMLElement *iface, BSTR *p)
361 xmlelem *This = impl_from_IXMLElement(iface);
364 TRACE("(%p, %p)\n", iface, p);
369 content = xmlNodeGetContent(This->node);
370 *p = bstr_from_xmlChar(content);
371 TRACE("returning %s\n", debugstr_w(*p));
377 static HRESULT WINAPI xmlelem_put_text(IXMLElement *iface, BSTR p)
379 xmlelem *This = impl_from_IXMLElement(iface);
382 TRACE("(%p, %s)\n", iface, debugstr_w(p));
384 /* FIXME: test which types can be used */
385 if (This->node->type == XML_ELEMENT_NODE)
388 content = xmlChar_from_wchar(p);
389 xmlNodeSetContent(This->node, content);
391 HeapFree( GetProcessHeap(), 0, content);
396 static HRESULT WINAPI xmlelem_addChild(IXMLElement *iface, IXMLElement *pChildElem,
397 LONG lIndex, LONG lreserved)
399 xmlelem *This = impl_from_IXMLElement(iface);
400 xmlelem *childElem = impl_from_IXMLElement(pChildElem);
403 TRACE("(%p, %p, %d, %d)\n", iface, pChildElem, lIndex, lreserved);
406 child = xmlAddChild(This->node, childElem->node);
408 child = xmlAddNextSibling(This->node, childElem->node->last);
410 return (child) ? S_OK : S_FALSE;
413 static HRESULT WINAPI xmlelem_removeChild(IXMLElement *iface, IXMLElement *pChildElem)
415 xmlelem *This = impl_from_IXMLElement(iface);
416 xmlelem *childElem = impl_from_IXMLElement(pChildElem);
418 TRACE("(%p, %p)\n", This, childElem);
423 /* only supported for This is childElem parent case */
424 if (This->node != childElem->node->parent)
427 xmlUnlinkNode(childElem->node);
432 static const struct IXMLElementVtbl xmlelem_vtbl =
434 xmlelem_QueryInterface,
437 xmlelem_GetTypeInfoCount,
439 xmlelem_GetIDsOfNames,
444 xmlelem_setAttribute,
445 xmlelem_getAttribute,
446 xmlelem_removeAttribute,
447 xmlelem_get_children,
455 HRESULT XMLElement_create(IUnknown *pUnkOuter, xmlNodePtr node, LPVOID *ppObj)
459 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
466 elem = HeapAlloc(GetProcessHeap(), 0, sizeof (*elem));
468 return E_OUTOFMEMORY;
470 elem->lpVtbl = &xmlelem_vtbl;
474 *ppObj = &elem->lpVtbl;
476 TRACE("returning iface %p\n", *ppObj);
480 /************************************************************************
481 * IXMLElementCollection
483 typedef struct _xmlelem_collection
485 const IXMLElementCollectionVtbl *lpVtbl;
486 const IEnumVARIANTVtbl *lpvtblIEnumVARIANT;
491 /* IEnumVARIANT members */
493 } xmlelem_collection;
495 static inline LONG xmlelem_collection_updatelength(xmlelem_collection *collection)
497 xmlNodePtr ptr = collection->node->children;
499 collection->length = 0;
502 collection->length++;
505 return collection->length;
508 static inline xmlelem_collection *impl_from_IXMLElementCollection(IXMLElementCollection *iface)
510 return (xmlelem_collection *)((char*)iface - FIELD_OFFSET(xmlelem_collection, lpVtbl));
513 static inline xmlelem_collection *impl_from_IEnumVARIANT(IEnumVARIANT *iface)
515 return (xmlelem_collection *)((char*)iface - FIELD_OFFSET(xmlelem_collection, lpvtblIEnumVARIANT));
518 static HRESULT WINAPI xmlelem_collection_QueryInterface(IXMLElementCollection *iface, REFIID riid, void** ppvObject)
520 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
522 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
524 if (IsEqualGUID(riid, &IID_IUnknown) ||
525 IsEqualGUID(riid, &IID_IXMLElementCollection))
529 else if (IsEqualGUID(riid, &IID_IEnumVARIANT))
531 *ppvObject = &(This->lpvtblIEnumVARIANT);
535 FIXME("interface %s not implemented\n", debugstr_guid(riid));
536 return E_NOINTERFACE;
539 IXMLElementCollection_AddRef(iface);
544 static ULONG WINAPI xmlelem_collection_AddRef(IXMLElementCollection *iface)
546 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
548 return InterlockedIncrement(&This->ref);
551 static ULONG WINAPI xmlelem_collection_Release(IXMLElementCollection *iface)
553 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
558 ref = InterlockedDecrement(&This->ref);
561 HeapFree(GetProcessHeap(), 0, This);
567 static HRESULT WINAPI xmlelem_collection_GetTypeInfoCount(IXMLElementCollection *iface, UINT* pctinfo)
573 static HRESULT WINAPI xmlelem_collection_GetTypeInfo(IXMLElementCollection *iface, UINT iTInfo,
574 LCID lcid, ITypeInfo** ppTInfo)
580 static HRESULT WINAPI xmlelem_collection_GetIDsOfNames(IXMLElementCollection *iface, REFIID riid,
581 LPOLESTR* rgszNames, UINT cNames,
582 LCID lcid, DISPID* rgDispId)
588 static HRESULT WINAPI xmlelem_collection_Invoke(IXMLElementCollection *iface, DISPID dispIdMember,
589 REFIID riid, LCID lcid, WORD wFlags,
590 DISPPARAMS* pDispParams, VARIANT* pVarResult,
591 EXCEPINFO* pExcepInfo, UINT* puArgErr)
597 static HRESULT WINAPI xmlelem_collection_put_length(IXMLElementCollection *iface, LONG v)
599 TRACE("(%p, %d)\n", iface, v);
603 static HRESULT WINAPI xmlelem_collection_get_length(IXMLElementCollection *iface, LONG *p)
605 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
607 TRACE("(%p, %p)\n", iface, p);
612 *p = xmlelem_collection_updatelength(This);
616 static HRESULT WINAPI xmlelem_collection_get__newEnum(IXMLElementCollection *iface, IUnknown **ppUnk)
618 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
620 TRACE("(%p, %p)\n", iface, ppUnk);
625 *ppUnk = (IUnknown *)This;
626 IUnknown_AddRef(*ppUnk);
630 static HRESULT WINAPI xmlelem_collection_item(IXMLElementCollection *iface, VARIANT var1,
631 VARIANT var2, IDispatch **ppDisp)
633 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
634 xmlNodePtr ptr = This->node->children;
637 TRACE("(%p, %p)\n", iface, ppDisp);
648 xmlelem_collection_updatelength(This);
649 if (index >= This->length)
652 for (i = 0; i < index; i++)
655 return XMLElement_create((IUnknown *)iface, ptr, (LPVOID *)ppDisp);
658 static const struct IXMLElementCollectionVtbl xmlelem_collection_vtbl =
660 xmlelem_collection_QueryInterface,
661 xmlelem_collection_AddRef,
662 xmlelem_collection_Release,
663 xmlelem_collection_GetTypeInfoCount,
664 xmlelem_collection_GetTypeInfo,
665 xmlelem_collection_GetIDsOfNames,
666 xmlelem_collection_Invoke,
667 xmlelem_collection_put_length,
668 xmlelem_collection_get_length,
669 xmlelem_collection_get__newEnum,
670 xmlelem_collection_item
673 /************************************************************************
674 * xmlelem_collection implementation of IEnumVARIANT.
676 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_QueryInterface(
677 IEnumVARIANT *iface, REFIID riid, LPVOID *ppvObj)
679 xmlelem_collection *this = impl_from_IEnumVARIANT(iface);
680 return IXMLDocument_QueryInterface((IXMLDocument *)this, riid, ppvObj);
683 static ULONG WINAPI xmlelem_collection_IEnumVARIANT_AddRef(
686 xmlelem_collection *this = impl_from_IEnumVARIANT(iface);
687 return IXMLDocument_AddRef((IXMLDocument *)this);
690 static ULONG WINAPI xmlelem_collection_IEnumVARIANT_Release(
693 xmlelem_collection *this = impl_from_IEnumVARIANT(iface);
694 return IXMLDocument_Release((IXMLDocument *)this);
697 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Next(
698 IEnumVARIANT *iface, ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
700 xmlelem_collection *This = impl_from_IEnumVARIANT(iface);
701 xmlNodePtr ptr = This->current;
703 TRACE("(%p, %d, %p, %p)\n", iface, celt, rgVar, pCeltFetched);
708 /* FIXME: handle celt */
712 This->current = This->current->next;
714 V_VT(rgVar) = VT_DISPATCH;
715 return XMLElement_create((IUnknown *)iface, ptr, (LPVOID *)&V_DISPATCH(rgVar));
718 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Skip(
719 IEnumVARIANT *iface, ULONG celt)
721 FIXME("(%p, %d): stub\n", iface, celt);
725 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Reset(
728 xmlelem_collection *This = impl_from_IEnumVARIANT(iface);
729 This->current = This->node->children;
733 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Clone(
734 IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
736 FIXME("(%p, %p): stub\n", iface, ppEnum);
740 static const struct IEnumVARIANTVtbl xmlelem_collection_IEnumVARIANTvtbl =
742 xmlelem_collection_IEnumVARIANT_QueryInterface,
743 xmlelem_collection_IEnumVARIANT_AddRef,
744 xmlelem_collection_IEnumVARIANT_Release,
745 xmlelem_collection_IEnumVARIANT_Next,
746 xmlelem_collection_IEnumVARIANT_Skip,
747 xmlelem_collection_IEnumVARIANT_Reset,
748 xmlelem_collection_IEnumVARIANT_Clone
751 static HRESULT XMLElementCollection_create(IUnknown *pUnkOuter, xmlNodePtr node, LPVOID *ppObj)
753 xmlelem_collection *collection;
755 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
762 collection = HeapAlloc(GetProcessHeap(), 0, sizeof (*collection));
764 return E_OUTOFMEMORY;
766 collection->lpVtbl = &xmlelem_collection_vtbl;
767 collection->lpvtblIEnumVARIANT = &xmlelem_collection_IEnumVARIANTvtbl;
769 collection->length = 0;
770 collection->node = node;
771 collection->current = node->children;
772 xmlelem_collection_updatelength(collection);
774 *ppObj = &collection->lpVtbl;
776 TRACE("returning iface %p\n", *ppObj);