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"
37 # ifdef HAVE_LIBXSLT_PATTERN_H
38 # include <libxslt/pattern.h>
40 # ifdef HAVE_LIBXSLT_TRANSFORM_H
41 # include <libxslt/transform.h>
43 # include <libxslt/xsltutils.h>
44 # include <libxslt/xsltInternals.h>
48 # include <libxml/HTMLtree.h>
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
57 static const WCHAR szBinBase64[] = {'b','i','n','.','b','a','s','e','6','4',0};
58 static const WCHAR szString[] = {'s','t','r','i','n','g',0};
59 static const WCHAR szNumber[] = {'n','u','m','b','e','r',0};
60 static const WCHAR szInt[] = {'I','n','t',0};
61 static const WCHAR szFixed[] = {'F','i','x','e','d','.','1','4','.','4',0};
62 static const WCHAR szBoolean[] = {'B','o','o','l','e','a','n',0};
63 static const WCHAR szDateTime[] = {'d','a','t','e','T','i','m','e',0};
64 static const WCHAR szDateTimeTZ[] = {'d','a','t','e','T','i','m','e','.','t','z',0};
65 static const WCHAR szDate[] = {'D','a','t','e',0};
66 static const WCHAR szTime[] = {'T','i','m','e',0};
67 static const WCHAR szTimeTZ[] = {'T','i','m','e','.','t','z',0};
68 static const WCHAR szI1[] = {'i','1',0};
69 static const WCHAR szI2[] = {'i','2',0};
70 static const WCHAR szI4[] = {'i','4',0};
71 static const WCHAR szIU1[] = {'u','i','1',0};
72 static const WCHAR szIU2[] = {'u','i','2',0};
73 static const WCHAR szIU4[] = {'u','i','4',0};
74 static const WCHAR szR4[] = {'r','4',0};
75 static const WCHAR szR8[] = {'r','8',0};
76 static const WCHAR szFloat[] = {'f','l','o','a','t',0};
77 static const WCHAR szUUID[] = {'u','u','i','d',0};
78 static const WCHAR szBinHex[] = {'b','i','n','.','h','e','x',0};
80 static inline xmlnode *impl_from_InternalUnknown( IUnknown *iface )
82 return (xmlnode *)((char*)iface - FIELD_OFFSET(xmlnode, lpInternalUnkVtbl));
85 xmlNodePtr xmlNodePtr_from_domnode( IXMLDOMNode *iface, xmlElementType type )
91 This = impl_from_IXMLDOMNode( iface );
94 if ( type && This->node->type != type )
99 void attach_xmlnode( IXMLDOMNode *node, xmlNodePtr xml )
101 xmlnode *This = impl_from_IXMLDOMNode( node );
104 xmldoc_release(This->node->doc);
108 xmldoc_add_ref(This->node->doc);
113 static HRESULT WINAPI xmlnode_QueryInterface(
118 xmlnode *This = impl_from_IXMLDOMNode( iface );
119 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
121 return IUnknown_QueryInterface(This->pUnkOuter, riid, ppvObject);
124 static ULONG WINAPI xmlnode_AddRef(
127 xmlnode *This = impl_from_IXMLDOMNode( iface );
128 return IUnknown_AddRef(This->pUnkOuter);
131 static ULONG WINAPI xmlnode_Release(
134 xmlnode *This = impl_from_IXMLDOMNode( iface );
135 return IUnknown_Release(This->pUnkOuter);
138 static HRESULT WINAPI xmlnode_GetTypeInfoCount(
142 xmlnode *This = impl_from_IXMLDOMNode( iface );
144 TRACE("(%p)->(%p)\n", This, pctinfo);
151 static HRESULT WINAPI xmlnode_GetTypeInfo(
155 ITypeInfo** ppTInfo )
157 xmlnode *This = impl_from_IXMLDOMNode( iface );
160 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
162 hr = get_typeinfo(IXMLDOMNode_tid, ppTInfo);
167 static HRESULT WINAPI xmlnode_GetIDsOfNames(
175 xmlnode *This = impl_from_IXMLDOMNode( iface );
180 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
183 if(!rgszNames || cNames == 0 || !rgDispId)
186 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
189 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
190 ITypeInfo_Release(typeinfo);
196 static HRESULT WINAPI xmlnode_Invoke(
202 DISPPARAMS* pDispParams,
204 EXCEPINFO* pExcepInfo,
207 xmlnode *This = impl_from_IXMLDOMNode( iface );
211 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
212 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
214 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
217 hr = ITypeInfo_Invoke(typeinfo, &(This->lpVtbl), dispIdMember, wFlags, pDispParams,
218 pVarResult, pExcepInfo, puArgErr);
219 ITypeInfo_Release(typeinfo);
225 static HRESULT WINAPI xmlnode_get_nodeName(
229 xmlnode *This = impl_from_IXMLDOMNode( iface );
232 TRACE("%p\n", This );
240 switch( This->node->type )
242 case XML_CDATA_SECTION_NODE:
243 str = (const xmlChar*) "#cdata-section";
245 case XML_COMMENT_NODE:
246 str = (const xmlChar*) "#comment";
248 case XML_DOCUMENT_FRAG_NODE:
249 str = (const xmlChar*) "#document-fragment";
252 str = (const xmlChar*) "#text";
254 case XML_DOCUMENT_NODE:
255 str = (const xmlChar*) "#document";
257 case XML_ATTRIBUTE_NODE:
258 case XML_ELEMENT_NODE:
260 str = This->node->name;
263 FIXME("nodeName not mapped correctly (%d)\n", This->node->type);
264 str = This->node->name;
268 *name = bstr_from_xmlChar( str );
275 BSTR bstr_from_xmlChar( const xmlChar *buf )
284 len = MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) buf, -1, NULL, 0 );
285 str = (LPWSTR) HeapAlloc( GetProcessHeap(), 0, len * sizeof (WCHAR) );
288 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) buf, -1, str, len );
289 bstr = SysAllocString( str );
290 HeapFree( GetProcessHeap(), 0, str );
294 static HRESULT WINAPI xmlnode_get_nodeValue(
298 xmlnode *This = impl_from_IXMLDOMNode( iface );
301 TRACE("%p %p\n", This, value);
306 V_BSTR(value) = NULL;
307 V_VT(value) = VT_NULL;
309 switch ( This->node->type )
311 case XML_CDATA_SECTION_NODE:
312 case XML_COMMENT_NODE:
314 case XML_ATTRIBUTE_NODE:
316 xmlChar *content = xmlNodeGetContent(This->node);
317 V_VT(value) = VT_BSTR;
318 V_BSTR(value) = bstr_from_xmlChar( content );
324 V_VT(value) = VT_BSTR;
325 V_BSTR(value) = bstr_from_xmlChar( This->node->content );
328 case XML_ELEMENT_NODE:
329 case XML_DOCUMENT_NODE:
330 /* these seem to return NULL */
334 FIXME("node %p type %d\n", This, This->node->type);
337 TRACE("%p returned %s\n", This, debugstr_w( V_BSTR(value) ) );
342 static HRESULT WINAPI xmlnode_put_nodeValue(
346 xmlnode *This = impl_from_IXMLDOMNode( iface );
349 VARIANT string_value;
351 TRACE("%p type(%d)\n", This, This->node->type);
353 VariantInit(&string_value);
354 hr = VariantChangeType(&string_value, &value, 0, VT_BSTR);
357 VariantClear(&string_value);
358 WARN("Couldn't convert to VT_BSTR\n");
363 /* Document, Document Fragment, Document Type, Element,
364 Entity, Entity Reference, Notation aren't supported. */
365 switch ( This->node->type )
367 case XML_ATTRIBUTE_NODE:
368 case XML_CDATA_SECTION_NODE:
369 case XML_COMMENT_NODE:
373 str = xmlChar_from_wchar((WCHAR*)V_BSTR(&string_value));
374 xmlNodeSetContent(This->node, str);
375 HeapFree(GetProcessHeap(),0,str);
380 /* Do nothing for unsupported types. */
384 VariantClear(&string_value);
389 static HRESULT WINAPI xmlnode_get_nodeType(
393 xmlnode *This = impl_from_IXMLDOMNode( iface );
395 TRACE("%p %p\n", This, type);
397 assert( NODE_ELEMENT == XML_ELEMENT_NODE );
398 assert( NODE_NOTATION == XML_NOTATION_NODE );
400 *type = This->node->type;
405 static HRESULT get_node(
411 TRACE("%p->%s %p\n", This, name, node );
415 *out = create_node( node );
421 static HRESULT WINAPI xmlnode_get_parentNode(
423 IXMLDOMNode** parent)
425 xmlnode *This = impl_from_IXMLDOMNode( iface );
426 return get_node( This, "parent", This->node->parent, parent );
429 static HRESULT WINAPI xmlnode_get_childNodes(
431 IXMLDOMNodeList** childList)
433 xmlnode *This = impl_from_IXMLDOMNode( iface );
435 TRACE("%p %p\n", This, childList );
440 *childList = create_children_nodelist(This->node);
441 if (*childList == NULL)
442 return E_OUTOFMEMORY;
447 static HRESULT WINAPI xmlnode_get_firstChild(
449 IXMLDOMNode** firstChild)
451 xmlnode *This = impl_from_IXMLDOMNode( iface );
452 return get_node( This, "firstChild", This->node->children, firstChild );
455 static HRESULT WINAPI xmlnode_get_lastChild(
457 IXMLDOMNode** lastChild)
459 xmlnode *This = impl_from_IXMLDOMNode( iface );
461 TRACE("%p\n", This );
466 switch( This->node->type )
468 /* CDATASection, Comment, PI and Text Nodes do not support lastChild */
470 case XML_CDATA_SECTION_NODE:
472 case XML_COMMENT_NODE:
476 return get_node( This, "lastChild", This->node->last, lastChild );
480 static HRESULT WINAPI xmlnode_get_previousSibling(
482 IXMLDOMNode** previousSibling)
484 xmlnode *This = impl_from_IXMLDOMNode( iface );
486 TRACE("%p\n", This );
488 if (!previousSibling)
491 switch( This->node->type )
493 /* Attribute, Document and Document Fragment Nodes do not support previousSibling */
494 case XML_DOCUMENT_NODE:
495 case XML_DOCUMENT_FRAG_NODE:
496 case XML_ATTRIBUTE_NODE:
497 *previousSibling = NULL;
500 return get_node( This, "previous", This->node->prev, previousSibling );
504 static HRESULT WINAPI xmlnode_get_nextSibling(
506 IXMLDOMNode** nextSibling)
508 xmlnode *This = impl_from_IXMLDOMNode( iface );
510 TRACE("%p\n", This );
515 switch( This->node->type )
517 /* Attribute, Document and Document Fragment Nodes do not support nextSibling */
518 case XML_DOCUMENT_NODE:
519 case XML_DOCUMENT_FRAG_NODE:
520 case XML_ATTRIBUTE_NODE:
524 return get_node( This, "next", This->node->next, nextSibling );
528 static HRESULT WINAPI xmlnode_get_attributes(
530 IXMLDOMNamedNodeMap** attributeMap)
532 xmlnode *This = impl_from_IXMLDOMNode( iface );
538 switch( This->node->type )
540 /* Attribute, CDataSection, Comment, Documents, Documents Fragments,
541 Entity and Text Nodes does not support get_attributes */
542 case XML_ATTRIBUTE_NODE:
543 case XML_CDATA_SECTION_NODE:
544 case XML_COMMENT_NODE:
545 case XML_DOCUMENT_NODE:
546 case XML_DOCUMENT_FRAG_NODE:
547 case XML_ENTITY_NODE:
548 case XML_ENTITY_REF_NODE:
550 *attributeMap = NULL;
553 *attributeMap = create_nodemap( iface );
558 static HRESULT WINAPI xmlnode_insertBefore(
560 IXMLDOMNode* newChild,
562 IXMLDOMNode** outNewChild)
564 xmlnode *This = impl_from_IXMLDOMNode( iface );
565 xmlNodePtr before_node, new_child_node;
566 IXMLDOMNode *before = NULL, *new;
569 TRACE("(%p)->(%p,var,%p)\n",This,newChild,outNewChild);
574 switch(V_VT(&refChild))
581 hr = IUnknown_QueryInterface(V_UNKNOWN(&refChild), &IID_IXMLDOMNode, (LPVOID)&before);
582 if(FAILED(hr)) return hr;
586 hr = IDispatch_QueryInterface(V_DISPATCH(&refChild), &IID_IXMLDOMNode, (LPVOID)&before);
587 if(FAILED(hr)) return hr;
591 FIXME("refChild var type %x\n", V_VT(&refChild));
595 IXMLDOMNode_QueryInterface(newChild, &IID_IXMLDOMNode, (LPVOID)&new);
596 new_child_node = impl_from_IXMLDOMNode(new)->node;
597 TRACE("new_child_node %p This->node %p\n", new_child_node, This->node);
599 if(!new_child_node->parent)
600 if(xmldoc_remove_orphan(new_child_node->doc, new_child_node) != S_OK)
601 WARN("%p is not an orphan of %p\n", new_child_node, new_child_node->doc);
605 before_node = impl_from_IXMLDOMNode(before)->node;
606 xmlAddPrevSibling(before_node, new_child_node);
607 IXMLDOMNode_Release(before);
611 xmlAddChild(This->node, new_child_node);
614 IXMLDOMNode_Release(new);
615 IXMLDOMNode_AddRef(newChild);
617 *outNewChild = newChild;
623 static HRESULT WINAPI xmlnode_replaceChild(
625 IXMLDOMNode* newChild,
626 IXMLDOMNode* oldChild,
627 IXMLDOMNode** outOldChild)
629 xmlnode *This = impl_from_IXMLDOMNode( iface );
630 xmlNode *old_child_ptr, *new_child_ptr;
631 xmlDocPtr leaving_doc;
632 xmlNode *my_ancestor;
633 IXMLDOMNode *realOldChild;
636 TRACE("%p->(%p,%p,%p)\n",This,newChild,oldChild,outOldChild);
638 /* Do not believe any documentation telling that newChild == NULL
639 means removal. It does certainly *not* apply to msxml3! */
640 if(!newChild || !oldChild)
646 hr = IXMLDOMNode_QueryInterface(oldChild,&IID_IXMLDOMNode,(LPVOID*)&realOldChild);
650 old_child_ptr = impl_from_IXMLDOMNode(realOldChild)->node;
651 IXMLDOMNode_Release(realOldChild);
652 if(old_child_ptr->parent != This->node)
654 WARN("childNode %p is not a child of %p\n", oldChild, iface);
658 new_child_ptr = impl_from_IXMLDOMNode(newChild)->node;
659 my_ancestor = This->node;
662 if(my_ancestor == new_child_ptr)
664 WARN("tried to create loop\n");
667 my_ancestor = my_ancestor->parent;
670 if(!new_child_ptr->parent)
671 if(xmldoc_remove_orphan(new_child_ptr->doc, new_child_ptr) != S_OK)
672 WARN("%p is not an orphan of %p\n", new_child_ptr, new_child_ptr->doc);
674 leaving_doc = new_child_ptr->doc;
675 xmldoc_add_ref(old_child_ptr->doc);
676 xmlReplaceNode(old_child_ptr, new_child_ptr);
677 xmldoc_release(leaving_doc);
679 xmldoc_add_orphan(old_child_ptr->doc, old_child_ptr);
683 IXMLDOMNode_AddRef(oldChild);
684 *outOldChild = oldChild;
690 static HRESULT WINAPI xmlnode_removeChild(
692 IXMLDOMNode* childNode,
693 IXMLDOMNode** oldChild)
695 xmlnode *This = impl_from_IXMLDOMNode( iface );
696 xmlNode *child_node_ptr;
700 TRACE("%p->(%p, %p)\n", This, childNode, oldChild);
702 if(!childNode) return E_INVALIDARG;
707 hr = IXMLDOMNode_QueryInterface(childNode, &IID_IXMLDOMNode, (LPVOID)&child);
711 child_node_ptr = impl_from_IXMLDOMNode(child)->node;
712 if(child_node_ptr->parent != This->node)
714 WARN("childNode %p is not a child of %p\n", childNode, iface);
715 IXMLDOMNode_Release(child);
719 xmlUnlinkNode(child_node_ptr);
721 IXMLDOMNode_Release(child);
725 IXMLDOMNode_AddRef(childNode);
726 *oldChild = childNode;
732 static HRESULT WINAPI xmlnode_appendChild(
734 IXMLDOMNode* newChild,
735 IXMLDOMNode** outNewChild)
737 xmlnode *This = impl_from_IXMLDOMNode( iface );
742 TRACE("(%p)->(%p,%p)\n", This, newChild, outNewChild);
744 hr = IXMLDOMNode_get_nodeType(newChild, &type);
745 if(FAILED(hr) || type == NODE_ATTRIBUTE) {
746 if(outNewChild) *outNewChild = NULL;
751 return IXMLDOMNode_insertBefore(iface, newChild, var, outNewChild);
754 static HRESULT WINAPI xmlnode_hasChildNodes(
756 VARIANT_BOOL* hasChild)
758 xmlnode *This = impl_from_IXMLDOMNode( iface );
764 if (!This->node->children)
766 *hasChild = VARIANT_FALSE;
770 *hasChild = VARIANT_TRUE;
774 static HRESULT WINAPI xmlnode_get_ownerDocument(
776 IXMLDOMDocument** DOMDocument)
778 xmlnode *This = impl_from_IXMLDOMNode( iface );
780 TRACE("%p (%p)\n", This, DOMDocument);
782 return DOMDocument_create_from_xmldoc(This->node->doc, (IXMLDOMDocument2**)DOMDocument);
785 static HRESULT WINAPI xmlnode_cloneNode(
788 IXMLDOMNode** cloneRoot)
790 xmlnode *This = impl_from_IXMLDOMNode( iface );
791 xmlNodePtr pClone = NULL;
792 IXMLDOMNode *pNode = NULL;
794 TRACE("%p (%d)\n", This, deep);
799 pClone = xmlCopyNode(This->node, deep ? 1 : 2);
802 pClone->doc = This->node->doc;
803 xmldoc_add_orphan(pClone->doc, pClone);
805 pNode = create_node(pClone);
808 ERR("Copy failed\n");
816 ERR("Copy failed\n");
823 static HRESULT WINAPI xmlnode_get_nodeTypeString(
827 xmlnode *This = impl_from_IXMLDOMNode( iface );
830 TRACE("%p\n", This );
838 switch( This->node->type )
840 case XML_ATTRIBUTE_NODE:
841 str = (const xmlChar*) "attribute";
843 case XML_CDATA_SECTION_NODE:
844 str = (const xmlChar*) "cdatasection";
846 case XML_COMMENT_NODE:
847 str = (const xmlChar*) "comment";
849 case XML_DOCUMENT_NODE:
850 str = (const xmlChar*) "document";
852 case XML_DOCUMENT_FRAG_NODE:
853 str = (const xmlChar*) "documentfragment";
855 case XML_ELEMENT_NODE:
856 str = (const xmlChar*) "element";
858 case XML_ENTITY_NODE:
859 str = (const xmlChar*) "entity";
861 case XML_ENTITY_REF_NODE:
862 str = (const xmlChar*) "entityreference";
864 case XML_NOTATION_NODE:
865 str = (const xmlChar*) "notation";
868 str = (const xmlChar*) "processinginstruction";
871 str = (const xmlChar*) "text";
874 FIXME("Unknown node type (%d)\n", This->node->type);
875 str = This->node->name;
879 *xmlnodeType = bstr_from_xmlChar( str );
886 static HRESULT WINAPI xmlnode_get_text(
890 xmlnode *This = impl_from_IXMLDOMNode( iface );
898 switch(This->node->type)
900 case XML_ELEMENT_NODE:
901 case XML_ATTRIBUTE_NODE:
903 xmlNodePtr child = This->node->children;
904 if ( child && child->type == XML_TEXT_NODE )
905 str = bstr_from_xmlChar( child->content );
910 case XML_CDATA_SECTION_NODE:
912 case XML_COMMENT_NODE:
913 str = bstr_from_xmlChar( This->node->content );
917 FIXME("Unhandled node type %d\n", This->node->type);
920 /* Always return a string. */
921 if (!str) str = SysAllocStringLen( NULL, 0 );
923 TRACE("%p %s\n", This, debugstr_w(str) );
929 static HRESULT WINAPI xmlnode_put_text(
933 xmlnode *This = impl_from_IXMLDOMNode( iface );
934 xmlChar *str, *str2, *str3;
938 switch(This->node->type)
940 case XML_DOCUMENT_NODE:
946 str = xmlChar_from_wchar((WCHAR*)text);
948 /* Escape the string. */
949 str2 = xmlEncodeEntitiesReentrant(This->node->doc, str);
950 HeapFree(GetProcessHeap(), 0, str);
951 str3 = xmlEncodeSpecialChars(This->node->doc, str2);
954 xmlNodeSetContent(This->node, str3);
960 static HRESULT WINAPI xmlnode_get_specified(
962 VARIANT_BOOL* isSpecified)
968 static HRESULT WINAPI xmlnode_get_definition(
970 IXMLDOMNode** definitionNode)
976 static HRESULT WINAPI xmlnode_get_nodeTypedValue(
980 xmlnode *This = impl_from_IXMLDOMNode( iface );
983 FIXME("ignoring data type %p %p\n", This, typedValue);
988 V_VT(typedValue) = VT_NULL;
990 switch ( This->node->type )
992 case XML_ELEMENT_NODE:
994 xmlChar *content = xmlNodeGetContent(This->node);
995 V_VT(typedValue) = VT_BSTR;
996 V_BSTR(typedValue) = bstr_from_xmlChar( content );
1002 r = xmlnode_get_nodeValue(iface, typedValue);
1008 static HRESULT WINAPI xmlnode_put_nodeTypedValue(
1016 static HRESULT WINAPI xmlnode_get_dataType(
1018 VARIANT* dataTypeName)
1020 xmlnode *This = impl_from_IXMLDOMNode( iface );
1023 TRACE("iface %p\n", iface);
1026 return E_INVALIDARG;
1028 /* Attribute, CDATA Section, Comment, Document, Document Fragment,
1029 Entity, Notation, PI, and Text Node are non-typed. */
1030 V_BSTR(dataTypeName) = NULL;
1031 V_VT(dataTypeName) = VT_NULL;
1033 switch ( This->node->type )
1035 case XML_ELEMENT_NODE:
1036 pVal = xmlGetNsProp(This->node, (xmlChar*)"dt",
1037 (xmlChar*)"urn:schemas-microsoft-com:datatypes");
1040 V_VT(dataTypeName) = VT_BSTR;
1041 V_BSTR(dataTypeName) = bstr_from_xmlChar( pVal );
1045 case XML_ENTITY_REF_NODE:
1046 FIXME("XML_ENTITY_REF_NODE should return a valid value.\n");
1049 TRACE("Type %d returning NULL\n", This->node->type);
1052 /* non-typed nodes return S_FALSE */
1053 if(V_VT(dataTypeName) == VT_NULL)
1061 static HRESULT WINAPI xmlnode_put_dataType(
1065 xmlnode *This = impl_from_IXMLDOMNode( iface );
1066 HRESULT hr = E_FAIL;
1068 TRACE("iface %p\n", iface);
1070 if(dataTypeName == NULL)
1071 return E_INVALIDARG;
1073 /* An example of this is. The Text in the node needs to be a 0 or 1 for a boolean type.
1074 This applies to changing types (string->bool) or setting a new one
1076 FIXME("Need to Validate the data before allowing a type to be set.\n");
1078 /* Check all supported types. */
1079 if(lstrcmpiW(dataTypeName,szString) == 0 ||
1080 lstrcmpiW(dataTypeName,szNumber) == 0 ||
1081 lstrcmpiW(dataTypeName,szUUID) == 0 ||
1082 lstrcmpiW(dataTypeName,szInt) == 0 ||
1083 lstrcmpiW(dataTypeName,szI4) == 0 ||
1084 lstrcmpiW(dataTypeName,szFixed) == 0 ||
1085 lstrcmpiW(dataTypeName,szBoolean) == 0 ||
1086 lstrcmpiW(dataTypeName,szDateTime) == 0 ||
1087 lstrcmpiW(dataTypeName,szDateTimeTZ) == 0 ||
1088 lstrcmpiW(dataTypeName,szDate) == 0 ||
1089 lstrcmpiW(dataTypeName,szTime) == 0 ||
1090 lstrcmpiW(dataTypeName,szTimeTZ) == 0 ||
1091 lstrcmpiW(dataTypeName,szI1) == 0 ||
1092 lstrcmpiW(dataTypeName,szI2) == 0 ||
1093 lstrcmpiW(dataTypeName,szIU1) == 0 ||
1094 lstrcmpiW(dataTypeName,szIU2) == 0 ||
1095 lstrcmpiW(dataTypeName,szIU4) == 0 ||
1096 lstrcmpiW(dataTypeName,szR4) == 0 ||
1097 lstrcmpiW(dataTypeName,szR8) == 0 ||
1098 lstrcmpiW(dataTypeName,szFloat) == 0 ||
1099 lstrcmpiW(dataTypeName,szBinHex) == 0 ||
1100 lstrcmpiW(dataTypeName,szBinBase64) == 0)
1102 xmlNsPtr pNS = NULL;
1103 xmlAttrPtr pAttr = NULL;
1104 xmlChar* str = xmlChar_from_wchar((WCHAR*)dataTypeName);
1106 pAttr = xmlHasNsProp(This->node, (xmlChar*)"dt",
1107 (xmlChar*)"urn:schemas-microsoft-com:datatypes");
1110 pAttr = xmlSetNsProp(This->node, pAttr->ns, (xmlChar*)"dt", str);
1116 pNS = xmlNewNs(This->node, (xmlChar*)"urn:schemas-microsoft-com:datatypes", (xmlChar*)"dt");
1119 pAttr = xmlNewNsProp(This->node, pNS, (xmlChar*)"dt", str);
1122 xmlAddChild(This->node, (xmlNodePtr)pAttr);
1127 ERR("Failed to create Attribute\n");
1130 ERR("Failed to Create Namepsace\n");
1132 HeapFree( GetProcessHeap(), 0, str );
1138 static BSTR EnsureCorrectEOL(BSTR sInput)
1140 static const WCHAR SZ_RETURN[] = {'\n',0};
1141 static const WCHAR SZ_LINEFEED[] = {'\r',0};
1147 nLen = lstrlenW(sInput);
1148 /* Count line endings */
1149 for(i=0; i < nLen; i++)
1151 if(sInput[i] == SZ_RETURN[0])
1155 TRACE("len=%d, num=%d\n", nLen, nNum);
1157 /* Add linefeed as needed */
1161 sNew = SysAllocStringLen(NULL, nLen + nNum+1);
1162 for(i=0; i < nLen; i++)
1164 if(sInput[i] == SZ_RETURN[0])
1166 sNew[i+nPlace] = SZ_LINEFEED[0];
1169 sNew[i+nPlace] = sInput[i];
1172 SysFreeString(sInput);
1179 TRACE("len %d\n", lstrlenW(sNew));
1184 /* Removes encoding information and last character (nullbyte) */
1185 static BSTR EnsureNoEncoding(BSTR sInput)
1187 static const WCHAR wszEncoding[] = {'e','n','c','o','d','i','n','g','='};
1192 while(*pBeg != '\n' && memcmp(pBeg, wszEncoding, sizeof(wszEncoding)))
1197 SysReAllocStringLen(&sInput, sInput, SysStringLen(sInput)-1);
1202 pEnd = pBeg + sizeof(wszEncoding)/sizeof(WCHAR) + 2;
1203 while(*pEnd != '\"') pEnd++;
1206 sNew = SysAllocStringLen(NULL,
1207 pBeg-sInput + SysStringLen(sInput)-(pEnd-sInput)-1);
1208 memcpy(sNew, sInput, (pBeg-sInput)*sizeof(WCHAR));
1209 memcpy(&sNew[pBeg-sInput], pEnd, (SysStringLen(sInput)-(pEnd-sInput)-1)*sizeof(WCHAR));
1211 SysFreeString(sInput);
1216 * We are trying to replicate the same behaviour as msxml by converting
1217 * line endings to \r\n and using idents as \t. The problem is that msxml
1218 * only formats nodes that have a line ending. Using libxml we cannot
1219 * reproduce behaviour exactly.
1222 static HRESULT WINAPI xmlnode_get_xml(
1226 xmlnode *This = impl_from_IXMLDOMNode( iface );
1227 xmlBufferPtr pXmlBuf;
1230 TRACE("iface %p %d\n", iface, This->node->type);
1233 return E_INVALIDARG;
1237 pXmlBuf = xmlBufferCreate();
1240 nSize = xmlNodeDump(pXmlBuf, This->node->doc, This->node, 0, 1);
1243 const xmlChar *pContent;
1246 /* Attribute Nodes return a space in front of their name */
1247 pContent = xmlBufferContent(pXmlBuf);
1248 if( ((char*)pContent)[0] == ' ')
1249 bstrContent = bstr_from_xmlChar(pContent+1);
1251 bstrContent = bstr_from_xmlChar(pContent);
1253 switch(This->node->type)
1255 case XML_ELEMENT_NODE:
1256 *xmlString = EnsureCorrectEOL(bstrContent);
1258 case XML_DOCUMENT_NODE:
1259 *xmlString = EnsureCorrectEOL(bstrContent);
1260 *xmlString = EnsureNoEncoding(*xmlString);
1263 *xmlString = bstrContent;
1267 xmlBufferFree(pXmlBuf);
1270 /* Always returns a string. */
1271 if(*xmlString == NULL) *xmlString = SysAllocStringLen( NULL, 0 );
1276 static HRESULT WINAPI xmlnode_transformNode(
1278 IXMLDOMNode* styleSheet,
1282 xmlnode *This = impl_from_IXMLDOMNode( iface );
1283 xmlnode *pStyleSheet = NULL;
1284 xsltStylesheetPtr xsltSS = NULL;
1285 xmlDocPtr result = NULL;
1288 TRACE("%p %p %p\n", This, styleSheet, xmlString);
1290 if(!styleSheet || !xmlString)
1291 return E_INVALIDARG;
1295 if(IXMLDOMNode_QueryInterface(styleSheet, &IID_IXMLDOMNode, (LPVOID)&ssNew) == S_OK)
1297 pStyleSheet = impl_from_IXMLDOMNode( ssNew );
1299 xsltSS = xsltParseStylesheetDoc( pStyleSheet->node->doc);
1302 result = xsltApplyStylesheet(xsltSS, This->node->doc, NULL);
1305 const xmlChar *pContent;
1307 if(result->type == XML_HTML_DOCUMENT_NODE)
1309 xmlOutputBufferPtr pOutput = xmlAllocOutputBuffer(NULL);
1312 htmlDocContentDumpOutput(pOutput, result->doc, NULL);
1313 pContent = xmlBufferContent(pOutput->buffer);
1314 *xmlString = bstr_from_xmlChar(pContent);
1315 xmlOutputBufferClose(pOutput);
1320 xmlBufferPtr pXmlBuf;
1323 pXmlBuf = xmlBufferCreate();
1326 nSize = xmlNodeDump(pXmlBuf, NULL, (xmlNodePtr)result, 0, 0);
1329 pContent = xmlBufferContent(pXmlBuf);
1330 *xmlString = bstr_from_xmlChar(pContent);
1332 xmlBufferFree(pXmlBuf);
1337 /* libxslt "helpfully" frees the XML document the stylesheet was
1338 generated from, too */
1340 xsltFreeStylesheet(xsltSS);
1343 IXMLDOMNode_Release(ssNew);
1346 if(*xmlString == NULL)
1347 *xmlString = SysAllocStringLen(NULL, 0);
1351 FIXME("libxslt headers were not found at compile time\n");
1356 static HRESULT WINAPI xmlnode_selectNodes(
1359 IXMLDOMNodeList** resultList)
1361 xmlnode *This = impl_from_IXMLDOMNode( iface );
1363 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultList );
1365 return queryresult_create( This->node, queryString, resultList );
1368 static HRESULT WINAPI xmlnode_selectSingleNode(
1371 IXMLDOMNode** resultNode)
1373 xmlnode *This = impl_from_IXMLDOMNode( iface );
1374 IXMLDOMNodeList *list;
1377 TRACE("%p %s %p\n", This, debugstr_w(queryString), resultNode );
1380 r = IXMLDOMNode_selectNodes(iface, queryString, &list);
1383 r = IXMLDOMNodeList_nextNode(list, resultNode);
1384 IXMLDOMNodeList_Release(list);
1389 static HRESULT WINAPI xmlnode_get_parsed(
1391 VARIANT_BOOL* isParsed)
1397 static HRESULT WINAPI xmlnode_get_namespaceURI(
1401 xmlnode *This = impl_from_IXMLDOMNode( iface );
1402 HRESULT hr = S_FALSE;
1405 TRACE("%p %p\n", This, namespaceURI );
1408 return E_INVALIDARG;
1410 *namespaceURI = NULL;
1412 pNSList = xmlGetNsList(This->node->doc, This->node);
1415 *namespaceURI = bstr_from_xmlChar( pNSList[0]->href );
1424 static HRESULT WINAPI xmlnode_get_prefix(
1428 xmlnode *This = impl_from_IXMLDOMNode( iface );
1429 HRESULT hr = S_FALSE;
1432 TRACE("%p %p\n", This, prefixString );
1435 return E_INVALIDARG;
1437 *prefixString = NULL;
1439 pNSList = xmlGetNsList(This->node->doc, This->node);
1442 *prefixString = bstr_from_xmlChar( pNSList[0]->prefix );
1451 static HRESULT WINAPI xmlnode_get_baseName(
1455 xmlnode *This = impl_from_IXMLDOMNode( iface );
1457 HRESULT r = S_FALSE;
1459 TRACE("%p %p\n", This, nameString );
1462 return E_INVALIDARG;
1464 switch ( This->node->type )
1466 case XML_ELEMENT_NODE:
1467 case XML_ATTRIBUTE_NODE:
1468 str = bstr_from_xmlChar( This->node->name );
1474 ERR("Unhandled type %d\n", This->node->type );
1478 TRACE("returning %08x str = %s\n", r, debugstr_w( str ) );
1484 static HRESULT WINAPI xmlnode_transformNodeToObject(
1486 IXMLDOMNode* stylesheet,
1487 VARIANT outputObject)
1493 static const struct IXMLDOMNodeVtbl xmlnode_vtbl =
1495 xmlnode_QueryInterface,
1498 xmlnode_GetTypeInfoCount,
1499 xmlnode_GetTypeInfo,
1500 xmlnode_GetIDsOfNames,
1502 xmlnode_get_nodeName,
1503 xmlnode_get_nodeValue,
1504 xmlnode_put_nodeValue,
1505 xmlnode_get_nodeType,
1506 xmlnode_get_parentNode,
1507 xmlnode_get_childNodes,
1508 xmlnode_get_firstChild,
1509 xmlnode_get_lastChild,
1510 xmlnode_get_previousSibling,
1511 xmlnode_get_nextSibling,
1512 xmlnode_get_attributes,
1513 xmlnode_insertBefore,
1514 xmlnode_replaceChild,
1515 xmlnode_removeChild,
1516 xmlnode_appendChild,
1517 xmlnode_hasChildNodes,
1518 xmlnode_get_ownerDocument,
1520 xmlnode_get_nodeTypeString,
1523 xmlnode_get_specified,
1524 xmlnode_get_definition,
1525 xmlnode_get_nodeTypedValue,
1526 xmlnode_put_nodeTypedValue,
1527 xmlnode_get_dataType,
1528 xmlnode_put_dataType,
1530 xmlnode_transformNode,
1531 xmlnode_selectNodes,
1532 xmlnode_selectSingleNode,
1534 xmlnode_get_namespaceURI,
1536 xmlnode_get_baseName,
1537 xmlnode_transformNodeToObject,
1540 static HRESULT WINAPI Internal_QueryInterface(
1545 xmlnode *This = impl_from_InternalUnknown( iface );
1547 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
1550 if ( IsEqualGUID( riid, &IID_IUnknown ))
1552 else if ( IsEqualGUID( riid, &IID_IDispatch ) ||
1553 IsEqualGUID( riid, &IID_IXMLDOMNode ) )
1554 *ppvObject = &This->lpVtbl;
1557 FIXME("interface %s not implemented\n", debugstr_guid(riid));
1559 return E_NOINTERFACE;
1562 IUnknown_AddRef( (IUnknown*)*ppvObject );
1567 static ULONG WINAPI Internal_AddRef(
1570 xmlnode *This = impl_from_InternalUnknown( iface );
1571 return InterlockedIncrement( &This->ref );
1574 static ULONG WINAPI Internal_Release(
1577 xmlnode *This = impl_from_InternalUnknown( iface );
1580 ref = InterlockedDecrement( &This->ref );
1584 xmldoc_release( This->node->doc );
1585 HeapFree( GetProcessHeap(), 0, This );
1591 static const struct IUnknownVtbl internal_unk_vtbl =
1593 Internal_QueryInterface,
1598 IUnknown *create_basic_node( xmlNodePtr node, IUnknown *pUnkOuter )
1602 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
1607 xmldoc_add_ref( node->doc );
1609 This->lpVtbl = &xmlnode_vtbl;
1610 This->lpInternalUnkVtbl = &internal_unk_vtbl;
1613 This->pUnkOuter = pUnkOuter; /* Don't take a ref on outer Unknown */
1615 This->pUnkOuter = (IUnknown *)&This->lpInternalUnkVtbl;
1620 return (IUnknown*)&This->lpInternalUnkVtbl;
1623 IXMLDOMNode *create_node( xmlNodePtr node )
1632 TRACE("type %d\n", node->type);
1635 case XML_ELEMENT_NODE:
1636 pUnk = create_element( node, NULL );
1638 case XML_ATTRIBUTE_NODE:
1639 pUnk = create_attribute( node );
1642 pUnk = create_text( node );
1644 case XML_CDATA_SECTION_NODE:
1645 pUnk = create_cdata( node );
1647 case XML_COMMENT_NODE:
1648 pUnk = create_comment( node );
1650 case XML_DOCUMENT_NODE:
1651 pUnk = create_domdoc( node );
1654 FIXME("only creating basic node for type %d\n", node->type);
1655 pUnk = create_basic_node( node, NULL );
1658 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
1659 IUnknown_Release(pUnk);
1660 if(FAILED(hr)) return NULL;