mfplat: Remove duplicated GUID entry from attribute tracing.
[wine/zf.git] / dlls / msxml3 / element.c
blob05255508d4eb6240d9b7079838624927a994473f
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 "config.h"
25 #include <stdarg.h>
26 #ifdef HAVE_LIBXML2
27 # include <libxml/parser.h>
28 # include <libxml/xmlerror.h>
29 #endif
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "winnls.h"
35 #include "ole2.h"
36 #include "msxml6.h"
38 #include "msxml_private.h"
40 #include "wine/debug.h"
42 #ifdef HAVE_LIBXML2
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
51 xmlnode node;
52 IXMLDOMElement IXMLDOMElement_iface;
53 LONG ref;
54 } domelem;
56 static const struct nodemap_funcs domelem_attr_map;
58 static const tid_t domelem_se_tids[] = {
59 IXMLDOMNode_tid,
60 IXMLDOMElement_tid,
61 NULL_tid
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,
76 REFIID riid,
77 void** ppvObject )
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);
98 else
100 TRACE("interface %s not implemented\n", debugstr_guid(riid));
101 *ppvObject = NULL;
102 return E_NOINTERFACE;
105 IUnknown_AddRef( (IUnknown*)*ppvObject );
106 return S_OK;
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);
117 return 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);
128 if(!ref) {
129 destroy_xmlnode(&This->node);
130 heap_free(This);
133 return ref;
136 static HRESULT WINAPI domelem_GetTypeInfoCount(
137 IXMLDOMElement *iface,
138 UINT* pctinfo )
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,
177 BSTR* p )
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,
188 VARIANT* value)
190 domelem *This = impl_from_IXMLDOMElement( iface );
192 TRACE("(%p)->(%p)\n", This, value);
194 if(!value)
195 return E_INVALIDARG;
197 V_VT(value) = VT_NULL;
198 V_BSTR(value) = NULL; /* tests show that we should do this */
199 return S_FALSE;
202 static HRESULT WINAPI domelem_put_nodeValue(
203 IXMLDOMElement *iface,
204 VARIANT value)
206 domelem *This = impl_from_IXMLDOMElement( iface );
207 TRACE("(%p)->(%s)\n", This, debugstr_variant(&value));
208 return E_FAIL;
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;
220 return S_OK;
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);
298 return S_OK;
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 );
307 DOMNodeType type;
308 HRESULT hr;
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);
316 switch (type)
318 case NODE_DOCUMENT:
319 case NODE_DOCUMENT_TYPE:
320 case NODE_ENTITY:
321 case NODE_NOTATION:
322 if (old_node) *old_node = NULL;
323 return E_FAIL;
324 default:
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,
362 VARIANT_BOOL *ret)
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,
389 BSTR* p)
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,
401 BSTR* p)
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,
410 BSTR p)
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;
424 return S_OK;
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);
433 return E_NOTIMPL;
436 static inline BYTE hex_to_byte(xmlChar c)
438 if(c <= '9') return c-'0';
439 if(c <= 'F') return c-'A'+10;
440 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';
449 return c-'a'+26;
452 static inline HRESULT variant_from_dt(XDR_DT dt, xmlChar* str, VARIANT* v)
454 VARIANT src;
455 HRESULT hr = S_OK;
456 BOOL handled = FALSE;
458 VariantInit(&src);
460 switch (dt)
462 case DT_INVALID:
463 case DT_STRING:
464 case DT_NMTOKEN:
465 case DT_NMTOKENS:
466 case DT_NUMBER:
467 case DT_URI:
468 case DT_UUID:
470 V_VT(v) = VT_BSTR;
471 V_BSTR(v) = bstr_from_xmlChar(str);
473 if(!V_BSTR(v))
474 return E_OUTOFMEMORY;
475 handled = TRUE;
477 break;
478 case DT_DATE:
479 case DT_DATE_TZ:
480 case DT_DATETIME:
481 case DT_DATETIME_TZ:
482 case DT_TIME:
483 case DT_TIME_TZ:
485 WCHAR *p, *e;
486 SYSTEMTIME st;
487 DOUBLE date = 0.0;
489 st.wYear = 1899;
490 st.wMonth = 12;
491 st.wDay = 30;
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);
497 if(!V_BSTR(&src))
498 return E_OUTOFMEMORY;
500 p = V_BSTR(&src);
501 e = p + SysStringLen(V_BSTR(&src));
503 if(p+4<e && *(p+4)=='-') /* parse date (yyyy-mm-dd) */
505 st.wYear = atoiW(p);
506 st.wMonth = atoiW(p+5);
507 st.wDay = atoiW(p+8);
508 p += 10;
510 if(*p == 'T') p++;
513 if(p+2<e && *(p+2)==':') /* parse time (hh:mm:ss.?) */
515 st.wHour = atoiW(p);
516 st.wMinute = atoiW(p+3);
517 st.wSecond = atoiW(p+6);
518 p += 8;
520 if(*p == '.')
522 p++;
523 while (*p >= '0' && *p <= '9') p++;
527 SystemTimeToVariantTime(&st, &date);
528 V_VT(v) = VT_DATE;
529 V_DATE(v) = 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;
536 VariantClear(&src);
537 handled = TRUE;
539 break;
540 case DT_BIN_HEX:
542 SAFEARRAYBOUND sab;
543 int i, len;
545 len = xmlStrlen(str)/2;
546 sab.lLbound = 0;
547 sab.cElements = len;
549 V_VT(v) = (VT_ARRAY|VT_UI1);
550 V_ARRAY(v) = SafeArrayCreate(VT_UI1, 1, &sab);
552 if(!V_ARRAY(v))
553 return E_OUTOFMEMORY;
555 for(i=0; i<len; i++)
556 ((BYTE*)V_ARRAY(v)->pvData)[i] = (hex_to_byte(str[2*i])<<4)
557 + hex_to_byte(str[2*i+1]);
558 handled = TRUE;
560 break;
561 case DT_BIN_BASE64:
563 SAFEARRAYBOUND sab;
564 xmlChar *c1, *c2;
565 int i, len;
567 /* remove all formatting chars */
568 c1 = c2 = str;
569 len = 0;
570 while (*c2)
572 if ( *c2 == ' ' || *c2 == '\t' ||
573 *c2 == '\n' || *c2 == '\r' )
575 c2++;
576 continue;
578 *c1++ = *c2++;
579 len++;
582 /* skip padding */
583 if(str[len-2] == '=') i = 2;
584 else if(str[len-1] == '=') i = 1;
585 else i = 0;
587 sab.lLbound = 0;
588 sab.cElements = len/4*3-i;
590 V_VT(v) = (VT_ARRAY|VT_UI1);
591 V_ARRAY(v) = SafeArrayCreate(VT_UI1, 1, &sab);
593 if(!V_ARRAY(v))
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]);
607 handled = TRUE;
609 break;
610 case DT_BOOLEAN:
611 V_VT(v) = VT_BOOL;
612 break;
613 case DT_FIXED_14_4:
614 V_VT(v) = VT_CY;
615 break;
616 case DT_I1:
617 V_VT(v) = VT_I1;
618 break;
619 case DT_I2:
620 V_VT(v) = VT_I2;
621 break;
622 case DT_I4:
623 case DT_INT:
624 V_VT(v) = VT_I4;
625 break;
626 case DT_I8:
627 V_VT(v) = VT_I8;
628 break;
629 case DT_R4:
630 V_VT(v) = VT_R4;
631 break;
632 case DT_FLOAT:
633 case DT_R8:
634 V_VT(v) = VT_R8;
635 break;
636 case DT_UI1:
637 V_VT(v) = VT_UI1;
638 break;
639 case DT_UI2:
640 V_VT(v) = VT_UI2;
641 break;
642 case DT_UI4:
643 V_VT(v) = VT_UI4;
644 break;
645 case DT_UI8:
646 V_VT(v) = VT_UI8;
647 break;
648 case DT_CHAR:
649 case DT_ENTITY:
650 case DT_ENTITIES:
651 case DT_ENUMERATION:
652 case DT_ID:
653 case DT_IDREF:
654 case DT_IDREFS:
655 case DT_NOTATION:
656 FIXME("need to handle dt:%s\n", debugstr_dt(dt));
657 V_VT(v) = VT_BSTR;
658 V_BSTR(v) = bstr_from_xmlChar(str);
659 if (!V_BSTR(v))
660 return E_OUTOFMEMORY;
661 handled = TRUE;
662 break;
663 default:
664 WARN("unknown type %d\n", dt);
667 if (!handled)
669 V_VT(&src) = VT_BSTR;
670 V_BSTR(&src) = bstr_from_xmlChar(str);
672 if(!V_BSTR(&src))
673 return E_OUTOFMEMORY;
675 hr = VariantChangeTypeEx(v, &src,
676 MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT),0, V_VT(v));
677 VariantClear(&src);
679 return hr;
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");
690 return dt;
693 if (node->ns && xmlStrEqual(node->ns->href, DT_nsURI))
695 dt = str_to_dt(node->name, -1);
697 else
699 xmlChar* pVal = xmlGetNsProp(node, BAD_CAST "dt", DT_nsURI);
700 if (pVal)
702 dt = str_to_dt(pVal, -1);
703 xmlFree(pVal);
705 else if (node->doc)
707 IXMLDOMDocument3* doc = (IXMLDOMDocument3*)create_domdoc((xmlNodePtr)node->doc);
708 if (doc)
710 VARIANT v;
711 VariantInit(&v);
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);
718 VariantClear(&v);
719 IXMLDOMDocument3_Release(doc);
724 TRACE("=> dt:%s\n", debugstr_dt(dt));
725 return dt;
728 static HRESULT WINAPI domelem_get_nodeTypedValue(
729 IXMLDOMElement *iface,
730 VARIANT* v)
732 domelem *This = impl_from_IXMLDOMElement( iface );
733 XDR_DT dt;
734 xmlChar* content;
735 HRESULT hr;
737 TRACE("(%p)->(%p)\n", This, v);
739 if(!v) return E_INVALIDARG;
741 V_VT(v) = VT_NULL;
743 dt = element_get_dt(get_element(This));
744 content = xmlNodeGetContent(get_element(This));
745 hr = variant_from_dt(dt, content, v);
746 xmlFree(content);
748 return hr;
751 static HRESULT encode_base64(const BYTE *buf, int len, BSTR *ret)
753 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
754 const BYTE *d = buf;
755 int bytes, pad_bytes, div;
756 DWORD needed;
757 WCHAR *ptr;
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 */
769 div = len / 3;
771 ptr = *ret;
772 while (div > 0)
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];
784 d += 3;
785 div--;
788 switch (pad_bytes)
790 case 1:
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
797 * two zeroes */
798 *ptr++ = b64[ ((d[1] << 2) & 0x3c) ];
799 /* fourth char is a = to indicate one byte of padding */
800 *ptr++ = '=';
801 break;
802 case 2:
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
806 * four zeroes*/
807 *ptr++ = b64[ ((d[0] << 4) & 0x30)];
808 /* third char is = to indicate padding */
809 *ptr++ = '=';
810 /* fourth char is = to indicate padding */
811 *ptr++ = '=';
812 break;
815 return S_OK;
818 static HRESULT encode_binhex(const BYTE *buf, int len, BSTR *ret)
820 static const char byte_to_hex[16] = "0123456789abcdef";
821 int i;
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]];
832 return S_OK;
835 static HRESULT WINAPI domelem_put_nodeTypedValue(
836 IXMLDOMElement *iface,
837 VARIANT value)
839 domelem *This = impl_from_IXMLDOMElement( iface );
840 XDR_DT dt;
841 HRESULT hr;
843 TRACE("(%p)->(%s)\n", This, debugstr_variant(&value));
845 dt = element_get_dt(get_element(This));
846 switch (dt)
848 /* for untyped node coerce to BSTR and set */
849 case DT_INVALID:
850 case DT_INT:
851 if (V_VT(&value) != VT_BSTR)
853 VARIANT content;
854 VariantInit(&content);
855 hr = VariantChangeType(&content, &value, 0, VT_BSTR);
856 if (hr == S_OK)
858 hr = node_set_content(&This->node, V_BSTR(&content));
859 VariantClear(&content);
862 else
863 hr = node_set_content(&This->node, V_BSTR(&value));
864 break;
865 case DT_BIN_BASE64:
866 if (V_VT(&value) == VT_BSTR)
867 hr = node_set_content(&This->node, V_BSTR(&value));
868 else if (V_VT(&value) == (VT_UI1|VT_ARRAY))
870 UINT dim = SafeArrayGetDim(V_ARRAY(&value));
871 LONG lbound, ubound;
872 BSTR encoded;
873 BYTE *ptr;
874 int len;
876 if (dim > 1)
877 FIXME("unexpected array dimension count %u\n", dim);
879 SafeArrayGetUBound(V_ARRAY(&value), 1, &ubound);
880 SafeArrayGetLBound(V_ARRAY(&value), 1, &lbound);
882 len = (ubound - lbound + 1)*SafeArrayGetElemsize(V_ARRAY(&value));
884 hr = SafeArrayAccessData(V_ARRAY(&value), (void*)&ptr);
885 if (FAILED(hr)) return hr;
887 hr = encode_base64(ptr, len, &encoded);
888 SafeArrayUnaccessData(V_ARRAY(&value));
889 if (FAILED(hr)) return hr;
891 hr = node_set_content(&This->node, encoded);
892 SysFreeString(encoded);
894 else
896 FIXME("unhandled variant type %d for dt:%s\n", V_VT(&value), debugstr_dt(dt));
897 return E_NOTIMPL;
899 break;
900 case DT_BIN_HEX:
901 if (V_VT(&value) == (VT_UI1|VT_ARRAY))
903 UINT dim = SafeArrayGetDim(V_ARRAY(&value));
904 LONG lbound, ubound;
905 BSTR encoded;
906 BYTE *ptr;
907 int len;
909 if (dim > 1)
910 FIXME("unexpected array dimension count %u\n", dim);
912 SafeArrayGetUBound(V_ARRAY(&value), 1, &ubound);
913 SafeArrayGetLBound(V_ARRAY(&value), 1, &lbound);
915 len = (ubound - lbound + 1)*SafeArrayGetElemsize(V_ARRAY(&value));
917 hr = SafeArrayAccessData(V_ARRAY(&value), (void*)&ptr);
918 if (FAILED(hr)) return hr;
920 hr = encode_binhex(ptr, len, &encoded);
921 SafeArrayUnaccessData(V_ARRAY(&value));
922 if (FAILED(hr)) return hr;
924 hr = node_set_content(&This->node, encoded);
925 SysFreeString(encoded);
927 else
929 FIXME("unhandled variant type %d for dt:%s\n", V_VT(&value), debugstr_dt(dt));
930 return E_NOTIMPL;
932 break;
933 default:
934 FIXME("not implemented for dt:%s\n", debugstr_dt(dt));
935 return E_NOTIMPL;
938 return hr;
941 static HRESULT WINAPI domelem_get_dataType(
942 IXMLDOMElement *iface,
943 VARIANT* typename)
945 domelem *This = impl_from_IXMLDOMElement( iface );
946 XDR_DT dt;
948 TRACE("(%p)->(%p)\n", This, typename);
950 if (!typename)
951 return E_INVALIDARG;
953 dt = element_get_dt(get_element(This));
954 switch (dt)
956 case DT_BIN_BASE64:
957 case DT_BIN_HEX:
958 case DT_BOOLEAN:
959 case DT_CHAR:
960 case DT_DATE:
961 case DT_DATE_TZ:
962 case DT_DATETIME:
963 case DT_DATETIME_TZ:
964 case DT_FIXED_14_4:
965 case DT_FLOAT:
966 case DT_I1:
967 case DT_I2:
968 case DT_I4:
969 case DT_I8:
970 case DT_INT:
971 case DT_NUMBER:
972 case DT_R4:
973 case DT_R8:
974 case DT_TIME:
975 case DT_TIME_TZ:
976 case DT_UI1:
977 case DT_UI2:
978 case DT_UI4:
979 case DT_UI8:
980 case DT_URI:
981 case DT_UUID:
982 V_VT(typename) = VT_BSTR;
983 V_BSTR(typename) = SysAllocString(dt_to_bstr(dt));
985 if (!V_BSTR(typename))
986 return E_OUTOFMEMORY;
987 break;
988 default:
989 /* Other types (DTD equivalents) do not return anything here,
990 * but the pointer part of the VARIANT is set to NULL */
991 V_VT(typename) = VT_NULL;
992 V_BSTR(typename) = NULL;
993 break;
995 return (V_VT(typename) != VT_NULL) ? S_OK : S_FALSE;
998 static HRESULT WINAPI domelem_put_dataType(
999 IXMLDOMElement *iface,
1000 BSTR dtName)
1002 domelem *This = impl_from_IXMLDOMElement( iface );
1003 HRESULT hr = E_FAIL;
1004 xmlChar *str;
1005 XDR_DT dt;
1007 TRACE("(%p)->(%s)\n", This, debugstr_w(dtName));
1009 if(dtName == NULL)
1010 return E_INVALIDARG;
1012 dt = bstr_to_dt(dtName, -1);
1014 /* An example of this is. The Text in the node needs to be a 0 or 1 for a boolean type.
1015 This applies to changing types (string->bool) or setting a new one
1017 str = xmlNodeGetContent(get_element(This));
1018 hr = dt_validate(dt, str);
1019 xmlFree(str);
1021 /* Check all supported types. */
1022 if (hr == S_OK)
1024 switch (dt)
1026 case DT_BIN_BASE64:
1027 case DT_BIN_HEX:
1028 case DT_BOOLEAN:
1029 case DT_CHAR:
1030 case DT_DATE:
1031 case DT_DATE_TZ:
1032 case DT_DATETIME:
1033 case DT_DATETIME_TZ:
1034 case DT_FIXED_14_4:
1035 case DT_FLOAT:
1036 case DT_I1:
1037 case DT_I2:
1038 case DT_I4:
1039 case DT_I8:
1040 case DT_INT:
1041 case DT_NMTOKEN:
1042 case DT_NMTOKENS:
1043 case DT_NUMBER:
1044 case DT_R4:
1045 case DT_R8:
1046 case DT_STRING:
1047 case DT_TIME:
1048 case DT_TIME_TZ:
1049 case DT_UI1:
1050 case DT_UI2:
1051 case DT_UI4:
1052 case DT_UI8:
1053 case DT_URI:
1054 case DT_UUID:
1056 xmlAttrPtr attr = xmlHasNsProp(get_element(This), DT_prefix, DT_nsURI);
1057 if (attr)
1059 attr = xmlSetNsProp(get_element(This), attr->ns, DT_prefix, dt_to_str(dt));
1060 hr = S_OK;
1062 else
1064 xmlNsPtr ns = xmlNewNs(get_element(This), DT_nsURI, DT_prefix);
1065 if (ns)
1067 attr = xmlNewNsProp(get_element(This), ns, DT_prefix, dt_to_str(dt));
1068 if (attr)
1070 xmlAddChild(get_element(This), (xmlNodePtr)attr);
1071 hr = S_OK;
1073 else
1074 ERR("Failed to create Attribute\n");
1076 else
1077 ERR("Failed to create Namespace\n");
1080 break;
1081 default:
1082 FIXME("need to handle dt:%s\n", debugstr_dt(dt));
1083 break;
1087 return hr;
1090 static HRESULT WINAPI domelem_get_xml(
1091 IXMLDOMElement *iface,
1092 BSTR* p)
1094 domelem *This = impl_from_IXMLDOMElement( iface );
1096 TRACE("(%p)->(%p)\n", This, p);
1098 return node_get_xml(&This->node, TRUE, p);
1101 static HRESULT WINAPI domelem_transformNode(
1102 IXMLDOMElement *iface,
1103 IXMLDOMNode *node, BSTR *p)
1105 domelem *This = impl_from_IXMLDOMElement( iface );
1106 TRACE("(%p)->(%p %p)\n", This, node, p);
1107 return node_transform_node(&This->node, node, p);
1110 static HRESULT WINAPI domelem_selectNodes(
1111 IXMLDOMElement *iface,
1112 BSTR p, IXMLDOMNodeList** outList)
1114 domelem *This = impl_from_IXMLDOMElement( iface );
1115 TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), outList);
1116 return node_select_nodes(&This->node, p, outList);
1119 static HRESULT WINAPI domelem_selectSingleNode(
1120 IXMLDOMElement *iface,
1121 BSTR p, IXMLDOMNode** outNode)
1123 domelem *This = impl_from_IXMLDOMElement( iface );
1124 TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), outNode);
1125 return node_select_singlenode(&This->node, p, outNode);
1128 static HRESULT WINAPI domelem_get_parsed(
1129 IXMLDOMElement *iface,
1130 VARIANT_BOOL* isParsed)
1132 domelem *This = impl_from_IXMLDOMElement( iface );
1133 FIXME("(%p)->(%p) stub!\n", This, isParsed);
1134 *isParsed = VARIANT_TRUE;
1135 return S_OK;
1138 static HRESULT WINAPI domelem_get_namespaceURI(
1139 IXMLDOMElement *iface,
1140 BSTR* p)
1142 domelem *This = impl_from_IXMLDOMElement( iface );
1143 TRACE("(%p)->(%p)\n", This, p);
1144 return node_get_namespaceURI(&This->node, p);
1147 static HRESULT WINAPI domelem_get_prefix(
1148 IXMLDOMElement *iface,
1149 BSTR* prefix)
1151 domelem *This = impl_from_IXMLDOMElement( iface );
1152 TRACE("(%p)->(%p)\n", This, prefix);
1153 return node_get_prefix( &This->node, prefix );
1156 static HRESULT WINAPI domelem_get_baseName(
1157 IXMLDOMElement *iface,
1158 BSTR* name)
1160 domelem *This = impl_from_IXMLDOMElement( iface );
1161 TRACE("(%p)->(%p)\n", This, name);
1162 return node_get_base_name( &This->node, name );
1165 static HRESULT WINAPI domelem_transformNodeToObject(
1166 IXMLDOMElement *iface,
1167 IXMLDOMNode* domNode, VARIANT var1)
1169 domelem *This = impl_from_IXMLDOMElement( iface );
1170 FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
1171 return E_NOTIMPL;
1174 static HRESULT WINAPI domelem_get_tagName(
1175 IXMLDOMElement *iface,
1176 BSTR* p)
1178 domelem *This = impl_from_IXMLDOMElement( iface );
1179 xmlNodePtr element;
1180 const xmlChar *prefix;
1181 xmlChar *qname;
1183 TRACE("(%p)->(%p)\n", This, p );
1185 if (!p) return E_INVALIDARG;
1187 element = get_element( This );
1188 if ( !element )
1189 return E_FAIL;
1191 prefix = element->ns ? element->ns->prefix : NULL;
1192 qname = xmlBuildQName(element->name, prefix, NULL, 0);
1194 *p = bstr_from_xmlChar(qname);
1195 if (qname != element->name) xmlFree(qname);
1197 return *p ? S_OK : E_OUTOFMEMORY;
1200 static HRESULT WINAPI domelem_getAttribute(
1201 IXMLDOMElement *iface,
1202 BSTR name, VARIANT* value)
1204 domelem *This = impl_from_IXMLDOMElement( iface );
1205 xmlNodePtr element;
1206 xmlChar *xml_name, *xml_value = NULL;
1207 xmlChar *local, *prefix;
1208 HRESULT hr = S_FALSE;
1209 xmlNsPtr ns;
1211 TRACE("(%p)->(%s %p)\n", This, debugstr_w(name), value);
1213 if(!value || !name)
1214 return E_INVALIDARG;
1216 element = get_element( This );
1217 if ( !element )
1218 return E_FAIL;
1220 V_BSTR(value) = NULL;
1221 V_VT(value) = VT_NULL;
1223 xml_name = xmlchar_from_wchar( name );
1225 if(!xmlValidateNameValue(xml_name))
1226 hr = E_FAIL;
1227 else
1229 if ((local = xmlSplitQName2(xml_name, &prefix)))
1231 if (xmlStrEqual(prefix, BAD_CAST "xmlns"))
1233 ns = xmlSearchNs(element->doc, element, local);
1234 if (ns)
1235 xml_value = xmlStrdup(ns->href);
1237 else
1239 ns = xmlSearchNs(element->doc, element, prefix);
1240 if (ns)
1241 xml_value = xmlGetNsProp(element, local, ns->href);
1244 xmlFree(prefix);
1245 xmlFree(local);
1247 else
1248 xml_value = xmlGetNsProp(element, xml_name, NULL);
1251 heap_free(xml_name);
1252 if(xml_value)
1254 V_VT(value) = VT_BSTR;
1255 V_BSTR(value) = bstr_from_xmlChar( xml_value );
1256 xmlFree(xml_value);
1257 hr = S_OK;
1260 return hr;
1263 static HRESULT WINAPI domelem_setAttribute(
1264 IXMLDOMElement *iface,
1265 BSTR name, VARIANT value)
1267 domelem *This = impl_from_IXMLDOMElement( iface );
1268 xmlChar *xml_name, *xml_value, *local, *prefix;
1269 xmlNodePtr element;
1270 HRESULT hr = S_OK;
1272 TRACE("(%p)->(%s %s)\n", This, debugstr_w(name), debugstr_variant(&value));
1274 element = get_element( This );
1275 if ( !element )
1276 return E_FAIL;
1278 if (V_VT(&value) != VT_BSTR)
1280 VARIANT var;
1282 VariantInit(&var);
1283 hr = VariantChangeType(&var, &value, 0, VT_BSTR);
1284 if (hr != S_OK)
1286 FIXME("VariantChangeType failed\n");
1287 return hr;
1290 xml_value = xmlchar_from_wchar(V_BSTR(&var));
1291 VariantClear(&var);
1293 else
1294 xml_value = xmlchar_from_wchar(V_BSTR(&value));
1296 xml_name = xmlchar_from_wchar( name );
1298 if ((local = xmlSplitQName2(xml_name, &prefix)))
1300 static const xmlChar* xmlnsA = (const xmlChar*)"xmlns";
1301 xmlNsPtr ns = NULL;
1303 /* it's not allowed to modify existing namespace definition */
1304 if (xmlStrEqual(prefix, xmlnsA))
1305 ns = xmlSearchNs(element->doc, element, local);
1307 xmlFree(prefix);
1308 xmlFree(local);
1310 if (ns)
1312 int cmp = xmlStrEqual(ns->href, xml_value);
1313 heap_free(xml_value);
1314 heap_free(xml_name);
1315 return cmp ? S_OK : E_INVALIDARG;
1319 if (!xmlSetNsProp(element, NULL, xml_name, xml_value))
1320 hr = E_FAIL;
1322 heap_free(xml_value);
1323 heap_free(xml_name);
1325 return hr;
1328 static HRESULT WINAPI domelem_removeAttribute(
1329 IXMLDOMElement *iface,
1330 BSTR p)
1332 domelem *This = impl_from_IXMLDOMElement( iface );
1333 IXMLDOMNamedNodeMap *attr;
1334 HRESULT hr;
1336 TRACE("(%p)->(%s)\n", This, debugstr_w(p));
1338 hr = IXMLDOMElement_get_attributes(iface, &attr);
1339 if (hr != S_OK) return hr;
1341 hr = IXMLDOMNamedNodeMap_removeNamedItem(attr, p, NULL);
1342 IXMLDOMNamedNodeMap_Release(attr);
1344 return hr;
1347 static HRESULT WINAPI domelem_getAttributeNode(
1348 IXMLDOMElement *iface,
1349 BSTR p, IXMLDOMAttribute** attributeNode )
1351 domelem *This = impl_from_IXMLDOMElement( iface );
1352 xmlChar *local, *prefix, *nameA;
1353 HRESULT hr = S_FALSE;
1354 xmlNodePtr element;
1355 xmlAttrPtr attr;
1357 TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), attributeNode);
1359 element = get_element( This );
1360 if (!element) return E_FAIL;
1362 if (attributeNode) *attributeNode = NULL;
1364 nameA = xmlchar_from_wchar(p);
1365 if (!xmlValidateNameValue(nameA))
1367 heap_free(nameA);
1368 return E_FAIL;
1371 if (!attributeNode)
1373 heap_free(nameA);
1374 return S_FALSE;
1377 *attributeNode = NULL;
1379 local = xmlSplitQName2(nameA, &prefix);
1381 if (local)
1383 /* try to get namespace for supplied qualified name */
1384 xmlNsPtr ns = xmlSearchNs(element->doc, element, prefix);
1385 xmlFree(prefix);
1387 attr = xmlHasNsProp(element, local, ns ? ns->href : NULL);
1388 xmlFree(local);
1390 else
1392 attr = xmlHasProp(element, nameA);
1393 /* attribute has attached namespace and we requested non-qualified
1394 name - it's a failure case */
1395 if (attr && attr->ns) attr = NULL;
1398 heap_free(nameA);
1400 if (attr)
1402 IUnknown *unk = create_attribute((xmlNodePtr)attr, FALSE);
1403 hr = IUnknown_QueryInterface(unk, &IID_IXMLDOMAttribute, (void**)attributeNode);
1404 IUnknown_Release(unk);
1407 return hr;
1410 static HRESULT WINAPI domelem_setAttributeNode(
1411 IXMLDOMElement *iface,
1412 IXMLDOMAttribute* attribute,
1413 IXMLDOMAttribute** old)
1415 domelem *This = impl_from_IXMLDOMElement( iface );
1416 static const WCHAR xmlnsW[] = {'x','m','l','n','s',0};
1417 xmlChar *name, *value;
1418 BSTR nameW, prefix;
1419 xmlnode *attr_node;
1420 xmlAttrPtr attr;
1421 VARIANT valueW;
1422 HRESULT hr;
1424 FIXME("(%p)->(%p %p): semi-stub\n", This, attribute, old);
1426 if (!attribute) return E_INVALIDARG;
1428 attr_node = get_node_obj((IXMLDOMNode*)attribute);
1429 if (!attr_node) return E_FAIL;
1431 if (attr_node->parent)
1433 WARN("attempt to add already used attribute\n");
1434 return E_FAIL;
1437 hr = IXMLDOMAttribute_get_nodeName(attribute, &nameW);
1438 if (hr != S_OK) return hr;
1440 /* adding xmlns attribute doesn't change a tree or existing namespace definition */
1441 if (!strcmpW(nameW, xmlnsW))
1443 SysFreeString(nameW);
1444 return DISP_E_UNKNOWNNAME;
1447 hr = IXMLDOMAttribute_get_nodeValue(attribute, &valueW);
1448 if (hr != S_OK)
1450 SysFreeString(nameW);
1451 return hr;
1454 if (old) *old = NULL;
1456 TRACE("attribute: %s=%s\n", debugstr_w(nameW), debugstr_w(V_BSTR(&valueW)));
1458 hr = IXMLDOMAttribute_get_prefix(attribute, &prefix);
1459 if (hr == S_OK)
1461 FIXME("namespaces not supported: %s\n", debugstr_w(prefix));
1462 SysFreeString(prefix);
1465 name = xmlchar_from_wchar(nameW);
1466 value = xmlchar_from_wchar(V_BSTR(&valueW));
1468 if (!name || !value)
1470 SysFreeString(nameW);
1471 VariantClear(&valueW);
1472 heap_free(name);
1473 heap_free(value);
1474 return E_OUTOFMEMORY;
1477 attr = xmlSetNsProp(get_element(This), NULL, name, value);
1478 if (attr)
1479 attr_node->parent = (IXMLDOMNode*)iface;
1481 SysFreeString(nameW);
1482 VariantClear(&valueW);
1483 heap_free(name);
1484 heap_free(value);
1486 return attr ? S_OK : E_FAIL;
1489 static HRESULT WINAPI domelem_removeAttributeNode(
1490 IXMLDOMElement *iface,
1491 IXMLDOMAttribute* domAttribute,
1492 IXMLDOMAttribute** attributeNode)
1494 domelem *This = impl_from_IXMLDOMElement( iface );
1495 FIXME("(%p)->(%p %p)\n", This, domAttribute, attributeNode);
1496 return E_NOTIMPL;
1499 static HRESULT WINAPI domelem_getElementsByTagName(
1500 IXMLDOMElement *iface,
1501 BSTR tagName, IXMLDOMNodeList** resultList)
1503 domelem *This = impl_from_IXMLDOMElement( iface );
1504 xmlChar *query;
1505 HRESULT hr;
1506 BOOL XPath;
1508 TRACE("(%p)->(%s, %p)\n", This, debugstr_w(tagName), resultList);
1510 if (!tagName || !resultList) return E_INVALIDARG;
1512 XPath = is_xpathmode(get_element(This)->doc);
1513 set_xpathmode(get_element(This)->doc, TRUE);
1514 query = tagName_to_XPath(tagName);
1515 hr = create_selection(get_element(This), query, resultList);
1516 xmlFree(query);
1517 set_xpathmode(get_element(This)->doc, XPath);
1519 return hr;
1522 static HRESULT WINAPI domelem_normalize(
1523 IXMLDOMElement *iface )
1525 domelem *This = impl_from_IXMLDOMElement( iface );
1526 FIXME("%p\n", This);
1527 return E_NOTIMPL;
1530 static const struct IXMLDOMElementVtbl domelem_vtbl =
1532 domelem_QueryInterface,
1533 domelem_AddRef,
1534 domelem_Release,
1535 domelem_GetTypeInfoCount,
1536 domelem_GetTypeInfo,
1537 domelem_GetIDsOfNames,
1538 domelem_Invoke,
1539 domelem_get_nodeName,
1540 domelem_get_nodeValue,
1541 domelem_put_nodeValue,
1542 domelem_get_nodeType,
1543 domelem_get_parentNode,
1544 domelem_get_childNodes,
1545 domelem_get_firstChild,
1546 domelem_get_lastChild,
1547 domelem_get_previousSibling,
1548 domelem_get_nextSibling,
1549 domelem_get_attributes,
1550 domelem_insertBefore,
1551 domelem_replaceChild,
1552 domelem_removeChild,
1553 domelem_appendChild,
1554 domelem_hasChildNodes,
1555 domelem_get_ownerDocument,
1556 domelem_cloneNode,
1557 domelem_get_nodeTypeString,
1558 domelem_get_text,
1559 domelem_put_text,
1560 domelem_get_specified,
1561 domelem_get_definition,
1562 domelem_get_nodeTypedValue,
1563 domelem_put_nodeTypedValue,
1564 domelem_get_dataType,
1565 domelem_put_dataType,
1566 domelem_get_xml,
1567 domelem_transformNode,
1568 domelem_selectNodes,
1569 domelem_selectSingleNode,
1570 domelem_get_parsed,
1571 domelem_get_namespaceURI,
1572 domelem_get_prefix,
1573 domelem_get_baseName,
1574 domelem_transformNodeToObject,
1575 domelem_get_tagName,
1576 domelem_getAttribute,
1577 domelem_setAttribute,
1578 domelem_removeAttribute,
1579 domelem_getAttributeNode,
1580 domelem_setAttributeNode,
1581 domelem_removeAttributeNode,
1582 domelem_getElementsByTagName,
1583 domelem_normalize,
1586 static HRESULT domelem_get_qualified_item(const xmlNodePtr node, BSTR name, BSTR uri,
1587 IXMLDOMNode **item)
1589 xmlAttrPtr attr;
1590 xmlChar *nameA;
1591 xmlChar *href;
1593 TRACE("(%p)->(%s %s %p)\n", node, debugstr_w(name), debugstr_w(uri), item);
1595 if (!name || !item) return E_INVALIDARG;
1597 if (uri && *uri)
1599 href = xmlchar_from_wchar(uri);
1600 if (!href) return E_OUTOFMEMORY;
1602 else
1603 href = NULL;
1605 nameA = xmlchar_from_wchar(name);
1606 if (!nameA)
1608 heap_free(href);
1609 return E_OUTOFMEMORY;
1612 attr = xmlHasNsProp(node, nameA, href);
1614 heap_free(nameA);
1615 heap_free(href);
1617 if (!attr)
1619 *item = NULL;
1620 return S_FALSE;
1623 *item = create_node((xmlNodePtr)attr);
1625 return S_OK;
1628 static HRESULT domelem_get_named_item(const xmlNodePtr node, BSTR name, IXMLDOMNode **item)
1630 xmlChar *nameA, *local, *prefix;
1631 BSTR uriW, localW;
1632 xmlNsPtr ns;
1633 HRESULT hr;
1635 TRACE("(%p)->(%s %p)\n", node, debugstr_w(name), item );
1637 nameA = xmlchar_from_wchar(name);
1638 local = xmlSplitQName2(nameA, &prefix);
1639 heap_free(nameA);
1641 if (!local)
1642 return domelem_get_qualified_item(node, name, NULL, item);
1644 /* try to get namespace uri for supplied qualified name */
1645 ns = xmlSearchNs(node->doc, node, prefix);
1647 xmlFree(prefix);
1649 if (!ns)
1651 xmlFree(local);
1652 if (item) *item = NULL;
1653 return item ? S_FALSE : E_INVALIDARG;
1656 uriW = bstr_from_xmlChar(ns->href);
1657 localW = bstr_from_xmlChar(local);
1658 xmlFree(local);
1660 TRACE("got qualified node %s, uri=%s\n", debugstr_w(localW), debugstr_w(uriW));
1662 hr = domelem_get_qualified_item(node, localW, uriW, item);
1664 SysFreeString(localW);
1665 SysFreeString(uriW);
1667 return hr;
1670 static HRESULT domelem_set_named_item(xmlNodePtr node, IXMLDOMNode *newItem, IXMLDOMNode **namedItem)
1672 xmlNodePtr nodeNew;
1673 xmlnode *ThisNew;
1675 TRACE("(%p)->(%p %p)\n", node, newItem, namedItem );
1677 if(!newItem)
1678 return E_INVALIDARG;
1680 if(namedItem) *namedItem = NULL;
1682 /* Must be an Attribute */
1683 ThisNew = get_node_obj( newItem );
1684 if(!ThisNew) return E_FAIL;
1686 if(ThisNew->node->type != XML_ATTRIBUTE_NODE)
1687 return E_FAIL;
1689 if(!ThisNew->node->parent)
1690 if(xmldoc_remove_orphan(ThisNew->node->doc, ThisNew->node) != S_OK)
1691 WARN("%p is not an orphan of %p\n", ThisNew->node, ThisNew->node->doc);
1693 nodeNew = xmlAddChild(node, ThisNew->node);
1695 if(namedItem)
1696 *namedItem = create_node( nodeNew );
1697 return S_OK;
1700 static HRESULT domelem_remove_qualified_item(xmlNodePtr node, BSTR name, BSTR uri, IXMLDOMNode **item)
1702 xmlChar *nameA, *href;
1703 xmlAttrPtr attr;
1705 TRACE("(%p)->(%s %s %p)\n", node, debugstr_w(name), debugstr_w(uri), item);
1707 if (!name) return E_INVALIDARG;
1709 if (uri && *uri)
1711 href = xmlchar_from_wchar(uri);
1712 if (!href) return E_OUTOFMEMORY;
1714 else
1715 href = NULL;
1717 nameA = xmlchar_from_wchar(name);
1718 if (!nameA)
1720 heap_free(href);
1721 return E_OUTOFMEMORY;
1724 attr = xmlHasNsProp(node, nameA, href);
1726 heap_free(nameA);
1727 heap_free(href);
1729 if (!attr)
1731 if (item) *item = NULL;
1732 return S_FALSE;
1735 if (item)
1737 xmlUnlinkNode( (xmlNodePtr) attr );
1738 xmldoc_add_orphan( attr->doc, (xmlNodePtr) attr );
1739 *item = create_node( (xmlNodePtr) attr );
1741 else
1743 if (xmlRemoveProp(attr) == -1)
1744 ERR("xmlRemoveProp failed\n");
1747 return S_OK;
1750 static HRESULT domelem_remove_named_item(xmlNodePtr node, BSTR name, IXMLDOMNode **item)
1752 xmlChar *nameA, *local, *prefix;
1753 BSTR uriW, localW;
1754 xmlNsPtr ns;
1755 HRESULT hr;
1757 TRACE("(%p)->(%s %p)\n", node, debugstr_w(name), item);
1759 nameA = xmlchar_from_wchar(name);
1760 local = xmlSplitQName2(nameA, &prefix);
1761 heap_free(nameA);
1763 if (!local)
1764 return domelem_remove_qualified_item(node, name, NULL, item);
1766 ns = xmlSearchNs(node->doc, node, prefix);
1768 xmlFree(prefix);
1770 if (!ns)
1772 xmlFree(local);
1773 if (item) *item = NULL;
1774 return item ? S_FALSE : E_INVALIDARG;
1777 uriW = bstr_from_xmlChar(ns->href);
1778 localW = bstr_from_xmlChar(local);
1779 xmlFree(local);
1781 TRACE("removing qualified node %s, uri=%s\n", debugstr_w(localW), debugstr_w(uriW));
1783 hr = domelem_remove_qualified_item(node, localW, uriW, item);
1785 SysFreeString(localW);
1786 SysFreeString(uriW);
1788 return hr;
1791 static HRESULT domelem_get_item(const xmlNodePtr node, LONG index, IXMLDOMNode **item)
1793 xmlNsPtr ns, xmlns;
1794 xmlAttrPtr curr;
1795 LONG attrIndex;
1796 IUnknown *unk;
1797 HRESULT hr;
1799 TRACE("(%p)->(%d %p)\n", node, index, item);
1801 *item = NULL;
1803 if (index < 0)
1804 return S_FALSE;
1806 attrIndex = 0;
1807 curr = node->properties;
1808 if (curr) {
1809 for (; attrIndex < index && curr->next != NULL; attrIndex++)
1810 curr = curr->next;
1812 if (attrIndex == index) {
1813 *item = create_node( (xmlNodePtr) curr );
1814 return S_OK;
1818 if (!node->nsDef)
1819 return S_FALSE;
1821 attrIndex++;
1822 ns = node->nsDef;
1823 for (; attrIndex < index && ns->next != NULL; attrIndex++)
1824 ns = ns->next;
1826 if (attrIndex < index)
1827 return S_FALSE;
1829 xmlns = xmlNewNs(NULL, BAD_CAST "http://www.w3.org/2000/xmlns/", BAD_CAST "xmlns");
1830 if (!xmlns)
1831 return E_OUTOFMEMORY;
1833 curr = xmlNewNsProp(NULL, xmlns, ns->prefix, ns->href);
1834 if (!curr) {
1835 xmlFreeNs(xmlns);
1836 return E_OUTOFMEMORY;
1838 curr->doc = node->doc;
1840 unk = create_attribute((xmlNodePtr)curr, TRUE);
1841 if (!unk) {
1842 xmlFreeNs(xmlns);
1843 xmlFreeProp(curr);
1844 return E_OUTOFMEMORY;
1847 hr = IUnknown_QueryInterface(unk, &IID_IXMLDOMNode, (void**)item);
1848 IUnknown_Release(unk);
1850 return hr;
1853 static HRESULT domelem_get_length(const xmlNodePtr node, LONG *length)
1855 xmlAttrPtr curr;
1856 LONG attrCount;
1857 xmlNsPtr ns;
1859 TRACE("(%p)->(%p)\n", node, length);
1861 if( !length )
1862 return E_INVALIDARG;
1864 attrCount = 0;
1865 curr = node->properties;
1866 while (curr) {
1867 attrCount++;
1868 curr = curr->next;
1871 ns = node->nsDef;
1872 while (ns) {
1873 attrCount++;
1874 ns = ns->next;
1876 *length = attrCount;
1878 return S_OK;
1881 static HRESULT domelem_next_node(const xmlNodePtr node, LONG *iter, IXMLDOMNode **nextNode)
1883 xmlAttrPtr curr;
1884 LONG i;
1886 TRACE("(%p)->(%d: %p)\n", node, *iter, nextNode);
1888 *nextNode = NULL;
1890 curr = node->properties;
1892 for (i = 0; i < *iter; i++) {
1893 if (curr->next == NULL)
1894 return S_FALSE;
1895 else
1896 curr = curr->next;
1899 (*iter)++;
1900 *nextNode = create_node((xmlNodePtr)curr);
1902 return S_OK;
1905 static const struct nodemap_funcs domelem_attr_map = {
1906 domelem_get_named_item,
1907 domelem_set_named_item,
1908 domelem_remove_named_item,
1909 domelem_get_item,
1910 domelem_get_length,
1911 domelem_get_qualified_item,
1912 domelem_remove_qualified_item,
1913 domelem_next_node
1916 static const tid_t domelem_iface_tids[] = {
1917 IXMLDOMElement_tid,
1921 static dispex_static_data_t domelem_dispex = {
1922 NULL,
1923 IXMLDOMElement_tid,
1924 NULL,
1925 domelem_iface_tids
1928 IUnknown* create_element( xmlNodePtr element )
1930 domelem *This;
1932 This = heap_alloc( sizeof *This );
1933 if ( !This )
1934 return NULL;
1936 This->IXMLDOMElement_iface.lpVtbl = &domelem_vtbl;
1937 This->ref = 1;
1939 init_xmlnode(&This->node, element, (IXMLDOMNode*)&This->IXMLDOMElement_iface, &domelem_dispex);
1941 return (IUnknown*)&This->IXMLDOMElement_iface;
1944 #endif