kernel32: FindFirstChangeNotification needs a static IO_STATUS_BLOCK.
[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     FIXME("\n");
660     return E_NOTIMPL;
661 }
662
663 static DOMNodeType get_node_type(VARIANT Type)
664 {
665     if(V_VT(&Type) == VT_I4)
666         return V_I4(&Type);
667
668     FIXME("Unsupported variant type %x\n", V_VT(&Type));
669     return 0;
670 }
671
672 static HRESULT WINAPI domdoc_createNode(
673     IXMLDOMDocument *iface,
674     VARIANT Type,
675     BSTR name,
676     BSTR namespaceURI,
677     IXMLDOMNode** node )
678 {
679     domdoc *This = impl_from_IXMLDOMDocument( iface );
680     DOMNodeType node_type;
681     xmlNodePtr xmlnode = NULL;
682     xmlChar *xml_name;
683     xmlDocPtr xmldoc;
684
685     TRACE("(%p)->(type,%s,%s,%p)\n", This, debugstr_w(name), debugstr_w(namespaceURI), node);
686
687     node_type = get_node_type(Type);
688     TRACE("node_type %d\n", node_type);
689
690     xml_name = xmlChar_from_wchar((WCHAR*)name);
691
692     if(!get_doc(This))
693     {
694         xmldoc = xmlNewDoc(NULL);
695         xmldoc->_private = 0;
696         attach_xmlnode(This->node, (xmlNodePtr) xmldoc);
697     }
698
699     switch(node_type)
700     {
701     case NODE_ELEMENT:
702         xmlnode = xmlNewDocNode(get_doc(This), NULL, xml_name, NULL);
703         *node = create_node(xmlnode);
704         TRACE("created %p\n", xmlnode);
705         break;
706
707     default:
708         FIXME("unhandled node type %d\n", node_type);
709         break;
710     }
711
712     HeapFree(GetProcessHeap(), 0, xml_name);
713
714     if(xmlnode && *node)
715         return S_OK;
716
717     return E_FAIL;
718 }
719
720 static HRESULT WINAPI domdoc_nodeFromID(
721     IXMLDOMDocument *iface,
722     BSTR idString,
723     IXMLDOMNode** node )
724 {
725     FIXME("\n");
726     return E_NOTIMPL;
727 }
728
729 static xmlDocPtr doparse( char *ptr, int len )
730 {
731 #ifdef HAVE_XMLREADMEMORY
732     /*
733      * use xmlReadMemory if possible so we can suppress
734      * writing errors to stderr
735      */
736     return xmlReadMemory( ptr, len, NULL, NULL,
737                           XML_PARSE_NOERROR | XML_PARSE_NOWARNING );
738 #else
739     return xmlParseMemory( ptr, len );
740 #endif
741 }
742
743 static xmlDocPtr doread( LPWSTR filename )
744 {
745     HANDLE handle, mapping;
746     DWORD len;
747     xmlDocPtr xmldoc = NULL;
748     char *ptr;
749
750     TRACE("%s\n", debugstr_w( filename ));
751
752     handle = CreateFileW( filename, GENERIC_READ, FILE_SHARE_READ,
753                          NULL, OPEN_EXISTING, 0, NULL );
754     if( handle == INVALID_HANDLE_VALUE )
755         return xmldoc;
756
757     len = GetFileSize( handle, NULL );
758     if( len != INVALID_FILE_SIZE || GetLastError() == NO_ERROR )
759     {
760         mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, 0, NULL );
761         if ( mapping )
762         {
763             ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, len );
764             if ( ptr )
765             {
766                 xmldoc = doparse( ptr, len );
767                 UnmapViewOfFile( ptr );
768             }
769             CloseHandle( mapping );
770         }
771     }
772     CloseHandle( handle );
773
774     return xmldoc;
775 }
776
777
778 static HRESULT WINAPI domdoc_load(
779     IXMLDOMDocument *iface,
780     VARIANT xmlSource,
781     VARIANT_BOOL* isSuccessful )
782 {
783     domdoc *This = impl_from_IXMLDOMDocument( iface );
784     LPWSTR filename = NULL;
785     xmlDocPtr xmldoc;
786
787     TRACE("type %d\n", V_VT(&xmlSource) );
788
789     *isSuccessful = VARIANT_FALSE;
790
791     assert( This->node );
792
793     attach_xmlnode(This->node, NULL);
794
795     switch( V_VT(&xmlSource) )
796     {
797     case VT_BSTR:
798         filename = V_BSTR(&xmlSource);
799     }
800
801     if ( !filename )
802         return S_FALSE;
803
804     xmldoc = doread( filename );
805     if ( !xmldoc ) return S_FALSE;
806
807     xmldoc->_private = 0;
808     attach_xmlnode(This->node, (xmlNodePtr) xmldoc);
809
810     *isSuccessful = VARIANT_TRUE;
811     return S_OK;
812 }
813
814
815 static HRESULT WINAPI domdoc_get_readyState(
816     IXMLDOMDocument *iface,
817     long* value )
818 {
819     FIXME("\n");
820     return E_NOTIMPL;
821 }
822
823
824 static HRESULT WINAPI domdoc_get_parseError(
825     IXMLDOMDocument *iface,
826     IXMLDOMParseError** errorObj )
827 {
828     FIXME("(%p)->(%p): creating a dummy parseError\n", iface, errorObj);
829     *errorObj = create_parseError(0, NULL, NULL, NULL, 0, 0, 0);
830     if(!*errorObj) return E_OUTOFMEMORY;
831     return S_OK;
832 }
833
834
835 static HRESULT WINAPI domdoc_get_url(
836     IXMLDOMDocument *iface,
837     BSTR* urlString )
838 {
839     FIXME("\n");
840     return E_NOTIMPL;
841 }
842
843
844 static HRESULT WINAPI domdoc_get_async(
845     IXMLDOMDocument *iface,
846     VARIANT_BOOL* isAsync )
847 {
848     domdoc *This = impl_from_IXMLDOMDocument( iface );
849
850     TRACE("%p <- %d\n", isAsync, This->async);
851     *isAsync = This->async;
852     return S_OK;
853 }
854
855
856 static HRESULT WINAPI domdoc_put_async(
857     IXMLDOMDocument *iface,
858     VARIANT_BOOL isAsync )
859 {
860     domdoc *This = impl_from_IXMLDOMDocument( iface );
861
862     TRACE("%d\n", isAsync);
863     This->async = isAsync;
864     return S_OK;
865 }
866
867
868 static HRESULT WINAPI domdoc_abort(
869     IXMLDOMDocument *iface )
870 {
871     FIXME("\n");
872     return E_NOTIMPL;
873 }
874
875
876 BOOL bstr_to_utf8( BSTR bstr, char **pstr, int *plen )
877 {
878     UINT len, blen = SysStringLen( bstr );
879     LPSTR str;
880
881     len = WideCharToMultiByte( CP_UTF8, 0, bstr, blen, NULL, 0, NULL, NULL );
882     str = HeapAlloc( GetProcessHeap(), 0, len );
883     if ( !str )
884         return FALSE;
885     WideCharToMultiByte( CP_UTF8, 0, bstr, blen, str, len, NULL, NULL );
886     *plen = len;
887     *pstr = str;
888     return TRUE;
889 }
890
891 static HRESULT WINAPI domdoc_loadXML(
892     IXMLDOMDocument *iface,
893     BSTR bstrXML,
894     VARIANT_BOOL* isSuccessful )
895 {
896     domdoc *This = impl_from_IXMLDOMDocument( iface );
897     xmlDocPtr xmldoc;
898     char *str;
899     int len;
900
901     TRACE("%p %s %p\n", This, debugstr_w( bstrXML ), isSuccessful );
902
903     assert ( This->node );
904
905     attach_xmlnode( This->node, NULL );
906
907     if ( !isSuccessful )
908         return S_FALSE;
909
910     *isSuccessful = VARIANT_FALSE;
911
912     if ( !bstrXML )
913         return S_FALSE;
914
915     if ( !bstr_to_utf8( bstrXML, &str, &len ) )
916         return S_FALSE;
917
918     xmldoc = doparse( str, len );
919     HeapFree( GetProcessHeap(), 0, str );
920     if ( !xmldoc )
921         return S_FALSE;
922
923     xmldoc->_private = 0;
924     attach_xmlnode( This->node, (xmlNodePtr) xmldoc );
925
926     *isSuccessful = VARIANT_TRUE;
927     return S_OK;
928 }
929
930
931 static HRESULT WINAPI domdoc_save(
932     IXMLDOMDocument *iface,
933     VARIANT destination )
934 {
935     domdoc *This = impl_from_IXMLDOMDocument( iface );
936     HANDLE handle;
937     xmlChar *mem;
938     int size;
939     HRESULT ret = S_OK;
940     DWORD written;
941
942     TRACE("(%p)->(var(vt %x, %s))\n", This, V_VT(&destination),
943           V_VT(&destination) == VT_BSTR ? debugstr_w(V_BSTR(&destination)) : NULL);
944
945     if(V_VT(&destination) != VT_BSTR)
946     {
947         FIXME("Unhandled vt %x\n", V_VT(&destination));
948         return S_FALSE;
949     }
950
951     handle = CreateFileW( V_BSTR(&destination), GENERIC_WRITE, 0,
952                           NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
953     if( handle == INVALID_HANDLE_VALUE )
954     {
955         WARN("failed to create file\n");
956         return S_FALSE;
957     }
958
959     xmlDocDumpMemory(get_doc(This), &mem, &size);
960     if(!WriteFile(handle, mem, (DWORD)size, &written, NULL) || written != (DWORD)size)
961     {
962         WARN("write error\n");
963         ret = S_FALSE;
964     }
965
966     xmlFree(mem);
967     CloseHandle(handle);
968     return ret;
969 }
970
971 static HRESULT WINAPI domdoc_get_validateOnParse(
972     IXMLDOMDocument *iface,
973     VARIANT_BOOL* isValidating )
974 {
975     FIXME("\n");
976     return E_NOTIMPL;
977 }
978
979
980 static HRESULT WINAPI domdoc_put_validateOnParse(
981     IXMLDOMDocument *iface,
982     VARIANT_BOOL isValidating )
983 {
984     FIXME("\n");
985     return E_NOTIMPL;
986 }
987
988
989 static HRESULT WINAPI domdoc_get_resolveExternals(
990     IXMLDOMDocument *iface,
991     VARIANT_BOOL* isResolving )
992 {
993     FIXME("\n");
994     return E_NOTIMPL;
995 }
996
997
998 static HRESULT WINAPI domdoc_put_resolveExternals(
999     IXMLDOMDocument *iface,
1000     VARIANT_BOOL isValidating )
1001 {
1002     FIXME("\n");
1003     return E_NOTIMPL;
1004 }
1005
1006
1007 static HRESULT WINAPI domdoc_get_preserveWhiteSpace(
1008     IXMLDOMDocument *iface,
1009     VARIANT_BOOL* isPreserving )
1010 {
1011     FIXME("\n");
1012     return E_NOTIMPL;
1013 }
1014
1015
1016 static HRESULT WINAPI domdoc_put_preserveWhiteSpace(
1017     IXMLDOMDocument *iface,
1018     VARIANT_BOOL isPreserving )
1019 {
1020     FIXME("\n");
1021     return E_NOTIMPL;
1022 }
1023
1024
1025 static HRESULT WINAPI domdoc_put_onReadyStateChange(
1026     IXMLDOMDocument *iface,
1027     VARIANT readyStateChangeSink )
1028 {
1029     FIXME("\n");
1030     return E_NOTIMPL;
1031 }
1032
1033
1034 static HRESULT WINAPI domdoc_put_onDataAvailable(
1035     IXMLDOMDocument *iface,
1036     VARIANT onDataAvailableSink )
1037 {
1038     FIXME("\n");
1039     return E_NOTIMPL;
1040 }
1041
1042 static HRESULT WINAPI domdoc_put_onTransformNode(
1043     IXMLDOMDocument *iface,
1044     VARIANT onTransformNodeSink )
1045 {
1046     FIXME("\n");
1047     return E_NOTIMPL;
1048 }
1049
1050 const struct IXMLDOMDocumentVtbl domdoc_vtbl =
1051 {
1052     domdoc_QueryInterface,
1053     domdoc_AddRef,
1054     domdoc_Release,
1055     domdoc_GetTypeInfoCount,
1056     domdoc_GetTypeInfo,
1057     domdoc_GetIDsOfNames,
1058     domdoc_Invoke,
1059     domdoc_get_nodeName,
1060     domdoc_get_nodeValue,
1061     domdoc_put_nodeValue,
1062     domdoc_get_nodeType,
1063     domdoc_get_parentNode,
1064     domdoc_get_childNodes,
1065     domdoc_get_firstChild,
1066     domdoc_get_lastChild,
1067     domdoc_get_previousSibling,
1068     domdoc_get_nextSibling,
1069     domdoc_get_attributes,
1070     domdoc_insertBefore,
1071     domdoc_replaceChild,
1072     domdoc_removeChild,
1073     domdoc_appendChild,
1074     domdoc_hasChildNodes,
1075     domdoc_get_ownerDocument,
1076     domdoc_cloneNode,
1077     domdoc_get_nodeTypeString,
1078     domdoc_get_text,
1079     domdoc_put_text,
1080     domdoc_get_specified,
1081     domdoc_get_definition,
1082     domdoc_get_nodeTypedValue,
1083     domdoc_put_nodeTypedValue,
1084     domdoc_get_dataType,
1085     domdoc_put_dataType,
1086     domdoc_get_xml,
1087     domdoc_transformNode,
1088     domdoc_selectNodes,
1089     domdoc_selectSingleNode,
1090     domdoc_get_parsed,
1091     domdoc_get_namespaceURI,
1092     domdoc_get_prefix,
1093     domdoc_get_baseName,
1094     domdoc_transformNodeToObject,
1095     domdoc_get_doctype,
1096     domdoc_get_implementation,
1097     domdoc_get_documentElement,
1098     domdoc_documentElement,
1099     domdoc_createElement,
1100     domdoc_createDocumentFragment,
1101     domdoc_createTextNode,
1102     domdoc_createComment,
1103     domdoc_createCDATASection,
1104     domdoc_createProcessingInstruction,
1105     domdoc_createAttribute,
1106     domdoc_createEntityReference,
1107     domdoc_getElementsByTagName,
1108     domdoc_createNode,
1109     domdoc_nodeFromID,
1110     domdoc_load,
1111     domdoc_get_readyState,
1112     domdoc_get_parseError,
1113     domdoc_get_url,
1114     domdoc_get_async,
1115     domdoc_put_async,
1116     domdoc_abort,
1117     domdoc_loadXML,
1118     domdoc_save,
1119     domdoc_get_validateOnParse,
1120     domdoc_put_validateOnParse,
1121     domdoc_get_resolveExternals,
1122     domdoc_put_resolveExternals,
1123     domdoc_get_preserveWhiteSpace,
1124     domdoc_put_preserveWhiteSpace,
1125     domdoc_put_onReadyStateChange,
1126     domdoc_put_onDataAvailable,
1127     domdoc_put_onTransformNode,
1128 };
1129
1130 HRESULT DOMDocument_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1131 {
1132     domdoc *doc;
1133     HRESULT hr;
1134
1135     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
1136
1137     doc = HeapAlloc( GetProcessHeap(), 0, sizeof (*doc) );
1138     if( !doc )
1139         return E_OUTOFMEMORY;
1140
1141     doc->lpVtbl = &domdoc_vtbl;
1142     doc->ref = 1;
1143     doc->async = 0;
1144
1145     doc->node_unk = create_basic_node( NULL, (IUnknown*)&doc->lpVtbl );
1146     if(!doc->node_unk)
1147     {
1148         HeapFree(GetProcessHeap(), 0, doc);
1149         return E_FAIL;
1150     }
1151
1152     hr = IUnknown_QueryInterface(doc->node_unk, &IID_IXMLDOMNode, (LPVOID*)&doc->node);
1153     if(FAILED(hr))
1154     {
1155         IUnknown_Release(doc->node_unk);
1156         HeapFree( GetProcessHeap(), 0, doc );
1157         return E_FAIL;
1158     }
1159     /* The ref on doc->node is actually looped back into this object, so release it */
1160     IXMLDOMNode_Release(doc->node);
1161
1162     *ppObj = &doc->lpVtbl;
1163
1164     TRACE("returning iface %p\n", *ppObj);
1165     return S_OK;
1166 }
1167
1168 #else
1169
1170 HRESULT DOMDocument_create(IUnknown *pUnkOuter, LPVOID *ppObj)
1171 {
1172     MESSAGE("This program tried to use a DOMDocument object, but\n"
1173             "libxml2 support was not present at compile time.\n");
1174     return E_NOTIMPL;
1175 }
1176
1177 #endif