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