windows.networking.hostname/tests: Add IHostNameFactory::CreateHostName() tests.
[wine.git] / dlls / msxml3 / element.c
blob302849aced6f8df6f20f2f92ab9f385a65e4ab2e
1 /*
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
21 #define COBJMACROS
23 #include <stdarg.h>
24 #include <libxml/parser.h>
25 #include <libxml/xmlerror.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winnls.h"
31 #include "ole2.h"
32 #include "msxml6.h"
34 #include "msxml_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
40 static const xmlChar DT_prefix[] = "dt";
41 static const xmlChar DT_nsURI[] = "urn:schemas-microsoft-com:datatypes";
43 typedef struct _domelem
45 xmlnode node;
46 IXMLDOMElement IXMLDOMElement_iface;
47 LONG ref;
48 } domelem;
50 static const struct nodemap_funcs domelem_attr_map;
52 static const tid_t domelem_se_tids[] = {
53 IXMLDOMNode_tid,
54 IXMLDOMElement_tid,
55 NULL_tid
58 static inline domelem *impl_from_IXMLDOMElement( IXMLDOMElement *iface )
60 return CONTAINING_RECORD(iface, domelem, IXMLDOMElement_iface);
63 static inline xmlNodePtr get_element( const domelem *This )
65 return This->node.node;
68 static HRESULT WINAPI domelem_QueryInterface(
69 IXMLDOMElement *iface,
70 REFIID riid,
71 void** ppvObject )
73 domelem *This = impl_from_IXMLDOMElement( iface );
75 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
77 if ( IsEqualGUID( riid, &IID_IXMLDOMElement ) ||
78 IsEqualGUID( riid, &IID_IXMLDOMNode ) ||
79 IsEqualGUID( riid, &IID_IDispatch ) ||
80 IsEqualGUID( riid, &IID_IUnknown ) )
82 *ppvObject = &This->IXMLDOMElement_iface;
84 else if(node_query_interface(&This->node, riid, ppvObject))
86 return *ppvObject ? S_OK : E_NOINTERFACE;
88 else if(IsEqualGUID( riid, &IID_ISupportErrorInfo ))
90 return node_create_supporterrorinfo(domelem_se_tids, ppvObject);
92 else
94 TRACE("interface %s not implemented\n", debugstr_guid(riid));
95 *ppvObject = NULL;
96 return E_NOINTERFACE;
99 IUnknown_AddRef( (IUnknown*)*ppvObject );
100 return S_OK;
103 static ULONG WINAPI domelem_AddRef(IXMLDOMElement *iface)
105 domelem *element = impl_from_IXMLDOMElement(iface);
106 LONG ref = InterlockedIncrement(&element->ref);
108 TRACE("%p, refcount %ld.\n", iface, ref);
110 return ref;
113 static ULONG WINAPI domelem_Release(IXMLDOMElement *iface)
115 domelem *element = impl_from_IXMLDOMElement(iface);
116 ULONG ref = InterlockedDecrement(&element->ref);
118 TRACE("%p, refcount %lu.\n", iface, ref);
120 if (!ref)
122 destroy_xmlnode(&element->node);
123 heap_free(element);
126 return ref;
129 static HRESULT WINAPI domelem_GetTypeInfoCount(
130 IXMLDOMElement *iface,
131 UINT* pctinfo )
133 domelem *This = impl_from_IXMLDOMElement( iface );
134 return IDispatchEx_GetTypeInfoCount(&This->node.dispex.IDispatchEx_iface, pctinfo);
137 static HRESULT WINAPI domelem_GetTypeInfo(
138 IXMLDOMElement *iface,
139 UINT iTInfo, LCID lcid,
140 ITypeInfo** ppTInfo )
142 domelem *This = impl_from_IXMLDOMElement( iface );
143 return IDispatchEx_GetTypeInfo(&This->node.dispex.IDispatchEx_iface,
144 iTInfo, lcid, ppTInfo);
147 static HRESULT WINAPI domelem_GetIDsOfNames(
148 IXMLDOMElement *iface,
149 REFIID riid, LPOLESTR* rgszNames,
150 UINT cNames, LCID lcid, DISPID* rgDispId )
152 domelem *This = impl_from_IXMLDOMElement( iface );
153 return IDispatchEx_GetIDsOfNames(&This->node.dispex.IDispatchEx_iface,
154 riid, rgszNames, cNames, lcid, rgDispId);
157 static HRESULT WINAPI domelem_Invoke(
158 IXMLDOMElement *iface,
159 DISPID dispIdMember, REFIID riid, LCID lcid,
160 WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
161 EXCEPINFO* pExcepInfo, UINT* puArgErr )
163 domelem *This = impl_from_IXMLDOMElement( iface );
164 return IDispatchEx_Invoke(&This->node.dispex.IDispatchEx_iface,
165 dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
168 static HRESULT WINAPI domelem_get_nodeName(
169 IXMLDOMElement *iface,
170 BSTR* p )
172 domelem *This = impl_from_IXMLDOMElement( iface );
174 TRACE("(%p)->(%p)\n", This, p);
176 return node_get_nodeName(&This->node, p);
179 static HRESULT WINAPI domelem_get_nodeValue(
180 IXMLDOMElement *iface,
181 VARIANT* value)
183 domelem *This = impl_from_IXMLDOMElement( iface );
185 TRACE("(%p)->(%p)\n", This, value);
187 if(!value)
188 return E_INVALIDARG;
190 V_VT(value) = VT_NULL;
191 V_BSTR(value) = NULL; /* tests show that we should do this */
192 return S_FALSE;
195 static HRESULT WINAPI domelem_put_nodeValue(
196 IXMLDOMElement *iface,
197 VARIANT value)
199 domelem *This = impl_from_IXMLDOMElement( iface );
200 TRACE("(%p)->(%s)\n", This, debugstr_variant(&value));
201 return E_FAIL;
204 static HRESULT WINAPI domelem_get_nodeType(
205 IXMLDOMElement *iface,
206 DOMNodeType* domNodeType )
208 domelem *This = impl_from_IXMLDOMElement( iface );
210 TRACE("(%p)->(%p)\n", This, domNodeType);
212 *domNodeType = NODE_ELEMENT;
213 return S_OK;
216 static HRESULT WINAPI domelem_get_parentNode(
217 IXMLDOMElement *iface,
218 IXMLDOMNode** parent )
220 domelem *This = impl_from_IXMLDOMElement( iface );
222 TRACE("(%p)->(%p)\n", This, parent);
224 return node_get_parent(&This->node, parent);
227 static HRESULT WINAPI domelem_get_childNodes(
228 IXMLDOMElement *iface,
229 IXMLDOMNodeList** outList)
231 domelem *This = impl_from_IXMLDOMElement( iface );
233 TRACE("(%p)->(%p)\n", This, outList);
235 return node_get_child_nodes(&This->node, outList);
238 static HRESULT WINAPI domelem_get_firstChild(
239 IXMLDOMElement *iface,
240 IXMLDOMNode** domNode)
242 domelem *This = impl_from_IXMLDOMElement( iface );
244 TRACE("(%p)->(%p)\n", This, domNode);
246 return node_get_first_child(&This->node, domNode);
249 static HRESULT WINAPI domelem_get_lastChild(
250 IXMLDOMElement *iface,
251 IXMLDOMNode** domNode)
253 domelem *This = impl_from_IXMLDOMElement( iface );
255 TRACE("(%p)->(%p)\n", This, domNode);
257 return node_get_last_child(&This->node, domNode);
260 static HRESULT WINAPI domelem_get_previousSibling(
261 IXMLDOMElement *iface,
262 IXMLDOMNode** domNode)
264 domelem *This = impl_from_IXMLDOMElement( iface );
266 TRACE("(%p)->(%p)\n", This, domNode);
268 return node_get_previous_sibling(&This->node, domNode);
271 static HRESULT WINAPI domelem_get_nextSibling(
272 IXMLDOMElement *iface,
273 IXMLDOMNode** domNode)
275 domelem *This = impl_from_IXMLDOMElement( iface );
277 TRACE("(%p)->(%p)\n", This, domNode);
279 return node_get_next_sibling(&This->node, domNode);
282 static HRESULT WINAPI domelem_get_attributes(
283 IXMLDOMElement *iface,
284 IXMLDOMNamedNodeMap** map)
286 domelem *This = impl_from_IXMLDOMElement( iface );
288 TRACE("(%p)->(%p)\n", This, map);
290 *map = create_nodemap(This->node.node, &domelem_attr_map);
291 return S_OK;
294 static HRESULT WINAPI domelem_insertBefore(
295 IXMLDOMElement *iface,
296 IXMLDOMNode* newNode, VARIANT refChild,
297 IXMLDOMNode** old_node)
299 domelem *This = impl_from_IXMLDOMElement( iface );
300 DOMNodeType type;
301 HRESULT hr;
303 TRACE("(%p)->(%p %s %p)\n", This, newNode, debugstr_variant(&refChild), old_node);
305 if (!newNode) return E_INVALIDARG;
307 hr = IXMLDOMNode_get_nodeType(newNode, &type);
308 if (hr != S_OK) return hr;
310 TRACE("new node type %d\n", type);
311 switch (type)
313 case NODE_DOCUMENT:
314 case NODE_DOCUMENT_TYPE:
315 case NODE_ENTITY:
316 case NODE_NOTATION:
317 if (old_node) *old_node = NULL;
318 return E_FAIL;
319 default:
320 return node_insert_before(&This->node, newNode, &refChild, old_node);
324 static HRESULT WINAPI domelem_replaceChild(
325 IXMLDOMElement *iface,
326 IXMLDOMNode* newNode,
327 IXMLDOMNode* oldNode,
328 IXMLDOMNode** outOldNode)
330 domelem *This = impl_from_IXMLDOMElement( iface );
332 TRACE("(%p)->(%p %p %p)\n", This, newNode, oldNode, outOldNode);
334 return node_replace_child(&This->node, newNode, oldNode, outOldNode);
337 static HRESULT WINAPI domelem_removeChild(
338 IXMLDOMElement *iface,
339 IXMLDOMNode *child, IXMLDOMNode **oldChild)
341 domelem *This = impl_from_IXMLDOMElement( iface );
342 TRACE("(%p)->(%p %p)\n", This, child, oldChild);
343 return node_remove_child(&This->node, child, oldChild);
346 static HRESULT WINAPI domelem_appendChild(
347 IXMLDOMElement *iface,
348 IXMLDOMNode *child, IXMLDOMNode **outChild)
350 domelem *This = impl_from_IXMLDOMElement( iface );
351 TRACE("(%p)->(%p %p)\n", This, child, outChild);
352 return node_append_child(&This->node, child, outChild);
355 static HRESULT WINAPI domelem_hasChildNodes(
356 IXMLDOMElement *iface,
357 VARIANT_BOOL *ret)
359 domelem *This = impl_from_IXMLDOMElement( iface );
360 TRACE("(%p)->(%p)\n", This, ret);
361 return node_has_childnodes(&This->node, ret);
364 static HRESULT WINAPI domelem_get_ownerDocument(
365 IXMLDOMElement *iface,
366 IXMLDOMDocument **doc)
368 domelem *This = impl_from_IXMLDOMElement( iface );
369 TRACE("(%p)->(%p)\n", This, doc);
370 return node_get_owner_doc(&This->node, doc);
373 static HRESULT WINAPI domelem_cloneNode(
374 IXMLDOMElement *iface,
375 VARIANT_BOOL deep, IXMLDOMNode** outNode)
377 domelem *This = impl_from_IXMLDOMElement( iface );
378 TRACE("(%p)->(%d %p)\n", This, deep, outNode);
379 return node_clone( &This->node, deep, outNode );
382 static HRESULT WINAPI domelem_get_nodeTypeString(
383 IXMLDOMElement *iface,
384 BSTR* p)
386 domelem *This = impl_from_IXMLDOMElement( iface );
387 static const WCHAR elementW[] = {'e','l','e','m','e','n','t',0};
389 TRACE("(%p)->(%p)\n", This, p);
391 return return_bstr(elementW, p);
394 static HRESULT WINAPI domelem_get_text(
395 IXMLDOMElement *iface,
396 BSTR* p)
398 domelem *This = impl_from_IXMLDOMElement( iface );
399 TRACE("(%p)->(%p)\n", This, p);
400 return node_get_text(&This->node, p);
403 static HRESULT WINAPI domelem_put_text(
404 IXMLDOMElement *iface,
405 BSTR p)
407 domelem *This = impl_from_IXMLDOMElement( iface );
408 TRACE("(%p)->(%s)\n", This, debugstr_w(p));
409 return node_put_text( &This->node, p );
412 static HRESULT WINAPI domelem_get_specified(
413 IXMLDOMElement *iface,
414 VARIANT_BOOL* isSpecified)
416 domelem *This = impl_from_IXMLDOMElement( iface );
417 FIXME("(%p)->(%p) stub!\n", This, isSpecified);
418 *isSpecified = VARIANT_TRUE;
419 return S_OK;
422 static HRESULT WINAPI domelem_get_definition(
423 IXMLDOMElement *iface,
424 IXMLDOMNode** definitionNode)
426 domelem *This = impl_from_IXMLDOMElement( iface );
427 FIXME("(%p)->(%p)\n", This, definitionNode);
428 return E_NOTIMPL;
431 static inline BYTE hex_to_byte(xmlChar c)
433 if(c <= '9') return c-'0';
434 if(c <= 'F') return c-'A'+10;
435 return c-'a'+10;
438 static inline BYTE base64_to_byte(xmlChar c)
440 if(c == '+') return 62;
441 if(c == '/') return 63;
442 if(c <= '9') return c-'0'+52;
443 if(c <= 'Z') return c-'A';
444 return c-'a'+26;
447 static inline HRESULT variant_from_dt(XDR_DT dt, xmlChar* str, VARIANT* v)
449 VARIANT src;
450 HRESULT hr = S_OK;
451 BOOL handled = FALSE;
453 VariantInit(&src);
455 switch (dt)
457 case DT_STRING:
458 case DT_NMTOKEN:
459 case DT_NMTOKENS:
460 case DT_NUMBER:
461 case DT_URI:
462 case DT_UUID:
464 V_VT(v) = VT_BSTR;
465 V_BSTR(v) = bstr_from_xmlChar(str);
467 if(!V_BSTR(v))
468 return E_OUTOFMEMORY;
469 handled = TRUE;
471 break;
472 case DT_DATE:
473 case DT_DATE_TZ:
474 case DT_DATETIME:
475 case DT_DATETIME_TZ:
476 case DT_TIME:
477 case DT_TIME_TZ:
479 WCHAR *p, *e;
480 SYSTEMTIME st;
481 DOUBLE date = 0.0;
483 st.wYear = 1899;
484 st.wMonth = 12;
485 st.wDay = 30;
486 st.wDayOfWeek = st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
488 V_VT(&src) = VT_BSTR;
489 V_BSTR(&src) = bstr_from_xmlChar(str);
491 if(!V_BSTR(&src))
492 return E_OUTOFMEMORY;
494 p = V_BSTR(&src);
495 e = p + SysStringLen(V_BSTR(&src));
497 if(p+4<e && *(p+4)=='-') /* parse date (yyyy-mm-dd) */
499 st.wYear = wcstol(p, NULL, 10);
500 st.wMonth = wcstol(p+5, NULL, 10);
501 st.wDay = wcstol(p+8, NULL, 10);
502 p += 10;
504 if(*p == 'T') p++;
507 if(p+2<e && *(p+2)==':') /* parse time (hh:mm:ss.?) */
509 st.wHour = wcstol(p, NULL, 10);
510 st.wMinute = wcstol(p+3, NULL, 10);
511 st.wSecond = wcstol(p+6, NULL, 10);
512 p += 8;
514 if(*p == '.')
516 p++;
517 while (*p >= '0' && *p <= '9') p++;
521 SystemTimeToVariantTime(&st, &date);
522 V_VT(v) = VT_DATE;
523 V_DATE(v) = date;
525 if(*p == '+') /* parse timezone offset (+hh:mm) */
526 V_DATE(v) += (DOUBLE)wcstol(p+1, NULL, 10)/24 + (DOUBLE)wcstol(p+4, NULL, 10)/1440;
527 else if(*p == '-') /* parse timezone offset (-hh:mm) */
528 V_DATE(v) -= (DOUBLE)wcstol(p+1, NULL, 10)/24 + (DOUBLE)wcstol(p+4, NULL, 10)/1440;
530 VariantClear(&src);
531 handled = TRUE;
533 break;
534 case DT_BIN_HEX:
536 SAFEARRAYBOUND sab;
537 int i, len;
539 len = xmlStrlen(str)/2;
540 sab.lLbound = 0;
541 sab.cElements = len;
543 V_VT(v) = (VT_ARRAY|VT_UI1);
544 V_ARRAY(v) = SafeArrayCreate(VT_UI1, 1, &sab);
546 if(!V_ARRAY(v))
547 return E_OUTOFMEMORY;
549 for(i=0; i<len; i++)
550 ((BYTE*)V_ARRAY(v)->pvData)[i] = (hex_to_byte(str[2*i])<<4)
551 + hex_to_byte(str[2*i+1]);
552 handled = TRUE;
554 break;
555 case DT_BIN_BASE64:
557 SAFEARRAYBOUND sab;
558 xmlChar *c1, *c2;
559 int i, len;
561 /* remove all formatting chars */
562 c1 = c2 = str;
563 len = 0;
564 while (*c2)
566 if ( *c2 == ' ' || *c2 == '\t' ||
567 *c2 == '\n' || *c2 == '\r' )
569 c2++;
570 continue;
572 *c1++ = *c2++;
573 len++;
576 /* skip padding */
577 if(str[len-2] == '=') i = 2;
578 else if(str[len-1] == '=') i = 1;
579 else i = 0;
581 sab.lLbound = 0;
582 sab.cElements = len/4*3-i;
584 V_VT(v) = (VT_ARRAY|VT_UI1);
585 V_ARRAY(v) = SafeArrayCreate(VT_UI1, 1, &sab);
587 if(!V_ARRAY(v))
588 return E_OUTOFMEMORY;
590 for(i=0; i<len/4; i++)
592 ((BYTE*)V_ARRAY(v)->pvData)[3*i] = (base64_to_byte(str[4*i])<<2)
593 + (base64_to_byte(str[4*i+1])>>4);
594 if(3*i+1 < sab.cElements)
595 ((BYTE*)V_ARRAY(v)->pvData)[3*i+1] = (base64_to_byte(str[4*i+1])<<4)
596 + (base64_to_byte(str[4*i+2])>>2);
597 if(3*i+2 < sab.cElements)
598 ((BYTE*)V_ARRAY(v)->pvData)[3*i+2] = (base64_to_byte(str[4*i+2])<<6)
599 + base64_to_byte(str[4*i+3]);
601 handled = TRUE;
603 break;
604 case DT_BOOLEAN:
605 V_VT(v) = VT_BOOL;
606 break;
607 case DT_FIXED_14_4:
608 V_VT(v) = VT_CY;
609 break;
610 case DT_I1:
611 V_VT(v) = VT_I1;
612 break;
613 case DT_I2:
614 V_VT(v) = VT_I2;
615 break;
616 case DT_I4:
617 case DT_INT:
618 V_VT(v) = VT_I4;
619 break;
620 case DT_I8:
621 V_VT(v) = VT_I8;
622 break;
623 case DT_R4:
624 V_VT(v) = VT_R4;
625 break;
626 case DT_FLOAT:
627 case DT_R8:
628 V_VT(v) = VT_R8;
629 break;
630 case DT_UI1:
631 V_VT(v) = VT_UI1;
632 break;
633 case DT_UI2:
634 V_VT(v) = VT_UI2;
635 break;
636 case DT_UI4:
637 V_VT(v) = VT_UI4;
638 break;
639 case DT_UI8:
640 V_VT(v) = VT_UI8;
641 break;
642 case DT_CHAR:
643 case DT_ENTITY:
644 case DT_ENTITIES:
645 case DT_ENUMERATION:
646 case DT_ID:
647 case DT_IDREF:
648 case DT_IDREFS:
649 case DT_NOTATION:
650 FIXME("need to handle dt:%s\n", debugstr_dt(dt));
651 V_VT(v) = VT_BSTR;
652 V_BSTR(v) = bstr_from_xmlChar(str);
653 if (!V_BSTR(v))
654 return E_OUTOFMEMORY;
655 handled = TRUE;
656 break;
657 default:
658 WARN("unknown type %d\n", dt);
661 if (!handled)
663 V_VT(&src) = VT_BSTR;
664 V_BSTR(&src) = bstr_from_xmlChar(str);
666 if(!V_BSTR(&src))
667 return E_OUTOFMEMORY;
669 hr = VariantChangeTypeEx(v, &src,
670 MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT),0, V_VT(v));
671 VariantClear(&src);
673 return hr;
676 static XDR_DT element_get_dt(xmlNodePtr node)
678 XDR_DT dt = DT_INVALID;
680 TRACE("(%p)\n", node);
681 if(node->type != XML_ELEMENT_NODE)
683 FIXME("invalid element node\n");
684 return dt;
687 if (node->ns && xmlStrEqual(node->ns->href, DT_nsURI))
689 dt = str_to_dt(node->name, -1);
691 else
693 xmlChar* pVal = xmlGetNsProp(node, BAD_CAST "dt", DT_nsURI);
694 if (pVal)
696 dt = str_to_dt(pVal, -1);
697 xmlFree(pVal);
699 else if (node->doc)
701 IXMLDOMDocument3* doc = (IXMLDOMDocument3*)create_domdoc((xmlNodePtr)node->doc);
702 if (doc)
704 VARIANT v;
705 VariantInit(&v);
707 if (IXMLDOMDocument3_get_schemas(doc, &v) == S_OK &&
708 V_VT(&v) == VT_DISPATCH)
710 dt = SchemaCache_get_node_dt((IXMLDOMSchemaCollection2*)V_DISPATCH(&v), node);
712 VariantClear(&v);
713 IXMLDOMDocument3_Release(doc);
718 TRACE("=> dt:%s\n", debugstr_dt(dt));
719 return dt;
722 static HRESULT WINAPI domelem_get_nodeTypedValue(
723 IXMLDOMElement *iface,
724 VARIANT* v)
726 domelem *This = impl_from_IXMLDOMElement( iface );
727 XDR_DT dt;
728 xmlChar* content;
729 HRESULT hr;
731 TRACE("(%p)->(%p)\n", This, v);
733 if(!v) return E_INVALIDARG;
735 V_VT(v) = VT_NULL;
737 dt = element_get_dt(get_element(This));
739 if (dt == DT_INVALID)
741 if (SUCCEEDED(hr = node_get_text(&This->node, &V_BSTR(v))))
742 V_VT(v) = VT_BSTR;
743 return hr;
745 content = xmlNodeGetContent(get_element(This));
746 hr = variant_from_dt(dt, content, v);
747 xmlFree(content);
749 return hr;
752 static HRESULT encode_base64(const BYTE *buf, int len, BSTR *ret)
754 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
755 const BYTE *d = buf;
756 int bytes, pad_bytes, div;
757 DWORD needed;
758 WCHAR *ptr;
760 bytes = (len*8 + 5)/6;
761 pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
763 TRACE("%d, bytes is %d, pad bytes is %d\n", len, bytes, pad_bytes);
764 needed = bytes + pad_bytes + 1;
766 *ret = SysAllocStringLen(NULL, needed);
767 if (!*ret) return E_OUTOFMEMORY;
769 /* Three bytes of input give 4 chars of output */
770 div = len / 3;
772 ptr = *ret;
773 while (div > 0)
775 /* first char is the first 6 bits of the first byte*/
776 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
777 /* second char is the last 2 bits of the first byte and the first 4
778 * bits of the second byte */
779 *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
780 /* third char is the last 4 bits of the second byte and the first 2
781 * bits of the third byte */
782 *ptr++ = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)];
783 /* fourth char is the remaining 6 bits of the third byte */
784 *ptr++ = b64[ d[2] & 0x3f];
785 d += 3;
786 div--;
789 switch (pad_bytes)
791 case 1:
792 /* first char is the first 6 bits of the first byte*/
793 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
794 /* second char is the last 2 bits of the first byte and the first 4
795 * bits of the second byte */
796 *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
797 /* third char is the last 4 bits of the second byte padded with
798 * two zeroes */
799 *ptr++ = b64[ ((d[1] << 2) & 0x3c) ];
800 /* fourth char is a = to indicate one byte of padding */
801 *ptr++ = '=';
802 break;
803 case 2:
804 /* first char is the first 6 bits of the first byte*/
805 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
806 /* second char is the last 2 bits of the first byte padded with
807 * four zeroes*/
808 *ptr++ = b64[ ((d[0] << 4) & 0x30)];
809 /* third char is = to indicate padding */
810 *ptr++ = '=';
811 /* fourth char is = to indicate padding */
812 *ptr++ = '=';
813 break;
816 return S_OK;
819 static HRESULT encode_binhex(const BYTE *buf, int len, BSTR *ret)
821 static const char byte_to_hex[16] = "0123456789abcdef";
822 int i;
824 *ret = SysAllocStringLen(NULL, len*2);
825 if (!*ret) return E_OUTOFMEMORY;
827 for (i = 0; i < len; i++)
829 (*ret)[2*i] = byte_to_hex[buf[i] >> 4];
830 (*ret)[2*i+1] = byte_to_hex[0x0f & buf[i]];
833 return S_OK;
836 static HRESULT WINAPI domelem_put_nodeTypedValue(
837 IXMLDOMElement *iface,
838 VARIANT value)
840 domelem *This = impl_from_IXMLDOMElement( iface );
841 XDR_DT dt;
842 HRESULT hr;
844 TRACE("(%p)->(%s)\n", This, debugstr_variant(&value));
846 dt = element_get_dt(get_element(This));
847 switch (dt)
849 /* for untyped node coerce to BSTR and set */
850 case DT_INVALID:
851 case DT_INT:
852 if (V_VT(&value) != VT_BSTR)
854 VARIANT content;
855 VariantInit(&content);
856 hr = VariantChangeType(&content, &value, 0, VT_BSTR);
857 if (hr == S_OK)
859 hr = node_set_content(&This->node, V_BSTR(&content));
860 VariantClear(&content);
863 else
864 hr = node_set_content(&This->node, V_BSTR(&value));
865 break;
866 case DT_BIN_BASE64:
867 if (V_VT(&value) == VT_BSTR)
868 hr = node_set_content(&This->node, V_BSTR(&value));
869 else if (V_VT(&value) == (VT_UI1|VT_ARRAY))
871 UINT dim = SafeArrayGetDim(V_ARRAY(&value));
872 LONG lbound, ubound;
873 BSTR encoded;
874 BYTE *ptr;
875 int len;
877 if (dim > 1)
878 FIXME("unexpected array dimension count %u\n", dim);
880 SafeArrayGetUBound(V_ARRAY(&value), 1, &ubound);
881 SafeArrayGetLBound(V_ARRAY(&value), 1, &lbound);
883 len = (ubound - lbound + 1)*SafeArrayGetElemsize(V_ARRAY(&value));
885 hr = SafeArrayAccessData(V_ARRAY(&value), (void*)&ptr);
886 if (FAILED(hr)) return hr;
888 hr = encode_base64(ptr, len, &encoded);
889 SafeArrayUnaccessData(V_ARRAY(&value));
890 if (FAILED(hr)) return hr;
892 hr = node_set_content(&This->node, encoded);
893 SysFreeString(encoded);
895 else
897 FIXME("unhandled variant type %d for dt:%s\n", V_VT(&value), debugstr_dt(dt));
898 return E_NOTIMPL;
900 break;
901 case DT_BIN_HEX:
902 if (V_VT(&value) == (VT_UI1|VT_ARRAY))
904 UINT dim = SafeArrayGetDim(V_ARRAY(&value));
905 LONG lbound, ubound;
906 BSTR encoded;
907 BYTE *ptr;
908 int len;
910 if (dim > 1)
911 FIXME("unexpected array dimension count %u\n", dim);
913 SafeArrayGetUBound(V_ARRAY(&value), 1, &ubound);
914 SafeArrayGetLBound(V_ARRAY(&value), 1, &lbound);
916 len = (ubound - lbound + 1)*SafeArrayGetElemsize(V_ARRAY(&value));
918 hr = SafeArrayAccessData(V_ARRAY(&value), (void*)&ptr);
919 if (FAILED(hr)) return hr;
921 hr = encode_binhex(ptr, len, &encoded);
922 SafeArrayUnaccessData(V_ARRAY(&value));
923 if (FAILED(hr)) return hr;
925 hr = node_set_content(&This->node, encoded);
926 SysFreeString(encoded);
928 else
930 FIXME("unhandled variant type %d for dt:%s\n", V_VT(&value), debugstr_dt(dt));
931 return E_NOTIMPL;
933 break;
934 default:
935 FIXME("not implemented for dt:%s\n", debugstr_dt(dt));
936 return E_NOTIMPL;
939 return hr;
942 static HRESULT WINAPI domelem_get_dataType(
943 IXMLDOMElement *iface,
944 VARIANT* typename)
946 domelem *This = impl_from_IXMLDOMElement( iface );
947 XDR_DT dt;
949 TRACE("(%p)->(%p)\n", This, typename);
951 if (!typename)
952 return E_INVALIDARG;
954 dt = element_get_dt(get_element(This));
955 switch (dt)
957 case DT_BIN_BASE64:
958 case DT_BIN_HEX:
959 case DT_BOOLEAN:
960 case DT_CHAR:
961 case DT_DATE:
962 case DT_DATE_TZ:
963 case DT_DATETIME:
964 case DT_DATETIME_TZ:
965 case DT_FIXED_14_4:
966 case DT_FLOAT:
967 case DT_I1:
968 case DT_I2:
969 case DT_I4:
970 case DT_I8:
971 case DT_INT:
972 case DT_NUMBER:
973 case DT_R4:
974 case DT_R8:
975 case DT_TIME:
976 case DT_TIME_TZ:
977 case DT_UI1:
978 case DT_UI2:
979 case DT_UI4:
980 case DT_UI8:
981 case DT_URI:
982 case DT_UUID:
983 V_VT(typename) = VT_BSTR;
984 V_BSTR(typename) = SysAllocString(dt_to_bstr(dt));
986 if (!V_BSTR(typename))
987 return E_OUTOFMEMORY;
988 break;
989 default:
990 /* Other types (DTD equivalents) do not return anything here,
991 * but the pointer part of the VARIANT is set to NULL */
992 V_VT(typename) = VT_NULL;
993 V_BSTR(typename) = NULL;
994 break;
996 return (V_VT(typename) != VT_NULL) ? S_OK : S_FALSE;
999 static HRESULT WINAPI domelem_put_dataType(
1000 IXMLDOMElement *iface,
1001 BSTR dtName)
1003 domelem *This = impl_from_IXMLDOMElement( iface );
1004 HRESULT hr = E_FAIL;
1005 xmlChar *str;
1006 XDR_DT dt;
1008 TRACE("(%p)->(%s)\n", This, debugstr_w(dtName));
1010 if(dtName == NULL)
1011 return E_INVALIDARG;
1013 dt = bstr_to_dt(dtName, -1);
1015 /* An example of this is. The Text in the node needs to be a 0 or 1 for a boolean type.
1016 This applies to changing types (string->bool) or setting a new one
1018 str = xmlNodeGetContent(get_element(This));
1019 hr = dt_validate(dt, str);
1020 xmlFree(str);
1022 /* Check all supported types. */
1023 if (hr == S_OK)
1025 switch (dt)
1027 case DT_BIN_BASE64:
1028 case DT_BIN_HEX:
1029 case DT_BOOLEAN:
1030 case DT_CHAR:
1031 case DT_DATE:
1032 case DT_DATE_TZ:
1033 case DT_DATETIME:
1034 case DT_DATETIME_TZ:
1035 case DT_FIXED_14_4:
1036 case DT_FLOAT:
1037 case DT_I1:
1038 case DT_I2:
1039 case DT_I4:
1040 case DT_I8:
1041 case DT_INT:
1042 case DT_NMTOKEN:
1043 case DT_NMTOKENS:
1044 case DT_NUMBER:
1045 case DT_R4:
1046 case DT_R8:
1047 case DT_STRING:
1048 case DT_TIME:
1049 case DT_TIME_TZ:
1050 case DT_UI1:
1051 case DT_UI2:
1052 case DT_UI4:
1053 case DT_UI8:
1054 case DT_URI:
1055 case DT_UUID:
1057 xmlAttrPtr attr = xmlHasNsProp(get_element(This), DT_prefix, DT_nsURI);
1058 if (attr)
1060 attr = xmlSetNsProp(get_element(This), attr->ns, DT_prefix, dt_to_str(dt));
1061 hr = S_OK;
1063 else
1065 xmlNsPtr ns = xmlNewNs(get_element(This), DT_nsURI, DT_prefix);
1066 if (ns)
1068 attr = xmlNewNsProp(get_element(This), ns, DT_prefix, dt_to_str(dt));
1069 if (attr)
1071 xmlAddChild(get_element(This), (xmlNodePtr)attr);
1072 hr = S_OK;
1074 else
1075 ERR("Failed to create Attribute\n");
1077 else
1078 ERR("Failed to create Namespace\n");
1081 break;
1082 default:
1083 FIXME("need to handle dt:%s\n", debugstr_dt(dt));
1084 break;
1088 return hr;
1091 static HRESULT WINAPI domelem_get_xml(
1092 IXMLDOMElement *iface,
1093 BSTR* p)
1095 domelem *This = impl_from_IXMLDOMElement( iface );
1097 TRACE("(%p)->(%p)\n", This, p);
1099 return node_get_xml(&This->node, TRUE, p);
1102 static HRESULT WINAPI domelem_transformNode(
1103 IXMLDOMElement *iface,
1104 IXMLDOMNode *node, BSTR *p)
1106 domelem *This = impl_from_IXMLDOMElement( iface );
1107 TRACE("(%p)->(%p %p)\n", This, node, p);
1108 return node_transform_node(&This->node, node, p);
1111 static HRESULT WINAPI domelem_selectNodes(
1112 IXMLDOMElement *iface,
1113 BSTR p, IXMLDOMNodeList** outList)
1115 domelem *This = impl_from_IXMLDOMElement( iface );
1116 TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), outList);
1117 return node_select_nodes(&This->node, p, outList);
1120 static HRESULT WINAPI domelem_selectSingleNode(
1121 IXMLDOMElement *iface,
1122 BSTR p, IXMLDOMNode** outNode)
1124 domelem *This = impl_from_IXMLDOMElement( iface );
1125 TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), outNode);
1126 return node_select_singlenode(&This->node, p, outNode);
1129 static HRESULT WINAPI domelem_get_parsed(
1130 IXMLDOMElement *iface,
1131 VARIANT_BOOL* isParsed)
1133 domelem *This = impl_from_IXMLDOMElement( iface );
1134 FIXME("(%p)->(%p) stub!\n", This, isParsed);
1135 *isParsed = VARIANT_TRUE;
1136 return S_OK;
1139 static HRESULT WINAPI domelem_get_namespaceURI(
1140 IXMLDOMElement *iface,
1141 BSTR* p)
1143 domelem *This = impl_from_IXMLDOMElement( iface );
1144 TRACE("(%p)->(%p)\n", This, p);
1145 return node_get_namespaceURI(&This->node, p);
1148 static HRESULT WINAPI domelem_get_prefix(
1149 IXMLDOMElement *iface,
1150 BSTR* prefix)
1152 domelem *This = impl_from_IXMLDOMElement( iface );
1153 TRACE("(%p)->(%p)\n", This, prefix);
1154 return node_get_prefix( &This->node, prefix );
1157 static HRESULT WINAPI domelem_get_baseName(
1158 IXMLDOMElement *iface,
1159 BSTR* name)
1161 domelem *This = impl_from_IXMLDOMElement( iface );
1162 TRACE("(%p)->(%p)\n", This, name);
1163 return node_get_base_name( &This->node, name );
1166 static HRESULT WINAPI domelem_transformNodeToObject(
1167 IXMLDOMElement *iface,
1168 IXMLDOMNode* domNode, VARIANT var1)
1170 domelem *This = impl_from_IXMLDOMElement( iface );
1171 FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
1172 return E_NOTIMPL;
1175 static HRESULT WINAPI domelem_get_tagName(
1176 IXMLDOMElement *iface,
1177 BSTR* p)
1179 domelem *This = impl_from_IXMLDOMElement( iface );
1180 xmlNodePtr element;
1181 const xmlChar *prefix;
1182 xmlChar *qname;
1184 TRACE("(%p)->(%p)\n", This, p );
1186 if (!p) return E_INVALIDARG;
1188 element = get_element( This );
1189 if ( !element )
1190 return E_FAIL;
1192 prefix = element->ns ? element->ns->prefix : NULL;
1193 qname = xmlBuildQName(element->name, prefix, NULL, 0);
1195 *p = bstr_from_xmlChar(qname);
1196 if (qname != element->name) xmlFree(qname);
1198 return *p ? S_OK : E_OUTOFMEMORY;
1201 static HRESULT WINAPI domelem_getAttribute(
1202 IXMLDOMElement *iface,
1203 BSTR name, VARIANT* value)
1205 domelem *This = impl_from_IXMLDOMElement( iface );
1206 xmlNodePtr element;
1207 xmlChar *xml_name, *xml_value = NULL;
1208 xmlChar *local, *prefix;
1209 HRESULT hr = S_FALSE;
1210 xmlNsPtr ns;
1212 TRACE("(%p)->(%s %p)\n", This, debugstr_w(name), value);
1214 if(!value || !name)
1215 return E_INVALIDARG;
1217 element = get_element( This );
1218 if ( !element )
1219 return E_FAIL;
1221 V_BSTR(value) = NULL;
1222 V_VT(value) = VT_NULL;
1224 xml_name = xmlchar_from_wchar( name );
1226 if(!xmlValidateNameValue(xml_name))
1227 hr = E_FAIL;
1228 else
1230 if ((local = xmlSplitQName2(xml_name, &prefix)))
1232 if (xmlStrEqual(prefix, BAD_CAST "xmlns"))
1234 ns = xmlSearchNs(element->doc, element, local);
1235 if (ns)
1236 xml_value = xmlStrdup(ns->href);
1238 else
1240 ns = xmlSearchNs(element->doc, element, prefix);
1241 if (ns)
1242 xml_value = xmlGetNsProp(element, local, ns->href);
1245 xmlFree(prefix);
1246 xmlFree(local);
1248 else
1249 xml_value = xmlGetNsProp(element, xml_name, NULL);
1252 heap_free(xml_name);
1253 if(xml_value)
1255 V_VT(value) = VT_BSTR;
1256 V_BSTR(value) = bstr_from_xmlChar( xml_value );
1257 xmlFree(xml_value);
1258 hr = S_OK;
1261 return hr;
1264 static HRESULT WINAPI domelem_setAttribute(
1265 IXMLDOMElement *iface,
1266 BSTR name, VARIANT value)
1268 domelem *This = impl_from_IXMLDOMElement( iface );
1269 xmlChar *xml_name, *xml_value, *local, *prefix;
1270 xmlNodePtr element;
1271 HRESULT hr = S_OK;
1273 TRACE("(%p)->(%s %s)\n", This, debugstr_w(name), debugstr_variant(&value));
1275 element = get_element( This );
1276 if ( !element )
1277 return E_FAIL;
1279 if (V_VT(&value) != VT_BSTR)
1281 VARIANT var;
1283 VariantInit(&var);
1284 hr = VariantChangeType(&var, &value, 0, VT_BSTR);
1285 if (hr != S_OK)
1287 FIXME("VariantChangeType failed\n");
1288 return hr;
1291 xml_value = xmlchar_from_wchar(V_BSTR(&var));
1292 VariantClear(&var);
1294 else
1295 xml_value = xmlchar_from_wchar(V_BSTR(&value));
1297 xml_name = xmlchar_from_wchar( name );
1299 if ((local = xmlSplitQName2(xml_name, &prefix)))
1301 static const xmlChar* xmlnsA = (const xmlChar*)"xmlns";
1302 xmlNsPtr ns = NULL;
1304 /* it's not allowed to modify existing namespace definition */
1305 if (xmlStrEqual(prefix, xmlnsA))
1306 ns = xmlSearchNs(element->doc, element, local);
1308 xmlFree(prefix);
1309 xmlFree(local);
1311 if (ns)
1313 int cmp = xmlStrEqual(ns->href, xml_value);
1314 heap_free(xml_value);
1315 heap_free(xml_name);
1316 return cmp ? S_OK : E_INVALIDARG;
1320 if (!xmlSetNsProp(element, NULL, xml_name, xml_value))
1321 hr = E_FAIL;
1323 heap_free(xml_value);
1324 heap_free(xml_name);
1326 return hr;
1329 static HRESULT WINAPI domelem_removeAttribute(
1330 IXMLDOMElement *iface,
1331 BSTR p)
1333 domelem *This = impl_from_IXMLDOMElement( iface );
1334 IXMLDOMNamedNodeMap *attr;
1335 HRESULT hr;
1337 TRACE("(%p)->(%s)\n", This, debugstr_w(p));
1339 hr = IXMLDOMElement_get_attributes(iface, &attr);
1340 if (hr != S_OK) return hr;
1342 hr = IXMLDOMNamedNodeMap_removeNamedItem(attr, p, NULL);
1343 IXMLDOMNamedNodeMap_Release(attr);
1345 return hr;
1348 static HRESULT WINAPI domelem_getAttributeNode(
1349 IXMLDOMElement *iface,
1350 BSTR p, IXMLDOMAttribute** attributeNode )
1352 domelem *This = impl_from_IXMLDOMElement( iface );
1353 xmlChar *local, *prefix, *nameA;
1354 HRESULT hr = S_FALSE;
1355 xmlNodePtr element;
1356 xmlAttrPtr attr;
1358 TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), attributeNode);
1360 element = get_element( This );
1361 if (!element) return E_FAIL;
1363 if (attributeNode) *attributeNode = NULL;
1365 nameA = xmlchar_from_wchar(p);
1366 if (!xmlValidateNameValue(nameA))
1368 heap_free(nameA);
1369 return E_FAIL;
1372 if (!attributeNode)
1374 heap_free(nameA);
1375 return S_FALSE;
1378 *attributeNode = NULL;
1380 local = xmlSplitQName2(nameA, &prefix);
1382 if (local)
1384 /* try to get namespace for supplied qualified name */
1385 xmlNsPtr ns = xmlSearchNs(element->doc, element, prefix);
1386 xmlFree(prefix);
1388 attr = xmlHasNsProp(element, local, ns ? ns->href : NULL);
1389 xmlFree(local);
1391 else
1393 attr = xmlHasProp(element, nameA);
1394 /* attribute has attached namespace and we requested non-qualified
1395 name - it's a failure case */
1396 if (attr && attr->ns) attr = NULL;
1399 heap_free(nameA);
1401 if (attr)
1403 IUnknown *unk = create_attribute((xmlNodePtr)attr, FALSE);
1404 hr = IUnknown_QueryInterface(unk, &IID_IXMLDOMAttribute, (void**)attributeNode);
1405 IUnknown_Release(unk);
1408 return hr;
1411 static HRESULT WINAPI domelem_setAttributeNode(
1412 IXMLDOMElement *iface,
1413 IXMLDOMAttribute* attribute,
1414 IXMLDOMAttribute** old)
1416 domelem *This = impl_from_IXMLDOMElement( iface );
1417 static const WCHAR xmlnsW[] = {'x','m','l','n','s',0};
1418 xmlChar *name, *value;
1419 BSTR nameW, prefix;
1420 xmlnode *attr_node;
1421 xmlAttrPtr attr;
1422 VARIANT valueW;
1423 HRESULT hr;
1425 FIXME("(%p)->(%p %p): semi-stub\n", This, attribute, old);
1427 if (!attribute) return E_INVALIDARG;
1429 attr_node = get_node_obj((IXMLDOMNode*)attribute);
1430 if (!attr_node) return E_FAIL;
1432 if (attr_node->parent)
1434 WARN("attempt to add already used attribute\n");
1435 return E_FAIL;
1438 hr = IXMLDOMAttribute_get_nodeName(attribute, &nameW);
1439 if (hr != S_OK) return hr;
1441 /* adding xmlns attribute doesn't change a tree or existing namespace definition */
1442 if (!wcscmp(nameW, xmlnsW))
1444 SysFreeString(nameW);
1445 return DISP_E_UNKNOWNNAME;
1448 hr = IXMLDOMAttribute_get_nodeValue(attribute, &valueW);
1449 if (hr != S_OK)
1451 SysFreeString(nameW);
1452 return hr;
1455 if (old) *old = NULL;
1457 TRACE("attribute: %s=%s\n", debugstr_w(nameW), debugstr_w(V_BSTR(&valueW)));
1459 hr = IXMLDOMAttribute_get_prefix(attribute, &prefix);
1460 if (hr == S_OK)
1462 FIXME("namespaces not supported: %s\n", debugstr_w(prefix));
1463 SysFreeString(prefix);
1466 name = xmlchar_from_wchar(nameW);
1467 value = xmlchar_from_wchar(V_BSTR(&valueW));
1469 if (!name || !value)
1471 SysFreeString(nameW);
1472 VariantClear(&valueW);
1473 heap_free(name);
1474 heap_free(value);
1475 return E_OUTOFMEMORY;
1478 attr = xmlSetNsProp(get_element(This), NULL, name, value);
1479 if (attr)
1480 attr_node->parent = (IXMLDOMNode*)iface;
1482 SysFreeString(nameW);
1483 VariantClear(&valueW);
1484 heap_free(name);
1485 heap_free(value);
1487 return attr ? S_OK : E_FAIL;
1490 static HRESULT WINAPI domelem_removeAttributeNode(
1491 IXMLDOMElement *iface,
1492 IXMLDOMAttribute* domAttribute,
1493 IXMLDOMAttribute** attributeNode)
1495 domelem *This = impl_from_IXMLDOMElement( iface );
1496 FIXME("(%p)->(%p %p)\n", This, domAttribute, attributeNode);
1497 return E_NOTIMPL;
1500 static HRESULT WINAPI domelem_getElementsByTagName(
1501 IXMLDOMElement *iface,
1502 BSTR tagName, IXMLDOMNodeList** resultList)
1504 domelem *This = impl_from_IXMLDOMElement( iface );
1505 xmlChar *query;
1506 HRESULT hr;
1507 BOOL XPath;
1509 TRACE("(%p)->(%s, %p)\n", This, debugstr_w(tagName), resultList);
1511 if (!tagName || !resultList) return E_INVALIDARG;
1513 XPath = is_xpathmode(get_element(This)->doc);
1514 set_xpathmode(get_element(This)->doc, TRUE);
1515 query = tagName_to_XPath(tagName);
1516 hr = create_selection(get_element(This), query, resultList);
1517 xmlFree(query);
1518 set_xpathmode(get_element(This)->doc, XPath);
1520 return hr;
1523 static HRESULT WINAPI domelem_normalize(
1524 IXMLDOMElement *iface )
1526 domelem *This = impl_from_IXMLDOMElement( iface );
1527 FIXME("%p\n", This);
1528 return E_NOTIMPL;
1531 static const struct IXMLDOMElementVtbl domelem_vtbl =
1533 domelem_QueryInterface,
1534 domelem_AddRef,
1535 domelem_Release,
1536 domelem_GetTypeInfoCount,
1537 domelem_GetTypeInfo,
1538 domelem_GetIDsOfNames,
1539 domelem_Invoke,
1540 domelem_get_nodeName,
1541 domelem_get_nodeValue,
1542 domelem_put_nodeValue,
1543 domelem_get_nodeType,
1544 domelem_get_parentNode,
1545 domelem_get_childNodes,
1546 domelem_get_firstChild,
1547 domelem_get_lastChild,
1548 domelem_get_previousSibling,
1549 domelem_get_nextSibling,
1550 domelem_get_attributes,
1551 domelem_insertBefore,
1552 domelem_replaceChild,
1553 domelem_removeChild,
1554 domelem_appendChild,
1555 domelem_hasChildNodes,
1556 domelem_get_ownerDocument,
1557 domelem_cloneNode,
1558 domelem_get_nodeTypeString,
1559 domelem_get_text,
1560 domelem_put_text,
1561 domelem_get_specified,
1562 domelem_get_definition,
1563 domelem_get_nodeTypedValue,
1564 domelem_put_nodeTypedValue,
1565 domelem_get_dataType,
1566 domelem_put_dataType,
1567 domelem_get_xml,
1568 domelem_transformNode,
1569 domelem_selectNodes,
1570 domelem_selectSingleNode,
1571 domelem_get_parsed,
1572 domelem_get_namespaceURI,
1573 domelem_get_prefix,
1574 domelem_get_baseName,
1575 domelem_transformNodeToObject,
1576 domelem_get_tagName,
1577 domelem_getAttribute,
1578 domelem_setAttribute,
1579 domelem_removeAttribute,
1580 domelem_getAttributeNode,
1581 domelem_setAttributeNode,
1582 domelem_removeAttributeNode,
1583 domelem_getElementsByTagName,
1584 domelem_normalize,
1587 static HRESULT domelem_get_qualified_item(const xmlNodePtr node, BSTR name, BSTR uri,
1588 IXMLDOMNode **item)
1590 xmlAttrPtr attr;
1591 xmlChar *nameA;
1592 xmlChar *href;
1594 TRACE("(%p)->(%s %s %p)\n", node, debugstr_w(name), debugstr_w(uri), item);
1596 if (!name || !item) return E_INVALIDARG;
1598 if (uri && *uri)
1600 href = xmlchar_from_wchar(uri);
1601 if (!href) return E_OUTOFMEMORY;
1603 else
1604 href = NULL;
1606 nameA = xmlchar_from_wchar(name);
1607 if (!nameA)
1609 heap_free(href);
1610 return E_OUTOFMEMORY;
1613 attr = xmlHasNsProp(node, nameA, href);
1615 heap_free(nameA);
1616 heap_free(href);
1618 if (!attr)
1620 *item = NULL;
1621 return S_FALSE;
1624 *item = create_node((xmlNodePtr)attr);
1626 return S_OK;
1629 static HRESULT domelem_get_named_item(const xmlNodePtr node, BSTR name, IXMLDOMNode **item)
1631 xmlChar *nameA, *local, *prefix;
1632 BSTR uriW, localW;
1633 xmlNsPtr ns;
1634 HRESULT hr;
1636 TRACE("(%p)->(%s %p)\n", node, debugstr_w(name), item );
1638 nameA = xmlchar_from_wchar(name);
1639 local = xmlSplitQName2(nameA, &prefix);
1640 heap_free(nameA);
1642 if (!local)
1643 return domelem_get_qualified_item(node, name, NULL, item);
1645 /* try to get namespace uri for supplied qualified name */
1646 ns = xmlSearchNs(node->doc, node, prefix);
1648 xmlFree(prefix);
1650 if (!ns)
1652 xmlFree(local);
1653 if (item) *item = NULL;
1654 return item ? S_FALSE : E_INVALIDARG;
1657 uriW = bstr_from_xmlChar(ns->href);
1658 localW = bstr_from_xmlChar(local);
1659 xmlFree(local);
1661 TRACE("got qualified node %s, uri=%s\n", debugstr_w(localW), debugstr_w(uriW));
1663 hr = domelem_get_qualified_item(node, localW, uriW, item);
1665 SysFreeString(localW);
1666 SysFreeString(uriW);
1668 return hr;
1671 static HRESULT domelem_set_named_item(xmlNodePtr node, IXMLDOMNode *newItem, IXMLDOMNode **namedItem)
1673 xmlNodePtr nodeNew;
1674 xmlnode *ThisNew;
1676 TRACE("(%p)->(%p %p)\n", node, newItem, namedItem );
1678 if(!newItem)
1679 return E_INVALIDARG;
1681 if(namedItem) *namedItem = NULL;
1683 /* Must be an Attribute */
1684 ThisNew = get_node_obj( newItem );
1685 if(!ThisNew) return E_FAIL;
1687 if(ThisNew->node->type != XML_ATTRIBUTE_NODE)
1688 return E_FAIL;
1690 if(!ThisNew->node->parent)
1691 if(xmldoc_remove_orphan(ThisNew->node->doc, ThisNew->node) != S_OK)
1692 WARN("%p is not an orphan of %p\n", ThisNew->node, ThisNew->node->doc);
1694 nodeNew = xmlAddChild(node, ThisNew->node);
1696 if(namedItem)
1697 *namedItem = create_node( nodeNew );
1698 return S_OK;
1701 static HRESULT domelem_remove_qualified_item(xmlNodePtr node, BSTR name, BSTR uri, IXMLDOMNode **item)
1703 xmlChar *nameA, *href;
1704 xmlAttrPtr attr;
1706 TRACE("(%p)->(%s %s %p)\n", node, debugstr_w(name), debugstr_w(uri), item);
1708 if (!name) return E_INVALIDARG;
1710 if (uri && *uri)
1712 href = xmlchar_from_wchar(uri);
1713 if (!href) return E_OUTOFMEMORY;
1715 else
1716 href = NULL;
1718 nameA = xmlchar_from_wchar(name);
1719 if (!nameA)
1721 heap_free(href);
1722 return E_OUTOFMEMORY;
1725 attr = xmlHasNsProp(node, nameA, href);
1727 heap_free(nameA);
1728 heap_free(href);
1730 if (!attr)
1732 if (item) *item = NULL;
1733 return S_FALSE;
1736 if (item)
1738 xmlUnlinkNode( (xmlNodePtr) attr );
1739 xmldoc_add_orphan( attr->doc, (xmlNodePtr) attr );
1740 *item = create_node( (xmlNodePtr) attr );
1742 else
1744 if (xmlRemoveProp(attr) == -1)
1745 ERR("xmlRemoveProp failed\n");
1748 return S_OK;
1751 static HRESULT domelem_remove_named_item(xmlNodePtr node, BSTR name, IXMLDOMNode **item)
1753 xmlChar *nameA, *local, *prefix;
1754 BSTR uriW, localW;
1755 xmlNsPtr ns;
1756 HRESULT hr;
1758 TRACE("(%p)->(%s %p)\n", node, debugstr_w(name), item);
1760 nameA = xmlchar_from_wchar(name);
1761 local = xmlSplitQName2(nameA, &prefix);
1762 heap_free(nameA);
1764 if (!local)
1765 return domelem_remove_qualified_item(node, name, NULL, item);
1767 ns = xmlSearchNs(node->doc, node, prefix);
1769 xmlFree(prefix);
1771 if (!ns)
1773 xmlFree(local);
1774 if (item) *item = NULL;
1775 return item ? S_FALSE : E_INVALIDARG;
1778 uriW = bstr_from_xmlChar(ns->href);
1779 localW = bstr_from_xmlChar(local);
1780 xmlFree(local);
1782 TRACE("removing qualified node %s, uri=%s\n", debugstr_w(localW), debugstr_w(uriW));
1784 hr = domelem_remove_qualified_item(node, localW, uriW, item);
1786 SysFreeString(localW);
1787 SysFreeString(uriW);
1789 return hr;
1792 static HRESULT domelem_get_item(const xmlNodePtr node, LONG index, IXMLDOMNode **item)
1794 xmlNsPtr ns, xmlns;
1795 xmlAttrPtr curr;
1796 LONG attrIndex;
1797 IUnknown *unk;
1798 HRESULT hr;
1800 TRACE("%p, %ld, %p.\n", node, index, item);
1802 *item = NULL;
1804 if (index < 0)
1805 return S_FALSE;
1807 attrIndex = 0;
1808 curr = node->properties;
1809 if (curr) {
1810 for (; attrIndex < index && curr->next != NULL; attrIndex++)
1811 curr = curr->next;
1813 if (attrIndex == index) {
1814 *item = create_node( (xmlNodePtr) curr );
1815 return S_OK;
1819 if (!node->nsDef)
1820 return S_FALSE;
1822 attrIndex++;
1823 ns = node->nsDef;
1824 for (; attrIndex < index && ns->next != NULL; attrIndex++)
1825 ns = ns->next;
1827 if (attrIndex < index)
1828 return S_FALSE;
1830 xmlns = xmlNewNs(NULL, BAD_CAST "http://www.w3.org/2000/xmlns/", BAD_CAST "xmlns");
1831 if (!xmlns)
1832 return E_OUTOFMEMORY;
1834 curr = xmlNewNsProp(NULL, xmlns, ns->prefix, ns->href);
1835 if (!curr) {
1836 xmlFreeNs(xmlns);
1837 return E_OUTOFMEMORY;
1839 curr->doc = node->doc;
1841 unk = create_attribute((xmlNodePtr)curr, TRUE);
1842 if (!unk) {
1843 xmlFreeNs(xmlns);
1844 xmlFreeProp(curr);
1845 return E_OUTOFMEMORY;
1848 hr = IUnknown_QueryInterface(unk, &IID_IXMLDOMNode, (void**)item);
1849 IUnknown_Release(unk);
1851 return hr;
1854 static HRESULT domelem_get_length(const xmlNodePtr node, LONG *length)
1856 xmlAttrPtr curr;
1857 LONG attrCount;
1858 xmlNsPtr ns;
1860 TRACE("(%p)->(%p)\n", node, length);
1862 if( !length )
1863 return E_INVALIDARG;
1865 attrCount = 0;
1866 curr = node->properties;
1867 while (curr) {
1868 attrCount++;
1869 curr = curr->next;
1872 ns = node->nsDef;
1873 while (ns) {
1874 attrCount++;
1875 ns = ns->next;
1877 *length = attrCount;
1879 return S_OK;
1882 static HRESULT domelem_next_node(const xmlNodePtr node, LONG *iter, IXMLDOMNode **nextNode)
1884 xmlAttrPtr curr;
1885 LONG i;
1887 TRACE("%p, %ld, %p.\n", node, *iter, nextNode);
1889 *nextNode = NULL;
1891 curr = node->properties;
1892 if (curr == NULL)
1893 return S_FALSE;
1895 for (i = 0; i < *iter; i++) {
1896 if (curr->next == NULL)
1897 return S_FALSE;
1898 else
1899 curr = curr->next;
1902 (*iter)++;
1903 *nextNode = create_node((xmlNodePtr)curr);
1905 return S_OK;
1908 static const struct nodemap_funcs domelem_attr_map = {
1909 domelem_get_named_item,
1910 domelem_set_named_item,
1911 domelem_remove_named_item,
1912 domelem_get_item,
1913 domelem_get_length,
1914 domelem_get_qualified_item,
1915 domelem_remove_qualified_item,
1916 domelem_next_node
1919 static const tid_t domelem_iface_tids[] = {
1920 IXMLDOMElement_tid,
1924 static dispex_static_data_t domelem_dispex = {
1925 NULL,
1926 IXMLDOMElement_tid,
1927 NULL,
1928 domelem_iface_tids
1931 IUnknown* create_element( xmlNodePtr element )
1933 domelem *This;
1935 This = heap_alloc( sizeof *This );
1936 if ( !This )
1937 return NULL;
1939 This->IXMLDOMElement_iface.lpVtbl = &domelem_vtbl;
1940 This->ref = 1;
1942 init_xmlnode(&This->node, element, (IXMLDOMNode*)&This->IXMLDOMElement_iface, &domelem_dispex);
1944 return (IUnknown*)&This->IXMLDOMElement_iface;