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(
700 xmlnode *This = impl_from_IXMLDOMNode( iface );
705 switch(This->node->type)
707 case XML_DOCUMENT_NODE:
713 str = xmlChar_from_wchar((WCHAR*)text);
715 /* Escape the string. */
716 str = xmlEncodeEntitiesReentrant(This->node->doc, str);
717 str = xmlEncodeSpecialChars(This->node->doc, str);
719 xmlNodeSetContent(This->node, str);
725 static HRESULT WINAPI xmlnode_get_specified(
727 VARIANT_BOOL* isSpecified)
733 static HRESULT WINAPI xmlnode_get_definition(
735 IXMLDOMNode** definitionNode)
741 static HRESULT WINAPI xmlnode_get_nodeTypedValue(
745 FIXME("ignoring data type\n");
746 return xmlnode_get_nodeValue(iface, typedValue);
749 static HRESULT WINAPI xmlnode_put_nodeTypedValue(
757 static HRESULT WINAPI xmlnode_get_dataType(
759 VARIANT* dataTypeName)
761 xmlnode *This = impl_from_IXMLDOMNode( iface );
764 TRACE("iface %p\n", iface);
769 /* CDATA Section, Comment, Document, Document Fragment,
770 Entity, Notation, PI, and Text Node are non-typed. */
771 V_BSTR(dataTypeName) = NULL;
772 V_VT(dataTypeName) = VT_NULL;
774 switch ( This->node->type )
776 case XML_ATTRIBUTE_NODE:
777 FIXME("NODE_ATTRIBUTE_NODE should return a valid value.\n");
779 case XML_ELEMENT_NODE:
780 pVal = xmlGetNsProp(This->node, (xmlChar*)"dt",
781 (xmlChar*)"urn:schemas-microsoft-com:datatypes");
784 V_VT(dataTypeName) = VT_BSTR;
785 V_BSTR(dataTypeName) = bstr_from_xmlChar( pVal );
789 case XML_ENTITY_REF_NODE:
790 FIXME("XML_ENTITY_REF_NODE should return a valid value.\n");
793 TRACE("Type %d returning NULL\n", This->node->type);
796 /* non-typed nodes return S_FALSE */
797 if(V_VT(dataTypeName) == VT_NULL)
805 static HRESULT WINAPI xmlnode_put_dataType(
813 static HRESULT WINAPI xmlnode_get_xml(
817 xmlnode *This = impl_from_IXMLDOMNode( iface );
818 xmlBufferPtr pXmlBuf;
821 TRACE("iface %p\n", iface);
828 pXmlBuf = xmlBufferCreate();
831 nSize = xmlNodeDump(pXmlBuf, This->node->doc, This->node, 0, 0);
834 const xmlChar *pContent;
836 /* Attribute Nodes return a space infront of their name */
837 pContent = xmlBufferContent(pXmlBuf);
838 if( ((char*)pContent)[0] == ' ')
839 *xmlString = bstr_from_xmlChar(pContent+1);
841 *xmlString = bstr_from_xmlChar(pContent);
844 xmlBufferFree(pXmlBuf);
848 /* Always returns a string. */
849 if(*xmlString == NULL) *xmlString = SysAllocStringLen( NULL, 0 );
854 static HRESULT WINAPI xmlnode_transformNode(
856 IXMLDOMNode* styleSheet,
863 static HRESULT WINAPI xmlnode_selectNodes(
866 IXMLDOMNodeList** resultList)
868 xmlnode *This = impl_from_IXMLDOMNode( iface );
870 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultList );
872 return queryresult_create( This->node, queryString, resultList );
875 static HRESULT WINAPI xmlnode_selectSingleNode(
878 IXMLDOMNode** resultNode)
880 xmlnode *This = impl_from_IXMLDOMNode( iface );
881 IXMLDOMNodeList *list;
884 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultNode );
887 r = IXMLDOMNode_selectNodes(iface, queryString, &list);
890 r = IXMLDOMNodeList_nextNode(list, resultNode);
891 IXMLDOMNodeList_Release(list);
896 static HRESULT WINAPI xmlnode_get_parsed(
898 VARIANT_BOOL* isParsed)
904 static HRESULT WINAPI xmlnode_get_namespaceURI(
912 static HRESULT WINAPI xmlnode_get_prefix(
920 static HRESULT WINAPI xmlnode_get_baseName(
924 xmlnode *This = impl_from_IXMLDOMNode( iface );
928 TRACE("%p %p\n", This, nameString );
933 switch ( This->node->type )
935 case XML_ELEMENT_NODE:
936 case XML_ATTRIBUTE_NODE:
937 str = bstr_from_xmlChar( This->node->name );
943 ERR("Unhandled type %d\n", This->node->type );
947 TRACE("returning %08x str = %s\n", r, debugstr_w( str ) );
953 static HRESULT WINAPI xmlnode_transformNodeToObject(
955 IXMLDOMNode* stylesheet,
956 VARIANT outputObject)
962 static const struct IXMLDOMNodeVtbl xmlnode_vtbl =
964 xmlnode_QueryInterface,
967 xmlnode_GetTypeInfoCount,
969 xmlnode_GetIDsOfNames,
971 xmlnode_get_nodeName,
972 xmlnode_get_nodeValue,
973 xmlnode_put_nodeValue,
974 xmlnode_get_nodeType,
975 xmlnode_get_parentNode,
976 xmlnode_get_childNodes,
977 xmlnode_get_firstChild,
978 xmlnode_get_lastChild,
979 xmlnode_get_previousSibling,
980 xmlnode_get_nextSibling,
981 xmlnode_get_attributes,
982 xmlnode_insertBefore,
983 xmlnode_replaceChild,
986 xmlnode_hasChildNodes,
987 xmlnode_get_ownerDocument,
989 xmlnode_get_nodeTypeString,
992 xmlnode_get_specified,
993 xmlnode_get_definition,
994 xmlnode_get_nodeTypedValue,
995 xmlnode_put_nodeTypedValue,
996 xmlnode_get_dataType,
997 xmlnode_put_dataType,
999 xmlnode_transformNode,
1000 xmlnode_selectNodes,
1001 xmlnode_selectSingleNode,
1003 xmlnode_get_namespaceURI,
1005 xmlnode_get_baseName,
1006 xmlnode_transformNodeToObject,
1009 static HRESULT WINAPI Internal_QueryInterface(
1014 xmlnode *This = impl_from_InternalUnknown( iface );
1016 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
1019 if ( IsEqualGUID( riid, &IID_IUnknown ))
1021 else if ( IsEqualGUID( riid, &IID_IDispatch ) ||
1022 IsEqualGUID( riid, &IID_IXMLDOMNode ) )
1023 *ppvObject = &This->lpVtbl;
1026 FIXME("interface %s not implemented\n", debugstr_guid(riid));
1028 return E_NOINTERFACE;
1031 IUnknown_AddRef( (IUnknown*)*ppvObject );
1036 static ULONG WINAPI Internal_AddRef(
1039 xmlnode *This = impl_from_InternalUnknown( iface );
1040 return InterlockedIncrement( &This->ref );
1043 static ULONG WINAPI Internal_Release(
1046 xmlnode *This = impl_from_InternalUnknown( iface );
1049 ref = InterlockedDecrement( &This->ref );
1053 xmldoc_release( This->node->doc );
1054 HeapFree( GetProcessHeap(), 0, This );
1060 static const struct IUnknownVtbl internal_unk_vtbl =
1062 Internal_QueryInterface,
1067 IUnknown *create_basic_node( xmlNodePtr node, IUnknown *pUnkOuter )
1071 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
1076 xmldoc_add_ref( node->doc );
1078 This->lpVtbl = &xmlnode_vtbl;
1079 This->lpInternalUnkVtbl = &internal_unk_vtbl;
1082 This->pUnkOuter = pUnkOuter; /* Don't take a ref on outer Unknown */
1084 This->pUnkOuter = (IUnknown *)&This->lpInternalUnkVtbl;
1089 return (IUnknown*)&This->lpInternalUnkVtbl;
1092 IXMLDOMNode *create_node( xmlNodePtr node )
1101 TRACE("type %d\n", node->type);
1104 case XML_ELEMENT_NODE:
1105 pUnk = create_element( node, NULL );
1107 case XML_ATTRIBUTE_NODE:
1108 pUnk = create_attribute( node );
1111 pUnk = create_text( node );
1113 case XML_COMMENT_NODE:
1114 pUnk = create_comment( node );
1116 case XML_DOCUMENT_NODE:
1117 ERR("shouldn't be here!\n");
1120 FIXME("only creating basic node for type %d\n", node->type);
1121 pUnk = create_basic_node( node, NULL );
1124 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
1125 IUnknown_Release(pUnk);
1126 if(FAILED(hr)) return NULL;