msxml3: Added ISAXContentHandler_characters event.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22 #define NONAMELESSUNION
23
24 #include "config.h"
25
26 #include <stdarg.h>
27 #include <assert.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "ole2.h"
33 #include "msxml2.h"
34 #include "wininet.h"
35 #include "urlmon.h"
36 #include "winreg.h"
37 #include "shlwapi.h"
38 #include "ocidl.h"
39 #include "objsafe.h"
40
41 #include "wine/debug.h"
42
43 #include "msxml_private.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
46
47 #ifdef HAVE_LIBXML2
48
49 static const WCHAR SZ_PROPERTY_SELECTION_LANGUAGE[] = {'S','e','l','e','c','t','i','o','n','L','a','n','g','u','a','g','e',0};
50 static const WCHAR SZ_VALUE_XPATH[] = {'X','P','a','t','h',0};
51 static const WCHAR SZ_VALUE_XSLPATTERN[] = {'X','S','L','P','a','t','t','e','r','n',0};
52
53 typedef struct bsc_t bsc_t;
54
55 typedef struct _domdoc
56 {
57     const struct IXMLDOMDocument2Vtbl *lpVtbl;
58     const struct IPersistStreamVtbl   *lpvtblIPersistStream;
59     const struct IObjectWithSiteVtbl  *lpvtblIObjectWithSite;
60     const struct IObjectSafetyVtbl    *lpvtblIObjectSafety;
61     LONG ref;
62     VARIANT_BOOL async;
63     VARIANT_BOOL validating;
64     VARIANT_BOOL resolving;
65     VARIANT_BOOL preserving;
66     BOOL bUseXPath;
67     IUnknown *node_unk;
68     IXMLDOMNode *node;
69     IXMLDOMSchemaCollection *schema;
70     bsc_t *bsc;
71     HRESULT error;
72
73     /* IPersistStream */
74     IStream *stream;
75
76     /* IObjectWithSite*/
77     IUnknown *site;
78
79     /* IObjectSafety */
80     DWORD safeopt;
81 } domdoc;
82
83 struct bsc_t {
84     const struct IBindStatusCallbackVtbl *lpVtbl;
85
86     LONG ref;
87
88     domdoc *doc;
89     IBinding *binding;
90     IStream *memstream;
91 };
92
93 static inline bsc_t *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
94 {
95     return (bsc_t *)((char*)iface - FIELD_OFFSET(bsc_t, lpVtbl));
96 }
97
98 static xmlDocPtr doparse( char *ptr, int len )
99 {
100 #ifdef HAVE_XMLREADMEMORY
101     /*
102      * use xmlReadMemory if possible so we can suppress
103      * writing errors to stderr
104      */
105     return xmlReadMemory( ptr, len, NULL, NULL,
106                           XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NOBLANKS );
107 #else
108     return xmlParseMemory( ptr, len );
109 #endif
110 }
111
112 static HRESULT WINAPI bsc_QueryInterface(
113     IBindStatusCallback *iface,
114     REFIID riid,
115     LPVOID *ppobj )
116 {
117     if (IsEqualGUID(riid, &IID_IUnknown) ||
118         IsEqualGUID(riid, &IID_IBindStatusCallback))
119     {
120         IBindStatusCallback_AddRef( iface );
121         *ppobj = iface;
122         return S_OK;
123     }
124
125     FIXME("interface %s not implemented\n", debugstr_guid(riid));
126     return E_NOINTERFACE;
127 }
128
129 static ULONG WINAPI bsc_AddRef(
130     IBindStatusCallback *iface )
131 {
132     bsc_t *This = impl_from_IBindStatusCallback(iface);
133     LONG ref = InterlockedIncrement(&This->ref);
134
135     TRACE("(%p) ref=%d\n", This, ref);
136
137     return ref;
138 }
139
140 static ULONG WINAPI bsc_Release(
141     IBindStatusCallback *iface )
142 {
143     bsc_t *This = impl_from_IBindStatusCallback(iface);
144     LONG ref = InterlockedDecrement(&This->ref);
145
146     TRACE("(%p) ref=%d\n", This, ref);
147
148     if(!ref) {
149         if(This->binding)
150             IBinding_Release(This->binding);
151         if(This->doc && This->doc->bsc == This)
152             This->doc->bsc = NULL;
153         if(This->memstream)
154             IStream_Release(This->memstream);
155         HeapFree(GetProcessHeap(), 0, This);
156     }
157
158     return ref;
159 }
160
161 static HRESULT WINAPI bsc_OnStartBinding(
162         IBindStatusCallback* iface,
163         DWORD dwReserved,
164         IBinding* pib)
165 {
166     bsc_t *This = impl_from_IBindStatusCallback(iface);
167     HRESULT hr;
168
169     TRACE("(%p)->(%x %p)\n", This, dwReserved, pib);
170
171     This->binding = pib;
172     IBindStatusCallback_AddRef(pib);
173
174     hr = CreateStreamOnHGlobal(NULL, TRUE, &This->memstream);
175     if(FAILED(hr))
176         return hr;
177
178     return S_OK;
179 }
180
181 static HRESULT WINAPI bsc_GetPriority(
182         IBindStatusCallback* iface,
183         LONG* pnPriority)
184 {
185     return S_OK;
186 }
187
188 static HRESULT WINAPI bsc_OnLowResource(
189         IBindStatusCallback* iface,
190         DWORD reserved)
191 {
192     return S_OK;
193 }
194
195 static HRESULT WINAPI bsc_OnProgress(
196         IBindStatusCallback* iface,
197         ULONG ulProgress,
198         ULONG ulProgressMax,
199         ULONG ulStatusCode,
200         LPCWSTR szStatusText)
201 {
202     return S_OK;
203 }
204
205 static HRESULT WINAPI bsc_OnStopBinding(
206         IBindStatusCallback* iface,
207         HRESULT hresult,
208         LPCWSTR szError)
209 {
210     bsc_t *This = impl_from_IBindStatusCallback(iface);
211     HRESULT hr;
212
213     TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
214
215     if(This->binding) {
216         IBinding_Release(This->binding);
217         This->binding = NULL;
218     }
219
220     if(This->doc && SUCCEEDED(hresult)) {
221         HGLOBAL hglobal;
222         hr = GetHGlobalFromStream(This->memstream, &hglobal);
223         if(SUCCEEDED(hr))
224         {
225             DWORD len = GlobalSize(hglobal);
226             char *ptr = GlobalLock(hglobal);
227             xmlDocPtr xmldoc;
228             if(len != 0) {
229                 xmldoc = doparse( ptr, len );
230                 if(xmldoc) {
231                     xmldoc->_private = 0;
232                     attach_xmlnode(This->doc->node, (xmlNodePtr) xmldoc);
233                 }
234             }
235             GlobalUnlock(hglobal);
236         }
237     }
238
239     return S_OK;
240 }
241
242 static HRESULT WINAPI bsc_GetBindInfo(
243         IBindStatusCallback* iface,
244         DWORD* grfBINDF,
245         BINDINFO* pbindinfo)
246 {
247     *grfBINDF = BINDF_GETNEWESTVERSION|BINDF_PULLDATA|BINDF_RESYNCHRONIZE|BINDF_PRAGMA_NO_CACHE;
248     
249     return S_OK;
250 }
251
252 static HRESULT WINAPI bsc_OnDataAvailable(
253         IBindStatusCallback* iface,
254         DWORD grfBSCF,
255         DWORD dwSize,
256         FORMATETC* pformatetc,
257         STGMEDIUM* pstgmed)
258 {
259     bsc_t *This = impl_from_IBindStatusCallback(iface);
260     BYTE buf[4096];
261     DWORD read, written;
262     HRESULT hr;
263
264     TRACE("(%p)->(%x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
265
266     do
267     {
268         hr = IStream_Read(pstgmed->u.pstm, buf, sizeof(buf), &read);
269         if(FAILED(hr))
270             break;
271
272         hr = IStream_Write(This->memstream, buf, read, &written);
273     } while(SUCCEEDED(hr) && written != 0 && read != 0);
274
275     return S_OK;
276 }
277
278 static HRESULT WINAPI bsc_OnObjectAvailable(
279         IBindStatusCallback* iface,
280         REFIID riid,
281         IUnknown* punk)
282 {
283     return S_OK;
284 }
285
286 static const struct IBindStatusCallbackVtbl bsc_vtbl =
287 {
288     bsc_QueryInterface,
289     bsc_AddRef,
290     bsc_Release,
291     bsc_OnStartBinding,
292     bsc_GetPriority,
293     bsc_OnLowResource,
294     bsc_OnProgress,
295     bsc_OnStopBinding,
296     bsc_GetBindInfo,
297     bsc_OnDataAvailable,
298     bsc_OnObjectAvailable
299 };
300
301 static bsc_t *create_bsc(domdoc *doc)
302 {
303     bsc_t *bsc = HeapAlloc(GetProcessHeap(), 0, sizeof(bsc_t));
304
305     bsc->lpVtbl = &bsc_vtbl;
306     bsc->ref = 1;
307     bsc->doc = doc;
308     bsc->binding = NULL;
309     bsc->memstream = NULL;
310
311     return bsc;
312 }
313
314 LONG xmldoc_add_ref(xmlDocPtr doc)
315 {
316     LONG ref = InterlockedIncrement((LONG*)&doc->_private);
317     TRACE("%d\n", ref);
318     return ref;
319 }
320
321 LONG xmldoc_release(xmlDocPtr doc)
322 {
323     LONG ref = InterlockedDecrement((LONG*)&doc->_private);
324     TRACE("%d\n", ref);
325     if(ref == 0)
326     {
327         TRACE("freeing docptr %p\n", doc);
328         xmlFreeDoc(doc);
329     }
330
331     return ref;
332 }
333
334 static inline domdoc *impl_from_IXMLDOMDocument2( IXMLDOMDocument2 *iface )
335 {
336     return (domdoc *)((char*)iface - FIELD_OFFSET(domdoc, lpVtbl));
337 }
338
339 static inline xmlDocPtr get_doc( domdoc *This )
340 {
341     return (xmlDocPtr) xmlNodePtr_from_domnode( This->node, XML_DOCUMENT_NODE );
342 }
343
344 static inline domdoc *impl_from_IPersistStream(IPersistStream *iface)
345 {
346     return (domdoc *)((char*)iface - FIELD_OFFSET(domdoc, lpvtblIPersistStream));
347 }
348
349 static inline domdoc *impl_from_IObjectWithSite(IObjectWithSite *iface)
350 {
351     return (domdoc *)((char*)iface - FIELD_OFFSET(domdoc, lpvtblIObjectWithSite));
352 }
353
354 static inline domdoc *impl_from_IObjectSafety(IObjectSafety *iface)
355 {
356     return (domdoc *)((char*)iface - FIELD_OFFSET(domdoc, lpvtblIObjectSafety));
357 }
358
359
360 /************************************************************************
361  * xmldoc implementation of IPersistStream.
362  */
363 static HRESULT WINAPI xmldoc_IPersistStream_QueryInterface(
364     IPersistStream *iface, REFIID riid, LPVOID *ppvObj)
365 {
366     domdoc *this = impl_from_IPersistStream(iface);
367     return IXMLDocument_QueryInterface((IXMLDocument *)this, riid, ppvObj);
368 }
369
370 static ULONG WINAPI xmldoc_IPersistStream_AddRef(
371     IPersistStream *iface)
372 {
373     domdoc *this = impl_from_IPersistStream(iface);
374     return IXMLDocument_AddRef((IXMLDocument *)this);
375 }
376
377 static ULONG WINAPI xmldoc_IPersistStream_Release(
378     IPersistStream *iface)
379 {
380     domdoc *this = impl_from_IPersistStream(iface);
381     return IXMLDocument_Release((IXMLDocument *)this);
382 }
383
384 static HRESULT WINAPI xmldoc_IPersistStream_GetClassID(
385     IPersistStream *iface, CLSID *classid)
386 {
387     TRACE("(%p,%p): stub!\n", iface, classid);
388
389     if(!classid)
390         return E_POINTER;
391
392     *classid = CLSID_DOMDocument2;
393
394     return S_OK;
395 }
396
397 static HRESULT WINAPI xmldoc_IPersistStream_IsDirty(
398     IPersistStream *iface)
399 {
400     domdoc *This = impl_from_IPersistStream(iface);
401
402     FIXME("(%p->%p): stub!\n", iface, This);
403
404     return S_FALSE;
405 }
406
407 static HRESULT WINAPI xmldoc_IPersistStream_Load(
408     IPersistStream *iface, LPSTREAM pStm)
409 {
410     domdoc *This = impl_from_IPersistStream(iface);
411     HRESULT hr;
412     HGLOBAL hglobal;
413     DWORD read, written, len;
414     BYTE buf[4096];
415     char *ptr;
416     xmlDocPtr xmldoc = NULL;
417
418     TRACE("(%p, %p)\n", iface, pStm);
419
420     if (!pStm)
421         return E_INVALIDARG;
422
423     hr = CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
424     if (FAILED(hr))
425         return hr;
426
427     do
428     {
429         IStream_Read(pStm, buf, sizeof(buf), &read);
430         hr = IStream_Write(This->stream, buf, read, &written);
431     } while(SUCCEEDED(hr) && written != 0 && read != 0);
432
433     if (FAILED(hr))
434     {
435         ERR("Failed to copy stream\n");
436         return hr;
437     }
438
439     hr = GetHGlobalFromStream(This->stream, &hglobal);
440     if (FAILED(hr))
441         return hr;
442
443     len = GlobalSize(hglobal);
444     ptr = GlobalLock(hglobal);
445     if (len != 0)
446         xmldoc = parse_xml(ptr, len);
447     GlobalUnlock(hglobal);
448
449     if (!xmldoc)
450     {
451         ERR("Failed to parse xml\n");
452         return E_FAIL;
453     }
454
455     attach_xmlnode( This->node, (xmlNodePtr)xmldoc );
456
457     return S_OK;
458 }
459
460 static HRESULT WINAPI xmldoc_IPersistStream_Save(
461     IPersistStream *iface, LPSTREAM pStm, BOOL fClearDirty)
462 {
463     FIXME("(%p, %p, %d): stub!\n", iface, pStm, fClearDirty);
464     return E_NOTIMPL;
465 }
466
467 static HRESULT WINAPI xmldoc_IPersistStream_GetSizeMax(
468     IPersistStream *iface, ULARGE_INTEGER *pcbSize)
469 {
470     TRACE("(%p, %p): stub!\n", iface, pcbSize);
471     return E_NOTIMPL;
472 }
473
474 static const IPersistStreamVtbl xmldoc_IPersistStream_VTable =
475 {
476     xmldoc_IPersistStream_QueryInterface,
477     xmldoc_IPersistStream_AddRef,
478     xmldoc_IPersistStream_Release,
479     xmldoc_IPersistStream_GetClassID,
480     xmldoc_IPersistStream_IsDirty,
481     xmldoc_IPersistStream_Load,
482     xmldoc_IPersistStream_Save,
483     xmldoc_IPersistStream_GetSizeMax,
484 };
485
486 static HRESULT WINAPI domdoc_QueryInterface( IXMLDOMDocument2 *iface, REFIID riid, void** ppvObject )
487 {
488     domdoc *This = impl_from_IXMLDOMDocument2( iface );
489
490     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
491
492     *ppvObject = NULL;
493
494     if ( IsEqualGUID( riid, &IID_IUnknown ) ||
495          IsEqualGUID( riid, &IID_IDispatch ) ||
496          IsEqualGUID( riid, &IID_IXMLDOMDocument ) ||
497          IsEqualGUID( riid, &IID_IXMLDOMDocument2 ) )
498     {
499         *ppvObject = iface;
500     }
501     else if ( IsEqualGUID( riid, &IID_IXMLDOMNode ) )
502     {
503         return IUnknown_QueryInterface(This->node_unk, riid, ppvObject);
504     }
505     else if (IsEqualGUID(&IID_IPersistStream, riid))
506     {
507         *ppvObject = (IPersistStream*)&(This->lpvtblIPersistStream);
508     }
509     else if (IsEqualGUID(&IID_IObjectWithSite, riid))
510     {
511         *ppvObject = (IObjectWithSite*)&(This->lpvtblIObjectWithSite);
512     }
513     else if(IsEqualGUID(&IID_IRunnableObject, riid))
514     {
515         TRACE("IID_IRunnableObject not supported returning NULL\n");
516         return E_NOINTERFACE;
517     }
518     else
519     {
520         FIXME("interface %s not implemented\n", debugstr_guid(riid));
521         return E_NOINTERFACE;
522     }
523
524     IXMLDOMDocument_AddRef( iface );
525
526     return S_OK;
527 }
528
529
530 static ULONG WINAPI domdoc_AddRef(
531      IXMLDOMDocument2 *iface )
532 {
533     domdoc *This = impl_from_IXMLDOMDocument2( iface );
534     TRACE("%p\n", This );
535     return InterlockedIncrement( &This->ref );
536 }
537
538
539 static ULONG WINAPI domdoc_Release(
540      IXMLDOMDocument2 *iface )
541 {
542     domdoc *This = impl_from_IXMLDOMDocument2( iface );
543     LONG ref;
544
545     TRACE("%p\n", This );
546
547     ref = InterlockedDecrement( &This->ref );
548     if ( ref == 0 )
549     {
550         if(This->bsc) {
551             if(This->bsc->binding)
552                 IBinding_Abort(This->bsc->binding);
553             This->bsc->doc = NULL;
554             IBindStatusCallback_Release((IBindStatusCallback*)&This->bsc->lpVtbl);
555         }
556
557         if (This->site)
558             IUnknown_Release( This->site );
559         IUnknown_Release( This->node_unk );
560         if(This->schema) IXMLDOMSchemaCollection_Release( This->schema );
561         if (This->stream) IStream_Release(This->stream);
562         HeapFree( GetProcessHeap(), 0, This );
563     }
564
565     return ref;
566 }
567
568 static HRESULT WINAPI domdoc_GetTypeInfoCount( IXMLDOMDocument2 *iface, UINT* pctinfo )
569 {
570     domdoc *This = impl_from_IXMLDOMDocument2( iface );
571
572     TRACE("(%p)->(%p)\n", This, pctinfo);
573
574     *pctinfo = 1;
575
576     return S_OK;
577 }
578
579 static HRESULT WINAPI domdoc_GetTypeInfo(
580     IXMLDOMDocument2 *iface,
581     UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo )
582 {
583     domdoc *This = impl_from_IXMLDOMDocument2( iface );
584     HRESULT hr;
585
586     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
587
588     hr = get_typeinfo(IXMLDOMDocument2_tid, ppTInfo);
589
590     return hr;
591 }
592
593 static HRESULT WINAPI domdoc_GetIDsOfNames(
594     IXMLDOMDocument2 *iface,
595     REFIID riid,
596     LPOLESTR* rgszNames,
597     UINT cNames,
598     LCID lcid,
599     DISPID* rgDispId)
600 {
601     domdoc *This = impl_from_IXMLDOMDocument2( iface );
602     ITypeInfo *typeinfo;
603     HRESULT hr;
604
605     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
606           lcid, rgDispId);
607
608     if(!rgszNames || cNames == 0 || !rgDispId)
609         return E_INVALIDARG;
610
611     hr = get_typeinfo(IXMLDOMDocument2_tid, &typeinfo);
612     if(SUCCEEDED(hr))
613     {
614         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
615         ITypeInfo_Release(typeinfo);
616     }
617
618     return hr;
619 }
620
621
622 static HRESULT WINAPI domdoc_Invoke(
623     IXMLDOMDocument2 *iface,
624     DISPID dispIdMember,
625     REFIID riid,
626     LCID lcid,
627     WORD wFlags,
628     DISPPARAMS* pDispParams,
629     VARIANT* pVarResult,
630     EXCEPINFO* pExcepInfo,
631     UINT* puArgErr)
632 {
633     domdoc *This = impl_from_IXMLDOMDocument2( iface );
634     ITypeInfo *typeinfo;
635     HRESULT hr;
636
637     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
638           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
639
640     hr = get_typeinfo(IXMLDOMDocument2_tid, &typeinfo);
641     if(SUCCEEDED(hr))
642     {
643         hr = ITypeInfo_Invoke(typeinfo, &(This->lpVtbl), dispIdMember, wFlags, pDispParams,
644                 pVarResult, pExcepInfo, puArgErr);
645         ITypeInfo_Release(typeinfo);
646     }
647
648     return hr;
649 }
650
651
652 static HRESULT WINAPI domdoc_get_nodeName(
653     IXMLDOMDocument2 *iface,
654     BSTR* name )
655 {
656     domdoc *This = impl_from_IXMLDOMDocument2( iface );
657     return IXMLDOMNode_get_nodeName( This->node, name );
658 }
659
660
661 static HRESULT WINAPI domdoc_get_nodeValue(
662     IXMLDOMDocument2 *iface,
663     VARIANT* value )
664 {
665     domdoc *This = impl_from_IXMLDOMDocument2( iface );
666     return IXMLDOMNode_get_nodeValue( This->node, value );
667 }
668
669
670 static HRESULT WINAPI domdoc_put_nodeValue(
671     IXMLDOMDocument2 *iface,
672     VARIANT value)
673 {
674     domdoc *This = impl_from_IXMLDOMDocument2( iface );
675     return IXMLDOMNode_put_nodeValue( This->node, value );
676 }
677
678
679 static HRESULT WINAPI domdoc_get_nodeType(
680     IXMLDOMDocument2 *iface,
681     DOMNodeType* type )
682 {
683     domdoc *This = impl_from_IXMLDOMDocument2( iface );
684     return IXMLDOMNode_get_nodeType( This->node, type );
685 }
686
687
688 static HRESULT WINAPI domdoc_get_parentNode(
689     IXMLDOMDocument2 *iface,
690     IXMLDOMNode** parent )
691 {
692     domdoc *This = impl_from_IXMLDOMDocument2( iface );
693     return IXMLDOMNode_get_parentNode( This->node, parent );
694 }
695
696
697 static HRESULT WINAPI domdoc_get_childNodes(
698     IXMLDOMDocument2 *iface,
699     IXMLDOMNodeList** childList )
700 {
701     domdoc *This = impl_from_IXMLDOMDocument2( iface );
702     return IXMLDOMNode_get_childNodes( This->node, childList );
703 }
704
705
706 static HRESULT WINAPI domdoc_get_firstChild(
707     IXMLDOMDocument2 *iface,
708     IXMLDOMNode** firstChild )
709 {
710     domdoc *This = impl_from_IXMLDOMDocument2( iface );
711     return IXMLDOMNode_get_firstChild( This->node, firstChild );
712 }
713
714
715 static HRESULT WINAPI domdoc_get_lastChild(
716     IXMLDOMDocument2 *iface,
717     IXMLDOMNode** lastChild )
718 {
719     domdoc *This = impl_from_IXMLDOMDocument2( iface );
720     return IXMLDOMNode_get_lastChild( This->node, lastChild );
721 }
722
723
724 static HRESULT WINAPI domdoc_get_previousSibling(
725     IXMLDOMDocument2 *iface,
726     IXMLDOMNode** previousSibling )
727 {
728     domdoc *This = impl_from_IXMLDOMDocument2( iface );
729     return IXMLDOMNode_get_previousSibling( This->node, previousSibling );
730 }
731
732
733 static HRESULT WINAPI domdoc_get_nextSibling(
734     IXMLDOMDocument2 *iface,
735     IXMLDOMNode** nextSibling )
736 {
737     domdoc *This = impl_from_IXMLDOMDocument2( iface );
738     return IXMLDOMNode_get_nextSibling( This->node, nextSibling );
739 }
740
741
742 static HRESULT WINAPI domdoc_get_attributes(
743     IXMLDOMDocument2 *iface,
744     IXMLDOMNamedNodeMap** attributeMap )
745 {
746     domdoc *This = impl_from_IXMLDOMDocument2( iface );
747     return IXMLDOMNode_get_attributes( This->node, attributeMap );
748 }
749
750
751 static HRESULT WINAPI domdoc_insertBefore(
752     IXMLDOMDocument2 *iface,
753     IXMLDOMNode* newChild,
754     VARIANT refChild,
755     IXMLDOMNode** outNewChild )
756 {
757     domdoc *This = impl_from_IXMLDOMDocument2( iface );
758     return IXMLDOMNode_insertBefore( This->node, newChild, refChild, outNewChild );
759 }
760
761
762 static HRESULT WINAPI domdoc_replaceChild(
763     IXMLDOMDocument2 *iface,
764     IXMLDOMNode* newChild,
765     IXMLDOMNode* oldChild,
766     IXMLDOMNode** outOldChild)
767 {
768     domdoc *This = impl_from_IXMLDOMDocument2( iface );
769     return IXMLDOMNode_replaceChild( This->node, newChild, oldChild, outOldChild );
770 }
771
772
773 static HRESULT WINAPI domdoc_removeChild(
774     IXMLDOMDocument2 *iface,
775     IXMLDOMNode* childNode,
776     IXMLDOMNode** oldChild)
777 {
778     domdoc *This = impl_from_IXMLDOMDocument2( iface );
779     return IXMLDOMNode_removeChild( This->node, childNode, oldChild );
780 }
781
782
783 static HRESULT WINAPI domdoc_appendChild(
784     IXMLDOMDocument2 *iface,
785     IXMLDOMNode* newChild,
786     IXMLDOMNode** outNewChild)
787 {
788     domdoc *This = impl_from_IXMLDOMDocument2( iface );
789     return IXMLDOMNode_appendChild( This->node, newChild, outNewChild );
790 }
791
792
793 static HRESULT WINAPI domdoc_hasChildNodes(
794     IXMLDOMDocument2 *iface,
795     VARIANT_BOOL* hasChild)
796 {
797     domdoc *This = impl_from_IXMLDOMDocument2( iface );
798     return IXMLDOMNode_hasChildNodes( This->node, hasChild );
799 }
800
801
802 static HRESULT WINAPI domdoc_get_ownerDocument(
803     IXMLDOMDocument2 *iface,
804     IXMLDOMDocument** DOMDocument)
805 {
806     domdoc *This = impl_from_IXMLDOMDocument2( iface );
807     return IXMLDOMNode_get_ownerDocument( This->node, DOMDocument );
808 }
809
810
811 static HRESULT WINAPI domdoc_cloneNode(
812     IXMLDOMDocument2 *iface,
813     VARIANT_BOOL deep,
814     IXMLDOMNode** cloneRoot)
815 {
816     domdoc *This = impl_from_IXMLDOMDocument2( iface );
817     return IXMLDOMNode_cloneNode( This->node, deep, cloneRoot );
818 }
819
820
821 static HRESULT WINAPI domdoc_get_nodeTypeString(
822     IXMLDOMDocument2 *iface,
823     BSTR* nodeType )
824 {
825     domdoc *This = impl_from_IXMLDOMDocument2( iface );
826     return IXMLDOMNode_get_nodeTypeString( This->node, nodeType );
827 }
828
829
830 static HRESULT WINAPI domdoc_get_text(
831     IXMLDOMDocument2 *iface,
832     BSTR* text )
833 {
834     domdoc *This = impl_from_IXMLDOMDocument2( iface );
835     return IXMLDOMNode_get_text( This->node, text );
836 }
837
838
839 static HRESULT WINAPI domdoc_put_text(
840     IXMLDOMDocument2 *iface,
841     BSTR text )
842 {
843     domdoc *This = impl_from_IXMLDOMDocument2( iface );
844     return IXMLDOMNode_put_text( This->node, text );
845 }
846
847
848 static HRESULT WINAPI domdoc_get_specified(
849     IXMLDOMDocument2 *iface,
850     VARIANT_BOOL* isSpecified )
851 {
852     domdoc *This = impl_from_IXMLDOMDocument2( iface );
853     return IXMLDOMNode_get_specified( This->node, isSpecified );
854 }
855
856
857 static HRESULT WINAPI domdoc_get_definition(
858     IXMLDOMDocument2 *iface,
859     IXMLDOMNode** definitionNode )
860 {
861     domdoc *This = impl_from_IXMLDOMDocument2( iface );
862     return IXMLDOMNode_get_definition( This->node, definitionNode );
863 }
864
865
866 static HRESULT WINAPI domdoc_get_nodeTypedValue(
867     IXMLDOMDocument2 *iface,
868     VARIANT* typedValue )
869 {
870     domdoc *This = impl_from_IXMLDOMDocument2( iface );
871     return IXMLDOMNode_get_nodeTypedValue( This->node, typedValue );
872 }
873
874 static HRESULT WINAPI domdoc_put_nodeTypedValue(
875     IXMLDOMDocument2 *iface,
876     VARIANT typedValue )
877 {
878     domdoc *This = impl_from_IXMLDOMDocument2( iface );
879     return IXMLDOMNode_put_nodeTypedValue( This->node, typedValue );
880 }
881
882
883 static HRESULT WINAPI domdoc_get_dataType(
884     IXMLDOMDocument2 *iface,
885     VARIANT* dataTypeName )
886 {
887     domdoc *This = impl_from_IXMLDOMDocument2( iface );
888     return IXMLDOMNode_get_dataType( This->node, dataTypeName );
889 }
890
891
892 static HRESULT WINAPI domdoc_put_dataType(
893     IXMLDOMDocument2 *iface,
894     BSTR dataTypeName )
895 {
896     domdoc *This = impl_from_IXMLDOMDocument2( iface );
897     return IXMLDOMNode_put_dataType( This->node, dataTypeName );
898 }
899
900
901 static HRESULT WINAPI domdoc_get_xml(
902     IXMLDOMDocument2 *iface,
903     BSTR* xmlString )
904 {
905     domdoc *This = impl_from_IXMLDOMDocument2( iface );
906     return IXMLDOMNode_get_xml( This->node, xmlString );
907 }
908
909
910 static HRESULT WINAPI domdoc_transformNode(
911     IXMLDOMDocument2 *iface,
912     IXMLDOMNode* styleSheet,
913     BSTR* xmlString )
914 {
915     domdoc *This = impl_from_IXMLDOMDocument2( iface );
916     return IXMLDOMNode_transformNode( This->node, styleSheet, xmlString );
917 }
918
919
920 static HRESULT WINAPI domdoc_selectNodes(
921     IXMLDOMDocument2 *iface,
922     BSTR queryString,
923     IXMLDOMNodeList** resultList )
924 {
925     domdoc *This = impl_from_IXMLDOMDocument2( iface );
926     return IXMLDOMNode_selectNodes( This->node, queryString, resultList );
927 }
928
929
930 static HRESULT WINAPI domdoc_selectSingleNode(
931     IXMLDOMDocument2 *iface,
932     BSTR queryString,
933     IXMLDOMNode** resultNode )
934 {
935     domdoc *This = impl_from_IXMLDOMDocument2( iface );
936     return IXMLDOMNode_selectSingleNode( This->node, queryString, resultNode );
937 }
938
939
940 static HRESULT WINAPI domdoc_get_parsed(
941     IXMLDOMDocument2 *iface,
942     VARIANT_BOOL* isParsed )
943 {
944     domdoc *This = impl_from_IXMLDOMDocument2( iface );
945     return IXMLDOMNode_get_parsed( This->node, isParsed );
946 }
947
948
949 static HRESULT WINAPI domdoc_get_namespaceURI(
950     IXMLDOMDocument2 *iface,
951     BSTR* namespaceURI )
952 {
953     domdoc *This = impl_from_IXMLDOMDocument2( iface );
954     return IXMLDOMNode_get_namespaceURI( This->node, namespaceURI );
955 }
956
957
958 static HRESULT WINAPI domdoc_get_prefix(
959     IXMLDOMDocument2 *iface,
960     BSTR* prefixString )
961 {
962     domdoc *This = impl_from_IXMLDOMDocument2( iface );
963     return IXMLDOMNode_get_prefix( This->node, prefixString );
964 }
965
966
967 static HRESULT WINAPI domdoc_get_baseName(
968     IXMLDOMDocument2 *iface,
969     BSTR* nameString )
970 {
971     domdoc *This = impl_from_IXMLDOMDocument2( iface );
972     return IXMLDOMNode_get_baseName( This->node, nameString );
973 }
974
975
976 static HRESULT WINAPI domdoc_transformNodeToObject(
977     IXMLDOMDocument2 *iface,
978     IXMLDOMNode* stylesheet,
979     VARIANT outputObject)
980 {
981     domdoc *This = impl_from_IXMLDOMDocument2( iface );
982     return IXMLDOMNode_transformNodeToObject( This->node, stylesheet, outputObject );
983 }
984
985
986 static HRESULT WINAPI domdoc_get_doctype(
987     IXMLDOMDocument2 *iface,
988     IXMLDOMDocumentType** documentType )
989 {
990     FIXME("\n");
991     return E_NOTIMPL;
992 }
993
994
995 static HRESULT WINAPI domdoc_get_implementation(
996     IXMLDOMDocument2 *iface,
997     IXMLDOMImplementation** impl )
998 {
999     if(!impl)
1000         return E_INVALIDARG;
1001
1002     *impl = (IXMLDOMImplementation*)create_doc_Implementation();
1003
1004     return S_OK;
1005 }
1006
1007 static HRESULT WINAPI domdoc_get_documentElement(
1008     IXMLDOMDocument2 *iface,
1009     IXMLDOMElement** DOMElement )
1010 {
1011     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1012     xmlDocPtr xmldoc = NULL;
1013     xmlNodePtr root = NULL;
1014     IXMLDOMNode *element_node;
1015     HRESULT hr;
1016
1017     TRACE("%p %p\n", This, This->node);
1018
1019     if(!DOMElement)
1020         return E_INVALIDARG;
1021
1022     *DOMElement = NULL;
1023
1024     xmldoc = get_doc( This );
1025
1026     root = xmlDocGetRootElement( xmldoc );
1027     if ( !root )
1028         return S_FALSE;
1029
1030     element_node = create_node( root );
1031     if(!element_node) return S_FALSE;
1032
1033     hr = IXMLDOMNode_QueryInterface(element_node, &IID_IXMLDOMElement, (LPVOID*)DOMElement);
1034     IXMLDOMNode_Release(element_node);
1035
1036     return hr;
1037 }
1038
1039
1040 static HRESULT WINAPI domdoc_put_documentElement(
1041     IXMLDOMDocument2 *iface,
1042     IXMLDOMElement* DOMElement )
1043 {
1044     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1045     IXMLDOMNode *elementNode;
1046     xmlnode *xmlNode;
1047     HRESULT hr;
1048
1049     TRACE("(%p)->(%p)\n", This, DOMElement);
1050
1051     hr = IXMLDOMElement_QueryInterface( DOMElement, &IID_IXMLDOMNode, (void**)&elementNode );
1052     if(FAILED(hr))
1053         return hr;
1054
1055     xmlNode = impl_from_IXMLDOMNode( elementNode );
1056     xmlDocSetRootElement( get_doc(This), xmlNode->node);
1057     IXMLDOMNode_Release( elementNode );
1058
1059     return S_OK;
1060 }
1061
1062
1063 static HRESULT WINAPI domdoc_createElement(
1064     IXMLDOMDocument2 *iface,
1065     BSTR tagname,
1066     IXMLDOMElement** element )
1067 {
1068     xmlNodePtr xmlnode;
1069     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1070     xmlChar *xml_name;
1071     IUnknown *elem_unk;
1072     HRESULT hr;
1073
1074     TRACE("%p->(%s,%p)\n", iface, debugstr_w(tagname), element);
1075
1076     xml_name = xmlChar_from_wchar((WCHAR*)tagname);
1077     xmlnode = xmlNewDocNode(get_doc(This), NULL, xml_name, NULL);
1078
1079     TRACE("created xmlptr %p\n", xmlnode);
1080     elem_unk = create_element(xmlnode, NULL);
1081     HeapFree(GetProcessHeap(), 0, xml_name);
1082
1083     hr = IUnknown_QueryInterface(elem_unk, &IID_IXMLDOMElement, (void **)element);
1084     IUnknown_Release(elem_unk);
1085     TRACE("returning %p\n", *element);
1086     return hr;
1087 }
1088
1089
1090 static HRESULT WINAPI domdoc_createDocumentFragment(
1091     IXMLDOMDocument2 *iface,
1092     IXMLDOMDocumentFragment** docFrag )
1093 {
1094     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1095     xmlNodePtr xmlnode;
1096
1097     TRACE("%p\n", iface);
1098
1099     if(!docFrag)
1100         return E_INVALIDARG;
1101
1102     *docFrag = NULL;
1103
1104     xmlnode = xmlNewDocFragment(get_doc( This ) );
1105
1106     if(!xmlnode)
1107         return E_FAIL;
1108
1109     xmlnode->doc = get_doc( This );
1110
1111     *docFrag = (IXMLDOMDocumentFragment*)create_doc_fragment(xmlnode);
1112
1113     return S_OK;
1114 }
1115
1116
1117 static HRESULT WINAPI domdoc_createTextNode(
1118     IXMLDOMDocument2 *iface,
1119     BSTR data,
1120     IXMLDOMText** text )
1121 {
1122     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1123     xmlNodePtr xmlnode;
1124     xmlChar *xml_content;
1125
1126     TRACE("%p->(%s %p)\n", iface, debugstr_w(data), text);
1127
1128     if(!text)
1129         return E_INVALIDARG;
1130
1131     *text = NULL;
1132
1133     xml_content = xmlChar_from_wchar((WCHAR*)data);
1134     xmlnode = xmlNewText(xml_content);
1135     HeapFree(GetProcessHeap(), 0, xml_content);
1136
1137     if(!xmlnode)
1138         return E_FAIL;
1139
1140     xmlnode->doc = get_doc( This );
1141
1142     *text = (IXMLDOMText*)create_text(xmlnode);
1143
1144     return S_OK;
1145 }
1146
1147
1148 static HRESULT WINAPI domdoc_createComment(
1149     IXMLDOMDocument2 *iface,
1150     BSTR data,
1151     IXMLDOMComment** comment )
1152 {
1153     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1154     xmlNodePtr xmlnode;
1155     xmlChar *xml_content;
1156
1157     TRACE("%p->(%s %p)\n", iface, debugstr_w(data), comment);
1158
1159     if(!comment)
1160         return E_INVALIDARG;
1161
1162     *comment = NULL;
1163
1164     xml_content = xmlChar_from_wchar((WCHAR*)data);
1165     xmlnode = xmlNewComment(xml_content);
1166     HeapFree(GetProcessHeap(), 0, xml_content);
1167
1168     if(!xmlnode)
1169         return E_FAIL;
1170
1171     xmlnode->doc = get_doc( This );
1172
1173     *comment = (IXMLDOMComment*)create_comment(xmlnode);
1174
1175     return S_OK;
1176 }
1177
1178
1179 static HRESULT WINAPI domdoc_createCDATASection(
1180     IXMLDOMDocument2 *iface,
1181     BSTR data,
1182     IXMLDOMCDATASection** cdata )
1183 {
1184     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1185     xmlNodePtr xmlnode;
1186     xmlChar *xml_content;
1187
1188     TRACE("%p->(%s %p)\n", iface, debugstr_w(data), cdata);
1189
1190     if(!cdata)
1191         return E_INVALIDARG;
1192
1193     *cdata = NULL;
1194
1195     xml_content = xmlChar_from_wchar((WCHAR*)data);
1196     xmlnode = xmlNewCDataBlock(get_doc( This ), xml_content, strlen( (char*)xml_content) );
1197     HeapFree(GetProcessHeap(), 0, xml_content);
1198
1199     if(!xmlnode)
1200         return E_FAIL;
1201
1202     xmlnode->doc = get_doc( This );
1203
1204     *cdata = (IXMLDOMCDATASection*)create_cdata(xmlnode);
1205
1206     return S_OK;
1207 }
1208
1209
1210 static HRESULT WINAPI domdoc_createProcessingInstruction(
1211     IXMLDOMDocument2 *iface,
1212     BSTR target,
1213     BSTR data,
1214     IXMLDOMProcessingInstruction** pi )
1215 {
1216 #ifdef HAVE_XMLNEWDOCPI
1217     xmlNodePtr xmlnode;
1218     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1219     xmlChar *xml_target, *xml_content;
1220
1221     TRACE("%p->(%s %s %p)\n", iface, debugstr_w(target), debugstr_w(data), pi);
1222
1223     if(!pi)
1224         return E_INVALIDARG;
1225
1226     if(!target || lstrlenW(target) == 0)
1227         return E_FAIL;
1228
1229     xml_target = xmlChar_from_wchar((WCHAR*)target);
1230     xml_content = xmlChar_from_wchar((WCHAR*)data);
1231
1232     xmlnode = xmlNewDocPI(get_doc(This), xml_target, xml_content);
1233     TRACE("created xmlptr %p\n", xmlnode);
1234     *pi = (IXMLDOMProcessingInstruction*)create_pi(xmlnode);
1235
1236     HeapFree(GetProcessHeap(), 0, xml_content);
1237     HeapFree(GetProcessHeap(), 0, xml_target);
1238
1239     return S_OK;
1240 #else
1241     FIXME("Libxml 2.6.15 or greater required.\n");
1242     return E_NOTIMPL;
1243 #endif
1244 }
1245
1246
1247 static HRESULT WINAPI domdoc_createAttribute(
1248     IXMLDOMDocument2 *iface,
1249     BSTR name,
1250     IXMLDOMAttribute** attribute )
1251 {
1252     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1253     xmlNodePtr xmlnode;
1254     xmlChar *xml_name;
1255
1256     TRACE("%p->(%s %p)\n", iface, debugstr_w(name), attribute);
1257
1258     if(!attribute)
1259         return E_INVALIDARG;
1260
1261     *attribute = NULL;
1262
1263     xml_name = xmlChar_from_wchar((WCHAR*)name);
1264     xmlnode = (xmlNode *)xmlNewProp(NULL, xml_name, NULL);
1265     HeapFree(GetProcessHeap(), 0, xml_name);
1266
1267     if(!xmlnode)
1268         return E_FAIL;
1269
1270     xmlnode->doc = get_doc( This );
1271
1272     *attribute = (IXMLDOMAttribute*)create_attribute(xmlnode);
1273
1274     return S_OK;
1275 }
1276
1277
1278 static HRESULT WINAPI domdoc_createEntityReference(
1279     IXMLDOMDocument2 *iface,
1280     BSTR name,
1281     IXMLDOMEntityReference** entityRef )
1282 {
1283     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1284     xmlNodePtr xmlnode;
1285     xmlChar *xml_name;
1286
1287     TRACE("%p\n", iface);
1288
1289     if(!entityRef)
1290         return E_INVALIDARG;
1291
1292     *entityRef = NULL;
1293
1294     xml_name = xmlChar_from_wchar((WCHAR*)name);
1295     xmlnode = xmlNewReference(get_doc( This ), xml_name );
1296     HeapFree(GetProcessHeap(), 0, xml_name);
1297
1298     if(!xmlnode)
1299         return E_FAIL;
1300
1301     xmlnode->doc = get_doc( This );
1302
1303     *entityRef = (IXMLDOMEntityReference*)create_doc_entity_ref(xmlnode);
1304
1305     return S_OK;
1306 }
1307
1308
1309 static HRESULT WINAPI domdoc_getElementsByTagName(
1310     IXMLDOMDocument2 *iface,
1311     BSTR tagName,
1312     IXMLDOMNodeList** resultList )
1313 {
1314     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1315     LPWSTR szPattern;
1316     HRESULT hr;
1317     TRACE("(%p)->(%s, %p)\n", This, debugstr_w(tagName), resultList);
1318
1319     szPattern = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(2+lstrlenW(tagName)+1));
1320     szPattern[0] = szPattern[1] = '/';
1321     lstrcpyW(szPattern + 2, tagName);
1322
1323     hr = queryresult_create((xmlNodePtr)get_doc(This), szPattern, resultList);
1324     HeapFree(GetProcessHeap(), 0, szPattern);
1325
1326     return hr;
1327 }
1328
1329 static DOMNodeType get_node_type(VARIANT Type)
1330 {
1331     if(V_VT(&Type) == VT_I4)
1332         return V_I4(&Type);
1333
1334     FIXME("Unsupported variant type %x\n", V_VT(&Type));
1335     return 0;
1336 }
1337
1338 static HRESULT WINAPI domdoc_createNode(
1339     IXMLDOMDocument2 *iface,
1340     VARIANT Type,
1341     BSTR name,
1342     BSTR namespaceURI,
1343     IXMLDOMNode** node )
1344 {
1345     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1346     DOMNodeType node_type;
1347     xmlNodePtr xmlnode = NULL;
1348     xmlChar *xml_name;
1349
1350     TRACE("(%p)->(type,%s,%s,%p)\n", This, debugstr_w(name), debugstr_w(namespaceURI), node);
1351
1352     node_type = get_node_type(Type);
1353     TRACE("node_type %d\n", node_type);
1354
1355     xml_name = xmlChar_from_wchar((WCHAR*)name);
1356
1357     switch(node_type)
1358     {
1359     case NODE_ELEMENT:
1360         xmlnode = xmlNewDocNode(get_doc(This), NULL, xml_name, NULL);
1361         *node = create_node(xmlnode);
1362         TRACE("created %p\n", xmlnode);
1363         break;
1364     case NODE_ATTRIBUTE:
1365         xmlnode = (xmlNode *)xmlNewProp(NULL, xml_name, NULL);
1366         if(xmlnode)
1367         {
1368             xmlnode->doc = get_doc( This );
1369
1370             *node = (IXMLDOMNode*)create_attribute(xmlnode);
1371         }
1372
1373         TRACE("created %p\n", xmlnode);
1374         break;
1375
1376     default:
1377         FIXME("unhandled node type %d\n", node_type);
1378         break;
1379     }
1380
1381     HeapFree(GetProcessHeap(), 0, xml_name);
1382
1383     if(xmlnode && *node)
1384         return S_OK;
1385
1386     return E_FAIL;
1387 }
1388
1389 static HRESULT WINAPI domdoc_nodeFromID(
1390     IXMLDOMDocument2 *iface,
1391     BSTR idString,
1392     IXMLDOMNode** node )
1393 {
1394     FIXME("\n");
1395     return E_NOTIMPL;
1396 }
1397
1398 static HRESULT doread( domdoc *This, LPWSTR filename )
1399 {
1400     HRESULT hr;
1401     IBindCtx *pbc;
1402     WCHAR url[INTERNET_MAX_URL_LENGTH];
1403
1404     TRACE("%s\n", debugstr_w( filename ));
1405
1406     if(!PathIsURLW(filename))
1407     {
1408         WCHAR fullpath[MAX_PATH];
1409         DWORD needed = sizeof(url)/sizeof(WCHAR);
1410
1411         if(!PathSearchAndQualifyW(filename, fullpath, sizeof(fullpath)/sizeof(WCHAR)))
1412         {
1413             WARN("can't find path\n");
1414             return E_FAIL;
1415         }
1416
1417         if(FAILED(UrlCreateFromPathW(fullpath, url, &needed, 0)))
1418         {
1419             ERR("can't create url from path\n");
1420             return E_FAIL;
1421         }
1422         filename = url;
1423     }
1424
1425     hr = CreateBindCtx(0, &pbc);
1426     if(SUCCEEDED(hr))
1427     {
1428         if(This->bsc) {
1429             if(This->bsc->binding)
1430                 IBinding_Abort(This->bsc->binding);
1431             This->bsc->doc = NULL;
1432             IBindStatusCallback_Release((IBindStatusCallback*)&This->bsc->lpVtbl);
1433         }
1434
1435         This->bsc = create_bsc(This);
1436
1437         hr = RegisterBindStatusCallback(pbc, (IBindStatusCallback*)&This->bsc->lpVtbl, NULL, 0);
1438         if(SUCCEEDED(hr))
1439         {
1440             IMoniker *moniker;
1441             hr = CreateURLMoniker(NULL, filename, &moniker);
1442             if(SUCCEEDED(hr))
1443             {
1444                 IStream *stream;
1445                 hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (LPVOID*)&stream);
1446                 IMoniker_Release(moniker);
1447                 if(stream)
1448                     IStream_Release(stream);
1449             }
1450         }
1451         IBindCtx_Release(pbc);
1452     }
1453
1454     return hr;
1455 }
1456
1457 static HRESULT WINAPI domdoc_load(
1458     IXMLDOMDocument2 *iface,
1459     VARIANT xmlSource,
1460     VARIANT_BOOL* isSuccessful )
1461 {
1462     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1463     LPWSTR filename = NULL;
1464     HRESULT hr = S_FALSE;
1465     IXMLDOMDocument2 *pNewDoc = NULL;
1466     IStream *pStream = NULL;
1467     xmlDocPtr xmldoc;
1468
1469     TRACE("type %d\n", V_VT(&xmlSource) );
1470
1471     *isSuccessful = VARIANT_FALSE;
1472
1473     assert( This->node );
1474
1475     attach_xmlnode(This->node, NULL);
1476
1477     switch( V_VT(&xmlSource) )
1478     {
1479     case VT_BSTR:
1480         filename = V_BSTR(&xmlSource);
1481         break;
1482     case VT_UNKNOWN:
1483         hr = IUnknown_QueryInterface(V_UNKNOWN(&xmlSource), &IID_IXMLDOMDocument2, (void**)&pNewDoc);
1484         if(hr == S_OK)
1485         {
1486             if(pNewDoc)
1487             {
1488                 domdoc *newDoc = impl_from_IXMLDOMDocument2( pNewDoc );
1489                 xmldoc = xmlCopyDoc(get_doc(newDoc), 1);
1490                 attach_xmlnode(This->node, (xmlNodePtr) xmldoc);
1491
1492                 *isSuccessful = VARIANT_TRUE;
1493
1494                 return S_OK;
1495             }
1496         }
1497         hr = IUnknown_QueryInterface(V_UNKNOWN(&xmlSource), &IID_IStream, (void**)&pStream);
1498         if(hr == S_OK)
1499         {
1500             IPersistStream *pDocStream;
1501             hr = IUnknown_QueryInterface(iface, &IID_IPersistStream, (void**)&pDocStream);
1502             if(hr == S_OK)
1503             {
1504                 hr = xmldoc_IPersistStream_Load(pDocStream, pStream);
1505                 IStream_Release(pStream);
1506                 if(hr == S_OK)
1507                 {
1508                     *isSuccessful = VARIANT_TRUE;
1509
1510                     TRACE("Using ID_IStream to load Document\n");
1511                     return S_OK;
1512                 }
1513                 else
1514                 {
1515                     ERR("xmldoc_IPersistStream_Load failed (%d)\n", hr);
1516                 }
1517             }
1518             else
1519             {
1520                 ERR("QueryInterface IID_IPersistStream failed (%d)\n", hr);
1521             }
1522         }
1523         else
1524         {
1525             /* ISequentialStream */
1526             FIXME("Unknown type not supported (%d) (%p)(%p)\n", hr, pNewDoc, V_UNKNOWN(&xmlSource)->lpVtbl);
1527         }
1528         break;
1529      default:
1530             FIXME("VT type not supported (%d)\n", V_VT(&xmlSource));
1531      }
1532
1533     TRACE("filename (%s)\n", debugstr_w(filename));
1534
1535     if ( filename )
1536     {
1537         hr = doread( This, filename );
1538     
1539         if ( FAILED(hr) )
1540             This->error = E_FAIL;
1541         else
1542         {
1543             hr = This->error = S_OK;
1544             *isSuccessful = VARIANT_TRUE;
1545         }
1546     }
1547
1548     if(!filename || FAILED(hr)) {
1549         xmldoc = xmlNewDoc(NULL);
1550         xmldoc->_private = 0;
1551         attach_xmlnode(This->node, (xmlNodePtr) xmldoc);
1552         hr = S_FALSE;
1553     }
1554
1555     TRACE("ret (%d)\n", hr);
1556
1557     return hr;
1558 }
1559
1560
1561 static HRESULT WINAPI domdoc_get_readyState(
1562     IXMLDOMDocument2 *iface,
1563     long* value )
1564 {
1565     FIXME("\n");
1566     return E_NOTIMPL;
1567 }
1568
1569
1570 static HRESULT WINAPI domdoc_get_parseError(
1571     IXMLDOMDocument2 *iface,
1572     IXMLDOMParseError** errorObj )
1573 {
1574     BSTR error_string = NULL;
1575     static const WCHAR err[] = {'e','r','r','o','r',0};
1576     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1577
1578     FIXME("(%p)->(%p): creating a dummy parseError\n", iface, errorObj);
1579
1580     if(This->error)
1581         error_string = SysAllocString(err);
1582
1583     *errorObj = create_parseError(This->error, NULL, error_string, NULL, 0, 0, 0);
1584     if(!*errorObj) return E_OUTOFMEMORY;
1585     return S_OK;
1586 }
1587
1588
1589 static HRESULT WINAPI domdoc_get_url(
1590     IXMLDOMDocument2 *iface,
1591     BSTR* urlString )
1592 {
1593     FIXME("\n");
1594     return E_NOTIMPL;
1595 }
1596
1597
1598 static HRESULT WINAPI domdoc_get_async(
1599     IXMLDOMDocument2 *iface,
1600     VARIANT_BOOL* isAsync )
1601 {
1602     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1603
1604     TRACE("%p <- %d\n", isAsync, This->async);
1605     *isAsync = This->async;
1606     return S_OK;
1607 }
1608
1609
1610 static HRESULT WINAPI domdoc_put_async(
1611     IXMLDOMDocument2 *iface,
1612     VARIANT_BOOL isAsync )
1613 {
1614     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1615
1616     TRACE("%d\n", isAsync);
1617     This->async = isAsync;
1618     return S_OK;
1619 }
1620
1621
1622 static HRESULT WINAPI domdoc_abort(
1623     IXMLDOMDocument2 *iface )
1624 {
1625     FIXME("\n");
1626     return E_NOTIMPL;
1627 }
1628
1629
1630 static BOOL bstr_to_utf8( BSTR bstr, char **pstr, int *plen )
1631 {
1632     UINT len, blen = SysStringLen( bstr );
1633     LPSTR str;
1634
1635     len = WideCharToMultiByte( CP_UTF8, 0, bstr, blen, NULL, 0, NULL, NULL );
1636     str = HeapAlloc( GetProcessHeap(), 0, len );
1637     if ( !str )
1638         return FALSE;
1639     WideCharToMultiByte( CP_UTF8, 0, bstr, blen, str, len, NULL, NULL );
1640     *plen = len;
1641     *pstr = str;
1642     return TRUE;
1643 }
1644
1645 static HRESULT WINAPI domdoc_loadXML(
1646     IXMLDOMDocument2 *iface,
1647     BSTR bstrXML,
1648     VARIANT_BOOL* isSuccessful )
1649 {
1650     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1651     xmlDocPtr xmldoc = NULL;
1652     char *str;
1653     int len;
1654     HRESULT hr = S_FALSE;
1655
1656     TRACE("%p %s %p\n", This, debugstr_w( bstrXML ), isSuccessful );
1657
1658     assert ( This->node );
1659
1660     attach_xmlnode( This->node, NULL );
1661
1662     if ( isSuccessful )
1663     {
1664         *isSuccessful = VARIANT_FALSE;
1665
1666         if ( bstrXML  && bstr_to_utf8( bstrXML, &str, &len ) )
1667         {
1668             xmldoc = doparse( str, len );
1669             HeapFree( GetProcessHeap(), 0, str );
1670             if ( !xmldoc )
1671                 This->error = E_FAIL;
1672             else
1673             {
1674                 hr = This->error = S_OK;
1675                 *isSuccessful = VARIANT_TRUE;
1676             }
1677         }
1678     }
1679     if(!xmldoc)
1680         xmldoc = xmlNewDoc(NULL);
1681
1682     xmldoc->_private = 0;
1683     attach_xmlnode( This->node, (xmlNodePtr) xmldoc );
1684
1685     return hr;
1686 }
1687
1688
1689 static HRESULT WINAPI domdoc_save(
1690     IXMLDOMDocument2 *iface,
1691     VARIANT destination )
1692 {
1693     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1694     HANDLE handle;
1695     xmlChar *mem, *p;
1696     int size;
1697     HRESULT ret = S_OK;
1698     DWORD written;
1699
1700     TRACE("(%p)->(var(vt %x, %s))\n", This, V_VT(&destination),
1701           V_VT(&destination) == VT_BSTR ? debugstr_w(V_BSTR(&destination)) : NULL);
1702
1703     if(V_VT(&destination) != VT_BSTR && V_VT(&destination) != VT_UNKNOWN)
1704     {
1705         FIXME("Unhandled vt %d\n", V_VT(&destination));
1706         return S_FALSE;
1707     }
1708
1709     if(V_VT(&destination) == VT_UNKNOWN)
1710     {
1711         IUnknown *pUnk = V_UNKNOWN(&destination);
1712         IXMLDOMDocument *pDocument;
1713
1714         ret = IXMLDOMDocument_QueryInterface(pUnk, &IID_IXMLDOMDocument2, (void**)&pDocument);
1715         if(ret == S_OK)
1716         {
1717             BSTR bXML;
1718             VARIANT_BOOL bSuccessful;
1719
1720             ret = IXMLDOMDocument_get_xml(iface, &bXML);
1721             if(ret == S_OK)
1722             {
1723                 ret = IXMLDOMDocument_loadXML(pDocument, bXML, &bSuccessful);
1724
1725                 SysFreeString(bXML);
1726             }
1727
1728             IXMLDOMDocument_Release(pDocument);
1729         }
1730
1731         return ret;
1732     }
1733
1734     handle = CreateFileW( V_BSTR(&destination), GENERIC_WRITE, 0,
1735                           NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
1736     if( handle == INVALID_HANDLE_VALUE )
1737     {
1738         WARN("failed to create file\n");
1739         return S_FALSE;
1740     }
1741
1742     xmlDocDumpMemory(get_doc(This), &mem, &size);
1743
1744     /*
1745      * libxml2 always adds XML declaration on top of the file and one for each processing instruction node in DOM tree.
1746      * MSXML adds XML declaration only for processing instruction nodes.
1747      * We skip the first XML declaration generated by libxml2 to get exactly what we need.
1748      */
1749     p = mem;
1750     if(size > 2 && p[0] == '<' && p[1] == '?') {
1751         while(p < mem+size && (p[0] != '?' || p[1] != '>'))
1752             p++;
1753         p += 2;
1754         while(p < mem+size && isspace(*p))
1755             p++;
1756         size -= p-mem;
1757     }
1758
1759     if(!WriteFile(handle, p, (DWORD)size, &written, NULL) || written != (DWORD)size)
1760     {
1761         WARN("write error\n");
1762         ret = S_FALSE;
1763     }
1764
1765     xmlFree(mem);
1766     CloseHandle(handle);
1767     return ret;
1768 }
1769
1770 static HRESULT WINAPI domdoc_get_validateOnParse(
1771     IXMLDOMDocument2 *iface,
1772     VARIANT_BOOL* isValidating )
1773 {
1774     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1775
1776     TRACE("%p <- %d\n", isValidating, This->validating);
1777     *isValidating = This->validating;
1778     return S_OK;
1779 }
1780
1781
1782 static HRESULT WINAPI domdoc_put_validateOnParse(
1783     IXMLDOMDocument2 *iface,
1784     VARIANT_BOOL isValidating )
1785 {
1786     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1787
1788     TRACE("%d\n", isValidating);
1789     This->validating = isValidating;
1790     return S_OK;
1791 }
1792
1793
1794 static HRESULT WINAPI domdoc_get_resolveExternals(
1795     IXMLDOMDocument2 *iface,
1796     VARIANT_BOOL* isResolving )
1797 {
1798     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1799
1800     TRACE("%p <- %d\n", isResolving, This->resolving);
1801     *isResolving = This->resolving;
1802     return S_OK;
1803 }
1804
1805
1806 static HRESULT WINAPI domdoc_put_resolveExternals(
1807     IXMLDOMDocument2 *iface,
1808     VARIANT_BOOL isResolving )
1809 {
1810     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1811
1812     TRACE("%d\n", isResolving);
1813     This->resolving = isResolving;
1814     return S_OK;
1815 }
1816
1817
1818 static HRESULT WINAPI domdoc_get_preserveWhiteSpace(
1819     IXMLDOMDocument2 *iface,
1820     VARIANT_BOOL* isPreserving )
1821 {
1822     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1823
1824     TRACE("%p <- %d\n", isPreserving, This->preserving);
1825     *isPreserving = This->preserving;
1826     return S_OK;
1827 }
1828
1829
1830 static HRESULT WINAPI domdoc_put_preserveWhiteSpace(
1831     IXMLDOMDocument2 *iface,
1832     VARIANT_BOOL isPreserving )
1833 {
1834     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1835
1836     TRACE("%d\n", isPreserving);
1837     This->preserving = isPreserving;
1838     return S_OK;
1839 }
1840
1841
1842 static HRESULT WINAPI domdoc_put_onReadyStateChange(
1843     IXMLDOMDocument2 *iface,
1844     VARIANT readyStateChangeSink )
1845 {
1846     FIXME("\n");
1847     return E_NOTIMPL;
1848 }
1849
1850
1851 static HRESULT WINAPI domdoc_put_onDataAvailable(
1852     IXMLDOMDocument2 *iface,
1853     VARIANT onDataAvailableSink )
1854 {
1855     FIXME("\n");
1856     return E_NOTIMPL;
1857 }
1858
1859 static HRESULT WINAPI domdoc_put_onTransformNode(
1860     IXMLDOMDocument2 *iface,
1861     VARIANT onTransformNodeSink )
1862 {
1863     FIXME("\n");
1864     return E_NOTIMPL;
1865 }
1866
1867 static HRESULT WINAPI domdoc_get_namespaces(
1868     IXMLDOMDocument2* iface,
1869     IXMLDOMSchemaCollection** schemaCollection )
1870 {
1871     FIXME("\n");
1872     return E_NOTIMPL;
1873 }
1874
1875 static HRESULT WINAPI domdoc_get_schemas(
1876     IXMLDOMDocument2* iface,
1877     VARIANT* var1 )
1878 {
1879     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1880     HRESULT hr = S_FALSE;
1881     IXMLDOMSchemaCollection *cur_schema = This->schema;
1882
1883     TRACE("(%p)->(%p)\n", This, var1);
1884
1885     VariantInit(var1); /* Test shows we don't call VariantClear here */
1886     V_VT(var1) = VT_NULL;
1887
1888     if(cur_schema)
1889     {
1890         hr = IXMLDOMSchemaCollection_QueryInterface(cur_schema, &IID_IDispatch, (void**)&V_DISPATCH(var1));
1891         if(SUCCEEDED(hr))
1892             V_VT(var1) = VT_DISPATCH;
1893     }
1894     return hr;
1895 }
1896
1897 static HRESULT WINAPI domdoc_putref_schemas(
1898     IXMLDOMDocument2* iface,
1899     VARIANT var1)
1900 {
1901     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1902     HRESULT hr = E_FAIL;
1903     IXMLDOMSchemaCollection *new_schema = NULL;
1904
1905     FIXME("(%p): semi-stub\n", This);
1906     switch(V_VT(&var1))
1907     {
1908     case VT_UNKNOWN:
1909         hr = IUnknown_QueryInterface(V_UNKNOWN(&var1), &IID_IXMLDOMSchemaCollection, (void**)&new_schema);
1910         break;
1911
1912     case VT_DISPATCH:
1913         hr = IDispatch_QueryInterface(V_DISPATCH(&var1), &IID_IXMLDOMSchemaCollection, (void**)&new_schema);
1914         break;
1915
1916     case VT_NULL:
1917     case VT_EMPTY:
1918         hr = S_OK;
1919         break;
1920
1921     default:
1922         WARN("Can't get schema from vt %x\n", V_VT(&var1));
1923     }
1924
1925     if(SUCCEEDED(hr))
1926     {
1927         IXMLDOMSchemaCollection *old_schema = InterlockedExchangePointer((void**)&This->schema, new_schema);
1928         if(old_schema) IXMLDOMSchemaCollection_Release(old_schema);
1929     }
1930
1931     return hr;
1932 }
1933
1934 static HRESULT WINAPI domdoc_validate(
1935     IXMLDOMDocument2* iface,
1936     IXMLDOMParseError** err)
1937 {
1938     FIXME("\n");
1939     return E_NOTIMPL;
1940 }
1941
1942 static HRESULT WINAPI domdoc_setProperty(
1943     IXMLDOMDocument2* iface,
1944     BSTR p,
1945     VARIANT var)
1946 {
1947     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1948
1949     if (lstrcmpiW(p, SZ_PROPERTY_SELECTION_LANGUAGE) == 0)
1950     {
1951         VARIANT varStr;
1952         HRESULT hr;
1953         BSTR bstr;
1954
1955         V_VT(&varStr) = VT_EMPTY;
1956         if (V_VT(&var) != VT_BSTR)
1957         {
1958             if (FAILED(hr = VariantChangeType(&varStr, &var, 0, VT_BSTR)))
1959                 return hr;
1960             bstr = V_BSTR(&varStr);
1961         }
1962         else
1963             bstr = V_BSTR(&var);
1964
1965         hr = S_OK;
1966         if (lstrcmpiW(bstr, SZ_VALUE_XPATH) == 0)
1967             This->bUseXPath = TRUE;
1968         else if (lstrcmpiW(bstr, SZ_VALUE_XSLPATTERN) == 0)
1969             This->bUseXPath = FALSE;
1970         else
1971             hr = E_FAIL;
1972
1973         VariantClear(&varStr);
1974         return hr;
1975     }
1976
1977     FIXME("Unknown property %s\n", wine_dbgstr_w(p));
1978     return E_FAIL;
1979 }
1980
1981 static HRESULT WINAPI domdoc_getProperty(
1982     IXMLDOMDocument2* iface,
1983     BSTR p,
1984     VARIANT* var)
1985 {
1986     domdoc *This = impl_from_IXMLDOMDocument2( iface );
1987
1988     if (var == NULL)
1989         return E_INVALIDARG;
1990     if (lstrcmpiW(p, SZ_PROPERTY_SELECTION_LANGUAGE) == 0)
1991     {
1992         V_VT(var) = VT_BSTR;
1993         if (This->bUseXPath)
1994             V_BSTR(var) = SysAllocString(SZ_VALUE_XPATH);
1995         else
1996             V_BSTR(var) = SysAllocString(SZ_VALUE_XSLPATTERN);
1997         return S_OK;
1998     }
1999
2000     FIXME("Unknown property %s\n", wine_dbgstr_w(p));
2001     return E_FAIL;
2002 }
2003
2004 static const struct IXMLDOMDocument2Vtbl domdoc_vtbl =
2005 {
2006     domdoc_QueryInterface,
2007     domdoc_AddRef,
2008     domdoc_Release,
2009     domdoc_GetTypeInfoCount,
2010     domdoc_GetTypeInfo,
2011     domdoc_GetIDsOfNames,
2012     domdoc_Invoke,
2013     domdoc_get_nodeName,
2014     domdoc_get_nodeValue,
2015     domdoc_put_nodeValue,
2016     domdoc_get_nodeType,
2017     domdoc_get_parentNode,
2018     domdoc_get_childNodes,
2019     domdoc_get_firstChild,
2020     domdoc_get_lastChild,
2021     domdoc_get_previousSibling,
2022     domdoc_get_nextSibling,
2023     domdoc_get_attributes,
2024     domdoc_insertBefore,
2025     domdoc_replaceChild,
2026     domdoc_removeChild,
2027     domdoc_appendChild,
2028     domdoc_hasChildNodes,
2029     domdoc_get_ownerDocument,
2030     domdoc_cloneNode,
2031     domdoc_get_nodeTypeString,
2032     domdoc_get_text,
2033     domdoc_put_text,
2034     domdoc_get_specified,
2035     domdoc_get_definition,
2036     domdoc_get_nodeTypedValue,
2037     domdoc_put_nodeTypedValue,
2038     domdoc_get_dataType,
2039     domdoc_put_dataType,
2040     domdoc_get_xml,
2041     domdoc_transformNode,
2042     domdoc_selectNodes,
2043     domdoc_selectSingleNode,
2044     domdoc_get_parsed,
2045     domdoc_get_namespaceURI,
2046     domdoc_get_prefix,
2047     domdoc_get_baseName,
2048     domdoc_transformNodeToObject,
2049     domdoc_get_doctype,
2050     domdoc_get_implementation,
2051     domdoc_get_documentElement,
2052     domdoc_put_documentElement,
2053     domdoc_createElement,
2054     domdoc_createDocumentFragment,
2055     domdoc_createTextNode,
2056     domdoc_createComment,
2057     domdoc_createCDATASection,
2058     domdoc_createProcessingInstruction,
2059     domdoc_createAttribute,
2060     domdoc_createEntityReference,
2061     domdoc_getElementsByTagName,
2062     domdoc_createNode,
2063     domdoc_nodeFromID,
2064     domdoc_load,
2065     domdoc_get_readyState,
2066     domdoc_get_parseError,
2067     domdoc_get_url,
2068     domdoc_get_async,
2069     domdoc_put_async,
2070     domdoc_abort,
2071     domdoc_loadXML,
2072     domdoc_save,
2073     domdoc_get_validateOnParse,
2074     domdoc_put_validateOnParse,
2075     domdoc_get_resolveExternals,
2076     domdoc_put_resolveExternals,
2077     domdoc_get_preserveWhiteSpace,
2078     domdoc_put_preserveWhiteSpace,
2079     domdoc_put_onReadyStateChange,
2080     domdoc_put_onDataAvailable,
2081     domdoc_put_onTransformNode,
2082     domdoc_get_namespaces,
2083     domdoc_get_schemas,
2084     domdoc_putref_schemas,
2085     domdoc_validate,
2086     domdoc_setProperty,
2087     domdoc_getProperty
2088 };
2089
2090 /* xmldoc implementation of IObjectWithSite */
2091 static HRESULT WINAPI
2092 xmldoc_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2093 {
2094     domdoc *This = impl_from_IObjectWithSite(iface);
2095     return IXMLDocument_QueryInterface( (IXMLDocument *)This, riid, ppvObject );
2096 }
2097
2098 static ULONG WINAPI
2099 xmldoc_ObjectWithSite_AddRef( IObjectWithSite* iface )
2100 {
2101     domdoc *This = impl_from_IObjectWithSite(iface);
2102     return IXMLDocument_AddRef((IXMLDocument *)This);
2103 }
2104
2105 static ULONG WINAPI
2106 xmldoc_ObjectWithSite_Release( IObjectWithSite* iface )
2107 {
2108     domdoc *This = impl_from_IObjectWithSite(iface);
2109     return IXMLDocument_Release((IXMLDocument *)This);
2110 }
2111
2112 static HRESULT WINAPI
2113 xmldoc_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2114 {
2115     domdoc *This = impl_from_IObjectWithSite(iface);
2116
2117     TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2118
2119     if ( !This->site )
2120         return E_FAIL;
2121
2122     return IUnknown_QueryInterface( This->site, iid, ppvSite );
2123 }
2124
2125 static HRESULT WINAPI
2126 xmldoc_SetSite( IObjectWithSite *iface, IUnknown *punk )
2127 {
2128     domdoc *This = impl_from_IObjectWithSite(iface);
2129
2130     TRACE("%p %p\n", iface, punk);
2131
2132     if(!punk)
2133     {
2134         if(This->site)
2135         {
2136             IUnknown_Release( This->site );
2137             This->site = NULL;
2138         }
2139
2140         return S_OK;
2141     }
2142
2143     if ( punk )
2144         IUnknown_AddRef( punk );
2145
2146     if(This->site)
2147         IUnknown_Release( This->site );
2148
2149     This->site = punk;
2150
2151     return S_OK;
2152 }
2153
2154 static const IObjectWithSiteVtbl domdocObjectSite =
2155 {
2156     xmldoc_ObjectWithSite_QueryInterface,
2157     xmldoc_ObjectWithSite_AddRef,
2158     xmldoc_ObjectWithSite_Release,
2159     xmldoc_SetSite,
2160     xmldoc_GetSite,
2161 };
2162
2163 static HRESULT WINAPI xmldoc_Safety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
2164 {
2165     domdoc *This = impl_from_IObjectSafety(iface);
2166     return IXMLDocument_QueryInterface( (IXMLDocument *)This, riid, ppv );
2167 }
2168
2169 static ULONG WINAPI xmldoc_Safety_AddRef(IObjectSafety *iface)
2170 {
2171     domdoc *This = impl_from_IObjectSafety(iface);
2172     return IXMLDocument_AddRef((IXMLDocument *)This);
2173 }
2174
2175 static ULONG WINAPI xmldoc_Safety_Release(IObjectSafety *iface)
2176 {
2177     domdoc *This = impl_from_IObjectSafety(iface);
2178     return IXMLDocument_Release((IXMLDocument *)This);
2179 }
2180
2181 #define SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_SECURITY_MANAGER)
2182
2183 static HRESULT WINAPI xmldoc_Safety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
2184         DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
2185 {
2186     domdoc *This = impl_from_IObjectSafety(iface);
2187
2188     TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), pdwSupportedOptions, pdwEnabledOptions);
2189
2190     if(!pdwSupportedOptions || !pdwEnabledOptions)
2191         return E_POINTER;
2192
2193     *pdwSupportedOptions = SUPPORTED_OPTIONS;
2194     *pdwEnabledOptions = This->safeopt;
2195
2196     return S_OK;
2197 }
2198
2199 static HRESULT WINAPI xmldoc_Safety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
2200         DWORD dwOptionSetMask, DWORD dwEnabledOptions)
2201 {
2202     domdoc *This = impl_from_IObjectSafety(iface);
2203
2204     TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), dwOptionSetMask, dwEnabledOptions);
2205
2206     if(dwOptionSetMask & ~SUPPORTED_OPTIONS)
2207         return E_FAIL;
2208
2209     This->safeopt = dwEnabledOptions & dwEnabledOptions;
2210     return S_OK;
2211 }
2212
2213 static const IObjectSafetyVtbl domdocObjectSafetyVtbl = {
2214     xmldoc_Safety_QueryInterface,
2215     xmldoc_Safety_AddRef,
2216     xmldoc_Safety_Release,
2217     xmldoc_Safety_GetInterfaceSafetyOptions,
2218     xmldoc_Safety_SetInterfaceSafetyOptions
2219 };
2220
2221 HRESULT DOMDocument_create_from_xmldoc(xmlDocPtr xmldoc, IXMLDOMDocument2 **document)
2222 {
2223     domdoc *doc;
2224     HRESULT hr;
2225
2226     doc = HeapAlloc( GetProcessHeap(), 0, sizeof (*doc) );
2227     if( !doc )
2228         return E_OUTOFMEMORY;
2229
2230     doc->lpVtbl = &domdoc_vtbl;
2231     doc->lpvtblIPersistStream = &xmldoc_IPersistStream_VTable;
2232     doc->lpvtblIObjectWithSite = &domdocObjectSite;
2233     doc->lpvtblIObjectSafety = &domdocObjectSafetyVtbl;
2234     doc->ref = 1;
2235     doc->async = 0;
2236     doc->validating = 0;
2237     doc->resolving = 0;
2238     doc->preserving = 0;
2239     doc->bUseXPath = FALSE;
2240     doc->error = S_OK;
2241     doc->schema = NULL;
2242     doc->stream = NULL;
2243     doc->site = NULL;
2244     doc->safeopt = 0;
2245     doc->bsc = NULL;
2246
2247     doc->node_unk = create_basic_node( (xmlNodePtr)xmldoc, (IUnknown*)&doc->lpVtbl );
2248     if(!doc->node_unk)
2249     {
2250         HeapFree(GetProcessHeap(), 0, doc);
2251         return E_FAIL;
2252     }
2253
2254     hr = IUnknown_QueryInterface(doc->node_unk, &IID_IXMLDOMNode, (LPVOID*)&doc->node);
2255     if(FAILED(hr))
2256     {
2257         IUnknown_Release(doc->node_unk);
2258         HeapFree( GetProcessHeap(), 0, doc );
2259         return E_FAIL;
2260     }
2261     /* The ref on doc->node is actually looped back into this object, so release it */
2262     IXMLDOMNode_Release(doc->node);
2263
2264     *document = (IXMLDOMDocument2*)&doc->lpVtbl;
2265
2266     TRACE("returning iface %p\n", *document);
2267     return S_OK;
2268 }
2269
2270 HRESULT DOMDocument_create(IUnknown *pUnkOuter, LPVOID *ppObj)
2271 {
2272     xmlDocPtr xmldoc;
2273     HRESULT hr;
2274
2275     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
2276
2277     xmldoc = xmlNewDoc(NULL);
2278     if(!xmldoc)
2279         return E_OUTOFMEMORY;
2280
2281     xmldoc->_private = 0;
2282
2283     hr = DOMDocument_create_from_xmldoc(xmldoc, (IXMLDOMDocument2**)ppObj);
2284     if(FAILED(hr))
2285         xmlFreeDoc(xmldoc);
2286
2287     return hr;
2288 }
2289
2290 IUnknown* create_domdoc( xmlNodePtr document )
2291 {
2292     HRESULT hr;
2293     LPVOID pObj = NULL;
2294
2295     TRACE("(%p)\n", document);
2296
2297     hr = DOMDocument_create_from_xmldoc((xmlDocPtr)document, (IXMLDOMDocument2**)&pObj);
2298     if (FAILED(hr))
2299         return NULL;
2300
2301     return (IUnknown*)pObj;
2302 }
2303
2304 #else
2305
2306 HRESULT DOMDocument_create(IUnknown *pUnkOuter, LPVOID *ppObj)
2307 {
2308     MESSAGE("This program tried to use a DOMDocument object, but\n"
2309             "libxml2 support was not present at compile time.\n");
2310     return E_NOTIMPL;
2311 }
2312
2313 #endif