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 IXMLElement IXMLElement_iface;
54 static inline xmlelem *impl_from_IXMLElement(IXMLElement *iface)
56 return CONTAINING_RECORD(iface, xmlelem, IXMLElement_iface);
59 static HRESULT WINAPI xmlelem_QueryInterface(IXMLElement *iface, REFIID riid, void** ppvObject)
61 xmlelem *This = impl_from_IXMLElement(iface);
63 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
65 if (IsEqualGUID(riid, &IID_IUnknown) ||
66 IsEqualGUID(riid, &IID_IXMLElement))
72 FIXME("interface %s not implemented\n", debugstr_guid(riid));
76 IXMLElement_AddRef(iface);
81 static ULONG WINAPI xmlelem_AddRef(IXMLElement *iface)
83 xmlelem *This = impl_from_IXMLElement(iface);
85 return InterlockedIncrement(&This->ref);
88 static ULONG WINAPI xmlelem_Release(IXMLElement *iface)
90 xmlelem *This = impl_from_IXMLElement(iface);
95 ref = InterlockedDecrement(&This->ref);
98 if (This->own) xmlFreeNode(This->node);
105 static HRESULT WINAPI xmlelem_GetTypeInfoCount(IXMLElement *iface, UINT* pctinfo)
107 xmlelem *This = impl_from_IXMLElement(iface);
109 TRACE("(%p)->(%p)\n", This, pctinfo);
116 static HRESULT WINAPI xmlelem_GetTypeInfo(IXMLElement *iface, UINT iTInfo,
117 LCID lcid, ITypeInfo** ppTInfo)
119 xmlelem *This = impl_from_IXMLElement(iface);
122 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
124 hr = get_typeinfo(IXMLElement_tid, ppTInfo);
129 static HRESULT WINAPI xmlelem_GetIDsOfNames(IXMLElement *iface, REFIID riid,
130 LPOLESTR* rgszNames, UINT cNames,
131 LCID lcid, DISPID* rgDispId)
133 xmlelem *This = impl_from_IXMLElement(iface);
137 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
140 if(!rgszNames || cNames == 0 || !rgDispId)
143 hr = get_typeinfo(IXMLElement_tid, &typeinfo);
146 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
147 ITypeInfo_Release(typeinfo);
153 static HRESULT WINAPI xmlelem_Invoke(IXMLElement *iface, DISPID dispIdMember,
154 REFIID riid, LCID lcid, WORD wFlags,
155 DISPPARAMS* pDispParams, VARIANT* pVarResult,
156 EXCEPINFO* pExcepInfo, UINT* puArgErr)
158 xmlelem *This = impl_from_IXMLElement(iface);
162 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
163 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
165 hr = get_typeinfo(IXMLElement_tid, &typeinfo);
168 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLElement_iface, dispIdMember, wFlags, pDispParams,
169 pVarResult, pExcepInfo, puArgErr);
170 ITypeInfo_Release(typeinfo);
176 static HRESULT WINAPI xmlelem_get_tagName(IXMLElement *iface, BSTR *p)
178 xmlelem *This = impl_from_IXMLElement(iface);
180 TRACE("(%p, %p)\n", iface, p);
185 *p = bstr_from_xmlChar(This->node->name);
186 CharUpperBuffW(*p, SysStringLen(*p));
188 TRACE("returning %s\n", debugstr_w(*p));
193 static HRESULT WINAPI xmlelem_put_tagName(IXMLElement *iface, BSTR p)
195 FIXME("(%p, %p): stub\n", iface, p);
203 static HRESULT WINAPI xmlelem_get_parent(IXMLElement *iface, IXMLElement **parent)
205 xmlelem *This = impl_from_IXMLElement(iface);
207 TRACE("(%p, %p)\n", iface, parent);
214 if (!This->node->parent)
217 return XMLElement_create((IUnknown *)iface, This->node->parent, (LPVOID *)parent, FALSE);
220 static HRESULT WINAPI xmlelem_setAttribute(IXMLElement *iface, BSTR strPropertyName,
221 VARIANT PropertyValue)
223 xmlelem *This = impl_from_IXMLElement(iface);
224 xmlChar *name, *value;
227 TRACE("(%p, %s)\n", iface, debugstr_w(strPropertyName));
229 if (!strPropertyName || V_VT(&PropertyValue) != VT_BSTR)
232 name = xmlChar_from_wchar(strPropertyName);
233 value = xmlChar_from_wchar(V_BSTR(&PropertyValue));
234 attr = xmlSetProp(This->node, name, value);
238 return (attr) ? S_OK : S_FALSE;
241 static HRESULT WINAPI xmlelem_getAttribute(IXMLElement *iface, BSTR name,
244 static const WCHAR xmllangW[] = { 'x','m','l',':','l','a','n','g',0 };
245 xmlelem *This = impl_from_IXMLElement(iface);
248 TRACE("(%p, %s, %p)\n", iface, debugstr_w(name), value);
254 V_BSTR(value) = NULL;
259 /* case for xml:lang attribute */
260 if (!lstrcmpiW(name, xmllangW))
263 ns = xmlSearchNs(This->node->doc, This->node, (xmlChar*)"xml");
264 val = xmlGetNsProp(This->node, (xmlChar*)"lang", ns->href);
271 xml_name = xmlChar_from_wchar(name);
272 attr = This->node->properties;
277 attr_name = bstr_from_xmlChar(attr->name);
278 if (!lstrcmpiW(name, attr_name))
280 val = xmlNodeListGetString(attr->doc, attr->children, 1);
281 SysFreeString(attr_name);
286 SysFreeString(attr_name);
294 V_VT(value) = VT_BSTR;
295 V_BSTR(value) = bstr_from_xmlChar(val);
299 TRACE("returning %s\n", debugstr_w(V_BSTR(value)));
300 return (val) ? S_OK : S_FALSE;
303 static HRESULT WINAPI xmlelem_removeAttribute(IXMLElement *iface, BSTR strPropertyName)
305 xmlelem *This = impl_from_IXMLElement(iface);
309 HRESULT hr = S_FALSE;
311 TRACE("(%p, %s)\n", iface, debugstr_w(strPropertyName));
313 if (!strPropertyName)
316 name = xmlChar_from_wchar(strPropertyName);
317 attr = xmlHasProp(This->node, name);
321 res = xmlRemoveProp(attr);
331 static HRESULT WINAPI xmlelem_get_children(IXMLElement *iface, IXMLElementCollection **p)
333 xmlelem *This = impl_from_IXMLElement(iface);
335 TRACE("(%p, %p)\n", iface, p);
340 return XMLElementCollection_create((IUnknown *)iface, This->node, (LPVOID *)p);
343 static LONG type_libxml_to_msxml(xmlElementType type)
347 case XML_ELEMENT_NODE:
348 return XMLELEMTYPE_ELEMENT;
350 return XMLELEMTYPE_TEXT;
351 case XML_COMMENT_NODE:
352 return XMLELEMTYPE_COMMENT;
353 case XML_DOCUMENT_NODE:
354 return XMLELEMTYPE_DOCUMENT;
356 return XMLELEMTYPE_DTD;
358 return XMLELEMTYPE_PI;
363 return XMLELEMTYPE_OTHER;
366 static HRESULT WINAPI xmlelem_get_type(IXMLElement *iface, LONG *p)
368 xmlelem *This = impl_from_IXMLElement(iface);
370 TRACE("(%p, %p)\n", This, p);
375 *p = type_libxml_to_msxml(This->node->type);
376 TRACE("returning %d\n", *p);
380 static HRESULT WINAPI xmlelem_get_text(IXMLElement *iface, BSTR *p)
382 xmlelem *This = impl_from_IXMLElement(iface);
385 TRACE("(%p, %p)\n", iface, p);
390 content = xmlNodeGetContent(This->node);
391 *p = bstr_from_xmlChar(content);
392 TRACE("returning %s\n", debugstr_w(*p));
398 static HRESULT WINAPI xmlelem_put_text(IXMLElement *iface, BSTR p)
400 xmlelem *This = impl_from_IXMLElement(iface);
403 TRACE("(%p, %s)\n", iface, debugstr_w(p));
405 /* FIXME: test which types can be used */
406 if (This->node->type == XML_ELEMENT_NODE)
409 content = xmlChar_from_wchar(p);
410 xmlNodeSetContent(This->node, content);
417 static HRESULT WINAPI xmlelem_addChild(IXMLElement *iface, IXMLElement *pChildElem,
418 LONG lIndex, LONG lreserved)
420 xmlelem *This = impl_from_IXMLElement(iface);
421 xmlelem *childElem = impl_from_IXMLElement(pChildElem);
424 TRACE("(%p, %p, %d, %d)\n", iface, pChildElem, lIndex, lreserved);
427 child = xmlAddChild(This->node, childElem->node);
429 child = xmlAddNextSibling(This->node, childElem->node->last);
431 /* parent is responsible for child data */
432 if (child) childElem->own = FALSE;
434 return (child) ? S_OK : S_FALSE;
437 static HRESULT WINAPI xmlelem_removeChild(IXMLElement *iface, IXMLElement *pChildElem)
439 xmlelem *This = impl_from_IXMLElement(iface);
440 xmlelem *childElem = impl_from_IXMLElement(pChildElem);
442 TRACE("(%p, %p)\n", This, childElem);
447 /* only supported for This is childElem parent case */
448 if (This->node != childElem->node->parent)
451 xmlUnlinkNode(childElem->node);
452 /* standalone element now */
453 childElem->own = TRUE;
458 static const struct IXMLElementVtbl xmlelem_vtbl =
460 xmlelem_QueryInterface,
463 xmlelem_GetTypeInfoCount,
465 xmlelem_GetIDsOfNames,
470 xmlelem_setAttribute,
471 xmlelem_getAttribute,
472 xmlelem_removeAttribute,
473 xmlelem_get_children,
481 HRESULT XMLElement_create(IUnknown *pUnkOuter, xmlNodePtr node, LPVOID *ppObj, BOOL own)
485 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
492 elem = heap_alloc(sizeof (*elem));
494 return E_OUTOFMEMORY;
496 elem->IXMLElement_iface.lpVtbl = &xmlelem_vtbl;
501 *ppObj = &elem->IXMLElement_iface;
503 TRACE("returning iface %p\n", *ppObj);
507 /************************************************************************
508 * IXMLElementCollection
510 typedef struct _xmlelem_collection
512 IXMLElementCollection IXMLElementCollection_iface;
513 IEnumVARIANT IEnumVARIANT_iface;
518 /* IEnumVARIANT members */
520 } xmlelem_collection;
522 static inline LONG xmlelem_collection_updatelength(xmlelem_collection *collection)
524 xmlNodePtr ptr = collection->node->children;
526 collection->length = 0;
529 collection->length++;
532 return collection->length;
535 static inline xmlelem_collection *impl_from_IXMLElementCollection(IXMLElementCollection *iface)
537 return CONTAINING_RECORD(iface, xmlelem_collection, IXMLElementCollection_iface);
540 static inline xmlelem_collection *impl_from_IEnumVARIANT(IEnumVARIANT *iface)
542 return CONTAINING_RECORD(iface, xmlelem_collection, IEnumVARIANT_iface);
545 static HRESULT WINAPI xmlelem_collection_QueryInterface(IXMLElementCollection *iface, REFIID riid, void** ppvObject)
547 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
549 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
551 if (IsEqualGUID(riid, &IID_IUnknown) ||
552 IsEqualGUID(riid, &IID_IXMLElementCollection))
556 else if (IsEqualGUID(riid, &IID_IEnumVARIANT))
558 *ppvObject = &This->IEnumVARIANT_iface;
562 FIXME("interface %s not implemented\n", debugstr_guid(riid));
563 return E_NOINTERFACE;
566 IXMLElementCollection_AddRef(iface);
571 static ULONG WINAPI xmlelem_collection_AddRef(IXMLElementCollection *iface)
573 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
575 return InterlockedIncrement(&This->ref);
578 static ULONG WINAPI xmlelem_collection_Release(IXMLElementCollection *iface)
580 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
585 ref = InterlockedDecrement(&This->ref);
594 static HRESULT WINAPI xmlelem_collection_GetTypeInfoCount(IXMLElementCollection *iface, UINT* pctinfo)
600 static HRESULT WINAPI xmlelem_collection_GetTypeInfo(IXMLElementCollection *iface, UINT iTInfo,
601 LCID lcid, ITypeInfo** ppTInfo)
607 static HRESULT WINAPI xmlelem_collection_GetIDsOfNames(IXMLElementCollection *iface, REFIID riid,
608 LPOLESTR* rgszNames, UINT cNames,
609 LCID lcid, DISPID* rgDispId)
615 static HRESULT WINAPI xmlelem_collection_Invoke(IXMLElementCollection *iface, DISPID dispIdMember,
616 REFIID riid, LCID lcid, WORD wFlags,
617 DISPPARAMS* pDispParams, VARIANT* pVarResult,
618 EXCEPINFO* pExcepInfo, UINT* puArgErr)
624 static HRESULT WINAPI xmlelem_collection_put_length(IXMLElementCollection *iface, LONG v)
626 TRACE("(%p, %d)\n", iface, v);
630 static HRESULT WINAPI xmlelem_collection_get_length(IXMLElementCollection *iface, LONG *p)
632 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
634 TRACE("(%p, %p)\n", iface, p);
639 *p = xmlelem_collection_updatelength(This);
643 static HRESULT WINAPI xmlelem_collection_get__newEnum(IXMLElementCollection *iface, IUnknown **ppUnk)
645 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
647 TRACE("(%p, %p)\n", iface, ppUnk);
652 *ppUnk = (IUnknown *)This;
653 IUnknown_AddRef(*ppUnk);
657 static HRESULT WINAPI xmlelem_collection_item(IXMLElementCollection *iface, VARIANT var1,
658 VARIANT var2, IDispatch **ppDisp)
660 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
661 xmlNodePtr ptr = This->node->children;
664 TRACE("(%p, %p)\n", iface, ppDisp);
675 xmlelem_collection_updatelength(This);
676 if (index >= This->length)
679 for (i = 0; i < index; i++)
682 return XMLElement_create((IUnknown *)iface, ptr, (LPVOID *)ppDisp, FALSE);
685 static const struct IXMLElementCollectionVtbl xmlelem_collection_vtbl =
687 xmlelem_collection_QueryInterface,
688 xmlelem_collection_AddRef,
689 xmlelem_collection_Release,
690 xmlelem_collection_GetTypeInfoCount,
691 xmlelem_collection_GetTypeInfo,
692 xmlelem_collection_GetIDsOfNames,
693 xmlelem_collection_Invoke,
694 xmlelem_collection_put_length,
695 xmlelem_collection_get_length,
696 xmlelem_collection_get__newEnum,
697 xmlelem_collection_item
700 /************************************************************************
701 * xmlelem_collection implementation of IEnumVARIANT.
703 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_QueryInterface(
704 IEnumVARIANT *iface, REFIID riid, LPVOID *ppvObj)
706 xmlelem_collection *this = impl_from_IEnumVARIANT(iface);
707 return IXMLDocument_QueryInterface((IXMLDocument *)this, riid, ppvObj);
710 static ULONG WINAPI xmlelem_collection_IEnumVARIANT_AddRef(
713 xmlelem_collection *this = impl_from_IEnumVARIANT(iface);
714 return IXMLDocument_AddRef((IXMLDocument *)this);
717 static ULONG WINAPI xmlelem_collection_IEnumVARIANT_Release(
720 xmlelem_collection *this = impl_from_IEnumVARIANT(iface);
721 return IXMLDocument_Release((IXMLDocument *)this);
724 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Next(
725 IEnumVARIANT *iface, ULONG celt, VARIANT *rgVar, ULONG *fetched)
727 xmlelem_collection *This = impl_from_IEnumVARIANT(iface);
728 xmlNodePtr ptr = This->current;
730 TRACE("(%p, %d, %p, %p)\n", iface, celt, rgVar, fetched);
735 /* FIXME: handle celt */
740 This->current = This->current->next;
743 V_VT(rgVar) = VT_EMPTY;
744 if (fetched) *fetched = 0;
748 V_VT(rgVar) = VT_DISPATCH;
749 return XMLElement_create((IUnknown *)iface, ptr, (LPVOID *)&V_DISPATCH(rgVar), FALSE);
752 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Skip(
753 IEnumVARIANT *iface, ULONG celt)
755 FIXME("(%p, %d): stub\n", iface, celt);
759 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Reset(
762 xmlelem_collection *This = impl_from_IEnumVARIANT(iface);
763 This->current = This->node->children;
767 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Clone(
768 IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
770 FIXME("(%p, %p): stub\n", iface, ppEnum);
774 static const struct IEnumVARIANTVtbl xmlelem_collection_IEnumVARIANTvtbl =
776 xmlelem_collection_IEnumVARIANT_QueryInterface,
777 xmlelem_collection_IEnumVARIANT_AddRef,
778 xmlelem_collection_IEnumVARIANT_Release,
779 xmlelem_collection_IEnumVARIANT_Next,
780 xmlelem_collection_IEnumVARIANT_Skip,
781 xmlelem_collection_IEnumVARIANT_Reset,
782 xmlelem_collection_IEnumVARIANT_Clone
785 static HRESULT XMLElementCollection_create(IUnknown *pUnkOuter, xmlNodePtr node, LPVOID *ppObj)
787 xmlelem_collection *collection;
789 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
796 collection = heap_alloc(sizeof (*collection));
798 return E_OUTOFMEMORY;
800 collection->IXMLElementCollection_iface.lpVtbl = &xmlelem_collection_vtbl;
801 collection->IEnumVARIANT_iface.lpVtbl = &xmlelem_collection_IEnumVARIANTvtbl;
803 collection->length = 0;
804 collection->node = node;
805 collection->current = node->children;
806 xmlelem_collection_updatelength(collection);
808 *ppObj = &collection->IXMLElementCollection_iface;
810 TRACE("returning iface %p\n", *ppObj);