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
34 #include "msxml_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
42 static inline xmlnode *impl_from_InternalUnknown( IUnknown *iface )
44 return (xmlnode *)((char*)iface - FIELD_OFFSET(xmlnode, lpInternalUnkVtbl));
47 xmlNodePtr xmlNodePtr_from_domnode( IXMLDOMNode *iface, xmlElementType type )
53 This = impl_from_IXMLDOMNode( iface );
56 if ( type && This->node->type != type )
61 void attach_xmlnode( IXMLDOMNode *node, xmlNodePtr xml )
63 xmlnode *This = impl_from_IXMLDOMNode( node );
66 xmldoc_release(This->node->doc);
70 xmldoc_add_ref(This->node->doc);
75 static HRESULT WINAPI xmlnode_QueryInterface(
80 xmlnode *This = impl_from_IXMLDOMNode( iface );
81 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
83 return IUnknown_QueryInterface(This->pUnkOuter, riid, ppvObject);
86 static ULONG WINAPI xmlnode_AddRef(
89 xmlnode *This = impl_from_IXMLDOMNode( iface );
90 return IUnknown_AddRef(This->pUnkOuter);
93 static ULONG WINAPI xmlnode_Release(
96 xmlnode *This = impl_from_IXMLDOMNode( iface );
97 return IUnknown_Release(This->pUnkOuter);
100 static HRESULT WINAPI xmlnode_GetTypeInfoCount(
108 static HRESULT WINAPI xmlnode_GetTypeInfo(
112 ITypeInfo** ppTInfo )
118 static HRESULT WINAPI xmlnode_GetIDsOfNames(
130 static HRESULT WINAPI xmlnode_Invoke(
136 DISPPARAMS* pDispParams,
138 EXCEPINFO* pExcepInfo,
145 static HRESULT WINAPI xmlnode_get_nodeName(
149 xmlnode *This = impl_from_IXMLDOMNode( iface );
152 TRACE("%p\n", This );
160 switch( This->node->type )
162 case XML_CDATA_SECTION_NODE:
163 str = (const xmlChar*) "#cdata-section";
165 case XML_COMMENT_NODE:
166 str = (const xmlChar*) "#comment";
168 case XML_DOCUMENT_FRAG_NODE:
169 str = (const xmlChar*) "#document-fragment";
172 str = (const xmlChar*) "#text";
174 case XML_DOCUMENT_NODE:
175 str = (const xmlChar*) "#document";
177 case XML_ATTRIBUTE_NODE:
178 case XML_ELEMENT_NODE:
179 str = This->node->name;
182 FIXME("nodeName not mapped correctly (%d)\n", This->node->type);
183 str = This->node->name;
187 *name = bstr_from_xmlChar( str );
194 BSTR bstr_from_xmlChar( const xmlChar *buf )
203 len = MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) buf, -1, NULL, 0 );
204 str = (LPWSTR) HeapAlloc( GetProcessHeap(), 0, len * sizeof (WCHAR) );
207 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) buf, -1, str, len );
208 bstr = SysAllocString( str );
209 HeapFree( GetProcessHeap(), 0, str );
213 static HRESULT WINAPI xmlnode_get_nodeValue(
217 xmlnode *This = impl_from_IXMLDOMNode( iface );
220 TRACE("%p %p\n", This, value);
225 V_BSTR(value) = NULL;
226 V_VT(value) = VT_NULL;
228 switch ( This->node->type )
230 case XML_ATTRIBUTE_NODE:
232 xmlChar *content = xmlNodeGetContent(This->node);
233 V_VT(value) = VT_BSTR;
234 V_BSTR(value) = bstr_from_xmlChar( content );
240 V_VT(value) = VT_BSTR;
241 V_BSTR(value) = bstr_from_xmlChar( This->node->content );
244 case XML_ELEMENT_NODE:
245 case XML_DOCUMENT_NODE:
246 /* these seem to return NULL */
250 FIXME("node %p type %d\n", This, This->node->type);
253 TRACE("%p returned %s\n", This, debugstr_w( V_BSTR(value) ) );
258 static HRESULT WINAPI xmlnode_put_nodeValue(
266 static HRESULT WINAPI xmlnode_get_nodeType(
270 xmlnode *This = impl_from_IXMLDOMNode( iface );
272 TRACE("%p %p\n", This, type);
274 assert( NODE_ELEMENT == XML_ELEMENT_NODE );
275 assert( NODE_NOTATION == XML_NOTATION_NODE );
277 *type = This->node->type;
282 static HRESULT get_node(
288 TRACE("%p->%s %p\n", This, name, node );
292 *out = create_node( node );
298 static HRESULT WINAPI xmlnode_get_parentNode(
300 IXMLDOMNode** parent)
302 xmlnode *This = impl_from_IXMLDOMNode( iface );
303 return get_node( This, "parent", This->node->parent, parent );
306 static HRESULT WINAPI xmlnode_get_childNodes(
308 IXMLDOMNodeList** childList)
310 xmlnode *This = impl_from_IXMLDOMNode( iface );
312 TRACE("%p %p\n", This, childList );
317 *childList = create_children_nodelist(This->node);
318 if (*childList == NULL)
319 return E_OUTOFMEMORY;
324 static HRESULT WINAPI xmlnode_get_firstChild(
326 IXMLDOMNode** firstChild)
328 xmlnode *This = impl_from_IXMLDOMNode( iface );
329 return get_node( This, "firstChild", This->node->children, firstChild );
332 static HRESULT WINAPI xmlnode_get_lastChild(
334 IXMLDOMNode** lastChild)
336 xmlnode *This = impl_from_IXMLDOMNode( iface );
338 TRACE("%p\n", This );
343 switch( This->node->type )
345 /* CDATASection, Comment, PI and Text Nodes do not support lastChild */
347 case XML_CDATA_SECTION_NODE:
349 case XML_COMMENT_NODE:
353 return get_node( This, "lastChild", This->node->last, lastChild );
357 static HRESULT WINAPI xmlnode_get_previousSibling(
359 IXMLDOMNode** previousSibling)
361 xmlnode *This = impl_from_IXMLDOMNode( iface );
363 TRACE("%p\n", This );
365 if (!previousSibling)
368 switch( This->node->type )
370 /* Attribute, Document and Document Fragment Nodes do not support previousSibling */
371 case XML_DOCUMENT_NODE:
372 case XML_DOCUMENT_FRAG_NODE:
373 case XML_ATTRIBUTE_NODE:
374 *previousSibling = NULL;
377 return get_node( This, "previous", This->node->prev, previousSibling );
381 static HRESULT WINAPI xmlnode_get_nextSibling(
383 IXMLDOMNode** nextSibling)
385 xmlnode *This = impl_from_IXMLDOMNode( iface );
387 TRACE("%p\n", This );
392 switch( This->node->type )
394 /* Attribute, Document and Document Fragment Nodes do not support nextSibling */
395 case XML_DOCUMENT_NODE:
396 case XML_DOCUMENT_FRAG_NODE:
397 case XML_ATTRIBUTE_NODE:
401 return get_node( This, "next", This->node->next, nextSibling );
405 static HRESULT WINAPI xmlnode_get_attributes(
407 IXMLDOMNamedNodeMap** attributeMap)
409 xmlnode *This = impl_from_IXMLDOMNode( iface );
415 switch( This->node->type )
417 /* Attribute, CDataSection, Comment, Documents, Documents Fragments,
418 Entity and Text Nodes does not support get_attributes */
419 case XML_ATTRIBUTE_NODE:
420 case XML_CDATA_SECTION_NODE:
421 case XML_COMMENT_NODE:
422 case XML_DOCUMENT_NODE:
423 case XML_DOCUMENT_FRAG_NODE:
424 case XML_ENTITY_NODE:
426 *attributeMap = NULL;
429 *attributeMap = create_nodemap( iface );
434 static HRESULT WINAPI xmlnode_insertBefore(
436 IXMLDOMNode* newChild,
438 IXMLDOMNode** outNewChild)
440 xmlnode *This = impl_from_IXMLDOMNode( iface );
441 xmlNodePtr before_node, new_child_node;
442 IXMLDOMNode *before = NULL, *new;
445 TRACE("(%p)->(%p,var,%p)\n",This,newChild,outNewChild);
450 switch(V_VT(&refChild))
457 hr = IUnknown_QueryInterface(V_UNKNOWN(&refChild), &IID_IXMLDOMNode, (LPVOID)&before);
458 if(FAILED(hr)) return hr;
462 hr = IDispatch_QueryInterface(V_DISPATCH(&refChild), &IID_IXMLDOMNode, (LPVOID)&before);
463 if(FAILED(hr)) return hr;
467 FIXME("refChild var type %x\n", V_VT(&refChild));
471 IXMLDOMNode_QueryInterface(newChild, &IID_IXMLDOMNode, (LPVOID)&new);
472 new_child_node = impl_from_IXMLDOMNode(new)->node;
473 TRACE("new_child_node %p This->node %p\n", new_child_node, This->node);
477 before_node = impl_from_IXMLDOMNode(before)->node;
478 xmlAddPrevSibling(before_node, new_child_node);
479 IXMLDOMNode_Release(before);
483 xmlAddChild(This->node, new_child_node);
486 IXMLDOMNode_Release(new);
487 IXMLDOMNode_AddRef(newChild);
489 *outNewChild = newChild;
495 static HRESULT WINAPI xmlnode_replaceChild(
497 IXMLDOMNode* newChild,
498 IXMLDOMNode* oldChild,
499 IXMLDOMNode** outOldChild)
505 static HRESULT WINAPI xmlnode_removeChild(
507 IXMLDOMNode* childNode,
508 IXMLDOMNode** oldChild)
510 xmlnode *This = impl_from_IXMLDOMNode( iface );
511 xmlNode *ancestor, *child_node_ptr;
515 TRACE("%p->(%p, %p)\n", This, childNode, oldChild);
519 if(!childNode) return E_INVALIDARG;
521 hr = IXMLDOMNode_QueryInterface(childNode, &IID_IXMLDOMNode, (LPVOID)&child);
525 child_node_ptr = ancestor = impl_from_IXMLDOMNode(child)->node;
526 while(ancestor->parent)
528 if(ancestor->parent == This->node)
530 ancestor = ancestor->parent;
532 if(!ancestor->parent)
534 WARN("childNode %p is not a child of %p\n", childNode, iface);
535 IXMLDOMNode_Release(child);
539 xmlUnlinkNode(child_node_ptr);
541 IXMLDOMNode_Release(child);
542 IXMLDOMNode_AddRef(childNode);
543 *oldChild = childNode;
547 static HRESULT WINAPI xmlnode_appendChild(
549 IXMLDOMNode* newChild,
550 IXMLDOMNode** outNewChild)
552 xmlnode *This = impl_from_IXMLDOMNode( iface );
553 IXMLDOMNode *pAttr = NULL;
556 TRACE("(%p)->(%p,%p)\n", This, newChild, outNewChild);
558 /* Cannot Append an Attribute node. */
559 IUnknown_QueryInterface(newChild, &IID_IXMLDOMNode, (LPVOID*)&pAttr);
562 xmlnode *ThisNew = impl_from_IXMLDOMNode( pAttr );
564 if(ThisNew->node->type == XML_ATTRIBUTE_NODE)
566 if(outNewChild) *outNewChild = NULL;
568 IUnknown_Release(pAttr);
573 IUnknown_Release(pAttr);
577 return IXMLDOMNode_insertBefore(iface, newChild, var, outNewChild);
580 static HRESULT WINAPI xmlnode_hasChildNodes(
582 VARIANT_BOOL* hasChild)
584 xmlnode *This = impl_from_IXMLDOMNode( iface );
590 if (!This->node->children)
592 *hasChild = VARIANT_FALSE;
596 *hasChild = VARIANT_TRUE;
600 static HRESULT WINAPI xmlnode_get_ownerDocument(
602 IXMLDOMDocument** DOMDocument)
608 static HRESULT WINAPI xmlnode_cloneNode(
611 IXMLDOMNode** cloneRoot)
613 xmlnode *This = impl_from_IXMLDOMNode( iface );
614 xmlNodePtr pClone = NULL;
615 IXMLDOMNode *pNode = NULL;
617 TRACE("%p (%d)\n", This, deep);
622 pClone = xmlCopyNode(This->node, deep ? 1 : 2);
625 pClone->doc = This->node->doc;
627 pNode = create_node(pClone);
630 ERR("Copy failed\n");
638 ERR("Copy failed\n");
645 static HRESULT WINAPI xmlnode_get_nodeTypeString(
653 static HRESULT WINAPI xmlnode_get_text(
657 xmlnode *This = impl_from_IXMLDOMNode( iface );
665 switch(This->node->type)
667 case XML_ELEMENT_NODE:
668 case XML_ATTRIBUTE_NODE:
670 xmlNodePtr child = This->node->children;
671 if ( child && child->type == XML_TEXT_NODE )
672 str = bstr_from_xmlChar( child->content );
677 case XML_CDATA_SECTION_NODE:
679 case XML_COMMENT_NODE:
680 str = bstr_from_xmlChar( This->node->content );
684 FIXME("Unhandled node type %d\n", This->node->type);
687 /* Always return a string. */
688 if (!str) str = SysAllocStringLen( NULL, 0 );
690 TRACE("%p %s\n", This, debugstr_w(str) );
696 static HRESULT WINAPI xmlnode_put_text(
704 static HRESULT WINAPI xmlnode_get_specified(
706 VARIANT_BOOL* isSpecified)
712 static HRESULT WINAPI xmlnode_get_definition(
714 IXMLDOMNode** definitionNode)
720 static HRESULT WINAPI xmlnode_get_nodeTypedValue(
724 FIXME("ignoring data type\n");
725 return xmlnode_get_nodeValue(iface, typedValue);
728 static HRESULT WINAPI xmlnode_put_nodeTypedValue(
736 static HRESULT WINAPI xmlnode_get_dataType(
738 VARIANT* dataTypeName)
740 xmlnode *This = impl_from_IXMLDOMNode( iface );
743 TRACE("iface %p\n", iface);
748 /* CDATA Section, Comment, Document, Document Fragment,
749 Entity, Notation, PI, and Text Node are non-typed. */
750 V_BSTR(dataTypeName) = NULL;
751 V_VT(dataTypeName) = VT_NULL;
753 switch ( This->node->type )
755 case XML_ATTRIBUTE_NODE:
756 FIXME("NODE_ATTRIBUTE_NODE should return a valid value.\n");
758 case XML_ELEMENT_NODE:
759 pVal = xmlGetNsProp(This->node, (xmlChar*)"dt",
760 (xmlChar*)"urn:schemas-microsoft-com:datatypes");
763 V_VT(dataTypeName) = VT_BSTR;
764 V_BSTR(dataTypeName) = bstr_from_xmlChar( pVal );
768 case XML_ENTITY_REF_NODE:
769 FIXME("XML_ENTITY_REF_NODE should return a valid value.\n");
772 TRACE("Type %d returning NULL\n", This->node->type);
775 /* non-typed nodes return S_FALSE */
776 if(V_VT(dataTypeName) == VT_NULL)
784 static HRESULT WINAPI xmlnode_put_dataType(
792 static HRESULT WINAPI xmlnode_get_xml(
796 xmlnode *This = impl_from_IXMLDOMNode( iface );
797 xmlBufferPtr pXmlBuf;
800 TRACE("iface %p\n", iface);
807 pXmlBuf = xmlBufferCreate();
810 nSize = xmlNodeDump(pXmlBuf, This->node->doc, This->node, 0, 0);
813 const xmlChar *pContent;
815 /* Attribute Nodes return a space infront of their name */
816 pContent = xmlBufferContent(pXmlBuf);
817 if( ((char*)pContent)[0] == ' ')
818 *xmlString = bstr_from_xmlChar(pContent+1);
820 *xmlString = bstr_from_xmlChar(pContent);
823 xmlBufferFree(pXmlBuf);
827 /* Always returns a string. */
828 if(*xmlString == NULL) *xmlString = SysAllocStringLen( NULL, 0 );
833 static HRESULT WINAPI xmlnode_transformNode(
835 IXMLDOMNode* styleSheet,
842 static HRESULT WINAPI xmlnode_selectNodes(
845 IXMLDOMNodeList** resultList)
847 xmlnode *This = impl_from_IXMLDOMNode( iface );
849 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultList );
851 return queryresult_create( This->node, queryString, resultList );
854 static HRESULT WINAPI xmlnode_selectSingleNode(
857 IXMLDOMNode** resultNode)
859 xmlnode *This = impl_from_IXMLDOMNode( iface );
860 IXMLDOMNodeList *list;
863 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultNode );
866 r = IXMLDOMNode_selectNodes(iface, queryString, &list);
869 r = IXMLDOMNodeList_nextNode(list, resultNode);
870 IXMLDOMNodeList_Release(list);
875 static HRESULT WINAPI xmlnode_get_parsed(
877 VARIANT_BOOL* isParsed)
883 static HRESULT WINAPI xmlnode_get_namespaceURI(
891 static HRESULT WINAPI xmlnode_get_prefix(
899 static HRESULT WINAPI xmlnode_get_baseName(
903 xmlnode *This = impl_from_IXMLDOMNode( iface );
907 TRACE("%p %p\n", This, nameString );
912 switch ( This->node->type )
914 case XML_ELEMENT_NODE:
915 case XML_ATTRIBUTE_NODE:
916 str = bstr_from_xmlChar( This->node->name );
922 ERR("Unhandled type %d\n", This->node->type );
926 TRACE("returning %08x str = %s\n", r, debugstr_w( str ) );
932 static HRESULT WINAPI xmlnode_transformNodeToObject(
934 IXMLDOMNode* stylesheet,
935 VARIANT outputObject)
941 static const struct IXMLDOMNodeVtbl xmlnode_vtbl =
943 xmlnode_QueryInterface,
946 xmlnode_GetTypeInfoCount,
948 xmlnode_GetIDsOfNames,
950 xmlnode_get_nodeName,
951 xmlnode_get_nodeValue,
952 xmlnode_put_nodeValue,
953 xmlnode_get_nodeType,
954 xmlnode_get_parentNode,
955 xmlnode_get_childNodes,
956 xmlnode_get_firstChild,
957 xmlnode_get_lastChild,
958 xmlnode_get_previousSibling,
959 xmlnode_get_nextSibling,
960 xmlnode_get_attributes,
961 xmlnode_insertBefore,
962 xmlnode_replaceChild,
965 xmlnode_hasChildNodes,
966 xmlnode_get_ownerDocument,
968 xmlnode_get_nodeTypeString,
971 xmlnode_get_specified,
972 xmlnode_get_definition,
973 xmlnode_get_nodeTypedValue,
974 xmlnode_put_nodeTypedValue,
975 xmlnode_get_dataType,
976 xmlnode_put_dataType,
978 xmlnode_transformNode,
980 xmlnode_selectSingleNode,
982 xmlnode_get_namespaceURI,
984 xmlnode_get_baseName,
985 xmlnode_transformNodeToObject,
988 static HRESULT WINAPI Internal_QueryInterface(
993 xmlnode *This = impl_from_InternalUnknown( iface );
995 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
998 if ( IsEqualGUID( riid, &IID_IUnknown ))
1000 else if ( IsEqualGUID( riid, &IID_IDispatch ) ||
1001 IsEqualGUID( riid, &IID_IXMLDOMNode ) )
1002 *ppvObject = &This->lpVtbl;
1005 FIXME("interface %s not implemented\n", debugstr_guid(riid));
1007 return E_NOINTERFACE;
1010 IUnknown_AddRef( (IUnknown*)*ppvObject );
1015 static ULONG WINAPI Internal_AddRef(
1018 xmlnode *This = impl_from_InternalUnknown( iface );
1019 return InterlockedIncrement( &This->ref );
1022 static ULONG WINAPI Internal_Release(
1025 xmlnode *This = impl_from_InternalUnknown( iface );
1028 ref = InterlockedDecrement( &This->ref );
1032 xmldoc_release( This->node->doc );
1033 HeapFree( GetProcessHeap(), 0, This );
1039 static const struct IUnknownVtbl internal_unk_vtbl =
1041 Internal_QueryInterface,
1046 IUnknown *create_basic_node( xmlNodePtr node, IUnknown *pUnkOuter )
1050 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
1055 xmldoc_add_ref( node->doc );
1057 This->lpVtbl = &xmlnode_vtbl;
1058 This->lpInternalUnkVtbl = &internal_unk_vtbl;
1061 This->pUnkOuter = pUnkOuter; /* Don't take a ref on outer Unknown */
1063 This->pUnkOuter = (IUnknown *)&This->lpInternalUnkVtbl;
1068 return (IUnknown*)&This->lpInternalUnkVtbl;
1071 IXMLDOMNode *create_node( xmlNodePtr node )
1080 TRACE("type %d\n", node->type);
1083 case XML_ELEMENT_NODE:
1084 pUnk = create_element( node, NULL );
1086 case XML_ATTRIBUTE_NODE:
1087 pUnk = create_attribute( node );
1090 pUnk = create_text( node );
1092 case XML_COMMENT_NODE:
1093 pUnk = create_comment( node );
1095 case XML_DOCUMENT_NODE:
1096 ERR("shouldn't be here!\n");
1099 FIXME("only creating basic node for type %d\n", node->type);
1100 pUnk = create_basic_node( node, NULL );
1103 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
1104 IUnknown_Release(pUnk);
1105 if(FAILED(hr)) return NULL;