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_IXMLDocument) ||
86 IsEqualGUID(riid, &IID_IXMLDOMDocument))
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);
111 return InterlockedIncrement(&This->ref);
114 static ULONG WINAPI xmldoc_Release(IXMLDocument *iface)
116 xmldoc *This = impl_from_IXMLDocument(iface);
121 ref = InterlockedDecrement(&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);
149 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
151 hr = get_typeinfo(IXMLDocument_tid, ppTInfo);
156 static HRESULT WINAPI xmldoc_GetIDsOfNames(IXMLDocument *iface, REFIID riid,
157 LPOLESTR* rgszNames, UINT cNames,
158 LCID lcid, DISPID* rgDispId)
160 xmldoc *This = impl_from_IXMLDocument(iface);
164 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
167 if(!rgszNames || cNames == 0 || !rgDispId)
170 hr = get_typeinfo(IXMLDocument_tid, &typeinfo);
173 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
174 ITypeInfo_Release(typeinfo);
180 static HRESULT WINAPI xmldoc_Invoke(IXMLDocument *iface, DISPID dispIdMember,
181 REFIID riid, LCID lcid, WORD wFlags,
182 DISPPARAMS* pDispParams, VARIANT* pVarResult,
183 EXCEPINFO* pExcepInfo, UINT* puArgErr)
185 xmldoc *This = impl_from_IXMLDocument(iface);
189 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
190 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
192 hr = get_typeinfo(IXMLDocument_tid, &typeinfo);
195 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLDocument_iface, dispIdMember, wFlags,
196 pDispParams, pVarResult, pExcepInfo, puArgErr);
197 ITypeInfo_Release(typeinfo);
203 static HRESULT WINAPI xmldoc_get_root(IXMLDocument *iface, IXMLElement **p)
205 xmldoc *This = impl_from_IXMLDocument(iface);
208 TRACE("(%p, %p)\n", iface, p);
215 if (!(root = xmlDocGetRootElement(This->xmldoc)))
218 return XMLElement_create((IUnknown *)This, root, (LPVOID *)p, FALSE);
221 static HRESULT WINAPI xmldoc_get_fileSize(IXMLDocument *iface, BSTR *p)
223 FIXME("(%p, %p): stub\n", iface, p);
227 static HRESULT WINAPI xmldoc_put_fileModifiedDate(IXMLDocument *iface, BSTR *p)
229 FIXME("(%p, %p): stub\n", iface, p);
233 static HRESULT WINAPI xmldoc_get_fileUpdatedDate(IXMLDocument *iface, BSTR *p)
235 FIXME("(%p, %p): stub\n", iface, p);
239 static HRESULT WINAPI xmldoc_get_URL(IXMLDocument *iface, BSTR *p)
241 FIXME("(%p, %p): stub\n", iface, p);
246 IBindStatusCallback IBindStatusCallback_iface;
249 static HRESULT WINAPI bsc_QueryInterface(
250 IBindStatusCallback *iface,
254 if (IsEqualGUID(riid, &IID_IUnknown) ||
255 IsEqualGUID(riid, &IID_IBindStatusCallback))
257 IBindStatusCallback_AddRef( iface );
262 TRACE("interface %s not implemented\n", debugstr_guid(riid));
263 return E_NOINTERFACE;
266 static ULONG WINAPI bsc_AddRef(
267 IBindStatusCallback *iface )
272 static ULONG WINAPI bsc_Release(
273 IBindStatusCallback *iface )
278 static HRESULT WINAPI bsc_OnStartBinding(
279 IBindStatusCallback* iface,
286 static HRESULT WINAPI bsc_GetPriority(
287 IBindStatusCallback* iface,
293 static HRESULT WINAPI bsc_OnLowResource(
294 IBindStatusCallback* iface,
300 static HRESULT WINAPI bsc_OnProgress(
301 IBindStatusCallback* iface,
305 LPCWSTR szStatusText)
310 static HRESULT WINAPI bsc_OnStopBinding(
311 IBindStatusCallback* iface,
318 static HRESULT WINAPI bsc_GetBindInfo(
319 IBindStatusCallback* iface,
323 *grfBINDF = BINDF_RESYNCHRONIZE;
328 static HRESULT WINAPI bsc_OnDataAvailable(
329 IBindStatusCallback* iface,
332 FORMATETC* pformatetc,
338 static HRESULT WINAPI bsc_OnObjectAvailable(
339 IBindStatusCallback* iface,
346 static const struct IBindStatusCallbackVtbl bsc_vtbl =
358 bsc_OnObjectAvailable
361 static bsc xmldoc_bsc = { { &bsc_vtbl } };
363 static HRESULT WINAPI xmldoc_put_URL(IXMLDocument *iface, BSTR p)
365 WCHAR url[INTERNET_MAX_URL_LENGTH];
369 IPersistStreamInit *persist;
372 TRACE("(%p, %s)\n", iface, debugstr_w(p));
379 WCHAR fullpath[MAX_PATH];
380 DWORD needed = sizeof(url) / sizeof(WCHAR);
382 if (!PathSearchAndQualifyW(p, fullpath, sizeof(fullpath) / sizeof(WCHAR)))
384 ERR("can't find path\n");
388 if (FAILED(UrlCreateFromPathW(fullpath, url, &needed, 0)))
390 ERR("can't create url from path\n");
397 hr = CreateURLMoniker(NULL, p, &moniker);
401 CreateAsyncBindCtx(0, &xmldoc_bsc.IBindStatusCallback_iface, 0, &bctx);
403 hr = IMoniker_BindToStorage(moniker, bctx, NULL, &IID_IStream, (LPVOID *)&stream);
404 IBindCtx_Release(bctx);
405 IMoniker_Release(moniker);
409 hr = IXMLDocument_QueryInterface(iface, &IID_IPersistStreamInit, (LPVOID *)&persist);
412 IStream_Release(stream);
416 hr = IPersistStreamInit_Load(persist, stream);
417 IPersistStreamInit_Release(persist);
418 IStream_Release(stream);
423 static HRESULT WINAPI xmldoc_get_mimeType(IXMLDocument *iface, BSTR *p)
425 FIXME("(%p, %p): stub\n", iface, p);
429 static HRESULT WINAPI xmldoc_get_readyState(IXMLDocument *iface, LONG *p)
431 FIXME("(%p, %p): stub\n", iface, p);
435 static HRESULT WINAPI xmldoc_get_charset(IXMLDocument *iface, BSTR *p)
437 FIXME("(%p, %p): stub\n", iface, p);
441 static HRESULT WINAPI xmldoc_put_charset(IXMLDocument *iface, BSTR p)
443 FIXME("(%p, %p): stub\n", iface, p);
447 static HRESULT WINAPI xmldoc_get_version(IXMLDocument *iface, BSTR *p)
449 xmldoc *This = impl_from_IXMLDocument(iface);
451 TRACE("(%p, %p)\n", This, p);
453 if (!p) return E_INVALIDARG;
454 *p = bstr_from_xmlChar(This->xmldoc->version);
459 static HRESULT WINAPI xmldoc_get_doctype(IXMLDocument *iface, BSTR *p)
461 xmldoc *This = impl_from_IXMLDocument(iface);
464 TRACE("(%p, %p)\n", This, p);
466 if (!p) return E_INVALIDARG;
468 dtd = xmlGetIntSubset(This->xmldoc);
469 if (!dtd) return S_FALSE;
471 *p = bstr_from_xmlChar(dtd->name);
472 CharUpperBuffW(*p, SysStringLen(*p));
477 static HRESULT WINAPI xmldoc_get_dtdURl(IXMLDocument *iface, BSTR *p)
479 FIXME("(%p, %p): stub\n", iface, p);
483 static xmlElementType type_msxml_to_libxml(LONG type)
487 case XMLELEMTYPE_ELEMENT:
488 return XML_ELEMENT_NODE;
489 case XMLELEMTYPE_TEXT:
490 return XML_TEXT_NODE;
491 case XMLELEMTYPE_COMMENT:
492 return XML_COMMENT_NODE;
493 case XMLELEMTYPE_DOCUMENT:
494 return XML_DOCUMENT_NODE;
495 case XMLELEMTYPE_DTD:
503 return -1; /* FIXME: what is OTHER in msxml? */
506 static HRESULT WINAPI xmldoc_createElement(IXMLDocument *iface, VARIANT vType,
507 VARIANT var1, IXMLElement **ppElem)
510 static const xmlChar empty[] = "\0";
512 TRACE("(%p)->(%s %s %p)\n", iface, debugstr_variant(&vType),
513 debugstr_variant(&var1), ppElem);
520 if (V_VT(&vType) != VT_I4)
523 if(type_msxml_to_libxml(V_I4(&vType)) == -1)
526 node = xmlNewNode(NULL, empty);
527 node->type = type_msxml_to_libxml(V_I4(&vType));
529 /* FIXME: create xmlNodePtr based on vType and var1 */
530 return XMLElement_create((IUnknown *)iface, node, (LPVOID *)ppElem, TRUE);
533 static const struct IXMLDocumentVtbl xmldoc_vtbl =
535 xmldoc_QueryInterface,
538 xmldoc_GetTypeInfoCount,
540 xmldoc_GetIDsOfNames,
544 xmldoc_put_fileModifiedDate,
545 xmldoc_get_fileUpdatedDate,
549 xmldoc_get_readyState,
558 /************************************************************************
559 * xmldoc implementation of IPersistStreamInit.
561 static HRESULT WINAPI xmldoc_IPersistStreamInit_QueryInterface(
562 IPersistStreamInit *iface, REFIID riid, LPVOID *ppvObj)
564 xmldoc *this = impl_from_IPersistStreamInit(iface);
565 return IXMLDocument_QueryInterface(&this->IXMLDocument_iface, riid, ppvObj);
568 static ULONG WINAPI xmldoc_IPersistStreamInit_AddRef(
569 IPersistStreamInit *iface)
571 xmldoc *this = impl_from_IPersistStreamInit(iface);
572 return IXMLDocument_AddRef(&this->IXMLDocument_iface);
575 static ULONG WINAPI xmldoc_IPersistStreamInit_Release(
576 IPersistStreamInit *iface)
578 xmldoc *this = impl_from_IPersistStreamInit(iface);
579 return IXMLDocument_Release(&this->IXMLDocument_iface);
582 static HRESULT WINAPI xmldoc_IPersistStreamInit_GetClassID(
583 IPersistStreamInit *iface, CLSID *classid)
585 xmldoc *this = impl_from_IPersistStreamInit(iface);
586 TRACE("(%p,%p)\n", this, classid);
588 if (!classid) return E_POINTER;
590 *classid = CLSID_XMLDocument;
594 static HRESULT WINAPI xmldoc_IPersistStreamInit_IsDirty(
595 IPersistStreamInit *iface)
597 FIXME("(%p): stub!\n", iface);
601 static xmlDocPtr parse_xml(char *ptr, int len)
603 #ifdef HAVE_XMLREADMEMORY
604 return xmlReadMemory(ptr, len, NULL, NULL,
605 XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NOBLANKS);
607 return xmlParseMemory(ptr, len);
611 static HRESULT WINAPI xmldoc_IPersistStreamInit_Load(
612 IPersistStreamInit *iface, LPSTREAM pStm)
614 xmldoc *This = impl_from_IPersistStreamInit(iface);
617 DWORD read, written, len;
621 TRACE("(%p, %p)\n", iface, pStm);
626 /* release previously allocated stream */
627 if (This->stream) IStream_Release(This->stream);
628 hr = CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
634 IStream_Read(pStm, buf, sizeof(buf), &read);
635 hr = IStream_Write(This->stream, buf, read, &written);
636 } while(SUCCEEDED(hr) && written != 0 && read != 0);
640 ERR("Failed to copy stream\n");
644 hr = GetHGlobalFromStream(This->stream, &hglobal);
648 len = GlobalSize(hglobal);
649 ptr = GlobalLock(hglobal);
652 xmlFreeDoc(This->xmldoc);
653 This->xmldoc = parse_xml(ptr, len);
655 GlobalUnlock(hglobal);
659 ERR("Failed to parse xml\n");
666 static HRESULT WINAPI xmldoc_IPersistStreamInit_Save(
667 IPersistStreamInit *iface, LPSTREAM pStm, BOOL fClearDirty)
669 FIXME("(%p, %p, %d): stub!\n", iface, pStm, fClearDirty);
673 static HRESULT WINAPI xmldoc_IPersistStreamInit_GetSizeMax(
674 IPersistStreamInit *iface, ULARGE_INTEGER *pcbSize)
676 xmldoc *This = impl_from_IPersistStreamInit(iface);
677 TRACE("(%p, %p)\n", This, pcbSize);
681 static HRESULT WINAPI xmldoc_IPersistStreamInit_InitNew(
682 IPersistStreamInit *iface)
684 xmldoc *This = impl_from_IPersistStreamInit(iface);
685 TRACE("(%p)\n", This);
689 static const IPersistStreamInitVtbl xmldoc_IPersistStreamInit_VTable =
691 xmldoc_IPersistStreamInit_QueryInterface,
692 xmldoc_IPersistStreamInit_AddRef,
693 xmldoc_IPersistStreamInit_Release,
694 xmldoc_IPersistStreamInit_GetClassID,
695 xmldoc_IPersistStreamInit_IsDirty,
696 xmldoc_IPersistStreamInit_Load,
697 xmldoc_IPersistStreamInit_Save,
698 xmldoc_IPersistStreamInit_GetSizeMax,
699 xmldoc_IPersistStreamInit_InitNew
702 HRESULT XMLDocument_create(IUnknown *pUnkOuter, LPVOID *ppObj)
706 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
708 doc = heap_alloc(sizeof (*doc));
710 return E_OUTOFMEMORY;
712 doc->IXMLDocument_iface.lpVtbl = &xmldoc_vtbl;
713 doc->IPersistStreamInit_iface.lpVtbl = &xmldoc_IPersistStreamInit_VTable;
719 *ppObj = &doc->IXMLDocument_iface;
721 TRACE("returning iface %p\n", *ppObj);
727 HRESULT XMLDocument_create(IUnknown *pUnkOuter, LPVOID *ppObj)
729 MESSAGE("This program tried to use an XMLDocument object, but\n"
730 "libxml2 support was not present at compile time.\n");