2 * XML Document implementation
4 * Copyright 2007 James Hawkins
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 # include <libxml/parser.h>
28 # include <libxml/xmlerror.h>
41 #include "wine/debug.h"
43 #include "msxml_private.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(msxml
);
49 /* FIXME: IXMLDocument needs to implement
54 typedef struct _xmldoc
56 IXMLDocument IXMLDocument_iface
;
57 IPersistStreamInit IPersistStreamInit_iface
;
68 static inline xmldoc
*impl_from_IXMLDocument(IXMLDocument
*iface
)
70 return CONTAINING_RECORD(iface
, xmldoc
, IXMLDocument_iface
);
73 static inline xmldoc
*impl_from_IPersistStreamInit(IPersistStreamInit
*iface
)
75 return CONTAINING_RECORD(iface
, xmldoc
, IPersistStreamInit_iface
);
78 static HRESULT WINAPI
xmldoc_QueryInterface(IXMLDocument
*iface
, REFIID riid
, void** ppvObject
)
80 xmldoc
*This
= impl_from_IXMLDocument(iface
);
82 TRACE("%p %s %p\n", This
, debugstr_guid(riid
), ppvObject
);
84 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
85 IsEqualGUID(riid
, &IID_IDispatch
) ||
86 IsEqualGUID(riid
, &IID_IXMLDocument
))
90 else if (IsEqualGUID(&IID_IPersistStreamInit
, riid
) ||
91 IsEqualGUID(&IID_IPersistStream
, riid
))
93 *ppvObject
= &This
->IPersistStreamInit_iface
;
97 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
102 IXMLDocument_AddRef(iface
);
107 static ULONG WINAPI
xmldoc_AddRef(IXMLDocument
*iface
)
109 xmldoc
*This
= impl_from_IXMLDocument(iface
);
110 ULONG ref
= InterlockedIncrement(&This
->ref
);
111 TRACE("(%p)->(%d)\n", This
, ref
);
115 static ULONG WINAPI
xmldoc_Release(IXMLDocument
*iface
)
117 xmldoc
*This
= impl_from_IXMLDocument(iface
);
118 LONG ref
= InterlockedDecrement(&This
->ref
);
120 TRACE("(%p)->(%d)\n", This
, ref
);
124 xmlFreeDoc(This
->xmldoc
);
125 if (This
->stream
) IStream_Release(This
->stream
);
132 static HRESULT WINAPI
xmldoc_GetTypeInfoCount(IXMLDocument
*iface
, UINT
* pctinfo
)
134 xmldoc
*This
= impl_from_IXMLDocument(iface
);
136 TRACE("(%p)->(%p)\n", This
, pctinfo
);
143 static HRESULT WINAPI
xmldoc_GetTypeInfo(IXMLDocument
*iface
, UINT iTInfo
,
144 LCID lcid
, ITypeInfo
** ppTInfo
)
146 xmldoc
*This
= impl_from_IXMLDocument(iface
);
148 TRACE("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
150 return get_typeinfo(IXMLDocument_tid
, ppTInfo
);
153 static HRESULT WINAPI
xmldoc_GetIDsOfNames(IXMLDocument
*iface
, REFIID riid
,
154 LPOLESTR
* rgszNames
, UINT cNames
,
155 LCID lcid
, DISPID
* rgDispId
)
157 xmldoc
*This
= impl_from_IXMLDocument(iface
);
161 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
164 if(!rgszNames
|| cNames
== 0 || !rgDispId
)
167 hr
= get_typeinfo(IXMLDocument_tid
, &typeinfo
);
170 hr
= ITypeInfo_GetIDsOfNames(typeinfo
, rgszNames
, cNames
, rgDispId
);
171 ITypeInfo_Release(typeinfo
);
177 static HRESULT WINAPI
xmldoc_Invoke(IXMLDocument
*iface
, DISPID dispIdMember
,
178 REFIID riid
, LCID lcid
, WORD wFlags
,
179 DISPPARAMS
* pDispParams
, VARIANT
* pVarResult
,
180 EXCEPINFO
* pExcepInfo
, UINT
* puArgErr
)
182 xmldoc
*This
= impl_from_IXMLDocument(iface
);
186 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
187 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
189 hr
= get_typeinfo(IXMLDocument_tid
, &typeinfo
);
192 hr
= ITypeInfo_Invoke(typeinfo
, &This
->IXMLDocument_iface
, dispIdMember
, wFlags
,
193 pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
194 ITypeInfo_Release(typeinfo
);
200 static HRESULT WINAPI
xmldoc_get_root(IXMLDocument
*iface
, IXMLElement
**p
)
202 xmldoc
*This
= impl_from_IXMLDocument(iface
);
205 TRACE("(%p, %p)\n", iface
, p
);
212 if (!(root
= xmlDocGetRootElement(This
->xmldoc
)))
215 return XMLElement_create(root
, (LPVOID
*)p
, FALSE
);
218 static HRESULT WINAPI
xmldoc_get_fileSize(IXMLDocument
*iface
, BSTR
*p
)
220 FIXME("(%p, %p): stub\n", iface
, p
);
224 static HRESULT WINAPI
xmldoc_put_fileModifiedDate(IXMLDocument
*iface
, BSTR
*p
)
226 FIXME("(%p, %p): stub\n", iface
, p
);
230 static HRESULT WINAPI
xmldoc_get_fileUpdatedDate(IXMLDocument
*iface
, BSTR
*p
)
232 FIXME("(%p, %p): stub\n", iface
, p
);
236 static HRESULT WINAPI
xmldoc_get_URL(IXMLDocument
*iface
, BSTR
*p
)
238 FIXME("(%p, %p): stub\n", iface
, p
);
243 IBindStatusCallback IBindStatusCallback_iface
;
246 static HRESULT WINAPI
bsc_QueryInterface(
247 IBindStatusCallback
*iface
,
251 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
252 IsEqualGUID(riid
, &IID_IBindStatusCallback
))
254 IBindStatusCallback_AddRef( iface
);
259 TRACE("interface %s not implemented\n", debugstr_guid(riid
));
260 return E_NOINTERFACE
;
263 static ULONG WINAPI
bsc_AddRef(
264 IBindStatusCallback
*iface
)
269 static ULONG WINAPI
bsc_Release(
270 IBindStatusCallback
*iface
)
275 static HRESULT WINAPI
bsc_OnStartBinding(
276 IBindStatusCallback
* iface
,
283 static HRESULT WINAPI
bsc_GetPriority(
284 IBindStatusCallback
* iface
,
290 static HRESULT WINAPI
bsc_OnLowResource(
291 IBindStatusCallback
* iface
,
297 static HRESULT WINAPI
bsc_OnProgress(
298 IBindStatusCallback
* iface
,
302 LPCWSTR szStatusText
)
307 static HRESULT WINAPI
bsc_OnStopBinding(
308 IBindStatusCallback
* iface
,
315 static HRESULT WINAPI
bsc_GetBindInfo(
316 IBindStatusCallback
* iface
,
320 *grfBINDF
= BINDF_RESYNCHRONIZE
;
325 static HRESULT WINAPI
bsc_OnDataAvailable(
326 IBindStatusCallback
* iface
,
329 FORMATETC
* pformatetc
,
335 static HRESULT WINAPI
bsc_OnObjectAvailable(
336 IBindStatusCallback
* iface
,
343 static const struct IBindStatusCallbackVtbl bsc_vtbl
=
355 bsc_OnObjectAvailable
358 static bsc xmldoc_bsc
= { { &bsc_vtbl
} };
360 static HRESULT WINAPI
xmldoc_put_URL(IXMLDocument
*iface
, BSTR p
)
362 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
366 IPersistStreamInit
*persist
;
369 TRACE("(%p, %s)\n", iface
, debugstr_w(p
));
376 WCHAR fullpath
[MAX_PATH
];
377 DWORD needed
= sizeof(url
) / sizeof(WCHAR
);
379 if (!PathSearchAndQualifyW(p
, fullpath
, sizeof(fullpath
) / sizeof(WCHAR
)))
381 ERR("can't find path\n");
385 if (FAILED(UrlCreateFromPathW(fullpath
, url
, &needed
, 0)))
387 ERR("can't create url from path\n");
394 hr
= CreateURLMoniker(NULL
, p
, &moniker
);
398 CreateAsyncBindCtx(0, &xmldoc_bsc
.IBindStatusCallback_iface
, 0, &bctx
);
400 hr
= IMoniker_BindToStorage(moniker
, bctx
, NULL
, &IID_IStream
, (LPVOID
*)&stream
);
401 IBindCtx_Release(bctx
);
402 IMoniker_Release(moniker
);
406 hr
= IXMLDocument_QueryInterface(iface
, &IID_IPersistStreamInit
, (LPVOID
*)&persist
);
409 IStream_Release(stream
);
413 hr
= IPersistStreamInit_Load(persist
, stream
);
414 IPersistStreamInit_Release(persist
);
415 IStream_Release(stream
);
420 static HRESULT WINAPI
xmldoc_get_mimeType(IXMLDocument
*iface
, BSTR
*p
)
422 FIXME("(%p, %p): stub\n", iface
, p
);
426 static HRESULT WINAPI
xmldoc_get_readyState(IXMLDocument
*iface
, LONG
*p
)
428 FIXME("(%p, %p): stub\n", iface
, p
);
432 static HRESULT WINAPI
xmldoc_get_charset(IXMLDocument
*iface
, BSTR
*p
)
434 FIXME("(%p, %p): stub\n", iface
, p
);
438 static HRESULT WINAPI
xmldoc_put_charset(IXMLDocument
*iface
, BSTR p
)
440 FIXME("(%p, %p): stub\n", iface
, p
);
444 static HRESULT WINAPI
xmldoc_get_version(IXMLDocument
*iface
, BSTR
*p
)
446 xmldoc
*This
= impl_from_IXMLDocument(iface
);
448 TRACE("(%p, %p)\n", This
, p
);
450 if (!p
) return E_INVALIDARG
;
451 *p
= bstr_from_xmlChar(This
->xmldoc
->version
);
456 static HRESULT WINAPI
xmldoc_get_doctype(IXMLDocument
*iface
, BSTR
*p
)
458 xmldoc
*This
= impl_from_IXMLDocument(iface
);
461 TRACE("(%p, %p)\n", This
, p
);
463 if (!p
) return E_INVALIDARG
;
465 dtd
= xmlGetIntSubset(This
->xmldoc
);
466 if (!dtd
) return S_FALSE
;
468 *p
= bstr_from_xmlChar(dtd
->name
);
469 CharUpperBuffW(*p
, SysStringLen(*p
));
474 static HRESULT WINAPI
xmldoc_get_dtdURl(IXMLDocument
*iface
, BSTR
*p
)
476 FIXME("(%p, %p): stub\n", iface
, p
);
480 static xmlElementType
type_msxml_to_libxml(LONG type
)
484 case XMLELEMTYPE_ELEMENT
:
485 return XML_ELEMENT_NODE
;
486 case XMLELEMTYPE_TEXT
:
487 return XML_TEXT_NODE
;
488 case XMLELEMTYPE_COMMENT
:
489 return XML_COMMENT_NODE
;
490 case XMLELEMTYPE_DOCUMENT
:
491 return XML_DOCUMENT_NODE
;
492 case XMLELEMTYPE_DTD
:
500 return -1; /* FIXME: what is OTHER in msxml? */
503 static HRESULT WINAPI
xmldoc_createElement(IXMLDocument
*iface
, VARIANT vType
,
504 VARIANT var1
, IXMLElement
**ppElem
)
507 static const xmlChar empty
[] = "\0";
509 TRACE("(%p)->(%s %s %p)\n", iface
, debugstr_variant(&vType
),
510 debugstr_variant(&var1
), ppElem
);
517 if (V_VT(&vType
) != VT_I4
)
520 if(type_msxml_to_libxml(V_I4(&vType
)) == -1)
523 node
= xmlNewNode(NULL
, empty
);
524 node
->type
= type_msxml_to_libxml(V_I4(&vType
));
526 /* FIXME: create xmlNodePtr based on vType and var1 */
527 return XMLElement_create(node
, (LPVOID
*)ppElem
, TRUE
);
530 static const struct IXMLDocumentVtbl xmldoc_vtbl
=
532 xmldoc_QueryInterface
,
535 xmldoc_GetTypeInfoCount
,
537 xmldoc_GetIDsOfNames
,
541 xmldoc_put_fileModifiedDate
,
542 xmldoc_get_fileUpdatedDate
,
546 xmldoc_get_readyState
,
555 /************************************************************************
556 * xmldoc implementation of IPersistStreamInit.
558 static HRESULT WINAPI
xmldoc_IPersistStreamInit_QueryInterface(
559 IPersistStreamInit
*iface
, REFIID riid
, LPVOID
*ppvObj
)
561 xmldoc
*this = impl_from_IPersistStreamInit(iface
);
562 return IXMLDocument_QueryInterface(&this->IXMLDocument_iface
, riid
, ppvObj
);
565 static ULONG WINAPI
xmldoc_IPersistStreamInit_AddRef(
566 IPersistStreamInit
*iface
)
568 xmldoc
*this = impl_from_IPersistStreamInit(iface
);
569 return IXMLDocument_AddRef(&this->IXMLDocument_iface
);
572 static ULONG WINAPI
xmldoc_IPersistStreamInit_Release(
573 IPersistStreamInit
*iface
)
575 xmldoc
*this = impl_from_IPersistStreamInit(iface
);
576 return IXMLDocument_Release(&this->IXMLDocument_iface
);
579 static HRESULT WINAPI
xmldoc_IPersistStreamInit_GetClassID(
580 IPersistStreamInit
*iface
, CLSID
*classid
)
582 xmldoc
*this = impl_from_IPersistStreamInit(iface
);
583 TRACE("(%p,%p)\n", this, classid
);
585 if (!classid
) return E_POINTER
;
587 *classid
= CLSID_XMLDocument
;
591 static HRESULT WINAPI
xmldoc_IPersistStreamInit_IsDirty(
592 IPersistStreamInit
*iface
)
594 FIXME("(%p): stub!\n", iface
);
598 static xmlDocPtr
parse_xml(char *ptr
, int len
)
600 #ifdef HAVE_XMLREADMEMORY
601 return xmlReadMemory(ptr
, len
, NULL
, NULL
,
602 XML_PARSE_NOERROR
| XML_PARSE_NOWARNING
| XML_PARSE_NOBLANKS
);
604 return xmlParseMemory(ptr
, len
);
608 static HRESULT WINAPI
xmldoc_IPersistStreamInit_Load(
609 IPersistStreamInit
*iface
, LPSTREAM pStm
)
611 xmldoc
*This
= impl_from_IPersistStreamInit(iface
);
614 DWORD read
, written
, len
;
618 TRACE("(%p, %p)\n", iface
, pStm
);
623 /* release previously allocated stream */
624 if (This
->stream
) IStream_Release(This
->stream
);
625 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &This
->stream
);
631 IStream_Read(pStm
, buf
, sizeof(buf
), &read
);
632 hr
= IStream_Write(This
->stream
, buf
, read
, &written
);
633 } while(SUCCEEDED(hr
) && written
!= 0 && read
!= 0);
637 ERR("Failed to copy stream\n");
641 hr
= GetHGlobalFromStream(This
->stream
, &hglobal
);
645 len
= GlobalSize(hglobal
);
646 ptr
= GlobalLock(hglobal
);
649 xmlFreeDoc(This
->xmldoc
);
650 This
->xmldoc
= parse_xml(ptr
, len
);
652 GlobalUnlock(hglobal
);
656 ERR("Failed to parse xml\n");
663 static HRESULT WINAPI
xmldoc_IPersistStreamInit_Save(
664 IPersistStreamInit
*iface
, LPSTREAM pStm
, BOOL fClearDirty
)
666 FIXME("(%p, %p, %d): stub!\n", iface
, pStm
, fClearDirty
);
670 static HRESULT WINAPI
xmldoc_IPersistStreamInit_GetSizeMax(
671 IPersistStreamInit
*iface
, ULARGE_INTEGER
*pcbSize
)
673 xmldoc
*This
= impl_from_IPersistStreamInit(iface
);
674 TRACE("(%p, %p)\n", This
, pcbSize
);
678 static HRESULT WINAPI
xmldoc_IPersistStreamInit_InitNew(
679 IPersistStreamInit
*iface
)
681 xmldoc
*This
= impl_from_IPersistStreamInit(iface
);
682 TRACE("(%p)\n", This
);
686 static const IPersistStreamInitVtbl xmldoc_IPersistStreamInit_VTable
=
688 xmldoc_IPersistStreamInit_QueryInterface
,
689 xmldoc_IPersistStreamInit_AddRef
,
690 xmldoc_IPersistStreamInit_Release
,
691 xmldoc_IPersistStreamInit_GetClassID
,
692 xmldoc_IPersistStreamInit_IsDirty
,
693 xmldoc_IPersistStreamInit_Load
,
694 xmldoc_IPersistStreamInit_Save
,
695 xmldoc_IPersistStreamInit_GetSizeMax
,
696 xmldoc_IPersistStreamInit_InitNew
699 HRESULT
XMLDocument_create(LPVOID
*ppObj
)
703 TRACE("(%p)\n", ppObj
);
705 doc
= heap_alloc(sizeof (*doc
));
707 return E_OUTOFMEMORY
;
709 doc
->IXMLDocument_iface
.lpVtbl
= &xmldoc_vtbl
;
710 doc
->IPersistStreamInit_iface
.lpVtbl
= &xmldoc_IPersistStreamInit_VTable
;
716 *ppObj
= &doc
->IXMLDocument_iface
;
718 TRACE("returning iface %p\n", *ppObj
);
724 HRESULT
XMLDocument_create(LPVOID
*ppObj
)
726 MESSAGE("This program tried to use an XMLDocument object, but\n"
727 "libxml2 support was not present at compile time.\n");