mshtml: Added hack to allow pass post data to IPersistMoniker::Load.
[wine] / dlls / msxml3 / domdoc.c
1 /*
2  *    DOM Document implementation
3  *
4  * Copyright 2005 Mike McCormack
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #define COBJMACROS
22
23 #include "config.h"
24
25 #include <stdarg.h>
26 #include <assert.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winnls.h"
31 #include "ole2.h"
32 #include "msxml2.h"
33
34 #include "wine/debug.h"
35
36 #include "msxml_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
39
40 #ifdef HAVE_LIBXML2
41
42 typedef struct _domdoc
43 {
44     const struct IXMLDOMDocumentVtbl *lpVtbl;
45     LONG ref;
46     VARIANT_BOOL async;
47     IUnknown *node_unk;
48     IXMLDOMNode *node;
49 } domdoc;
50
51 LONG xmldoc_add_ref(xmlDocPtr doc)
52 {
53     LONG ref = InterlockedIncrement((LONG*)&doc->_private);
54     TRACE("%ld\n", ref);
55     return ref;
56 }
57
58 LONG xmldoc_release(xmlDocPtr doc)
59 {
60     LONG ref = InterlockedDecrement((LONG*)&doc->_private);
61     TRACE("%ld\n", ref);
62     if(ref == 0)
63     {
64         TRACE("freeing docptr %p\n", doc);
65         xmlFreeDoc(doc);
66     }
67
68     return ref;
69 }
70
71 static inline domdoc *impl_from_IXMLDOMDocument( IXMLDOMDocument *iface )
72 {
73     return (domdoc *)((char*)iface - FIELD_OFFSET(domdoc, lpVtbl));
74 }
75
76 static inline xmlDocPtr get_doc( domdoc *This )
77 {
78     return (xmlDocPtr) xmlNodePtr_from_domnode( This->node, XML_DOCUMENT_NODE );
79 }
80
81 static HRESULT WINAPI domdoc_QueryInterface( IXMLDOMDocument *iface, REFIID riid, void** ppvObject )
82 {
83     domdoc *This = impl_from_IXMLDOMDocument( iface );
84
85     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
86
87     if ( IsEqualGUID( riid, &IID_IXMLDOMDocument ) ||
88          IsEqualGUID( riid, &IID_IUnknown ) )
89     {
90         *ppvObject = iface;
91     }
92     else if ( IsEqualGUID( riid, &IID_IXMLDOMNode ) ||
93               IsEqualGUID( riid, &IID_IDispatch ) )
94     {
95         return IUnknown_QueryInterface(This->node_unk, riid, ppvObject);
96     }
97     else
98     {
99         FIXME("interface %s not implemented\n", debugstr_guid(riid));
100         return E_NOINTERFACE;
101     }
102
103     IXMLDOMDocument_AddRef( iface );
104
105     return S_OK;
106 }
107
108
109 static ULONG WINAPI domdoc_AddRef(
110      IXMLDOMDocument *iface )
111 {
112     domdoc *This = impl_from_IXMLDOMDocument( iface );
113     TRACE("%p\n", This );
114     return InterlockedIncrement( &This->ref );
115 }
116
117
118 static ULONG WINAPI domdoc_Release(
119      IXMLDOMDocument *iface )
120 {
121     domdoc *This = impl_from_IXMLDOMDocument( iface );
122     LONG ref;
123
124     TRACE("%p\n", This );
125
126     ref = InterlockedDecrement( &This->ref );
127     if ( ref == 0 )
128     {
129         IUnknown_Release( This->node_unk );
130         HeapFree( GetProcessHeap(), 0, This );
131     }
132
133     return ref;
134 }
135
136 static HRESULT WINAPI domdoc_GetTypeInfoCount( IXMLDOMDocument *iface, UINT* pctinfo )
137 {
138     FIXME("\n");
139     return E_NOTIMPL;
140 }
141
142 static HRESULT WINAPI domdoc_GetTypeInfo(
143     IXMLDOMDocument *iface,
144     UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo )
145 {
146     FIXME("\n");
147     return E_NOTIMPL;
148 }
149
150 static HRESULT WINAPI domdoc_GetIDsOfNames(
151     IXMLDOMDocument *iface,
152     REFIID riid,
153     LPOLESTR* rgszNames,
154     UINT cNames,
155     LCID lcid,
156     DISPID* rgDispId)
157 {
158     FIXME("\n");
159     return E_NOTIMPL;
160 }
161
162
163 static HRESULT WINAPI domdoc_Invoke(
164     IXMLDOMDocument *iface,
165     DISPID dispIdMember,
166     REFIID riid,
167     LCID lcid,
168     WORD wFlags,
169     DISPPARAMS* pDispParams,
170     VARIANT* pVarResult,
171     EXCEPINFO* pExcepInfo,
172     UINT* puArgErr)
173 {
174     FIXME("\n");
175     return E_NOTIMPL;
176 }
177
178
179 static HRESULT WINAPI domdoc_get_nodeName(
180     IXMLDOMDocument *iface,
181     BSTR* name )
182 {
183     domdoc *This = impl_from_IXMLDOMDocument( iface );
184     return IXMLDOMNode_get_nodeName( This->node, name );
185 }
186
187
188 static HRESULT WINAPI domdoc_get_nodeValue(
189     IXMLDOMDocument *iface,
190     VARIANT* value )
191 {
192     domdoc *This = impl_from_IXMLDOMDocument( iface );
193     return IXMLDOMNode_get_nodeValue( This->node, value );
194 }
195
196
197 static HRESULT WINAPI domdoc_put_nodeValue(
198     IXMLDOMDocument *iface,
199     VARIANT value)
200 {
201     domdoc *This = impl_from_IXMLDOMDocument( iface );
202     return IXMLDOMNode_put_nodeValue( This->node, value );
203 }
204
205
206 static HRESULT WINAPI domdoc_get_nodeType(
207     IXMLDOMDocument *iface,
208     DOMNodeType* type )
209 {
210     domdoc *This = impl_from_IXMLDOMDocument( iface );
211     return IXMLDOMNode_get_nodeType( This->node, type );
212 }
213
214
215 static HRESULT WINAPI domdoc_get_parentNode(
216     IXMLDOMDocument *iface,
217     IXMLDOMNode** parent )
218 {
219     domdoc *This = impl_from_IXMLDOMDocument( iface );
220     return IXMLDOMNode_get_parentNode( This->node, parent );
221 }
222
223
224 static HRESULT WINAPI domdoc_get_childNodes(
225     IXMLDOMDocument *iface,
226     IXMLDOMNodeList** childList )
227 {
228     domdoc *This = impl_from_IXMLDOMDocument( iface );
229     return IXMLDOMNode_get_childNodes( This->node, childList );
230 }
231
232
233 static HRESULT WINAPI domdoc_get_firstChild(
234     IXMLDOMDocument *iface,
235     IXMLDOMNode** firstChild )
236 {
237     domdoc *This = impl_from_IXMLDOMDocument( iface );
238     return IXMLDOMNode_get_firstChild( This->node, firstChild );
239 }
240
241
242 static HRESULT WINAPI domdoc_get_lastChild(
243     IXMLDOMDocument *iface,
244     IXMLDOMNode** lastChild )
245 {
246     domdoc *This = impl_from_IXMLDOMDocument( iface );
247     return IXMLDOMNode_get_lastChild( This->node, lastChild );
248 }
249
250
251 static HRESULT WINAPI domdoc_get_previousSibling(
252     IXMLDOMDocument *iface,
253     IXMLDOMNode** previousSibling )
254 {
255     domdoc *This = impl_from_IXMLDOMDocument( iface );
256     return IXMLDOMNode_get_previousSibling( This->node, previousSibling );
257 }
258
259
260 static HRESULT WINAPI domdoc_get_nextSibling(
261     IXMLDOMDocument *iface,
262     IXMLDOMNode** nextSibling )
263 {
264     domdoc *This = impl_from_IXMLDOMDocument( iface );
265     return IXMLDOMNode_get_nextSibling( This->node, nextSibling );
266 }
267
268
269 static HRESULT WINAPI domdoc_get_attributes(
270     IXMLDOMDocument *iface,
271     IXMLDOMNamedNodeMap** attributeMap )
272 {
273     domdoc *This = impl_from_IXMLDOMDocument( iface );
274     return IXMLDOMNode_get_attributes( This->node, attributeMap );
275 }
276
277
278 static HRESULT WINAPI domdoc_insertBefore(
279     IXMLDOMDocument *iface,
280     IXMLDOMNode* newChild,
281     VARIANT refChild,
282     IXMLDOMNode** outNewChild )
283 {
284     domdoc *This = impl_from_IXMLDOMDocument( iface );
285     return IXMLDOMNode_insertBefore( This->node, newChild, refChild, outNewChild );
286 }
287
288
289 static HRESULT WINAPI domdoc_replaceChild(
290     IXMLDOMDocument *iface,
291     IXMLDOMNode* newChild,
292     IXMLDOMNode* oldChild,
293     IXMLDOMNode** outOldChild)
294 {
295     domdoc *This = impl_from_IXMLDOMDocument( iface );
296     return IXMLDOMNode_replaceChild( This->node, newChild, oldChild, outOldChild );
297 }
298
299
300 static HRESULT WINAPI domdoc_removeChild(
301     IXMLDOMDocument *iface,
302     IXMLDOMNode* childNode,
303     IXMLDOMNode** oldChild)
304 {
305     domdoc *This = impl_from_IXMLDOMDocument( iface );
306     return IXMLDOMNode_removeChild( This->node, childNode, oldChild );
307 }
308
309
310 static HRESULT WINAPI domdoc_appendChild(
311     IXMLDOMDocument *iface,
312     IXMLDOMNode* newChild,
313     IXMLDOMNode** outNewChild)
314 {
315     domdoc *This = impl_from_IXMLDOMDocument( iface );
316     return IXMLDOMNode_appendChild( This->node, newChild, outNewChild );
317 }
318
319
320 static HRESULT WINAPI domdoc_hasChildNodes(
321     IXMLDOMDocument *iface,
322     VARIANT_BOOL* hasChild)
323 {
324     domdoc *This = impl_from_IXMLDOMDocument( iface );
325     return IXMLDOMNode_hasChildNodes( This->node, hasChild );
326 }
327
328
329 static HRESULT WINAPI domdoc_get_ownerDocument(
330     IXMLDOMDocument *iface,
331     IXMLDOMDocument** DOMDocument)
332 {
333     domdoc *This = impl_from_IXMLDOMDocument( iface );
334     return IXMLDOMNode_get_ownerDocument( This->node, DOMDocument );
335 }
336
337
338 static HRESULT WINAPI domdoc_cloneNode(
339     IXMLDOMDocument *iface,
340     VARIANT_BOOL deep,
341     IXMLDOMNode** cloneRoot)
342 {
343     domdoc *This = impl_from_IXMLDOMDocument( iface );
344     return IXMLDOMNode_cloneNode( This->node, deep, cloneRoot );
345 }
346
347
348 static HRESULT WINAPI domdoc_get_nodeTypeString(
349     IXMLDOMDocument *iface,
350     BSTR* nodeType )
351 {
352     domdoc *This = impl_from_IXMLDOMDocument( iface );
353     return IXMLDOMNode_get_nodeTypeString( This->node, nodeType );
354 }
355
356
357 static HRESULT WINAPI domdoc_get_text(
358     IXMLDOMDocument *iface,
359     BSTR* text )
360 {
361     domdoc *This = impl_from_IXMLDOMDocument( iface );
362     return IXMLDOMNode_get_text( This->node, text );
363 }
364
365
366 static HRESULT WINAPI domdoc_put_text(
367     IXMLDOMDocument *iface,
368     BSTR text )
369 {
370     domdoc *This = impl_from_IXMLDOMDocument( iface );
371     return IXMLDOMNode_put_text( This->node, text );
372 }
373
374
375 static HRESULT WINAPI domdoc_get_specified(
376     IXMLDOMDocument *iface,
377     VARIANT_BOOL* isSpecified )
378 {
379     domdoc *This = impl_from_IXMLDOMDocument( iface );
380     return IXMLDOMNode_get_specified( This->node, isSpecified );
381 }
382
383
384 static HRESULT WINAPI domdoc_get_definition(
385     IXMLDOMDocument *iface,
386     IXMLDOMNode** definitionNode )
387 {
388     domdoc *This = impl_from_IXMLDOMDocument( iface );
389     return IXMLDOMNode_get_definition( This->node, definitionNode );
390 }
391
392
393 static HRESULT WINAPI domdoc_get_nodeTypedValue(
394     IXMLDOMDocument *iface,
395     VARIANT* typedValue )
396 {
397     domdoc *This = impl_from_IXMLDOMDocument( iface );
398     return IXMLDOMNode_get_nodeTypedValue( This->node, typedValue );
399 }
400
401 static HRESULT WINAPI domdoc_put_nodeTypedValue(
402     IXMLDOMDocument *iface,
403     VARIANT typedValue )
404 {
405     domdoc *This = impl_from_IXMLDOMDocument( iface );
406     return IXMLDOMNode_put_nodeTypedValue( This->node, typedValue );
407 }
408
409
410 static HRESULT WINAPI domdoc_get_dataType(
411     IXMLDOMDocument *iface,
412     VARIANT* dataTypeName )
413 {
414     domdoc *This = impl_from_IXMLDOMDocument( iface );
415     return IXMLDOMNode_get_dataType( This->node, dataTypeName );
416 }
417
418
419 static HRESULT WINAPI domdoc_put_dataType(
420     IXMLDOMDocument *iface,
421     BSTR dataTypeName )
422 {
423     domdoc *This = impl_from_IXMLDOMDocument( iface );
424     return IXMLDOMNode_put_dataType( This->node, dataTypeName );
425 }
426
427
428 static HRESULT WINAPI domdoc_get_xml(
429     IXMLDOMDocument *iface,
430     BSTR* xmlString )
431 {
432     domdoc *This = impl_from_IXMLDOMDocument( iface );
433     return IXMLDOMNode_get_xml( This->node, xmlString );
434 }
435
436
437 static HRESULT WINAPI domdoc_transformNode(
438     IXMLDOMDocument *iface,
439     IXMLDOMNode* styleSheet,
440     BSTR* xmlString )
441 {
442     domdoc *This = impl_from_IXMLDOMDocument( iface );
443     return IXMLDOMNode_transformNode( This->node, styleSheet, xmlString );
444 }
445
446
447 static HRESULT WINAPI domdoc_selectNodes(
448     IXMLDOMDocument *iface,
449     BSTR queryString,
450     IXMLDOMNodeList** resultList )
451 {
452     domdoc *This = impl_from_IXMLDOMDocument( iface );
453     return IXMLDOMNode_selectNodes( This->node, queryString, resultList );
454 }
455
456
457 static HRESULT WINAPI domdoc_selectSingleNode(
458     IXMLDOMDocument *iface,
459     BSTR queryString,
460     IXMLDOMNode** resultNode )
461 {
462     domdoc *This = impl_from_IXMLDOMDocument( iface );
463     return IXMLDOMNode_selectSingleNode( This->node, queryString, resultNode );
464 }
465
466
467 static HRESULT WINAPI domdoc_get_parsed(
468     IXMLDOMDocument *iface,
469     VARIANT_BOOL* isParsed )
470 {
471     domdoc *This = impl_from_IXMLDOMDocument( iface );
472     return IXMLDOMNode_get_parsed( This->node, isParsed );
473 }
474
475
476 static HRESULT WINAPI domdoc_get_namespaceURI(
477     IXMLDOMDocument *iface,
478     BSTR* namespaceURI )
479 {
480     domdoc *This = impl_from_IXMLDOMDocument( iface );
481     return IXMLDOMNode_get_namespaceURI( This->node, namespaceURI );
482 }
483
484
485 static HRESULT WINAPI domdoc_get_prefix(
486     IXMLDOMDocument *iface,
487     BSTR* prefixString )
488 {
489     domdoc *This = impl_from_IXMLDOMDocument( iface );
490     return IXMLDOMNode_get_prefix( This->node, prefixString );
491 }
492
493
494 static HRESULT WINAPI domdoc_get_baseName(
495     IXMLDOMDocument *iface,
496     BSTR* nameString )
497 {
498     domdoc *This = impl_from_IXMLDOMDocument( iface );
499     return IXMLDOMNode_get_baseName( This->node, nameString );
500 }
501
502
503 static HRESULT WINAPI domdoc_transformNodeToObject(
504     IXMLDOMDocument *iface,
505     IXMLDOMNode* stylesheet,
506     VARIANT outputObject)
507 {
508     domdoc *This = impl_from_IXMLDOMDocument( iface );
509     return IXMLDOMNode_transformNodeToObject( This->node, stylesheet, outputObject );
510 }
511
512
513 static HRESULT WINAPI domdoc_get_doctype(
514     IXMLDOMDocument *iface,
515     IXMLDOMDocumentType** documentType )
516 {
517     FIXME("\n");
518     return E_NOTIMPL;
519 }
520
521
522 static HRESULT WINAPI domdoc_get_implementation(
523     IXMLDOMDocument *iface,
524     IXMLDOMImplementation** impl )
525 {
526     FIXME("\n");
527     return E_NOTIMPL;
528 }
529
530 static HRESULT WINAPI domdoc_get_documentElement(
531     IXMLDOMDocument *iface,
532     IXMLDOMElement** DOMElement )
533 {
534     domdoc *This = impl_from_IXMLDOMDocument( iface );
535     xmlDocPtr xmldoc = NULL;
536     xmlNodePtr root = NULL;
537     IXMLDOMNode *element_node;
538     HRESULT hr;
539
540     TRACE("%p\n", This);
541
542     *DOMElement = NULL;
543
544     if ( !This->node )
545         return S_FALSE;
546
547     xmldoc = get_doc( This );
548     if ( !xmldoc )
549         return S_FALSE;
550
551     root = xmlDocGetRootElement( xmldoc );
552     if ( !root )
553         return S_FALSE;
554
555     element_node = create_node( root );
556     if(!element_node) return S_FALSE;
557
558     hr = IXMLDOMNode_QueryInterface(element_node, &IID_IXMLDOMElement, (LPVOID*)DOMElement);
559     IXMLDOMNode_Release(element_node);
560
561     return hr;
562 }
563
564
565 static HRESULT WINAPI domdoc_documentElement(
566     IXMLDOMDocument *iface,
567     IXMLDOMElement* DOMElement )
568 {
569     FIXME("\n");
570     return E_NOTIMPL;
571 }
572
573
574 static HRESULT WINAPI domdoc_createElement(
575     IXMLDOMDocument *iface,
576     BSTR tagname,
577     IXMLDOMElement** element )
578 {
579     FIXME("\n");
580     return E_NOTIMPL;
581 }
582
583
584 static HRESULT WINAPI domdoc_createDocumentFragment(
585     IXMLDOMDocument *iface,
586     IXMLDOMDocumentFragment** docFrag )
587 {
588     FIXME("\n");
589     return E_NOTIMPL;
590 }
591
592
593 static HRESULT WINAPI domdoc_createTextNode(
594     IXMLDOMDocument *iface,
595     BSTR data,
596     IXMLDOMText** text )
597 {
598     FIXME("\n");
599     return E_NOTIMPL;
600 }
601
602
603 static HRESULT WINAPI domdoc_createComment(
604     IXMLDOMDocument *iface,
605     BSTR data,
606     IXMLDOMComment** comment )
607 {
608     FIXME("\n");
609     return E_NOTIMPL;
610 }
611
612
613 static HRESULT WINAPI domdoc_createCDATASection(
614     IXMLDOMDocument *iface,
615     BSTR data,
616     IXMLDOMCDATASection** cdata )
617 {
618     FIXME("\n");
619     return E_NOTIMPL;
620 }
621
622
623 static HRESULT WINAPI domdoc_createProcessingInstruction(
624     IXMLDOMDocument *iface,
625     BSTR target,
626     BSTR data,
627     IXMLDOMProcessingInstruction** pi )
628 {
629     FIXME("\n");
630     return E_NOTIMPL;
631 }
632
633
634 static HRESULT WINAPI domdoc_createAttribute(
635     IXMLDOMDocument *iface,
636     BSTR name,
637     IXMLDOMAttribute** attribute )
638 {
639     FIXME("\n");
640     return E_NOTIMPL;
641 }
642
643
644 static HRESULT WINAPI domdoc_createEntityReference(
645     IXMLDOMDocument *iface,
646     BSTR name,
647     IXMLDOMEntityReference** entityRef )
648 {
649     FIXME("\n");
650     return E_NOTIMPL;
651 }
652
653
654 static HRESULT WINAPI domdoc_getElementsByTagName(
655     IXMLDOMDocument *iface,
656     BSTR tagName,
657     IXMLDOMNodeList** resultList )
658 {
659     domdoc *This = impl_from_IXMLDOMDocument( iface );
660     xmlChar *name;
661     TRACE("(%p)->(%s, %p)\n", This, debugstr_w(tagName), resultList);
662
663     name = xmlChar_from_wchar((WCHAR*)tagName);
664     *resultList = create_filtered_nodelist((xmlNodePtr)get_doc(This), name, TRUE);
665     HeapFree(GetProcessHeap(), 0, name);
666
667     if(!*resultList) return S_FALSE;
668     return S_OK;
669 }
670
671 static DOMNodeType get_node_type(VARIANT Type)
672 {
673     if(V_VT(&Type) == VT_I4)
674         return V_I4(&Type);
675
676     FIXME("Unsupported variant type %x\n", V_VT(&Type));
677     return 0;
678 }
679
680 static HRESULT WINAPI domdoc_createNode(
681     IXMLDOMDocument *iface,
682     VARIANT Type,
683     BSTR name,
684     BSTR namespaceURI,
685     IXMLDOMNode** node )
686 {
687     domdoc *This = impl_from_IXMLDOMDocument( iface );
688     DOMNodeType node_type;
689     xmlNodePtr xmlnode = NULL;
690     xmlChar *xml_name;
691     xmlDocPtr xmldoc;
692
693     TRACE("(%p)->(type,%s,%s,%p)\n", This, debugstr_w(name), debugstr_w(namespaceURI), node);
694
695     node_type = get_node_type(Type);
696     TRACE("node_type %d\n", node_type);
697
698     xml_name = xmlChar_from_wchar((WCHAR*)name);
699
700     if(!get_doc(This))
701     {
702         xmldoc = xmlNewDoc(NULL);
703         xmldoc->_private = 0;
704         attach_xmlnode(This->node, (xmlNodePtr) xmldoc);
705     }
706
707     switch(node_type)
708     {
709     case NODE_ELEMENT:
710         xmlnode = xmlNewDocNode(get_doc(This), NULL, xml_name, NULL);
711         *node = create_node(xmlnode);
712         TRACE("created %p\n", xmlnode);
713         break;
714
715     default:
716         FIXME("unhandled node type %d\n", node_type);
717         break;
718     }
719
720     HeapFree(GetProcessHeap(), 0, xml_name);
721
722     if(xmlnode && *node)
723         return S_OK;
724
725     return E_FAIL;
726 }
727
728 static HRESULT WINAPI domdoc_nodeFromID(
729     IXMLDOMDocument *iface,
730     BSTR idString,
731     IXMLDOMNode** node )
732 {
733     FIXME("\n");
734     return E_NOTIMPL;
735 }
736
737 static xmlDocPtr doparse( char *ptr, int len )
738 {
739 #ifdef HAVE_XMLREADMEMORY
740     /*
741      * use xmlReadMemory if possible so we can suppress
742      * writing errors to stderr
743      */
744     return xmlReadMemory( ptr, len, NULL, NULL,
745                           XML_PARSE_NOERROR | XML_PARSE_NOWARNING );
746 #else
747     return xmlParseMemory( ptr, len );
748 #endif
749 }
750
751 static xmlDocPtr doread( LPWSTR filename )
752 {
753     HANDLE handle, mapping;
754     DWORD len;
755     xmlDocPtr xmldoc = NULL;
756     char *ptr;
757
758     TRACE("%s\n", debugstr_w( filename ));
759
760     handle = CreateFileW( filename, GENERIC_READ, FILE_SHARE_READ,
761                          NULL, OPEN_EXISTING, 0, NULL );
762     if( handle == INVALID_HANDLE_VALUE )
763         return xmldoc;
764
765     len = GetFileSize( handle, NULL );
766     if( len != INVALID_FILE_SIZE || GetLastError() == NO_ERROR )
767     {
768         mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, 0, NULL );
769         if ( mapping )
770         {
771             ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, len );
772             if ( ptr )
773             {
774                 xmldoc = doparse( ptr, len );
775                 UnmapViewOfFile( ptr );
776             }
777             CloseHandle( mapping );
778         }
779     }
780     CloseHandle( handle );
781
782     return xmldoc;
783 }
784
785
786 static HRESULT WINAPI domdoc_load(
787     IXMLDOMDocument *iface,
788     VARIANT xmlSource,
789     VARIANT_BOOL* isSuccessful )
790 {
791     domdoc *This = impl_from_IXMLDOMDocument( iface );
792     LPWSTR filename = NULL;
793     xmlDocPtr xmldoc;
794
795     TRACE("type %d\n", V_VT(&xmlSource) );
796
797     *isSuccessful = VARIANT_FALSE;
798
799     assert( This->node );
800
801     attach_xmlnode(This->node, NULL);
802
803     switch( V_VT(&xmlSource) )
804     {
805     case VT_BSTR:
806         filename = V_BSTR(&xmlSource);
807     }
808
809     if ( !filename )
810         return S_FALSE;
811
812     xmldoc = doread( filename );
813     if ( !xmldoc ) return S_FALSE;
814
815     xmldoc->_private = 0;
816     attach_xmlnode(This->node, (xmlNodePtr) xmldoc);
817
818     *isSuccessful = VARIANT_TRUE;
819     return S_OK;
820 }
821
822
823 static HRESULT WINAPI domdoc_get_readyState(
824     IXMLDOMDocument *iface,
825     long* value )
826 {
827     FIXME("\n");
828     return E_NOTIMPL;
829 }
830
831
832 static HRESULT WINAPI domdoc_get_parseError(
833     IXMLDOMDocument *iface,
834     IXMLDOMParseError** errorObj )
835 {
836     FIXME("(%p)->(%p): creating a dummy parseError\n", iface, errorObj);
837     *errorObj = create_parseError(0, NULL, NULL, NULL, 0, 0, 0);
838     if(!*errorObj) return E_OUTOFMEMORY;
839     return S_OK;
840 }
841
842
843 static HRESULT WINAPI domdoc_get_url(
844     IXMLDOMDocument *iface,
845     BSTR* urlString )
846 {
847     FIXME("\n");
848     return E_NOTIMPL;
849 }
850
851
852 static HRESULT WINAPI domdoc_get_async(
853     IXMLDOMDocument *iface,
854     VARIANT_BOOL* isAsync )
855 {
856     domdoc *This = impl_from_IXMLDOMDocument( iface );
857
858     TRACE("%p <- %d\n", isAsync, This->async);
859     *isAsync = This->async;
860     return S_OK;
861 }
862
863
864 static HRESULT WINAPI domdoc_put_async(
865     IXMLDOMDocument *iface,
866     VARIANT_BOOL isAsync )
867 {
868     domdoc *This = impl_from_IXMLDOMDocument( iface );
869
870     TRACE("%d\n", isAsync);
871     This->async = isAsync;
872     return S_OK;
873 }
874
875
876 static HRESULT WINAPI domdoc_abort(
877     IXMLDOMDocument *iface )
878 {
879     FIXME("\n");
880     return E_NOTIMPL;
881 }
882
883
884 BOOL bstr_to_utf8( BSTR bstr, char **pstr, int *plen )
885 {
886     UINT len, blen = SysStringLen( bstr );
887     LPSTR str;
888
889     len = WideCharToMultiByte( CP_UTF8, 0, bstr, blen, NULL, 0, NULL, NULL );
890     str = HeapAlloc( GetProcessHeap(), 0, len );
891     if ( !str )
892         return FALSE;
893     WideCharToMultiByte( CP_UTF8, 0, bstr, blen, str, len, NULL, NULL );
894     *plen = len;
895     *pstr = str;
896     return TRUE;
897 }
898
899 static HRESULT WINAPI domdoc_loadXML(
900     IXMLDOMDocument *iface,
901     BSTR bstrXML,
902     VARIANT_BOOL* isSuccessful )
903 {
904     domdoc *This = impl_from_IXMLDOMDocument( iface );
905     xmlDocPtr xmldoc;
906     char *str;
907     int len;
908
909     TRACE("%p %s %p\n", This, debugstr_w( bstrXML ), isSuccessful );
910
911     assert ( This->node );
912
913     attach_xmlnode( This->node, NULL );
914
915     if ( !isSuccessful )
916         return S_FALSE;
917
918     *isSuccessful = VARIANT_FALSE;
919
920     if ( !bstrXML )
921         return S_FALSE;
922
923     if ( !bstr_to_utf8( bstrXML, &str, &len ) )
924         return S_FALSE;
925
926     xmldoc = doparse( str, len );
927     HeapFree( GetProcessHeap(), 0, str );
928     if ( !xmldoc )
929         return S_FALSE;
930
931     xmldoc->_private = 0;
932     attach_xmlnode( This->node, (xmlNodePtr) xmldoc );
933
934     *isSuccessful = VARIANT_TRUE;
935     return S_OK;
936 }
937
938
939 static HRESULT WINAPI domdoc_save(
940     IXMLDOMDocument *iface,
941     VARIANT destination )
942 {
943     domdoc *This = impl_from_IXMLDOMDocument( iface );
944     HANDLE handle;
945     xmlChar *mem;
946     int size;
947     HRESULT ret = S_OK;
948     DWORD written;
949
950     TRACE("(%p)->(var(vt %x, %s))\n", This, V_VT(&destination),
951           V_VT(&destination) == VT_BSTR ? debugstr_w(V_BSTR(&destination)) : NULL);
952
953     if(V_VT(&destination) != VT_BSTR)
954     {
955         FIXME("Unhandled vt %x\n", V_VT(&destination));
956         return S_FALSE;
957     }
958
959     handle = CreateFileW( V_BSTR(&destination), GENERIC_WRITE, 0,
960                           NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
961     if( handle == INVALID_HANDLE_VALUE )
962     {
963         WARN("failed to create file\n");
964         return S_FALSE;
965     }
966
967     xmlDocDumpMemory(get_doc(This), &mem, &size);
968     if(!WriteFile(handle, mem, (DWORD)size, &written, NULL) || written != (DWORD)size)
969     {
970         WARN("write error\n");
971         ret = S_FALSE;
972     }
973
974     xmlFree(mem);
975     CloseHandle(handle);
976     return ret;
977 }
978
979 static HRESULT WINAPI domdoc_get_validateOnParse(
980     IXMLDOMDocument *iface,
981     VARIANT_BOOL* isValidating )
982 {
983     FIXME("\n");
984     return E_NOTIMPL;
985 }
986
987
988 static HRESULT WINAPI domdoc_put_validateOnParse(
989     IXMLDOMDocument *iface,
990     VARIANT_BOOL isValidating )
991 {
992     FIXME("\n");
993     return E_NOTIMPL;
994 }
995
996
997 static HRESULT WINAPI domdoc_get_resolveExternals(
998     IXMLDOMDocument *iface,
999     VARIANT_BOOL* isResolving )
1000 {
1001     FIXME("\n");
1002     return E_NOTIMPL;
1003 }
1004
1005
1006 static HRESULT WINAPI domdoc_put_resolveExternals(
1007     IXMLDOMDocument *iface,
1008     VARIANT_BOOL isValidating )
1009 {
1010     FIXME("\n");
1011     return E_NOTIMPL;
1012 }
1013
1014
1015 static HRESULT WINAPI domdoc_get_preserveWhiteSpace(
1016     IXMLDOMDocument *iface,
1017     VARIANT_BOOL* isPreserving )
1018 {
1019     FIXME("\n");
1020     return E_NOTIMPL;
1021 }
1022
1023
1024 static HRESULT WINAPI domdoc_put_preserveWhiteSpace(
1025     IXMLDOMDocument *iface,
1026     VARIANT_BOOL isPreserving )
1027 {
1028     FIXME("\n");
1029     return E_NOTIMPL;
1030 }
1031
1032
1033 static HRESULT WINAPI domdoc_put_onReadyStateChange(
1034     IXMLDOMDocument *iface,
1035     VARIANT readyStateChangeSink )
1036 {
1037     FIXME("\n");
1038     return E_NOTIMPL;
1039 }
1040
1041
1042 static HRESULT WINAPI domdoc_put_onDataAvailable(
1043     IXMLDOMDocument *iface,
1044     VARIANT onDataAvailableSink )
1045 {
1046     FIXME("\n");
1047     return E_NOTIMPL;
1048 }
1049
1050 static HRESULT WINAPI domdoc_put_onTransformNode(
1051     IXMLDOMDocument *iface,
1052     VARIANT onTransformNodeSink )
1053 {
1054     FIXME("\n");
1055     return E_NOTIMPL;
1056 }
1057
1058 const struct IXMLDOMDocumentVtbl domdoc_vtbl =
1059 {
1060     domdoc_QueryInterface,
1061     domdoc_AddRef,
1062     domdoc_Release,
1063     domdoc_GetTypeInfoCount,
1064     domdoc_GetTypeInfo,
1065     domdoc_GetIDsOfNames,
1066     domdoc_Invoke,
1067     domdoc_get_nodeName,
1068     domdoc_get_nodeValue,
1069     domdoc_put_nodeValue,
1070     domdoc_get_nodeType,
1071     domdoc_get_parentNode,
1072     domdoc_get_childNodes,
1073     domdoc_get_firstChild,
1074     domdoc_get_lastChild,
1075     domdoc_get_previousSibling,
1076     domdoc_get_nextSibling,
1077     domdoc_get_attributes,
1078     domdoc_insertBefore,
1079     domdoc_replaceChild,
1080     domdoc_removeChild,
1081     domdoc_appendChild,
1082     domdoc_hasChildNodes,
1083     domdoc_get_ownerDocument,
1084     domdoc_cloneNode,
1085     domdoc_get_nodeTypeString,
1086     domdoc_get_text,
1087     domdoc_put_text,
1088     domdoc_get_specified,
1089     domdoc_get_definition,
1090     domdoc_get_nodeTypedValue,
1091     domdoc_put_nodeTypedValue,
1092     domdoc_get_dataType,
1093     domdoc_put_dataType,
1094     domdoc_get_xml,
1095     domdoc_transformNode,
1096     domdoc_selectNodes,
1097     domdoc_selectSingleNode,
1098     domdoc_get_parsed,
1099     domdoc_get_namespaceURI,
1100     domdoc_get_prefix,
1101     domdoc_get_baseName,
1102     domdoc_transformNodeToObject,
1103     domdoc_get_doctype,
1104     domdoc_get_implementation,
1105     domdoc_get_documentElement,
1106     domdoc_documentElement,
1107     domdoc_createElement,
1108     domdoc_createDocumentFragment,
1109     domdoc_createTextNode,
1110     domdoc_createComment,
1111     domdoc_createCDATASection,
1112     domdoc_createProcessingInstruction,
1113     domdoc_createAttribute,
1114     domdoc_createEntityReference,
1115     domdoc_getElementsByTagName,
1116     domdoc_createNode,
1117     domdoc_nodeFromID,
1118     domdoc_load,
1119     domdoc_get_readyState,
1120     domdoc_get_parseError,
1121     domdoc_get_url,
1122     domdoc_get_async,
1123     domdoc_put_async,
1124     domdoc_abort,
1125     domdoc_loadXML,
1126     domdoc_save,
1127     domdoc_get_validateOnParse,
1128     domdoc_put_validateOnParse,
1129     domdoc_get_resolveExternals,
1130     domdoc_put_resolveExternals,
1131     domdoc_get_preserveWhiteSpace,
1132     domdoc_put_preserveWhiteSpace,
1133     domdoc_put_onReadyStateChange,
1134     domdoc_put_onDataAvailable,
1135     domdoc_put_onTransformNode,
1136 };
1137
1138 HRESULT DOMDocument_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1139 {
1140     domdoc *doc;
1141     HRESULT hr;
1142
1143     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
1144
1145     doc = HeapAlloc( GetProcessHeap(), 0, sizeof (*doc) );
1146     if( !doc )
1147         return E_OUTOFMEMORY;
1148
1149     doc->lpVtbl = &domdoc_vtbl;
1150     doc->ref = 1;
1151     doc->async = 0;
1152
1153     doc->node_unk = create_basic_node( NULL, (IUnknown*)&doc->lpVtbl );
1154     if(!doc->node_unk)
1155     {
1156         HeapFree(GetProcessHeap(), 0, doc);
1157         return E_FAIL;
1158     }
1159
1160     hr = IUnknown_QueryInterface(doc->node_unk, &IID_IXMLDOMNode, (LPVOID*)&doc->node);
1161     if(FAILED(hr))
1162     {
1163         IUnknown_Release(doc->node_unk);
1164         HeapFree( GetProcessHeap(), 0, doc );
1165         return E_FAIL;
1166     }
1167     /* The ref on doc->node is actually looped back into this object, so release it */
1168     IXMLDOMNode_Release(doc->node);
1169
1170     *ppObj = &doc->lpVtbl;
1171
1172     TRACE("returning iface %p\n", *ppObj);
1173     return S_OK;
1174 }
1175
1176 #else
1177
1178 HRESULT DOMDocument_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1179 {
1180     MESSAGE("This program tried to use a DOMDocument object, but\n"
1181             "libxml2 support was not present at compile time.\n");
1182     return E_NOTIMPL;
1183 }
1184
1185 #endif