push bd0608bf727e2241b2885edad5c5c753477ffd22
[wine/hacks.git] / dlls / msxml3 / xmlelem.c
blob787b13fcce8577f42b5518273b67b60ca8779beb
1 /*
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
21 #define COBJMACROS
23 #include "config.h"
25 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30 #include "msxml2.h"
31 #include "ocidl.h"
33 #include "wine/debug.h"
35 #include "msxml_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
39 #ifdef HAVE_LIBXML2
41 /**********************************************************************
42 * IXMLElement
44 typedef struct _xmlelem
46 const IXMLElementVtbl *lpVtbl;
47 LONG ref;
48 xmlNodePtr node;
49 } xmlelem;
51 static inline xmlelem *impl_from_IXMLElement(IXMLElement *iface)
53 return (xmlelem *)((char*)iface - FIELD_OFFSET(xmlelem, lpVtbl));
56 static HRESULT WINAPI xmlelem_QueryInterface(IXMLElement *iface, REFIID riid, void** ppvObject)
58 xmlelem *This = impl_from_IXMLElement(iface);
60 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
62 if (IsEqualGUID(riid, &IID_IUnknown) ||
63 IsEqualGUID(riid, &IID_IXMLElement))
65 *ppvObject = iface;
67 else
69 FIXME("interface %s not implemented\n", debugstr_guid(riid));
70 return E_NOINTERFACE;
73 IXMLElement_AddRef(iface);
75 return S_OK;
78 static ULONG WINAPI xmlelem_AddRef(IXMLElement *iface)
80 xmlelem *This = impl_from_IXMLElement(iface);
81 TRACE("%p\n", This);
82 return InterlockedIncrement(&This->ref);
85 static ULONG WINAPI xmlelem_Release(IXMLElement *iface)
87 xmlelem *This = impl_from_IXMLElement(iface);
88 LONG ref;
90 TRACE("%p\n", This);
92 ref = InterlockedDecrement(&This->ref);
93 if (ref == 0)
95 HeapFree(GetProcessHeap(), 0, This);
98 return ref;
101 static HRESULT WINAPI xmlelem_GetTypeInfoCount(IXMLElement *iface, UINT* pctinfo)
103 FIXME("\n");
104 return E_NOTIMPL;
107 static HRESULT WINAPI xmlelem_GetTypeInfo(IXMLElement *iface, UINT iTInfo,
108 LCID lcid, ITypeInfo** ppTInfo)
110 FIXME("\n");
111 return E_NOTIMPL;
114 static HRESULT WINAPI xmlelem_GetIDsOfNames(IXMLElement *iface, REFIID riid,
115 LPOLESTR* rgszNames, UINT cNames,
116 LCID lcid, DISPID* rgDispId)
118 FIXME("\n");
119 return E_NOTIMPL;
122 static HRESULT WINAPI xmlelem_Invoke(IXMLElement *iface, DISPID dispIdMember,
123 REFIID riid, LCID lcid, WORD wFlags,
124 DISPPARAMS* pDispParams, VARIANT* pVarResult,
125 EXCEPINFO* pExcepInfo, UINT* puArgErr)
127 FIXME("\n");
128 return E_NOTIMPL;
131 static inline BSTR str_dup_upper(BSTR str)
133 INT len = (lstrlenW(str) + 1) * sizeof(WCHAR);
134 BSTR p = SysAllocStringLen(NULL, len);
135 if (p)
137 memcpy(p, str, len);
138 CharUpperW(p);
140 return p;
143 static HRESULT WINAPI xmlelem_get_tagName(IXMLElement *iface, BSTR *p)
145 xmlelem *This = impl_from_IXMLElement(iface);
146 BSTR temp;
148 TRACE("(%p, %p)\n", iface, p);
150 if (!p)
151 return E_INVALIDARG;
153 temp = bstr_from_xmlChar(This->node->name);
154 *p = str_dup_upper(temp);
155 SysFreeString(temp);
157 TRACE("returning %s\n", debugstr_w(*p));
159 return S_OK;
162 static HRESULT WINAPI xmlelem_put_tagName(IXMLElement *iface, BSTR p)
164 FIXME("(%p, %p): stub\n", iface, p);
166 if (!p)
167 return E_INVALIDARG;
169 return E_NOTIMPL;
172 static HRESULT WINAPI xmlelem_get_parent(IXMLElement *iface, IXMLElement **parent)
174 xmlelem *This = impl_from_IXMLElement(iface);
176 TRACE("(%p, %p)\n", iface, parent);
178 if (!parent)
179 return E_INVALIDARG;
181 *parent = NULL;
183 if (!This->node->parent)
184 return S_FALSE;
186 return XMLElement_create((IUnknown *)iface, This->node->parent, (LPVOID *)parent);
189 static HRESULT WINAPI xmlelem_setAttribute(IXMLElement *iface, BSTR strPropertyName,
190 VARIANT PropertyValue)
192 xmlelem *This = impl_from_IXMLElement(iface);
193 xmlChar *name, *value;
194 xmlAttrPtr attr;
196 TRACE("(%p, %s)\n", iface, debugstr_w(strPropertyName));
198 if (!strPropertyName || V_VT(&PropertyValue) != VT_BSTR)
199 return E_INVALIDARG;
201 name = xmlChar_from_wchar(strPropertyName);
202 value = xmlChar_from_wchar(V_BSTR(&PropertyValue));
203 attr = xmlSetProp(This->node, name, value);
205 HeapFree(GetProcessHeap(), 0, name);
206 HeapFree(GetProcessHeap(), 0, value);
207 return (attr) ? S_OK : S_FALSE;
210 static HRESULT WINAPI xmlelem_getAttribute(IXMLElement *iface, BSTR strPropertyName,
211 VARIANT *PropertyValue)
213 xmlelem *This = impl_from_IXMLElement(iface);
214 xmlChar *val = NULL, *name;
215 xmlAttrPtr ptr;
217 TRACE("(%p, %s, %p)\n", iface, debugstr_w(strPropertyName), PropertyValue);
219 if (!PropertyValue)
220 return E_INVALIDARG;
222 VariantInit(PropertyValue);
223 V_BSTR(PropertyValue) = NULL;
225 if (!strPropertyName)
226 return E_INVALIDARG;
228 name = xmlChar_from_wchar(strPropertyName);
229 ptr = This->node->properties;
230 while (ptr)
232 if (!lstrcmpiA((LPSTR)name, (LPCSTR)ptr->name))
234 val = xmlNodeListGetString(ptr->doc, ptr->children, 1);
235 break;
238 ptr = ptr->next;
241 if (val)
243 V_VT(PropertyValue) = VT_BSTR;
244 V_BSTR(PropertyValue) = bstr_from_xmlChar(val);
247 HeapFree(GetProcessHeap(), 0, name);
248 xmlFree(val);
249 TRACE("returning %s\n", debugstr_w(V_BSTR(PropertyValue)));
250 return (val) ? S_OK : S_FALSE;
253 static HRESULT WINAPI xmlelem_removeAttribute(IXMLElement *iface, BSTR strPropertyName)
255 xmlelem *This = impl_from_IXMLElement(iface);
256 xmlChar *name;
257 xmlAttrPtr attr;
258 int res;
259 HRESULT hr = S_FALSE;
261 TRACE("(%p, %s)\n", iface, debugstr_w(strPropertyName));
263 if (!strPropertyName)
264 return E_INVALIDARG;
266 name = xmlChar_from_wchar(strPropertyName);
267 attr = xmlHasProp(This->node, name);
268 if (!attr)
269 goto done;
271 res = xmlRemoveProp(attr);
273 if (res == 0)
274 hr = S_OK;
276 done:
277 HeapFree(GetProcessHeap(), 0, name);
278 return hr;
281 static HRESULT WINAPI xmlelem_get_children(IXMLElement *iface, IXMLElementCollection **p)
283 xmlelem *This = impl_from_IXMLElement(iface);
285 TRACE("(%p, %p)\n", iface, p);
287 if (!p)
288 return E_INVALIDARG;
290 return XMLElementCollection_create((IUnknown *)iface, This->node->children, (LPVOID *)p);
293 static long type_libxml_to_msxml(xmlElementType type)
295 switch (type)
297 case XML_ELEMENT_NODE:
298 return XMLELEMTYPE_ELEMENT;
299 case XML_TEXT_NODE:
300 return XMLELEMTYPE_TEXT;
301 case XML_COMMENT_NODE:
302 return XMLELEMTYPE_COMMENT;
303 case XML_DOCUMENT_NODE:
304 return XMLELEMTYPE_DOCUMENT;
305 case XML_DTD_NODE:
306 return XMLELEMTYPE_DTD;
307 case XML_PI_NODE:
308 return XMLELEMTYPE_PI;
309 default:
310 break;
313 return XMLELEMTYPE_OTHER;
316 static HRESULT WINAPI xmlelem_get_type(IXMLElement *iface, long *p)
318 xmlelem *This = impl_from_IXMLElement(iface);
320 TRACE("(%p, %p)\n", This, p);
322 if (!p)
323 return E_INVALIDARG;
325 *p = type_libxml_to_msxml(This->node->type);
326 TRACE("returning %ld\n", *p);
327 return S_OK;
330 static HRESULT WINAPI xmlelem_get_text(IXMLElement *iface, BSTR *p)
332 xmlelem *This = impl_from_IXMLElement(iface);
333 xmlChar *content;
335 TRACE("(%p, %p)\n", iface, p);
337 if (!p)
338 return E_INVALIDARG;
340 content = xmlNodeGetContent(This->node);
341 *p = bstr_from_xmlChar(content);
342 TRACE("returning %s\n", debugstr_w(*p));
343 return S_OK;
346 static HRESULT WINAPI xmlelem_put_text(IXMLElement *iface, BSTR p)
348 xmlelem *This = impl_from_IXMLElement(iface);
349 xmlChar *content;
351 TRACE("(%p, %s)\n", iface, debugstr_w(p));
353 /* FIXME: test which types can be used */
354 if (This->node->type == XML_ELEMENT_NODE)
355 return E_NOTIMPL;
357 content = xmlChar_from_wchar(p);
358 xmlNodeSetContent(This->node, content);
360 HeapFree( GetProcessHeap(), 0, content);
362 return S_OK;
365 static HRESULT WINAPI xmlelem_addChild(IXMLElement *iface, IXMLElement *pChildElem,
366 long lIndex, long lreserved)
368 xmlelem *This = impl_from_IXMLElement(iface);
369 xmlelem *childElem = impl_from_IXMLElement(pChildElem);
370 xmlNodePtr child;
372 TRACE("(%p, %p, %ld, %ld)\n", iface, pChildElem, lIndex, lreserved);
374 if (lIndex == 0)
375 child = xmlAddChild(This->node, childElem->node);
376 else
377 child = xmlAddNextSibling(This->node, childElem->node->last);
379 return (child) ? S_OK : S_FALSE;
382 static HRESULT WINAPI xmlelem_removeChild(IXMLElement *iface, IXMLElement *pChildElem)
384 FIXME("(%p, %p): stub\n", iface, pChildElem);
385 return E_NOTIMPL;
388 static const struct IXMLElementVtbl xmlelem_vtbl =
390 xmlelem_QueryInterface,
391 xmlelem_AddRef,
392 xmlelem_Release,
393 xmlelem_GetTypeInfoCount,
394 xmlelem_GetTypeInfo,
395 xmlelem_GetIDsOfNames,
396 xmlelem_Invoke,
397 xmlelem_get_tagName,
398 xmlelem_put_tagName,
399 xmlelem_get_parent,
400 xmlelem_setAttribute,
401 xmlelem_getAttribute,
402 xmlelem_removeAttribute,
403 xmlelem_get_children,
404 xmlelem_get_type,
405 xmlelem_get_text,
406 xmlelem_put_text,
407 xmlelem_addChild,
408 xmlelem_removeChild
411 HRESULT XMLElement_create(IUnknown *pUnkOuter, xmlNodePtr node, LPVOID *ppObj)
413 xmlelem *elem;
415 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
417 if (!ppObj)
418 return E_INVALIDARG;
420 *ppObj = NULL;
422 elem = HeapAlloc(GetProcessHeap(), 0, sizeof (*elem));
423 if(!elem)
424 return E_OUTOFMEMORY;
426 elem->lpVtbl = &xmlelem_vtbl;
427 elem->ref = 1;
428 elem->node = node;
430 *ppObj = &elem->lpVtbl;
432 TRACE("returning iface %p\n", *ppObj);
433 return S_OK;
436 /************************************************************************
437 * IXMLElementCollection
439 typedef struct _xmlelem_collection
441 const IXMLElementCollectionVtbl *lpVtbl;
442 const IEnumVARIANTVtbl *lpvtblIEnumVARIANT;
443 LONG ref;
444 LONG length;
445 xmlNodePtr node;
447 /* IEnumVARIANT members */
448 xmlNodePtr current;
449 } xmlelem_collection;
451 static inline xmlelem_collection *impl_from_IXMLElementCollection(IXMLElementCollection *iface)
453 return (xmlelem_collection *)((char*)iface - FIELD_OFFSET(xmlelem_collection, lpVtbl));
456 static inline xmlelem_collection *impl_from_IEnumVARIANT(IEnumVARIANT *iface)
458 return (xmlelem_collection *)((char*)iface - FIELD_OFFSET(xmlelem_collection, lpvtblIEnumVARIANT));
461 static HRESULT WINAPI xmlelem_collection_QueryInterface(IXMLElementCollection *iface, REFIID riid, void** ppvObject)
463 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
465 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
467 if (IsEqualGUID(riid, &IID_IUnknown) ||
468 IsEqualGUID(riid, &IID_IXMLElementCollection))
470 *ppvObject = iface;
472 else if (IsEqualGUID(riid, &IID_IEnumVARIANT))
474 *ppvObject = (IEnumVARIANT *)&(This->lpvtblIEnumVARIANT);
476 else
478 FIXME("interface %s not implemented\n", debugstr_guid(riid));
479 return E_NOINTERFACE;
482 IXMLElementCollection_AddRef(iface);
484 return S_OK;
487 static ULONG WINAPI xmlelem_collection_AddRef(IXMLElementCollection *iface)
489 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
490 TRACE("%p\n", This);
491 return InterlockedIncrement(&This->ref);
494 static ULONG WINAPI xmlelem_collection_Release(IXMLElementCollection *iface)
496 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
497 LONG ref;
499 TRACE("%p\n", This);
501 ref = InterlockedDecrement(&This->ref);
502 if (ref == 0)
504 HeapFree(GetProcessHeap(), 0, This);
507 return ref;
510 static HRESULT WINAPI xmlelem_collection_GetTypeInfoCount(IXMLElementCollection *iface, UINT* pctinfo)
512 FIXME("\n");
513 return E_NOTIMPL;
516 static HRESULT WINAPI xmlelem_collection_GetTypeInfo(IXMLElementCollection *iface, UINT iTInfo,
517 LCID lcid, ITypeInfo** ppTInfo)
519 FIXME("\n");
520 return E_NOTIMPL;
523 static HRESULT WINAPI xmlelem_collection_GetIDsOfNames(IXMLElementCollection *iface, REFIID riid,
524 LPOLESTR* rgszNames, UINT cNames,
525 LCID lcid, DISPID* rgDispId)
527 FIXME("\n");
528 return E_NOTIMPL;
531 static HRESULT WINAPI xmlelem_collection_Invoke(IXMLElementCollection *iface, DISPID dispIdMember,
532 REFIID riid, LCID lcid, WORD wFlags,
533 DISPPARAMS* pDispParams, VARIANT* pVarResult,
534 EXCEPINFO* pExcepInfo, UINT* puArgErr)
536 FIXME("\n");
537 return E_NOTIMPL;
540 static HRESULT WINAPI xmlelem_collection_put_length(IXMLElementCollection *iface, long v)
542 TRACE("(%p, %ld)\n", iface, v);
543 return E_FAIL;
546 static HRESULT WINAPI xmlelem_collection_get_length(IXMLElementCollection *iface, long *p)
548 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
550 TRACE("(%p, %p)\n", iface, p);
552 if (!p)
553 return E_INVALIDARG;
555 *p = This->length;
556 return S_OK;
559 static HRESULT WINAPI xmlelem_collection_get__newEnum(IXMLElementCollection *iface, IUnknown **ppUnk)
561 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
563 TRACE("(%p, %p)\n", iface, ppUnk);
565 if (!ppUnk)
566 return E_INVALIDARG;
568 *ppUnk = (IUnknown *)This;
569 IUnknown_AddRef(*ppUnk);
570 return S_OK;
573 static HRESULT WINAPI xmlelem_collection_item(IXMLElementCollection *iface, VARIANT var1,
574 VARIANT var2, IDispatch **ppDisp)
576 xmlelem_collection *This = impl_from_IXMLElementCollection(iface);
577 xmlNodePtr ptr = This->node;
578 int index, i;
580 TRACE("(%p, %p)\n", iface, ppDisp);
582 if (!ppDisp)
583 return E_INVALIDARG;
585 *ppDisp = NULL;
587 index = V_I4(&var1);
588 if (index < 0)
589 return E_INVALIDARG;
590 if (index >= This->length)
591 return E_FAIL;
593 for (i = 0; i < index; i++)
594 ptr = ptr->next;
596 return XMLElement_create((IUnknown *)iface, ptr, (LPVOID *)ppDisp);
599 static const struct IXMLElementCollectionVtbl xmlelem_collection_vtbl =
601 xmlelem_collection_QueryInterface,
602 xmlelem_collection_AddRef,
603 xmlelem_collection_Release,
604 xmlelem_collection_GetTypeInfoCount,
605 xmlelem_collection_GetTypeInfo,
606 xmlelem_collection_GetIDsOfNames,
607 xmlelem_collection_Invoke,
608 xmlelem_collection_put_length,
609 xmlelem_collection_get_length,
610 xmlelem_collection_get__newEnum,
611 xmlelem_collection_item
614 /************************************************************************
615 * xmlelem_collection implementation of IEnumVARIANT.
617 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_QueryInterface(
618 IEnumVARIANT *iface, REFIID riid, LPVOID *ppvObj)
620 xmlelem_collection *this = impl_from_IEnumVARIANT(iface);
621 return IXMLDocument_QueryInterface((IXMLDocument *)this, riid, ppvObj);
624 static ULONG WINAPI xmlelem_collection_IEnumVARIANT_AddRef(
625 IEnumVARIANT *iface)
627 xmlelem_collection *this = impl_from_IEnumVARIANT(iface);
628 return IXMLDocument_AddRef((IXMLDocument *)this);
631 static ULONG WINAPI xmlelem_collection_IEnumVARIANT_Release(
632 IEnumVARIANT *iface)
634 xmlelem_collection *this = impl_from_IEnumVARIANT(iface);
635 return IXMLDocument_Release((IXMLDocument *)this);
638 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Next(
639 IEnumVARIANT *iface, ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
641 xmlelem_collection *This = impl_from_IEnumVARIANT(iface);
642 xmlNodePtr ptr = This->current;
644 TRACE("(%p, %d, %p, %p)\n", iface, celt, rgVar, pCeltFetched);
646 if (!rgVar)
647 return E_INVALIDARG;
649 /* FIXME: handle celt */
650 if (pCeltFetched)
651 *pCeltFetched = 1;
653 This->current = This->current->next;
655 V_VT(rgVar) = VT_DISPATCH;
656 return XMLElement_create((IUnknown *)iface, ptr, (LPVOID *)&V_DISPATCH(rgVar));
659 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Skip(
660 IEnumVARIANT *iface, ULONG celt)
662 FIXME("(%p, %d): stub\n", iface, celt);
663 return E_NOTIMPL;
666 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Reset(
667 IEnumVARIANT *iface)
669 xmlelem_collection *This = impl_from_IEnumVARIANT(iface);
670 This->current = This->node;
671 return S_OK;
674 static HRESULT WINAPI xmlelem_collection_IEnumVARIANT_Clone(
675 IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
677 FIXME("(%p, %p): stub\n", iface, ppEnum);
678 return E_NOTIMPL;
681 static const struct IEnumVARIANTVtbl xmlelem_collection_IEnumVARIANTvtbl =
683 xmlelem_collection_IEnumVARIANT_QueryInterface,
684 xmlelem_collection_IEnumVARIANT_AddRef,
685 xmlelem_collection_IEnumVARIANT_Release,
686 xmlelem_collection_IEnumVARIANT_Next,
687 xmlelem_collection_IEnumVARIANT_Skip,
688 xmlelem_collection_IEnumVARIANT_Reset,
689 xmlelem_collection_IEnumVARIANT_Clone
692 HRESULT XMLElementCollection_create(IUnknown *pUnkOuter, xmlNodePtr node, LPVOID *ppObj)
694 xmlelem_collection *collection;
695 xmlNodePtr ptr;
697 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
699 *ppObj = NULL;
701 if (!node)
702 return S_FALSE;
704 collection = HeapAlloc(GetProcessHeap(), 0, sizeof (*collection));
705 if(!collection)
706 return E_OUTOFMEMORY;
708 collection->lpVtbl = &xmlelem_collection_vtbl;
709 collection->lpvtblIEnumVARIANT = &xmlelem_collection_IEnumVARIANTvtbl;
710 collection->ref = 1;
711 collection->length = 0;
712 collection->node = node;
713 collection->current = node;
715 ptr = node;
716 while (ptr)
718 collection->length++;
719 ptr = ptr->next;
722 *ppObj = &collection->lpVtbl;
724 TRACE("returning iface %p\n", *ppObj);
725 return S_OK;
728 #endif