msxml: Allow the element implementation to be aggregatable.
[wine.git] / dlls / msxml3 / element.c
blob7997b2fb409aa6bead57ccc4de3826ba7b673f8f
1 /*
2 * DOM Document implementation
4 * Copyright 2005 Mike McCormack
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 "winnls.h"
30 #include "ole2.h"
31 #include "msxml2.h"
33 #include "msxml_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
39 #ifdef HAVE_LIBXML2
41 typedef struct _domelem
43 const struct IXMLDOMElementVtbl *lpVtbl;
44 const struct IUnknownVtbl *lpInternalUnkVtbl;
45 IUnknown *pUnkOuter;
46 LONG ref;
47 IUnknown *node_unk;
48 IXMLDOMNode *node;
49 } domelem;
51 static inline domelem *impl_from_IXMLDOMElement( IXMLDOMElement *iface )
53 return (domelem *)((char*)iface - FIELD_OFFSET(domelem, lpVtbl));
56 static inline domelem *impl_from_InternalUnknown( IUnknown *iface )
58 return (domelem *)((char*)iface - FIELD_OFFSET(domelem, lpInternalUnkVtbl));
61 static inline xmlNodePtr get_element( domelem *This )
63 return xmlNodePtr_from_domnode( This->node, XML_ELEMENT_NODE );
66 static HRESULT WINAPI domelem_QueryInterface(
67 IXMLDOMElement *iface,
68 REFIID riid,
69 void** ppvObject )
71 domelem *This = impl_from_IXMLDOMElement( iface );
72 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
74 return IUnknown_QueryInterface(This->pUnkOuter, riid, ppvObject);
77 static ULONG WINAPI domelem_AddRef(
78 IXMLDOMElement *iface )
80 domelem *This = impl_from_IXMLDOMElement( iface );
81 return IUnknown_AddRef(This->pUnkOuter);
84 static ULONG WINAPI domelem_Release(
85 IXMLDOMElement *iface )
87 domelem *This = impl_from_IXMLDOMElement( iface );
88 return IUnknown_Release(This->pUnkOuter);
91 static HRESULT WINAPI domelem_GetTypeInfoCount(
92 IXMLDOMElement *iface,
93 UINT* pctinfo )
95 FIXME("\n");
96 return E_NOTIMPL;
99 static HRESULT WINAPI domelem_GetTypeInfo(
100 IXMLDOMElement *iface,
101 UINT iTInfo, LCID lcid,
102 ITypeInfo** ppTInfo )
104 FIXME("\n");
105 return E_NOTIMPL;
108 static HRESULT WINAPI domelem_GetIDsOfNames(
109 IXMLDOMElement *iface,
110 REFIID riid, LPOLESTR* rgszNames,
111 UINT cNames, LCID lcid, DISPID* rgDispId )
113 FIXME("\n");
114 return E_NOTIMPL;
117 static HRESULT WINAPI domelem_Invoke(
118 IXMLDOMElement *iface,
119 DISPID dispIdMember, REFIID riid, LCID lcid,
120 WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
121 EXCEPINFO* pExcepInfo, UINT* puArgErr )
123 FIXME("\n");
124 return E_NOTIMPL;
127 static HRESULT WINAPI domelem_get_nodeName(
128 IXMLDOMElement *iface,
129 BSTR* p )
131 domelem *This = impl_from_IXMLDOMElement( iface );
132 return IXMLDOMNode_get_nodeName( This->node, p );
135 static HRESULT WINAPI domelem_get_nodeValue(
136 IXMLDOMElement *iface,
137 VARIANT* var1 )
139 domelem *This = impl_from_IXMLDOMElement( iface );
140 return IXMLDOMNode_get_nodeValue( This->node, var1 );
143 static HRESULT WINAPI domelem_put_nodeValue(
144 IXMLDOMElement *iface,
145 VARIANT var1 )
147 domelem *This = impl_from_IXMLDOMElement( iface );
148 return IXMLDOMNode_put_nodeValue( This->node, var1 );
151 static HRESULT WINAPI domelem_get_nodeType(
152 IXMLDOMElement *iface,
153 DOMNodeType* domNodeType )
155 domelem *This = impl_from_IXMLDOMElement( iface );
156 return IXMLDOMNode_get_nodeType( This->node, domNodeType );
159 static HRESULT WINAPI domelem_get_parentNode(
160 IXMLDOMElement *iface,
161 IXMLDOMNode** parent )
163 domelem *This = impl_from_IXMLDOMElement( iface );
164 return IXMLDOMNode_get_parentNode( This->node, parent );
167 static HRESULT WINAPI domelem_get_childNodes(
168 IXMLDOMElement *iface,
169 IXMLDOMNodeList** outList)
171 domelem *This = impl_from_IXMLDOMElement( iface );
172 return IXMLDOMNode_get_childNodes( This->node, outList );
175 static HRESULT WINAPI domelem_get_firstChild(
176 IXMLDOMElement *iface,
177 IXMLDOMNode** domNode)
179 domelem *This = impl_from_IXMLDOMElement( iface );
180 return IXMLDOMNode_get_firstChild( This->node, domNode );
183 static HRESULT WINAPI domelem_get_lastChild(
184 IXMLDOMElement *iface,
185 IXMLDOMNode** domNode)
187 domelem *This = impl_from_IXMLDOMElement( iface );
188 return IXMLDOMNode_get_lastChild( This->node, domNode );
191 static HRESULT WINAPI domelem_get_previousSibling(
192 IXMLDOMElement *iface,
193 IXMLDOMNode** domNode)
195 domelem *This = impl_from_IXMLDOMElement( iface );
196 return IXMLDOMNode_get_previousSibling( This->node, domNode );
199 static HRESULT WINAPI domelem_get_nextSibling(
200 IXMLDOMElement *iface,
201 IXMLDOMNode** domNode)
203 domelem *This = impl_from_IXMLDOMElement( iface );
204 return IXMLDOMNode_get_nextSibling( This->node, domNode );
207 static HRESULT WINAPI domelem_get_attributes(
208 IXMLDOMElement *iface,
209 IXMLDOMNamedNodeMap** attributeMap)
211 domelem *This = impl_from_IXMLDOMElement( iface );
212 return IXMLDOMNode_get_attributes( This->node, attributeMap );
215 static HRESULT WINAPI domelem_insertBefore(
216 IXMLDOMElement *iface,
217 IXMLDOMNode* newNode, VARIANT var1,
218 IXMLDOMNode** outOldNode)
220 domelem *This = impl_from_IXMLDOMElement( iface );
221 return IXMLDOMNode_insertBefore( This->node, newNode, var1, outOldNode );
224 static HRESULT WINAPI domelem_replaceChild(
225 IXMLDOMElement *iface,
226 IXMLDOMNode* newNode,
227 IXMLDOMNode* oldNode,
228 IXMLDOMNode** outOldNode)
230 domelem *This = impl_from_IXMLDOMElement( iface );
231 return IXMLDOMNode_replaceChild( This->node, newNode, oldNode, outOldNode );
234 static HRESULT WINAPI domelem_removeChild(
235 IXMLDOMElement *iface,
236 IXMLDOMNode* domNode, IXMLDOMNode** oldNode)
238 domelem *This = impl_from_IXMLDOMElement( iface );
239 return IXMLDOMNode_removeChild( This->node, domNode, oldNode );
242 static HRESULT WINAPI domelem_appendChild(
243 IXMLDOMElement *iface,
244 IXMLDOMNode* newNode, IXMLDOMNode** outNewNode)
246 domelem *This = impl_from_IXMLDOMElement( iface );
247 return IXMLDOMNode_appendChild( This->node, newNode, outNewNode );
250 static HRESULT WINAPI domelem_hasChildNodes(
251 IXMLDOMElement *iface,
252 VARIANT_BOOL* pbool)
254 domelem *This = impl_from_IXMLDOMElement( iface );
255 return IXMLDOMNode_hasChildNodes( This->node, pbool );
258 static HRESULT WINAPI domelem_get_ownerDocument(
259 IXMLDOMElement *iface,
260 IXMLDOMDocument** domDocument)
262 domelem *This = impl_from_IXMLDOMElement( iface );
263 return IXMLDOMNode_get_ownerDocument( This->node, domDocument );
266 static HRESULT WINAPI domelem_cloneNode(
267 IXMLDOMElement *iface,
268 VARIANT_BOOL pbool, IXMLDOMNode** outNode)
270 domelem *This = impl_from_IXMLDOMElement( iface );
271 return IXMLDOMNode_cloneNode( This->node, pbool, outNode );
274 static HRESULT WINAPI domelem_get_nodeTypeString(
275 IXMLDOMElement *iface,
276 BSTR* p)
278 domelem *This = impl_from_IXMLDOMElement( iface );
279 return IXMLDOMNode_get_nodeTypeString( This->node, p );
282 static HRESULT WINAPI domelem_get_text(
283 IXMLDOMElement *iface,
284 BSTR* p)
286 domelem *This = impl_from_IXMLDOMElement( iface );
287 return IXMLDOMNode_get_text( This->node, p );
290 static HRESULT WINAPI domelem_put_text(
291 IXMLDOMElement *iface,
292 BSTR p)
294 domelem *This = impl_from_IXMLDOMElement( iface );
295 return IXMLDOMNode_put_text( This->node, p );
298 static HRESULT WINAPI domelem_get_specified(
299 IXMLDOMElement *iface,
300 VARIANT_BOOL* pbool)
302 domelem *This = impl_from_IXMLDOMElement( iface );
303 return IXMLDOMNode_get_specified( This->node, pbool );
306 static HRESULT WINAPI domelem_get_definition(
307 IXMLDOMElement *iface,
308 IXMLDOMNode** domNode)
310 domelem *This = impl_from_IXMLDOMElement( iface );
311 return IXMLDOMNode_get_definition( This->node, domNode );
314 static HRESULT WINAPI domelem_get_nodeTypedValue(
315 IXMLDOMElement *iface,
316 VARIANT* var1)
318 domelem *This = impl_from_IXMLDOMElement( iface );
319 return IXMLDOMNode_get_nodeTypedValue( This->node, var1 );
322 static HRESULT WINAPI domelem_put_nodeTypedValue(
323 IXMLDOMElement *iface,
324 VARIANT var1)
326 domelem *This = impl_from_IXMLDOMElement( iface );
327 return IXMLDOMNode_put_nodeTypedValue( This->node, var1 );
330 static HRESULT WINAPI domelem_get_dataType(
331 IXMLDOMElement *iface,
332 VARIANT* var1)
334 domelem *This = impl_from_IXMLDOMElement( iface );
335 return IXMLDOMNode_get_dataType( This->node, var1 );
338 static HRESULT WINAPI domelem_put_dataType(
339 IXMLDOMElement *iface,
340 BSTR p)
342 domelem *This = impl_from_IXMLDOMElement( iface );
343 return IXMLDOMNode_put_dataType( This->node, p );
346 static HRESULT WINAPI domelem_get_xml(
347 IXMLDOMElement *iface,
348 BSTR* p)
350 domelem *This = impl_from_IXMLDOMElement( iface );
351 return IXMLDOMNode_get_xml( This->node, p );
354 static HRESULT WINAPI domelem_transformNode(
355 IXMLDOMElement *iface,
356 IXMLDOMNode* domNode, BSTR* p)
358 domelem *This = impl_from_IXMLDOMElement( iface );
359 return IXMLDOMNode_transformNode( This->node, domNode, p );
362 static HRESULT WINAPI domelem_selectNodes(
363 IXMLDOMElement *iface,
364 BSTR p, IXMLDOMNodeList** outList)
366 domelem *This = impl_from_IXMLDOMElement( iface );
367 return IXMLDOMNode_selectNodes( This->node, p, outList );
370 static HRESULT WINAPI domelem_selectSingleNode(
371 IXMLDOMElement *iface,
372 BSTR p, IXMLDOMNode** outNode)
374 domelem *This = impl_from_IXMLDOMElement( iface );
375 return IXMLDOMNode_selectSingleNode( This->node, p, outNode );
378 static HRESULT WINAPI domelem_get_parsed(
379 IXMLDOMElement *iface,
380 VARIANT_BOOL* pbool)
382 domelem *This = impl_from_IXMLDOMElement( iface );
383 return IXMLDOMNode_get_parsed( This->node, pbool );
386 static HRESULT WINAPI domelem_get_namespaceURI(
387 IXMLDOMElement *iface,
388 BSTR* p)
390 domelem *This = impl_from_IXMLDOMElement( iface );
391 return IXMLDOMNode_get_namespaceURI( This->node, p );
394 static HRESULT WINAPI domelem_get_prefix(
395 IXMLDOMElement *iface,
396 BSTR* p)
398 domelem *This = impl_from_IXMLDOMElement( iface );
399 return IXMLDOMNode_get_prefix( This->node, p );
402 static HRESULT WINAPI domelem_get_baseName(
403 IXMLDOMElement *iface,
404 BSTR* p)
406 domelem *This = impl_from_IXMLDOMElement( iface );
407 return IXMLDOMNode_get_baseName( This->node, p );
410 static HRESULT WINAPI domelem_transformNodeToObject(
411 IXMLDOMElement *iface,
412 IXMLDOMNode* domNode, VARIANT var1)
414 domelem *This = impl_from_IXMLDOMElement( iface );
415 return IXMLDOMNode_transformNodeToObject( This->node, domNode, var1 );
418 static HRESULT WINAPI domelem_get_tagName(
419 IXMLDOMElement *iface,
420 BSTR* p)
422 domelem *This = impl_from_IXMLDOMElement( iface );
423 xmlNodePtr element;
424 DWORD len;
425 DWORD offset = 0;
426 LPWSTR str;
428 TRACE("%p\n", This );
430 if ( !This->node )
431 return E_FAIL;
433 element = get_element( This );
434 if ( !element )
435 return E_FAIL;
437 len = MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) element->name, -1, NULL, 0 );
438 if (element->ns)
439 len += MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) element->ns->prefix, -1, NULL, 0 );
440 str = (LPWSTR) HeapAlloc( GetProcessHeap(), 0, len * sizeof (WCHAR) );
441 if ( !str )
442 return E_OUTOFMEMORY;
443 if (element->ns)
445 offset = MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) element->ns->prefix, -1, str, len );
446 str[offset - 1] = ':';
448 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) element->name, -1, str + offset, len - offset );
449 *p = SysAllocString( str );
450 HeapFree( GetProcessHeap(), 0, str );
452 return S_OK;
455 static HRESULT WINAPI domelem_getAttribute(
456 IXMLDOMElement *iface,
457 BSTR name, VARIANT* value)
459 domelem *This = impl_from_IXMLDOMElement( iface );
460 xmlNodePtr element;
461 xmlChar *xml_name, *xml_value;
462 HRESULT hr = E_FAIL;
464 TRACE("(%p)->(%s,%p)\n", This, debugstr_w(name), value);
466 element = get_element( This );
467 if ( !element )
468 return E_FAIL;
470 VariantInit(value);
471 xml_name = xmlChar_from_wchar( name );
472 xml_value = xmlGetNsProp(element, xml_name, NULL);
473 HeapFree(GetProcessHeap(), 0, xml_name);
474 if(xml_value)
476 V_VT(value) = VT_BSTR;
477 V_BSTR(value) = bstr_from_xmlChar( xml_value );
478 xmlFree(xml_value);
479 hr = S_OK;
482 return hr;
485 static HRESULT WINAPI domelem_setAttribute(
486 IXMLDOMElement *iface,
487 BSTR name, VARIANT value)
489 domelem *This = impl_from_IXMLDOMElement( iface );
490 xmlNodePtr element;
491 xmlChar *xml_name, *xml_value;
492 HRESULT hr;
493 VARIANT var;
495 TRACE("(%p)->(%s, var)\n", This, debugstr_w(name));
497 element = get_element( This );
498 if ( !element )
499 return E_FAIL;
501 VariantInit(&var);
502 hr = VariantChangeType(&var, &value, 0, VT_BSTR);
503 if(hr != S_OK)
505 FIXME("VariantChangeType failed\n");
506 return hr;
509 xml_name = xmlChar_from_wchar( name );
510 xml_value = xmlChar_from_wchar( V_BSTR(&var) );
512 if(!xmlSetNsProp(element, NULL, xml_name, xml_value))
513 hr = E_FAIL;
515 HeapFree(GetProcessHeap(), 0, xml_value);
516 HeapFree(GetProcessHeap(), 0, xml_name);
517 VariantClear(&var);
519 return hr;
522 static HRESULT WINAPI domelem_removeAttribute(
523 IXMLDOMElement *iface,
524 BSTR p)
526 FIXME("\n");
527 return E_NOTIMPL;
530 static HRESULT WINAPI domelem_getAttributeNode(
531 IXMLDOMElement *iface,
532 BSTR p, IXMLDOMAttribute** attributeNode )
534 FIXME("\n");
535 return E_NOTIMPL;
538 static HRESULT WINAPI domelem_setAttributeNode(
539 IXMLDOMElement *iface,
540 IXMLDOMAttribute* domAttribute,
541 IXMLDOMAttribute** attributeNode)
543 FIXME("\n");
544 return E_NOTIMPL;
547 static HRESULT WINAPI domelem_removeAttributeNode(
548 IXMLDOMElement *iface,
549 IXMLDOMAttribute* domAttribute,
550 IXMLDOMAttribute** attributeNode)
552 FIXME("\n");
553 return E_NOTIMPL;
556 static HRESULT WINAPI domelem_getElementsByTagName(
557 IXMLDOMElement *iface,
558 BSTR p, IXMLDOMNodeList** resultList)
560 FIXME("\n");
561 return E_NOTIMPL;
564 static HRESULT WINAPI domelem_normalize(
565 IXMLDOMElement *iface )
567 FIXME("\n");
568 return E_NOTIMPL;
571 static const struct IXMLDOMElementVtbl domelem_vtbl =
573 domelem_QueryInterface,
574 domelem_AddRef,
575 domelem_Release,
576 domelem_GetTypeInfoCount,
577 domelem_GetTypeInfo,
578 domelem_GetIDsOfNames,
579 domelem_Invoke,
580 domelem_get_nodeName,
581 domelem_get_nodeValue,
582 domelem_put_nodeValue,
583 domelem_get_nodeType,
584 domelem_get_parentNode,
585 domelem_get_childNodes,
586 domelem_get_firstChild,
587 domelem_get_lastChild,
588 domelem_get_previousSibling,
589 domelem_get_nextSibling,
590 domelem_get_attributes,
591 domelem_insertBefore,
592 domelem_replaceChild,
593 domelem_removeChild,
594 domelem_appendChild,
595 domelem_hasChildNodes,
596 domelem_get_ownerDocument,
597 domelem_cloneNode,
598 domelem_get_nodeTypeString,
599 domelem_get_text,
600 domelem_put_text,
601 domelem_get_specified,
602 domelem_get_definition,
603 domelem_get_nodeTypedValue,
604 domelem_put_nodeTypedValue,
605 domelem_get_dataType,
606 domelem_put_dataType,
607 domelem_get_xml,
608 domelem_transformNode,
609 domelem_selectNodes,
610 domelem_selectSingleNode,
611 domelem_get_parsed,
612 domelem_get_namespaceURI,
613 domelem_get_prefix,
614 domelem_get_baseName,
615 domelem_transformNodeToObject,
616 domelem_get_tagName,
617 domelem_getAttribute,
618 domelem_setAttribute,
619 domelem_removeAttribute,
620 domelem_getAttributeNode,
621 domelem_setAttributeNode,
622 domelem_removeAttributeNode,
623 domelem_getElementsByTagName,
624 domelem_normalize,
627 static HRESULT WINAPI Internal_QueryInterface(
628 IUnknown *iface,
629 REFIID riid,
630 void** ppvObject )
632 domelem *This = impl_from_InternalUnknown( iface );
633 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
635 if ( IsEqualGUID( riid, &IID_IXMLDOMElement ) ||
636 IsEqualGUID( riid, &IID_IUnknown ) )
638 *ppvObject = &This->lpVtbl;
640 else if ( IsEqualGUID( riid, &IID_IDispatch ) ||
641 IsEqualGUID( riid, &IID_IXMLDOMNode ) )
643 return IUnknown_QueryInterface(This->node_unk, riid, ppvObject);
645 else
647 FIXME("interface %s not implemented\n", debugstr_guid(riid));
648 return E_NOINTERFACE;
651 IUnknown_AddRef( (IUnknown*)*ppvObject );
653 return S_OK;
656 static ULONG WINAPI Internal_AddRef(
657 IUnknown *iface )
659 domelem *This = impl_from_InternalUnknown( iface );
660 return InterlockedIncrement( &This->ref );
663 static ULONG WINAPI Internal_Release(
664 IUnknown *iface )
666 domelem *This = impl_from_InternalUnknown( iface );
667 ULONG ref;
669 ref = InterlockedDecrement( &This->ref );
670 if ( ref == 0 )
672 IUnknown_Release( This->node_unk );
673 HeapFree( GetProcessHeap(), 0, This );
676 return ref;
679 static const struct IUnknownVtbl internal_unk_vtbl =
681 Internal_QueryInterface,
682 Internal_AddRef,
683 Internal_Release
686 IUnknown* create_element( xmlNodePtr element, IUnknown *pUnkOuter )
688 domelem *This;
689 HRESULT hr;
691 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
692 if ( !This )
693 return NULL;
695 This->lpVtbl = &domelem_vtbl;
696 This->ref = 1;
697 This->lpInternalUnkVtbl = &internal_unk_vtbl;
699 if(pUnkOuter)
700 This->pUnkOuter = pUnkOuter; /* Don't take a ref on outer Unknown */
701 else
702 This->pUnkOuter = (IUnknown *)&This->lpInternalUnkVtbl;
704 This->node_unk = create_basic_node( element, (IUnknown*)&This->lpVtbl );
705 if(!This->node_unk)
707 HeapFree(GetProcessHeap(), 0, This);
708 return NULL;
711 hr = IUnknown_QueryInterface(This->node_unk, &IID_IXMLDOMNode, (LPVOID*)&This->node);
712 if(FAILED(hr))
714 IUnknown_Release(This->node_unk);
715 HeapFree( GetProcessHeap(), 0, This );
716 return NULL;
718 /* The ref on This->node is actually looped back into this object, so release it */
719 IXMLDOMNode_Release(This->node);
721 return (IUnknown*) &This->lpInternalUnkVtbl;
724 #endif