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
27 # include <libxml/parser.h>
28 # include <libxml/xmlerror.h>
38 #include "msxml_private.h"
40 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(msxml
);
46 static const xmlChar DT_prefix
[] = "dt";
47 static const xmlChar DT_nsURI
[] = "urn:schemas-microsoft-com:datatypes";
49 typedef struct _domelem
52 IXMLDOMElement IXMLDOMElement_iface
;
56 static const struct nodemap_funcs domelem_attr_map
;
58 static const tid_t domelem_se_tids
[] = {
64 static inline domelem
*impl_from_IXMLDOMElement( IXMLDOMElement
*iface
)
66 return CONTAINING_RECORD(iface
, domelem
, IXMLDOMElement_iface
);
69 static inline xmlNodePtr
get_element( const domelem
*This
)
71 return This
->node
.node
;
74 static HRESULT WINAPI
domelem_QueryInterface(
75 IXMLDOMElement
*iface
,
79 domelem
*This
= impl_from_IXMLDOMElement( iface
);
81 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppvObject
);
83 if ( IsEqualGUID( riid
, &IID_IXMLDOMElement
) ||
84 IsEqualGUID( riid
, &IID_IXMLDOMNode
) ||
85 IsEqualGUID( riid
, &IID_IDispatch
) ||
86 IsEqualGUID( riid
, &IID_IUnknown
) )
88 *ppvObject
= &This
->IXMLDOMElement_iface
;
90 else if(node_query_interface(&This
->node
, riid
, ppvObject
))
92 return *ppvObject
? S_OK
: E_NOINTERFACE
;
94 else if(IsEqualGUID( riid
, &IID_ISupportErrorInfo
))
96 return node_create_supporterrorinfo(domelem_se_tids
, ppvObject
);
100 TRACE("interface %s not implemented\n", debugstr_guid(riid
));
102 return E_NOINTERFACE
;
105 IUnknown_AddRef( (IUnknown
*)*ppvObject
);
109 static ULONG WINAPI
domelem_AddRef(
110 IXMLDOMElement
*iface
)
112 domelem
*This
= impl_from_IXMLDOMElement( iface
);
113 LONG ref
= InterlockedIncrement(&This
->ref
);
115 TRACE("(%p)->(%d)\n", This
, ref
);
120 static ULONG WINAPI
domelem_Release(
121 IXMLDOMElement
*iface
)
123 domelem
*This
= impl_from_IXMLDOMElement( iface
);
124 ULONG ref
= InterlockedDecrement(&This
->ref
);
126 TRACE("(%p)->(%d)\n", This
, ref
);
129 destroy_xmlnode(&This
->node
);
136 static HRESULT WINAPI
domelem_GetTypeInfoCount(
137 IXMLDOMElement
*iface
,
140 domelem
*This
= impl_from_IXMLDOMElement( iface
);
141 return IDispatchEx_GetTypeInfoCount(&This
->node
.dispex
.IDispatchEx_iface
, pctinfo
);
144 static HRESULT WINAPI
domelem_GetTypeInfo(
145 IXMLDOMElement
*iface
,
146 UINT iTInfo
, LCID lcid
,
147 ITypeInfo
** ppTInfo
)
149 domelem
*This
= impl_from_IXMLDOMElement( iface
);
150 return IDispatchEx_GetTypeInfo(&This
->node
.dispex
.IDispatchEx_iface
,
151 iTInfo
, lcid
, ppTInfo
);
154 static HRESULT WINAPI
domelem_GetIDsOfNames(
155 IXMLDOMElement
*iface
,
156 REFIID riid
, LPOLESTR
* rgszNames
,
157 UINT cNames
, LCID lcid
, DISPID
* rgDispId
)
159 domelem
*This
= impl_from_IXMLDOMElement( iface
);
160 return IDispatchEx_GetIDsOfNames(&This
->node
.dispex
.IDispatchEx_iface
,
161 riid
, rgszNames
, cNames
, lcid
, rgDispId
);
164 static HRESULT WINAPI
domelem_Invoke(
165 IXMLDOMElement
*iface
,
166 DISPID dispIdMember
, REFIID riid
, LCID lcid
,
167 WORD wFlags
, DISPPARAMS
* pDispParams
, VARIANT
* pVarResult
,
168 EXCEPINFO
* pExcepInfo
, UINT
* puArgErr
)
170 domelem
*This
= impl_from_IXMLDOMElement( iface
);
171 return IDispatchEx_Invoke(&This
->node
.dispex
.IDispatchEx_iface
,
172 dispIdMember
, riid
, lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
175 static HRESULT WINAPI
domelem_get_nodeName(
176 IXMLDOMElement
*iface
,
179 domelem
*This
= impl_from_IXMLDOMElement( iface
);
181 TRACE("(%p)->(%p)\n", This
, p
);
183 return node_get_nodeName(&This
->node
, p
);
186 static HRESULT WINAPI
domelem_get_nodeValue(
187 IXMLDOMElement
*iface
,
190 domelem
*This
= impl_from_IXMLDOMElement( iface
);
192 TRACE("(%p)->(%p)\n", This
, value
);
197 V_VT(value
) = VT_NULL
;
198 V_BSTR(value
) = NULL
; /* tests show that we should do this */
202 static HRESULT WINAPI
domelem_put_nodeValue(
203 IXMLDOMElement
*iface
,
206 domelem
*This
= impl_from_IXMLDOMElement( iface
);
207 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&value
));
211 static HRESULT WINAPI
domelem_get_nodeType(
212 IXMLDOMElement
*iface
,
213 DOMNodeType
* domNodeType
)
215 domelem
*This
= impl_from_IXMLDOMElement( iface
);
217 TRACE("(%p)->(%p)\n", This
, domNodeType
);
219 *domNodeType
= NODE_ELEMENT
;
223 static HRESULT WINAPI
domelem_get_parentNode(
224 IXMLDOMElement
*iface
,
225 IXMLDOMNode
** parent
)
227 domelem
*This
= impl_from_IXMLDOMElement( iface
);
229 TRACE("(%p)->(%p)\n", This
, parent
);
231 return node_get_parent(&This
->node
, parent
);
234 static HRESULT WINAPI
domelem_get_childNodes(
235 IXMLDOMElement
*iface
,
236 IXMLDOMNodeList
** outList
)
238 domelem
*This
= impl_from_IXMLDOMElement( iface
);
240 TRACE("(%p)->(%p)\n", This
, outList
);
242 return node_get_child_nodes(&This
->node
, outList
);
245 static HRESULT WINAPI
domelem_get_firstChild(
246 IXMLDOMElement
*iface
,
247 IXMLDOMNode
** domNode
)
249 domelem
*This
= impl_from_IXMLDOMElement( iface
);
251 TRACE("(%p)->(%p)\n", This
, domNode
);
253 return node_get_first_child(&This
->node
, domNode
);
256 static HRESULT WINAPI
domelem_get_lastChild(
257 IXMLDOMElement
*iface
,
258 IXMLDOMNode
** domNode
)
260 domelem
*This
= impl_from_IXMLDOMElement( iface
);
262 TRACE("(%p)->(%p)\n", This
, domNode
);
264 return node_get_last_child(&This
->node
, domNode
);
267 static HRESULT WINAPI
domelem_get_previousSibling(
268 IXMLDOMElement
*iface
,
269 IXMLDOMNode
** domNode
)
271 domelem
*This
= impl_from_IXMLDOMElement( iface
);
273 TRACE("(%p)->(%p)\n", This
, domNode
);
275 return node_get_previous_sibling(&This
->node
, domNode
);
278 static HRESULT WINAPI
domelem_get_nextSibling(
279 IXMLDOMElement
*iface
,
280 IXMLDOMNode
** domNode
)
282 domelem
*This
= impl_from_IXMLDOMElement( iface
);
284 TRACE("(%p)->(%p)\n", This
, domNode
);
286 return node_get_next_sibling(&This
->node
, domNode
);
289 static HRESULT WINAPI
domelem_get_attributes(
290 IXMLDOMElement
*iface
,
291 IXMLDOMNamedNodeMap
** map
)
293 domelem
*This
= impl_from_IXMLDOMElement( iface
);
295 TRACE("(%p)->(%p)\n", This
, map
);
297 *map
= create_nodemap(This
->node
.node
, &domelem_attr_map
);
301 static HRESULT WINAPI
domelem_insertBefore(
302 IXMLDOMElement
*iface
,
303 IXMLDOMNode
* newNode
, VARIANT refChild
,
304 IXMLDOMNode
** old_node
)
306 domelem
*This
= impl_from_IXMLDOMElement( iface
);
310 TRACE("(%p)->(%p %s %p)\n", This
, newNode
, debugstr_variant(&refChild
), old_node
);
312 hr
= IXMLDOMNode_get_nodeType(newNode
, &type
);
313 if (hr
!= S_OK
) return hr
;
315 TRACE("new node type %d\n", type
);
319 case NODE_DOCUMENT_TYPE
:
322 if (old_node
) *old_node
= NULL
;
325 return node_insert_before(&This
->node
, newNode
, &refChild
, old_node
);
329 static HRESULT WINAPI
domelem_replaceChild(
330 IXMLDOMElement
*iface
,
331 IXMLDOMNode
* newNode
,
332 IXMLDOMNode
* oldNode
,
333 IXMLDOMNode
** outOldNode
)
335 domelem
*This
= impl_from_IXMLDOMElement( iface
);
337 TRACE("(%p)->(%p %p %p)\n", This
, newNode
, oldNode
, outOldNode
);
339 return node_replace_child(&This
->node
, newNode
, oldNode
, outOldNode
);
342 static HRESULT WINAPI
domelem_removeChild(
343 IXMLDOMElement
*iface
,
344 IXMLDOMNode
*child
, IXMLDOMNode
**oldChild
)
346 domelem
*This
= impl_from_IXMLDOMElement( iface
);
347 TRACE("(%p)->(%p %p)\n", This
, child
, oldChild
);
348 return node_remove_child(&This
->node
, child
, oldChild
);
351 static HRESULT WINAPI
domelem_appendChild(
352 IXMLDOMElement
*iface
,
353 IXMLDOMNode
*child
, IXMLDOMNode
**outChild
)
355 domelem
*This
= impl_from_IXMLDOMElement( iface
);
356 TRACE("(%p)->(%p %p)\n", This
, child
, outChild
);
357 return node_append_child(&This
->node
, child
, outChild
);
360 static HRESULT WINAPI
domelem_hasChildNodes(
361 IXMLDOMElement
*iface
,
364 domelem
*This
= impl_from_IXMLDOMElement( iface
);
365 TRACE("(%p)->(%p)\n", This
, ret
);
366 return node_has_childnodes(&This
->node
, ret
);
369 static HRESULT WINAPI
domelem_get_ownerDocument(
370 IXMLDOMElement
*iface
,
371 IXMLDOMDocument
**doc
)
373 domelem
*This
= impl_from_IXMLDOMElement( iface
);
374 TRACE("(%p)->(%p)\n", This
, doc
);
375 return node_get_owner_doc(&This
->node
, doc
);
378 static HRESULT WINAPI
domelem_cloneNode(
379 IXMLDOMElement
*iface
,
380 VARIANT_BOOL deep
, IXMLDOMNode
** outNode
)
382 domelem
*This
= impl_from_IXMLDOMElement( iface
);
383 TRACE("(%p)->(%d %p)\n", This
, deep
, outNode
);
384 return node_clone( &This
->node
, deep
, outNode
);
387 static HRESULT WINAPI
domelem_get_nodeTypeString(
388 IXMLDOMElement
*iface
,
391 domelem
*This
= impl_from_IXMLDOMElement( iface
);
392 static const WCHAR elementW
[] = {'e','l','e','m','e','n','t',0};
394 TRACE("(%p)->(%p)\n", This
, p
);
396 return return_bstr(elementW
, p
);
399 static HRESULT WINAPI
domelem_get_text(
400 IXMLDOMElement
*iface
,
403 domelem
*This
= impl_from_IXMLDOMElement( iface
);
404 TRACE("(%p)->(%p)\n", This
, p
);
405 return node_get_text(&This
->node
, p
);
408 static HRESULT WINAPI
domelem_put_text(
409 IXMLDOMElement
*iface
,
412 domelem
*This
= impl_from_IXMLDOMElement( iface
);
413 TRACE("(%p)->(%s)\n", This
, debugstr_w(p
));
414 return node_put_text( &This
->node
, p
);
417 static HRESULT WINAPI
domelem_get_specified(
418 IXMLDOMElement
*iface
,
419 VARIANT_BOOL
* isSpecified
)
421 domelem
*This
= impl_from_IXMLDOMElement( iface
);
422 FIXME("(%p)->(%p) stub!\n", This
, isSpecified
);
423 *isSpecified
= VARIANT_TRUE
;
427 static HRESULT WINAPI
domelem_get_definition(
428 IXMLDOMElement
*iface
,
429 IXMLDOMNode
** definitionNode
)
431 domelem
*This
= impl_from_IXMLDOMElement( iface
);
432 FIXME("(%p)->(%p)\n", This
, definitionNode
);
436 static inline BYTE
hex_to_byte(xmlChar c
)
438 if(c
<= '9') return c
-'0';
439 if(c
<= 'F') return c
-'A'+10;
443 static inline BYTE
base64_to_byte(xmlChar c
)
445 if(c
== '+') return 62;
446 if(c
== '/') return 63;
447 if(c
<= '9') return c
-'0'+52;
448 if(c
<= 'Z') return c
-'A';
452 static inline HRESULT
variant_from_dt(XDR_DT dt
, xmlChar
* str
, VARIANT
* v
)
456 BOOL handled
= FALSE
;
471 V_BSTR(v
) = bstr_from_xmlChar(str
);
474 return E_OUTOFMEMORY
;
492 st
.wDayOfWeek
= st
.wHour
= st
.wMinute
= st
.wSecond
= st
.wMilliseconds
= 0;
494 V_VT(&src
) = VT_BSTR
;
495 V_BSTR(&src
) = bstr_from_xmlChar(str
);
498 return E_OUTOFMEMORY
;
501 e
= p
+ SysStringLen(V_BSTR(&src
));
503 if(p
+4<e
&& *(p
+4)=='-') /* parse date (yyyy-mm-dd) */
506 st
.wMonth
= atoiW(p
+5);
507 st
.wDay
= atoiW(p
+8);
513 if(p
+2<e
&& *(p
+2)==':') /* parse time (hh:mm:ss.?) */
516 st
.wMinute
= atoiW(p
+3);
517 st
.wSecond
= atoiW(p
+6);
523 while(isdigitW(*p
)) p
++;
527 SystemTimeToVariantTime(&st
, &date
);
531 if(*p
== '+') /* parse timezone offset (+hh:mm) */
532 V_DATE(v
) += (DOUBLE
)atoiW(p
+1)/24 + (DOUBLE
)atoiW(p
+4)/1440;
533 else if(*p
== '-') /* parse timezone offset (-hh:mm) */
534 V_DATE(v
) -= (DOUBLE
)atoiW(p
+1)/24 + (DOUBLE
)atoiW(p
+4)/1440;
545 len
= xmlStrlen(str
)/2;
549 V_VT(v
) = (VT_ARRAY
|VT_UI1
);
550 V_ARRAY(v
) = SafeArrayCreate(VT_UI1
, 1, &sab
);
553 return E_OUTOFMEMORY
;
556 ((BYTE
*)V_ARRAY(v
)->pvData
)[i
] = (hex_to_byte(str
[2*i
])<<4)
557 + hex_to_byte(str
[2*i
+1]);
567 /* remove all formatting chars */
572 if ( *c2
== ' ' || *c2
== '\t' ||
573 *c2
== '\n' || *c2
== '\r' )
583 if(str
[len
-2] == '=') i
= 2;
584 else if(str
[len
-1] == '=') i
= 1;
588 sab
.cElements
= len
/4*3-i
;
590 V_VT(v
) = (VT_ARRAY
|VT_UI1
);
591 V_ARRAY(v
) = SafeArrayCreate(VT_UI1
, 1, &sab
);
594 return E_OUTOFMEMORY
;
596 for(i
=0; i
<len
/4; i
++)
598 ((BYTE
*)V_ARRAY(v
)->pvData
)[3*i
] = (base64_to_byte(str
[4*i
])<<2)
599 + (base64_to_byte(str
[4*i
+1])>>4);
600 if(3*i
+1 < sab
.cElements
)
601 ((BYTE
*)V_ARRAY(v
)->pvData
)[3*i
+1] = (base64_to_byte(str
[4*i
+1])<<4)
602 + (base64_to_byte(str
[4*i
+2])>>2);
603 if(3*i
+2 < sab
.cElements
)
604 ((BYTE
*)V_ARRAY(v
)->pvData
)[3*i
+2] = (base64_to_byte(str
[4*i
+2])<<6)
605 + base64_to_byte(str
[4*i
+3]);
656 FIXME("need to handle dt:%s\n", debugstr_dt(dt
));
658 V_BSTR(v
) = bstr_from_xmlChar(str
);
660 return E_OUTOFMEMORY
;
664 WARN("unknown type %d\n", dt
);
669 V_VT(&src
) = VT_BSTR
;
670 V_BSTR(&src
) = bstr_from_xmlChar(str
);
673 return E_OUTOFMEMORY
;
675 hr
= VariantChangeTypeEx(v
, &src
,
676 MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
),0, V_VT(v
));
682 static XDR_DT
element_get_dt(xmlNodePtr node
)
684 XDR_DT dt
= DT_INVALID
;
686 TRACE("(%p)\n", node
);
687 if(node
->type
!= XML_ELEMENT_NODE
)
689 FIXME("invalid element node\n");
693 if (node
->ns
&& xmlStrEqual(node
->ns
->href
, DT_nsURI
))
695 dt
= str_to_dt(node
->name
, -1);
699 xmlChar
* pVal
= xmlGetNsProp(node
, BAD_CAST
"dt", DT_nsURI
);
702 dt
= str_to_dt(pVal
, -1);
707 IXMLDOMDocument3
* doc
= (IXMLDOMDocument3
*)create_domdoc((xmlNodePtr
)node
->doc
);
713 if (IXMLDOMDocument3_get_schemas(doc
, &v
) == S_OK
&&
714 V_VT(&v
) == VT_DISPATCH
)
716 dt
= SchemaCache_get_node_dt((IXMLDOMSchemaCollection2
*)V_DISPATCH(&v
), node
);
719 IXMLDOMDocument3_Release(doc
);
724 TRACE("=> dt:%s\n", debugstr_dt(dt
));
728 static HRESULT WINAPI
domelem_get_nodeTypedValue(
729 IXMLDOMElement
*iface
,
732 domelem
*This
= impl_from_IXMLDOMElement( iface
);
737 TRACE("(%p)->(%p)\n", This
, v
);
739 if(!v
) return E_INVALIDARG
;
743 dt
= element_get_dt(get_element(This
));
744 content
= xmlNodeGetContent(get_element(This
));
745 hr
= variant_from_dt(dt
, content
, v
);
751 static HRESULT
encode_base64(const BYTE
*buf
, int len
, BSTR
*ret
)
753 static const char b64
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
755 int bytes
, pad_bytes
, div
;
759 bytes
= (len
*8 + 5)/6;
760 pad_bytes
= (bytes
% 4) ? 4 - (bytes
% 4) : 0;
762 TRACE("%d, bytes is %d, pad bytes is %d\n", len
, bytes
, pad_bytes
);
763 needed
= bytes
+ pad_bytes
+ 1;
765 *ret
= SysAllocStringLen(NULL
, needed
);
766 if (!*ret
) return E_OUTOFMEMORY
;
768 /* Three bytes of input give 4 chars of output */
774 /* first char is the first 6 bits of the first byte*/
775 *ptr
++ = b64
[ ( d
[0] >> 2) & 0x3f ];
776 /* second char is the last 2 bits of the first byte and the first 4
777 * bits of the second byte */
778 *ptr
++ = b64
[ ((d
[0] << 4) & 0x30) | (d
[1] >> 4 & 0x0f)];
779 /* third char is the last 4 bits of the second byte and the first 2
780 * bits of the third byte */
781 *ptr
++ = b64
[ ((d
[1] << 2) & 0x3c) | (d
[2] >> 6 & 0x03)];
782 /* fourth char is the remaining 6 bits of the third byte */
783 *ptr
++ = b64
[ d
[2] & 0x3f];
791 /* first char is the first 6 bits of the first byte*/
792 *ptr
++ = b64
[ ( d
[0] >> 2) & 0x3f ];
793 /* second char is the last 2 bits of the first byte and the first 4
794 * bits of the second byte */
795 *ptr
++ = b64
[ ((d
[0] << 4) & 0x30) | (d
[1] >> 4 & 0x0f)];
796 /* third char is the last 4 bits of the second byte padded with
798 *ptr
++ = b64
[ ((d
[1] << 2) & 0x3c) ];
799 /* fourth char is a = to indicate one byte of padding */
803 /* first char is the first 6 bits of the first byte*/
804 *ptr
++ = b64
[ ( d
[0] >> 2) & 0x3f ];
805 /* second char is the last 2 bits of the first byte padded with
807 *ptr
++ = b64
[ ((d
[0] << 4) & 0x30)];
808 /* third char is = to indicate padding */
810 /* fourth char is = to indicate padding */
818 static HRESULT
encode_binhex(const BYTE
*buf
, int len
, BSTR
*ret
)
820 static const char byte_to_hex
[16] = "0123456789abcdef";
823 *ret
= SysAllocStringLen(NULL
, len
*2);
824 if (!*ret
) return E_OUTOFMEMORY
;
826 for (i
= 0; i
< len
; i
++)
828 (*ret
)[2*i
] = byte_to_hex
[buf
[i
] >> 4];
829 (*ret
)[2*i
+1] = byte_to_hex
[0x0f & buf
[i
]];
835 static HRESULT WINAPI
domelem_put_nodeTypedValue(
836 IXMLDOMElement
*iface
,
839 domelem
*This
= impl_from_IXMLDOMElement( iface
);
843 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&value
));
845 dt
= element_get_dt(get_element(This
));
848 /* for untyped node coerce to BSTR and set */
850 if (V_VT(&value
) != VT_BSTR
)
853 VariantInit(&content
);
854 hr
= VariantChangeType(&content
, &value
, 0, VT_BSTR
);
857 hr
= node_set_content(&This
->node
, V_BSTR(&content
));
858 VariantClear(&content
);
862 hr
= node_set_content(&This
->node
, V_BSTR(&value
));
865 if (V_VT(&value
) == VT_BSTR
)
866 hr
= node_set_content(&This
->node
, V_BSTR(&value
));
867 else if (V_VT(&value
) == (VT_UI1
|VT_ARRAY
))
869 UINT dim
= SafeArrayGetDim(V_ARRAY(&value
));
876 FIXME("unexpected array dimension count %u\n", dim
);
878 SafeArrayGetUBound(V_ARRAY(&value
), 1, &ubound
);
879 SafeArrayGetLBound(V_ARRAY(&value
), 1, &lbound
);
881 len
= (ubound
- lbound
+ 1)*SafeArrayGetElemsize(V_ARRAY(&value
));
883 hr
= SafeArrayAccessData(V_ARRAY(&value
), (void*)&ptr
);
884 if (FAILED(hr
)) return hr
;
886 hr
= encode_base64(ptr
, len
, &encoded
);
887 SafeArrayUnaccessData(V_ARRAY(&value
));
888 if (FAILED(hr
)) return hr
;
890 hr
= node_set_content(&This
->node
, encoded
);
891 SysFreeString(encoded
);
895 FIXME("unhandled variant type %d for dt:%s\n", V_VT(&value
), debugstr_dt(dt
));
900 if (V_VT(&value
) == (VT_UI1
|VT_ARRAY
))
902 UINT dim
= SafeArrayGetDim(V_ARRAY(&value
));
909 FIXME("unexpected array dimension count %u\n", dim
);
911 SafeArrayGetUBound(V_ARRAY(&value
), 1, &ubound
);
912 SafeArrayGetLBound(V_ARRAY(&value
), 1, &lbound
);
914 len
= (ubound
- lbound
+ 1)*SafeArrayGetElemsize(V_ARRAY(&value
));
916 hr
= SafeArrayAccessData(V_ARRAY(&value
), (void*)&ptr
);
917 if (FAILED(hr
)) return hr
;
919 hr
= encode_binhex(ptr
, len
, &encoded
);
920 SafeArrayUnaccessData(V_ARRAY(&value
));
921 if (FAILED(hr
)) return hr
;
923 hr
= node_set_content(&This
->node
, encoded
);
924 SysFreeString(encoded
);
928 FIXME("unhandled variant type %d for dt:%s\n", V_VT(&value
), debugstr_dt(dt
));
933 FIXME("not implemented for dt:%s\n", debugstr_dt(dt
));
940 static HRESULT WINAPI
domelem_get_dataType(
941 IXMLDOMElement
*iface
,
944 domelem
*This
= impl_from_IXMLDOMElement( iface
);
947 TRACE("(%p)->(%p)\n", This
, typename
);
952 dt
= element_get_dt(get_element(This
));
981 V_VT(typename
) = VT_BSTR
;
982 V_BSTR(typename
) = SysAllocString(dt_to_bstr(dt
));
984 if (!V_BSTR(typename
))
985 return E_OUTOFMEMORY
;
988 /* Other types (DTD equivalents) do not return anything here,
989 * but the pointer part of the VARIANT is set to NULL */
990 V_VT(typename
) = VT_NULL
;
991 V_BSTR(typename
) = NULL
;
994 return (V_VT(typename
) != VT_NULL
) ? S_OK
: S_FALSE
;
997 static HRESULT WINAPI
domelem_put_dataType(
998 IXMLDOMElement
*iface
,
1001 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1002 HRESULT hr
= E_FAIL
;
1006 TRACE("(%p)->(%s)\n", This
, debugstr_w(dtName
));
1009 return E_INVALIDARG
;
1011 dt
= bstr_to_dt(dtName
, -1);
1013 /* An example of this is. The Text in the node needs to be a 0 or 1 for a boolean type.
1014 This applies to changing types (string->bool) or setting a new one
1016 str
= xmlNodeGetContent(get_element(This
));
1017 hr
= dt_validate(dt
, str
);
1020 /* Check all supported types. */
1032 case DT_DATETIME_TZ
:
1055 xmlAttrPtr attr
= xmlHasNsProp(get_element(This
), DT_prefix
, DT_nsURI
);
1058 attr
= xmlSetNsProp(get_element(This
), attr
->ns
, DT_prefix
, dt_to_str(dt
));
1063 xmlNsPtr ns
= xmlNewNs(get_element(This
), DT_nsURI
, DT_prefix
);
1066 attr
= xmlNewNsProp(get_element(This
), ns
, DT_prefix
, dt_to_str(dt
));
1069 xmlAddChild(get_element(This
), (xmlNodePtr
)attr
);
1073 ERR("Failed to create Attribute\n");
1076 ERR("Failed to create Namespace\n");
1081 FIXME("need to handle dt:%s\n", debugstr_dt(dt
));
1089 static HRESULT WINAPI
domelem_get_xml(
1090 IXMLDOMElement
*iface
,
1093 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1095 TRACE("(%p)->(%p)\n", This
, p
);
1097 return node_get_xml(&This
->node
, TRUE
, p
);
1100 static HRESULT WINAPI
domelem_transformNode(
1101 IXMLDOMElement
*iface
,
1102 IXMLDOMNode
*node
, BSTR
*p
)
1104 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1105 TRACE("(%p)->(%p %p)\n", This
, node
, p
);
1106 return node_transform_node(&This
->node
, node
, p
);
1109 static HRESULT WINAPI
domelem_selectNodes(
1110 IXMLDOMElement
*iface
,
1111 BSTR p
, IXMLDOMNodeList
** outList
)
1113 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1114 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(p
), outList
);
1115 return node_select_nodes(&This
->node
, p
, outList
);
1118 static HRESULT WINAPI
domelem_selectSingleNode(
1119 IXMLDOMElement
*iface
,
1120 BSTR p
, IXMLDOMNode
** outNode
)
1122 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1123 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(p
), outNode
);
1124 return node_select_singlenode(&This
->node
, p
, outNode
);
1127 static HRESULT WINAPI
domelem_get_parsed(
1128 IXMLDOMElement
*iface
,
1129 VARIANT_BOOL
* isParsed
)
1131 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1132 FIXME("(%p)->(%p) stub!\n", This
, isParsed
);
1133 *isParsed
= VARIANT_TRUE
;
1137 static HRESULT WINAPI
domelem_get_namespaceURI(
1138 IXMLDOMElement
*iface
,
1141 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1142 TRACE("(%p)->(%p)\n", This
, p
);
1143 return node_get_namespaceURI(&This
->node
, p
);
1146 static HRESULT WINAPI
domelem_get_prefix(
1147 IXMLDOMElement
*iface
,
1150 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1151 TRACE("(%p)->(%p)\n", This
, prefix
);
1152 return node_get_prefix( &This
->node
, prefix
);
1155 static HRESULT WINAPI
domelem_get_baseName(
1156 IXMLDOMElement
*iface
,
1159 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1160 TRACE("(%p)->(%p)\n", This
, name
);
1161 return node_get_base_name( &This
->node
, name
);
1164 static HRESULT WINAPI
domelem_transformNodeToObject(
1165 IXMLDOMElement
*iface
,
1166 IXMLDOMNode
* domNode
, VARIANT var1
)
1168 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1169 FIXME("(%p)->(%p %s)\n", This
, domNode
, debugstr_variant(&var1
));
1173 static HRESULT WINAPI
domelem_get_tagName(
1174 IXMLDOMElement
*iface
,
1177 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1179 const xmlChar
*prefix
;
1182 TRACE("(%p)->(%p)\n", This
, p
);
1184 if (!p
) return E_INVALIDARG
;
1186 element
= get_element( This
);
1190 prefix
= element
->ns
? element
->ns
->prefix
: NULL
;
1191 qname
= xmlBuildQName(element
->name
, prefix
, NULL
, 0);
1193 *p
= bstr_from_xmlChar(qname
);
1194 if (qname
!= element
->name
) xmlFree(qname
);
1196 return *p
? S_OK
: E_OUTOFMEMORY
;
1199 static HRESULT WINAPI
domelem_getAttribute(
1200 IXMLDOMElement
*iface
,
1201 BSTR name
, VARIANT
* value
)
1203 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1205 xmlChar
*xml_name
, *xml_value
= NULL
;
1206 HRESULT hr
= S_FALSE
;
1208 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(name
), value
);
1211 return E_INVALIDARG
;
1213 element
= get_element( This
);
1217 V_BSTR(value
) = NULL
;
1218 V_VT(value
) = VT_NULL
;
1220 xml_name
= xmlchar_from_wchar( name
);
1222 if(!xmlValidateNameValue(xml_name
))
1225 xml_value
= xmlGetNsProp(element
, xml_name
, NULL
);
1227 heap_free(xml_name
);
1230 V_VT(value
) = VT_BSTR
;
1231 V_BSTR(value
) = bstr_from_xmlChar( xml_value
);
1239 static HRESULT WINAPI
domelem_setAttribute(
1240 IXMLDOMElement
*iface
,
1241 BSTR name
, VARIANT value
)
1243 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1244 xmlChar
*xml_name
, *xml_value
, *local
, *prefix
;
1248 TRACE("(%p)->(%s %s)\n", This
, debugstr_w(name
), debugstr_variant(&value
));
1250 element
= get_element( This
);
1254 if (V_VT(&value
) != VT_BSTR
)
1259 hr
= VariantChangeType(&var
, &value
, 0, VT_BSTR
);
1262 FIXME("VariantChangeType failed\n");
1266 xml_value
= xmlchar_from_wchar(V_BSTR(&var
));
1270 xml_value
= xmlchar_from_wchar(V_BSTR(&value
));
1272 xml_name
= xmlchar_from_wchar( name
);
1274 if ((local
= xmlSplitQName2(xml_name
, &prefix
)))
1276 static const xmlChar
* xmlnsA
= (const xmlChar
*)"xmlns";
1279 /* it's not allowed to modify existing namespace definition */
1280 if (xmlStrEqual(prefix
, xmlnsA
))
1281 ns
= xmlSearchNs(element
->doc
, element
, local
);
1288 int cmp
= xmlStrEqual(ns
->href
, xml_value
);
1289 heap_free(xml_value
);
1290 heap_free(xml_name
);
1291 return cmp
? S_OK
: E_INVALIDARG
;
1295 if (!xmlSetNsProp(element
, NULL
, xml_name
, xml_value
))
1298 heap_free(xml_value
);
1299 heap_free(xml_name
);
1304 static HRESULT WINAPI
domelem_removeAttribute(
1305 IXMLDOMElement
*iface
,
1308 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1309 IXMLDOMNamedNodeMap
*attr
;
1312 TRACE("(%p)->(%s)\n", This
, debugstr_w(p
));
1314 hr
= IXMLDOMElement_get_attributes(iface
, &attr
);
1315 if (hr
!= S_OK
) return hr
;
1317 hr
= IXMLDOMNamedNodeMap_removeNamedItem(attr
, p
, NULL
);
1318 IXMLDOMNamedNodeMap_Release(attr
);
1323 static HRESULT WINAPI
domelem_getAttributeNode(
1324 IXMLDOMElement
*iface
,
1325 BSTR p
, IXMLDOMAttribute
** attributeNode
)
1327 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1328 xmlChar
*local
, *prefix
, *nameA
;
1329 HRESULT hr
= S_FALSE
;
1333 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(p
), attributeNode
);
1335 element
= get_element( This
);
1336 if (!element
) return E_FAIL
;
1338 if (attributeNode
) *attributeNode
= NULL
;
1340 nameA
= xmlchar_from_wchar(p
);
1341 if (!xmlValidateNameValue(nameA
))
1353 *attributeNode
= NULL
;
1355 local
= xmlSplitQName2(nameA
, &prefix
);
1359 /* try to get namespace for supplied qualified name */
1360 xmlNsPtr ns
= xmlSearchNs(element
->doc
, element
, prefix
);
1363 attr
= xmlHasNsProp(element
, local
, ns
? ns
->href
: NULL
);
1368 attr
= xmlHasProp(element
, nameA
);
1369 /* attribute has attached namespace and we requested non-qualified
1370 name - it's a failure case */
1371 if (attr
&& attr
->ns
) attr
= NULL
;
1378 IUnknown
*unk
= create_attribute((xmlNodePtr
)attr
);
1379 hr
= IUnknown_QueryInterface(unk
, &IID_IXMLDOMAttribute
, (void**)attributeNode
);
1380 IUnknown_Release(unk
);
1386 static HRESULT WINAPI
domelem_setAttributeNode(
1387 IXMLDOMElement
*iface
,
1388 IXMLDOMAttribute
* attribute
,
1389 IXMLDOMAttribute
** old
)
1391 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1392 static const WCHAR xmlnsW
[] = {'x','m','l','n','s',0};
1393 xmlChar
*name
, *value
;
1400 FIXME("(%p)->(%p %p): semi-stub\n", This
, attribute
, old
);
1402 if (!attribute
) return E_INVALIDARG
;
1404 attr_node
= get_node_obj((IXMLDOMNode
*)attribute
);
1405 if (!attr_node
) return E_FAIL
;
1407 if (attr_node
->parent
)
1409 WARN("attempt to add already used attribute\n");
1413 hr
= IXMLDOMAttribute_get_nodeName(attribute
, &nameW
);
1414 if (hr
!= S_OK
) return hr
;
1416 /* adding xmlns attribute doesn't change a tree or existing namespace definition */
1417 if (!strcmpW(nameW
, xmlnsW
))
1419 SysFreeString(nameW
);
1420 return DISP_E_UNKNOWNNAME
;
1423 hr
= IXMLDOMAttribute_get_nodeValue(attribute
, &valueW
);
1426 SysFreeString(nameW
);
1430 if (old
) *old
= NULL
;
1432 TRACE("attribute: %s=%s\n", debugstr_w(nameW
), debugstr_w(V_BSTR(&valueW
)));
1434 hr
= IXMLDOMAttribute_get_prefix(attribute
, &prefix
);
1437 FIXME("namespaces not supported: %s\n", debugstr_w(prefix
));
1438 SysFreeString(prefix
);
1441 name
= xmlchar_from_wchar(nameW
);
1442 value
= xmlchar_from_wchar(V_BSTR(&valueW
));
1444 if (!name
|| !value
)
1446 SysFreeString(nameW
);
1447 VariantClear(&valueW
);
1450 return E_OUTOFMEMORY
;
1453 attr
= xmlSetNsProp(get_element(This
), NULL
, name
, value
);
1455 attr_node
->parent
= (IXMLDOMNode
*)iface
;
1457 SysFreeString(nameW
);
1458 VariantClear(&valueW
);
1462 return attr
? S_OK
: E_FAIL
;
1465 static HRESULT WINAPI
domelem_removeAttributeNode(
1466 IXMLDOMElement
*iface
,
1467 IXMLDOMAttribute
* domAttribute
,
1468 IXMLDOMAttribute
** attributeNode
)
1470 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1471 FIXME("(%p)->(%p %p)\n", This
, domAttribute
, attributeNode
);
1475 static HRESULT WINAPI
domelem_getElementsByTagName(
1476 IXMLDOMElement
*iface
,
1477 BSTR tagName
, IXMLDOMNodeList
** resultList
)
1479 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1484 TRACE("(%p)->(%s, %p)\n", This
, debugstr_w(tagName
), resultList
);
1486 if (!tagName
|| !resultList
) return E_INVALIDARG
;
1488 XPath
= is_xpathmode(get_element(This
)->doc
);
1489 set_xpathmode(get_element(This
)->doc
, TRUE
);
1490 query
= tagName_to_XPath(tagName
);
1491 hr
= create_selection(get_element(This
), query
, resultList
);
1493 set_xpathmode(get_element(This
)->doc
, XPath
);
1498 static HRESULT WINAPI
domelem_normalize(
1499 IXMLDOMElement
*iface
)
1501 domelem
*This
= impl_from_IXMLDOMElement( iface
);
1502 FIXME("%p\n", This
);
1506 static const struct IXMLDOMElementVtbl domelem_vtbl
=
1508 domelem_QueryInterface
,
1511 domelem_GetTypeInfoCount
,
1512 domelem_GetTypeInfo
,
1513 domelem_GetIDsOfNames
,
1515 domelem_get_nodeName
,
1516 domelem_get_nodeValue
,
1517 domelem_put_nodeValue
,
1518 domelem_get_nodeType
,
1519 domelem_get_parentNode
,
1520 domelem_get_childNodes
,
1521 domelem_get_firstChild
,
1522 domelem_get_lastChild
,
1523 domelem_get_previousSibling
,
1524 domelem_get_nextSibling
,
1525 domelem_get_attributes
,
1526 domelem_insertBefore
,
1527 domelem_replaceChild
,
1528 domelem_removeChild
,
1529 domelem_appendChild
,
1530 domelem_hasChildNodes
,
1531 domelem_get_ownerDocument
,
1533 domelem_get_nodeTypeString
,
1536 domelem_get_specified
,
1537 domelem_get_definition
,
1538 domelem_get_nodeTypedValue
,
1539 domelem_put_nodeTypedValue
,
1540 domelem_get_dataType
,
1541 domelem_put_dataType
,
1543 domelem_transformNode
,
1544 domelem_selectNodes
,
1545 domelem_selectSingleNode
,
1547 domelem_get_namespaceURI
,
1549 domelem_get_baseName
,
1550 domelem_transformNodeToObject
,
1551 domelem_get_tagName
,
1552 domelem_getAttribute
,
1553 domelem_setAttribute
,
1554 domelem_removeAttribute
,
1555 domelem_getAttributeNode
,
1556 domelem_setAttributeNode
,
1557 domelem_removeAttributeNode
,
1558 domelem_getElementsByTagName
,
1562 static HRESULT
domelem_get_qualified_item(const xmlNodePtr node
, BSTR name
, BSTR uri
,
1569 TRACE("(%p)->(%s %s %p)\n", node
, debugstr_w(name
), debugstr_w(uri
), item
);
1571 if (!name
|| !item
) return E_INVALIDARG
;
1575 href
= xmlchar_from_wchar(uri
);
1576 if (!href
) return E_OUTOFMEMORY
;
1581 nameA
= xmlchar_from_wchar(name
);
1585 return E_OUTOFMEMORY
;
1588 attr
= xmlHasNsProp(node
, nameA
, href
);
1599 *item
= create_node((xmlNodePtr
)attr
);
1604 static HRESULT
domelem_get_named_item(const xmlNodePtr node
, BSTR name
, IXMLDOMNode
**item
)
1606 xmlChar
*nameA
, *local
, *prefix
;
1611 TRACE("(%p)->(%s %p)\n", node
, debugstr_w(name
), item
);
1613 nameA
= xmlchar_from_wchar(name
);
1614 local
= xmlSplitQName2(nameA
, &prefix
);
1618 return domelem_get_qualified_item(node
, name
, NULL
, item
);
1620 /* try to get namespace uri for supplied qualified name */
1621 ns
= xmlSearchNs(node
->doc
, node
, prefix
);
1628 if (item
) *item
= NULL
;
1629 return item
? S_FALSE
: E_INVALIDARG
;
1632 uriW
= bstr_from_xmlChar(ns
->href
);
1633 localW
= bstr_from_xmlChar(local
);
1636 TRACE("got qualified node %s, uri=%s\n", debugstr_w(localW
), debugstr_w(uriW
));
1638 hr
= domelem_get_qualified_item(node
, localW
, uriW
, item
);
1640 SysFreeString(localW
);
1641 SysFreeString(uriW
);
1646 static HRESULT
domelem_set_named_item(xmlNodePtr node
, IXMLDOMNode
*newItem
, IXMLDOMNode
**namedItem
)
1651 TRACE("(%p)->(%p %p)\n", node
, newItem
, namedItem
);
1654 return E_INVALIDARG
;
1656 if(namedItem
) *namedItem
= NULL
;
1658 /* Must be an Attribute */
1659 ThisNew
= get_node_obj( newItem
);
1660 if(!ThisNew
) return E_FAIL
;
1662 if(ThisNew
->node
->type
!= XML_ATTRIBUTE_NODE
)
1665 if(!ThisNew
->node
->parent
)
1666 if(xmldoc_remove_orphan(ThisNew
->node
->doc
, ThisNew
->node
) != S_OK
)
1667 WARN("%p is not an orphan of %p\n", ThisNew
->node
, ThisNew
->node
->doc
);
1669 nodeNew
= xmlAddChild(node
, ThisNew
->node
);
1672 *namedItem
= create_node( nodeNew
);
1676 static HRESULT
domelem_remove_qualified_item(xmlNodePtr node
, BSTR name
, BSTR uri
, IXMLDOMNode
**item
)
1678 xmlChar
*nameA
, *href
;
1681 TRACE("(%p)->(%s %s %p)\n", node
, debugstr_w(name
), debugstr_w(uri
), item
);
1683 if (!name
) return E_INVALIDARG
;
1687 href
= xmlchar_from_wchar(uri
);
1688 if (!href
) return E_OUTOFMEMORY
;
1693 nameA
= xmlchar_from_wchar(name
);
1697 return E_OUTOFMEMORY
;
1700 attr
= xmlHasNsProp(node
, nameA
, href
);
1707 if (item
) *item
= NULL
;
1713 xmlUnlinkNode( (xmlNodePtr
) attr
);
1714 xmldoc_add_orphan( attr
->doc
, (xmlNodePtr
) attr
);
1715 *item
= create_node( (xmlNodePtr
) attr
);
1719 if (xmlRemoveProp(attr
) == -1)
1720 ERR("xmlRemoveProp failed\n");
1726 static HRESULT
domelem_remove_named_item(xmlNodePtr node
, BSTR name
, IXMLDOMNode
**item
)
1728 TRACE("(%p)->(%s %p)\n", node
, debugstr_w(name
), item
);
1729 return domelem_remove_qualified_item(node
, name
, NULL
, item
);
1732 static HRESULT
domelem_get_item(const xmlNodePtr node
, LONG index
, IXMLDOMNode
**item
)
1737 TRACE("(%p)->(%d %p)\n", node
, index
, item
);
1744 curr
= node
->properties
;
1746 for (attrIndex
= 0; attrIndex
< index
; attrIndex
++) {
1747 if (curr
->next
== NULL
)
1753 *item
= create_node( (xmlNodePtr
) curr
);
1758 static HRESULT
domelem_get_length(const xmlNodePtr node
, LONG
*length
)
1764 TRACE("(%p)->(%p)\n", node
, length
);
1767 return E_INVALIDARG
;
1769 first
= node
->properties
;
1770 if (first
== NULL
) {
1777 while (curr
->next
) {
1781 *length
= attrCount
;
1786 static HRESULT
domelem_next_node(const xmlNodePtr node
, LONG
*iter
, IXMLDOMNode
**nextNode
)
1791 TRACE("(%p)->(%d: %p)\n", node
, *iter
, nextNode
);
1795 curr
= node
->properties
;
1797 for (i
= 0; i
< *iter
; i
++) {
1798 if (curr
->next
== NULL
)
1805 *nextNode
= create_node((xmlNodePtr
)curr
);
1810 static const struct nodemap_funcs domelem_attr_map
= {
1811 domelem_get_named_item
,
1812 domelem_set_named_item
,
1813 domelem_remove_named_item
,
1816 domelem_get_qualified_item
,
1817 domelem_remove_qualified_item
,
1821 static const tid_t domelem_iface_tids
[] = {
1826 static dispex_static_data_t domelem_dispex
= {
1833 IUnknown
* create_element( xmlNodePtr element
)
1837 This
= heap_alloc( sizeof *This
);
1841 This
->IXMLDOMElement_iface
.lpVtbl
= &domelem_vtbl
;
1844 init_xmlnode(&This
->node
, element
, (IXMLDOMNode
*)&This
->IXMLDOMElement_iface
, &domelem_dispex
);
1846 return (IUnknown
*)&This
->IXMLDOMElement_iface
;