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:
425 case XML_ENTITY_REF_NODE:
427 *attributeMap = NULL;
430 *attributeMap = create_nodemap( iface );
435 static HRESULT WINAPI xmlnode_insertBefore(
437 IXMLDOMNode* newChild,
439 IXMLDOMNode** outNewChild)
441 xmlnode *This = impl_from_IXMLDOMNode( iface );
442 xmlNodePtr before_node, new_child_node;
443 IXMLDOMNode *before = NULL, *new;
446 TRACE("(%p)->(%p,var,%p)\n",This,newChild,outNewChild);
451 switch(V_VT(&refChild))
458 hr = IUnknown_QueryInterface(V_UNKNOWN(&refChild), &IID_IXMLDOMNode, (LPVOID)&before);
459 if(FAILED(hr)) return hr;
463 hr = IDispatch_QueryInterface(V_DISPATCH(&refChild), &IID_IXMLDOMNode, (LPVOID)&before);
464 if(FAILED(hr)) return hr;
468 FIXME("refChild var type %x\n", V_VT(&refChild));
472 IXMLDOMNode_QueryInterface(newChild, &IID_IXMLDOMNode, (LPVOID)&new);
473 new_child_node = impl_from_IXMLDOMNode(new)->node;
474 TRACE("new_child_node %p This->node %p\n", new_child_node, This->node);
478 before_node = impl_from_IXMLDOMNode(before)->node;
479 xmlAddPrevSibling(before_node, new_child_node);
480 IXMLDOMNode_Release(before);
484 xmlAddChild(This->node, new_child_node);
487 IXMLDOMNode_Release(new);
488 IXMLDOMNode_AddRef(newChild);
490 *outNewChild = newChild;
496 static HRESULT WINAPI xmlnode_replaceChild(
498 IXMLDOMNode* newChild,
499 IXMLDOMNode* oldChild,
500 IXMLDOMNode** outOldChild)
506 static HRESULT WINAPI xmlnode_removeChild(
508 IXMLDOMNode* childNode,
509 IXMLDOMNode** oldChild)
511 xmlnode *This = impl_from_IXMLDOMNode( iface );
512 xmlNode *ancestor, *child_node_ptr;
516 TRACE("%p->(%p, %p)\n", This, childNode, oldChild);
520 if(!childNode) return E_INVALIDARG;
522 hr = IXMLDOMNode_QueryInterface(childNode, &IID_IXMLDOMNode, (LPVOID)&child);
526 child_node_ptr = ancestor = impl_from_IXMLDOMNode(child)->node;
527 while(ancestor->parent)
529 if(ancestor->parent == This->node)
531 ancestor = ancestor->parent;
533 if(!ancestor->parent)
535 WARN("childNode %p is not a child of %p\n", childNode, iface);
536 IXMLDOMNode_Release(child);
540 xmlUnlinkNode(child_node_ptr);
542 IXMLDOMNode_Release(child);
543 IXMLDOMNode_AddRef(childNode);
544 *oldChild = childNode;
548 static HRESULT WINAPI xmlnode_appendChild(
550 IXMLDOMNode* newChild,
551 IXMLDOMNode** outNewChild)
553 xmlnode *This = impl_from_IXMLDOMNode( iface );
554 IXMLDOMNode *pAttr = NULL;
557 TRACE("(%p)->(%p,%p)\n", This, newChild, outNewChild);
559 /* Cannot Append an Attribute node. */
560 IUnknown_QueryInterface(newChild, &IID_IXMLDOMNode, (LPVOID*)&pAttr);
563 xmlnode *ThisNew = impl_from_IXMLDOMNode( pAttr );
565 if(ThisNew->node->type == XML_ATTRIBUTE_NODE)
567 if(outNewChild) *outNewChild = NULL;
569 IUnknown_Release(pAttr);
574 IUnknown_Release(pAttr);
578 return IXMLDOMNode_insertBefore(iface, newChild, var, outNewChild);
581 static HRESULT WINAPI xmlnode_hasChildNodes(
583 VARIANT_BOOL* hasChild)
585 xmlnode *This = impl_from_IXMLDOMNode( iface );
591 if (!This->node->children)
593 *hasChild = VARIANT_FALSE;
597 *hasChild = VARIANT_TRUE;
601 static HRESULT WINAPI xmlnode_get_ownerDocument(
603 IXMLDOMDocument** DOMDocument)
609 static HRESULT WINAPI xmlnode_cloneNode(
612 IXMLDOMNode** cloneRoot)
614 xmlnode *This = impl_from_IXMLDOMNode( iface );
615 xmlNodePtr pClone = NULL;
616 IXMLDOMNode *pNode = NULL;
618 TRACE("%p (%d)\n", This, deep);
623 pClone = xmlCopyNode(This->node, deep ? 1 : 2);
626 pClone->doc = This->node->doc;
628 pNode = create_node(pClone);
631 ERR("Copy failed\n");
639 ERR("Copy failed\n");
646 static HRESULT WINAPI xmlnode_get_nodeTypeString(
650 xmlnode *This = impl_from_IXMLDOMNode( iface );
653 TRACE("%p\n", This );
661 switch( This->node->type )
663 case XML_ATTRIBUTE_NODE:
664 str = (const xmlChar*) "attribute";
666 case XML_CDATA_SECTION_NODE:
667 str = (const xmlChar*) "cdatasection";
669 case XML_COMMENT_NODE:
670 str = (const xmlChar*) "comment";
672 case XML_DOCUMENT_NODE:
673 str = (const xmlChar*) "document";
675 case XML_DOCUMENT_FRAG_NODE:
676 str = (const xmlChar*) "documentfragment";
678 case XML_ELEMENT_NODE:
679 str = (const xmlChar*) "element";
681 case XML_ENTITY_NODE:
682 str = (const xmlChar*) "entity";
684 case XML_ENTITY_REF_NODE:
685 str = (const xmlChar*) "entityreference";
687 case XML_NOTATION_NODE:
688 str = (const xmlChar*) "notation";
691 str = (const xmlChar*) "processinginstruction";
694 str = (const xmlChar*) "text";
697 FIXME("nodeName not mapped correctly (%d)\n", This->node->type);
698 str = This->node->name;
702 *xmlnodeType = bstr_from_xmlChar( str );
709 static HRESULT WINAPI xmlnode_get_text(
713 xmlnode *This = impl_from_IXMLDOMNode( iface );
721 switch(This->node->type)
723 case XML_ELEMENT_NODE:
724 case XML_ATTRIBUTE_NODE:
726 xmlNodePtr child = This->node->children;
727 if ( child && child->type == XML_TEXT_NODE )
728 str = bstr_from_xmlChar( child->content );
733 case XML_CDATA_SECTION_NODE:
735 case XML_COMMENT_NODE:
736 str = bstr_from_xmlChar( This->node->content );
740 FIXME("Unhandled node type %d\n", This->node->type);
743 /* Always return a string. */
744 if (!str) str = SysAllocStringLen( NULL, 0 );
746 TRACE("%p %s\n", This, debugstr_w(str) );
752 static HRESULT WINAPI xmlnode_put_text(
756 xmlnode *This = impl_from_IXMLDOMNode( iface );
761 switch(This->node->type)
763 case XML_DOCUMENT_NODE:
769 str = xmlChar_from_wchar((WCHAR*)text);
771 /* Escape the string. */
772 str = xmlEncodeEntitiesReentrant(This->node->doc, str);
773 str = xmlEncodeSpecialChars(This->node->doc, str);
775 xmlNodeSetContent(This->node, str);
781 static HRESULT WINAPI xmlnode_get_specified(
783 VARIANT_BOOL* isSpecified)
789 static HRESULT WINAPI xmlnode_get_definition(
791 IXMLDOMNode** definitionNode)
797 static HRESULT WINAPI xmlnode_get_nodeTypedValue(
801 FIXME("ignoring data type\n");
802 return xmlnode_get_nodeValue(iface, typedValue);
805 static HRESULT WINAPI xmlnode_put_nodeTypedValue(
813 static HRESULT WINAPI xmlnode_get_dataType(
815 VARIANT* dataTypeName)
817 xmlnode *This = impl_from_IXMLDOMNode( iface );
820 TRACE("iface %p\n", iface);
825 /* Attribute, CDATA Section, Comment, Document, Document Fragment,
826 Entity, Notation, PI, and Text Node are non-typed. */
827 V_BSTR(dataTypeName) = NULL;
828 V_VT(dataTypeName) = VT_NULL;
830 switch ( This->node->type )
832 case XML_ELEMENT_NODE:
833 pVal = xmlGetNsProp(This->node, (xmlChar*)"dt",
834 (xmlChar*)"urn:schemas-microsoft-com:datatypes");
837 V_VT(dataTypeName) = VT_BSTR;
838 V_BSTR(dataTypeName) = bstr_from_xmlChar( pVal );
842 case XML_ENTITY_REF_NODE:
843 FIXME("XML_ENTITY_REF_NODE should return a valid value.\n");
846 TRACE("Type %d returning NULL\n", This->node->type);
849 /* non-typed nodes return S_FALSE */
850 if(V_VT(dataTypeName) == VT_NULL)
858 static HRESULT WINAPI xmlnode_put_dataType(
866 static HRESULT WINAPI xmlnode_get_xml(
870 xmlnode *This = impl_from_IXMLDOMNode( iface );
871 xmlBufferPtr pXmlBuf;
874 TRACE("iface %p\n", iface);
881 pXmlBuf = xmlBufferCreate();
884 nSize = xmlNodeDump(pXmlBuf, This->node->doc, This->node, 0, 0);
887 const xmlChar *pContent;
889 /* Attribute Nodes return a space infront of their name */
890 pContent = xmlBufferContent(pXmlBuf);
891 if( ((char*)pContent)[0] == ' ')
892 *xmlString = bstr_from_xmlChar(pContent+1);
894 *xmlString = bstr_from_xmlChar(pContent);
897 xmlBufferFree(pXmlBuf);
901 /* Always returns a string. */
902 if(*xmlString == NULL) *xmlString = SysAllocStringLen( NULL, 0 );
907 static HRESULT WINAPI xmlnode_transformNode(
909 IXMLDOMNode* styleSheet,
916 static HRESULT WINAPI xmlnode_selectNodes(
919 IXMLDOMNodeList** resultList)
921 xmlnode *This = impl_from_IXMLDOMNode( iface );
923 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultList );
925 return queryresult_create( This->node, queryString, resultList );
928 static HRESULT WINAPI xmlnode_selectSingleNode(
931 IXMLDOMNode** resultNode)
933 xmlnode *This = impl_from_IXMLDOMNode( iface );
934 IXMLDOMNodeList *list;
937 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultNode );
940 r = IXMLDOMNode_selectNodes(iface, queryString, &list);
943 r = IXMLDOMNodeList_nextNode(list, resultNode);
944 IXMLDOMNodeList_Release(list);
949 static HRESULT WINAPI xmlnode_get_parsed(
951 VARIANT_BOOL* isParsed)
957 static HRESULT WINAPI xmlnode_get_namespaceURI(
965 static HRESULT WINAPI xmlnode_get_prefix(
973 static HRESULT WINAPI xmlnode_get_baseName(
977 xmlnode *This = impl_from_IXMLDOMNode( iface );
981 TRACE("%p %p\n", This, nameString );
986 switch ( This->node->type )
988 case XML_ELEMENT_NODE:
989 case XML_ATTRIBUTE_NODE:
990 str = bstr_from_xmlChar( This->node->name );
996 ERR("Unhandled type %d\n", This->node->type );
1000 TRACE("returning %08x str = %s\n", r, debugstr_w( str ) );
1006 static HRESULT WINAPI xmlnode_transformNodeToObject(
1008 IXMLDOMNode* stylesheet,
1009 VARIANT outputObject)
1015 static const struct IXMLDOMNodeVtbl xmlnode_vtbl =
1017 xmlnode_QueryInterface,
1020 xmlnode_GetTypeInfoCount,
1021 xmlnode_GetTypeInfo,
1022 xmlnode_GetIDsOfNames,
1024 xmlnode_get_nodeName,
1025 xmlnode_get_nodeValue,
1026 xmlnode_put_nodeValue,
1027 xmlnode_get_nodeType,
1028 xmlnode_get_parentNode,
1029 xmlnode_get_childNodes,
1030 xmlnode_get_firstChild,
1031 xmlnode_get_lastChild,
1032 xmlnode_get_previousSibling,
1033 xmlnode_get_nextSibling,
1034 xmlnode_get_attributes,
1035 xmlnode_insertBefore,
1036 xmlnode_replaceChild,
1037 xmlnode_removeChild,
1038 xmlnode_appendChild,
1039 xmlnode_hasChildNodes,
1040 xmlnode_get_ownerDocument,
1042 xmlnode_get_nodeTypeString,
1045 xmlnode_get_specified,
1046 xmlnode_get_definition,
1047 xmlnode_get_nodeTypedValue,
1048 xmlnode_put_nodeTypedValue,
1049 xmlnode_get_dataType,
1050 xmlnode_put_dataType,
1052 xmlnode_transformNode,
1053 xmlnode_selectNodes,
1054 xmlnode_selectSingleNode,
1056 xmlnode_get_namespaceURI,
1058 xmlnode_get_baseName,
1059 xmlnode_transformNodeToObject,
1062 static HRESULT WINAPI Internal_QueryInterface(
1067 xmlnode *This = impl_from_InternalUnknown( iface );
1069 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
1072 if ( IsEqualGUID( riid, &IID_IUnknown ))
1074 else if ( IsEqualGUID( riid, &IID_IDispatch ) ||
1075 IsEqualGUID( riid, &IID_IXMLDOMNode ) )
1076 *ppvObject = &This->lpVtbl;
1079 FIXME("interface %s not implemented\n", debugstr_guid(riid));
1081 return E_NOINTERFACE;
1084 IUnknown_AddRef( (IUnknown*)*ppvObject );
1089 static ULONG WINAPI Internal_AddRef(
1092 xmlnode *This = impl_from_InternalUnknown( iface );
1093 return InterlockedIncrement( &This->ref );
1096 static ULONG WINAPI Internal_Release(
1099 xmlnode *This = impl_from_InternalUnknown( iface );
1102 ref = InterlockedDecrement( &This->ref );
1106 xmldoc_release( This->node->doc );
1107 HeapFree( GetProcessHeap(), 0, This );
1113 static const struct IUnknownVtbl internal_unk_vtbl =
1115 Internal_QueryInterface,
1120 IUnknown *create_basic_node( xmlNodePtr node, IUnknown *pUnkOuter )
1124 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
1129 xmldoc_add_ref( node->doc );
1131 This->lpVtbl = &xmlnode_vtbl;
1132 This->lpInternalUnkVtbl = &internal_unk_vtbl;
1135 This->pUnkOuter = pUnkOuter; /* Don't take a ref on outer Unknown */
1137 This->pUnkOuter = (IUnknown *)&This->lpInternalUnkVtbl;
1142 return (IUnknown*)&This->lpInternalUnkVtbl;
1145 IXMLDOMNode *create_node( xmlNodePtr node )
1154 TRACE("type %d\n", node->type);
1157 case XML_ELEMENT_NODE:
1158 pUnk = create_element( node, NULL );
1160 case XML_ATTRIBUTE_NODE:
1161 pUnk = create_attribute( node );
1164 pUnk = create_text( node );
1166 case XML_COMMENT_NODE:
1167 pUnk = create_comment( node );
1169 case XML_DOCUMENT_NODE:
1170 ERR("shouldn't be here!\n");
1173 FIXME("only creating basic node for type %d\n", node->type);
1174 pUnk = create_basic_node( node, NULL );
1177 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
1178 IUnknown_Release(pUnk);
1179 if(FAILED(hr)) return NULL;