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
36 #include "wine/debug.h"
38 #include "msxml_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
44 /* FIXME: IXMLDocument needs to implement
49 typedef struct _xmldoc
51 const IXMLDocumentVtbl *lpVtbl;
52 const IPersistStreamInitVtbl *lpvtblIPersistStreamInit;
64 static inline xmldoc *impl_from_IXMLDocument(IXMLDocument *iface)
66 return (xmldoc *)((char*)iface - FIELD_OFFSET(xmldoc, lpVtbl));
69 static inline xmldoc *impl_from_IPersistStreamInit(IPersistStreamInit *iface)
71 return (xmldoc *)((char*)iface - FIELD_OFFSET(xmldoc, lpvtblIPersistStreamInit));
74 static HRESULT WINAPI xmldoc_QueryInterface(IXMLDocument *iface, REFIID riid, void** ppvObject)
76 xmldoc *This = impl_from_IXMLDocument(iface);
78 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
80 if (IsEqualGUID(riid, &IID_IUnknown) ||
81 IsEqualGUID(riid, &IID_IXMLDocument) ||
82 IsEqualGUID(riid, &IID_IXMLDOMDocument))
86 else if (IsEqualGUID(&IID_IPersistStreamInit, riid) ||
87 IsEqualGUID(&IID_IPersistStream, riid))
89 *ppvObject = &(This->lpvtblIPersistStreamInit);
93 FIXME("interface %s not implemented\n", debugstr_guid(riid));
97 IXMLDocument_AddRef(iface);
102 static ULONG WINAPI xmldoc_AddRef(IXMLDocument *iface)
104 xmldoc *This = impl_from_IXMLDocument(iface);
106 return InterlockedIncrement(&This->ref);
109 static ULONG WINAPI xmldoc_Release(IXMLDocument *iface)
111 xmldoc *This = impl_from_IXMLDocument(iface);
116 ref = InterlockedDecrement(&This->ref);
119 xmlFreeDoc(This->xmldoc);
120 if (This->stream) IStream_Release(This->stream);
121 HeapFree(GetProcessHeap(), 0, This);
127 static HRESULT WINAPI xmldoc_GetTypeInfoCount(IXMLDocument *iface, UINT* pctinfo)
129 xmldoc *This = impl_from_IXMLDocument(iface);
131 TRACE("(%p)->(%p)\n", This, pctinfo);
138 static HRESULT WINAPI xmldoc_GetTypeInfo(IXMLDocument *iface, UINT iTInfo,
139 LCID lcid, ITypeInfo** ppTInfo)
141 xmldoc *This = impl_from_IXMLDocument(iface);
144 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
146 hr = get_typeinfo(IXMLDocument_tid, ppTInfo);
151 static HRESULT WINAPI xmldoc_GetIDsOfNames(IXMLDocument *iface, REFIID riid,
152 LPOLESTR* rgszNames, UINT cNames,
153 LCID lcid, DISPID* rgDispId)
155 xmldoc *This = impl_from_IXMLDocument(iface);
159 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
162 if(!rgszNames || cNames == 0 || !rgDispId)
165 hr = get_typeinfo(IXMLDocument_tid, &typeinfo);
168 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
169 ITypeInfo_Release(typeinfo);
175 static HRESULT WINAPI xmldoc_Invoke(IXMLDocument *iface, DISPID dispIdMember,
176 REFIID riid, LCID lcid, WORD wFlags,
177 DISPPARAMS* pDispParams, VARIANT* pVarResult,
178 EXCEPINFO* pExcepInfo, UINT* puArgErr)
180 xmldoc *This = impl_from_IXMLDocument(iface);
184 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
185 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
187 hr = get_typeinfo(IXMLDocument_tid, &typeinfo);
190 hr = ITypeInfo_Invoke(typeinfo, &(This->lpVtbl), dispIdMember, wFlags, pDispParams,
191 pVarResult, pExcepInfo, puArgErr);
192 ITypeInfo_Release(typeinfo);
198 static HRESULT WINAPI xmldoc_get_root(IXMLDocument *iface, IXMLElement **p)
200 xmldoc *This = impl_from_IXMLDocument(iface);
202 TRACE("(%p, %p)\n", iface, p);
214 static HRESULT WINAPI xmldoc_get_fileSize(IXMLDocument *iface, BSTR *p)
216 FIXME("(%p, %p): stub\n", iface, p);
220 static HRESULT WINAPI xmldoc_put_fileModifiedDate(IXMLDocument *iface, BSTR *p)
222 FIXME("(%p, %p): stub\n", iface, p);
226 static HRESULT WINAPI xmldoc_get_fileUpdatedDate(IXMLDocument *iface, BSTR *p)
228 FIXME("(%p, %p): stub\n", iface, p);
232 static HRESULT WINAPI xmldoc_get_URL(IXMLDocument *iface, BSTR *p)
234 FIXME("(%p, %p): stub\n", iface, p);
239 const struct IBindStatusCallbackVtbl *lpVtbl;
242 static HRESULT WINAPI bsc_QueryInterface(
243 IBindStatusCallback *iface,
247 if (IsEqualGUID(riid, &IID_IUnknown) ||
248 IsEqualGUID(riid, &IID_IBindStatusCallback))
250 IBindStatusCallback_AddRef( iface );
255 FIXME("interface %s not implemented\n", debugstr_guid(riid));
256 return E_NOINTERFACE;
259 static ULONG WINAPI bsc_AddRef(
260 IBindStatusCallback *iface )
265 static ULONG WINAPI bsc_Release(
266 IBindStatusCallback *iface )
271 static HRESULT WINAPI bsc_OnStartBinding(
272 IBindStatusCallback* iface,
279 static HRESULT WINAPI bsc_GetPriority(
280 IBindStatusCallback* iface,
286 static HRESULT WINAPI bsc_OnLowResource(
287 IBindStatusCallback* iface,
293 static HRESULT WINAPI bsc_OnProgress(
294 IBindStatusCallback* iface,
298 LPCWSTR szStatusText)
303 static HRESULT WINAPI bsc_OnStopBinding(
304 IBindStatusCallback* iface,
311 static HRESULT WINAPI bsc_GetBindInfo(
312 IBindStatusCallback* iface,
316 *grfBINDF = BINDF_RESYNCHRONIZE;
321 static HRESULT WINAPI bsc_OnDataAvailable(
322 IBindStatusCallback* iface,
325 FORMATETC* pformatetc,
331 static HRESULT WINAPI bsc_OnObjectAvailable(
332 IBindStatusCallback* iface,
339 static const struct IBindStatusCallbackVtbl bsc_vtbl =
351 bsc_OnObjectAvailable
354 static bsc xmldoc_bsc = { &bsc_vtbl };
356 static HRESULT WINAPI xmldoc_put_URL(IXMLDocument *iface, BSTR p)
358 WCHAR url[INTERNET_MAX_URL_LENGTH];
362 IPersistStreamInit *persist;
365 TRACE("(%p, %s)\n", iface, debugstr_w(p));
372 WCHAR fullpath[MAX_PATH];
373 DWORD needed = sizeof(url) / sizeof(WCHAR);
375 if (!PathSearchAndQualifyW(p, fullpath, sizeof(fullpath) / sizeof(WCHAR)))
377 ERR("can't find path\n");
381 if (FAILED(UrlCreateFromPathW(fullpath, url, &needed, 0)))
383 ERR("can't create url from path\n");
390 hr = CreateURLMoniker(NULL, p, &moniker);
394 CreateAsyncBindCtx(0, (IBindStatusCallback *)&xmldoc_bsc, 0, &bctx);
396 hr = IMoniker_BindToStorage(moniker, bctx, NULL, &IID_IStream, (LPVOID *)&stream);
397 IBindCtx_Release(bctx);
398 IMoniker_Release(moniker);
402 hr = IXMLDocument_QueryInterface(iface, &IID_IPersistStreamInit, (LPVOID *)&persist);
405 IStream_Release(stream);
409 hr = IPersistStreamInit_Load(persist, stream);
410 IPersistStreamInit_Release(persist);
411 IStream_Release(stream);
416 static HRESULT WINAPI xmldoc_get_mimeType(IXMLDocument *iface, BSTR *p)
418 FIXME("(%p, %p): stub\n", iface, p);
422 static HRESULT WINAPI xmldoc_get_readyState(IXMLDocument *iface, LONG *p)
424 FIXME("(%p, %p): stub\n", iface, p);
428 static HRESULT WINAPI xmldoc_get_charset(IXMLDocument *iface, BSTR *p)
430 FIXME("(%p, %p): stub\n", iface, p);
434 static HRESULT WINAPI xmldoc_put_charset(IXMLDocument *iface, BSTR p)
436 FIXME("(%p, %p): stub\n", iface, p);
440 static HRESULT WINAPI xmldoc_get_version(IXMLDocument *iface, BSTR *p)
442 xmldoc *This = impl_from_IXMLDocument(iface);
444 TRACE("(%p, %p)\n", This, p);
446 if (!p) return E_INVALIDARG;
447 *p = bstr_from_xmlChar(This->xmldoc->version);
452 static HRESULT WINAPI xmldoc_get_doctype(IXMLDocument *iface, BSTR *p)
454 xmldoc *This = impl_from_IXMLDocument(iface);
457 TRACE("(%p, %p)\n", This, p);
459 if (!p) return E_INVALIDARG;
461 dtd = xmlGetIntSubset(This->xmldoc);
462 if (!dtd) return S_FALSE;
464 *p = bstr_from_xmlChar(dtd->name);
465 CharUpperBuffW(*p, SysStringLen(*p));
470 static HRESULT WINAPI xmldoc_get_dtdURl(IXMLDocument *iface, BSTR *p)
472 FIXME("(%p, %p): stub\n", iface, p);
476 static xmlElementType type_msxml_to_libxml(LONG type)
480 case XMLELEMTYPE_ELEMENT:
481 return XML_ELEMENT_NODE;
482 case XMLELEMTYPE_TEXT:
483 return XML_TEXT_NODE;
484 case XMLELEMTYPE_COMMENT:
485 return XML_COMMENT_NODE;
486 case XMLELEMTYPE_DOCUMENT:
487 return XML_DOCUMENT_NODE;
488 case XMLELEMTYPE_DTD:
496 return -1; /* FIXME: what is OTHER in msxml? */
499 static HRESULT WINAPI xmldoc_createElement(IXMLDocument *iface, VARIANT vType,
500 VARIANT var1, IXMLElement **ppElem)
503 static const xmlChar empty[] = "\0";
505 TRACE("(%p, %p)\n", iface, ppElem);
512 if (V_VT(&vType) != VT_I4)
515 if(type_msxml_to_libxml(V_I4(&vType)) == -1)
518 node = xmlNewNode(NULL, empty);
519 node->type = type_msxml_to_libxml(V_I4(&vType));
521 /* FIXME: create xmlNodePtr based on vType and var1 */
522 return XMLElement_create((IUnknown *)iface, node, (LPVOID *)ppElem);
525 static const struct IXMLDocumentVtbl xmldoc_vtbl =
527 xmldoc_QueryInterface,
530 xmldoc_GetTypeInfoCount,
532 xmldoc_GetIDsOfNames,
536 xmldoc_put_fileModifiedDate,
537 xmldoc_get_fileUpdatedDate,
541 xmldoc_get_readyState,
550 /************************************************************************
551 * xmldoc implementation of IPersistStreamInit.
553 static HRESULT WINAPI xmldoc_IPersistStreamInit_QueryInterface(
554 IPersistStreamInit *iface, REFIID riid, LPVOID *ppvObj)
556 xmldoc *this = impl_from_IPersistStreamInit(iface);
557 return IXMLDocument_QueryInterface((IXMLDocument *)this, riid, ppvObj);
560 static ULONG WINAPI xmldoc_IPersistStreamInit_AddRef(
561 IPersistStreamInit *iface)
563 xmldoc *this = impl_from_IPersistStreamInit(iface);
564 return IXMLDocument_AddRef((IXMLDocument *)this);
567 static ULONG WINAPI xmldoc_IPersistStreamInit_Release(
568 IPersistStreamInit *iface)
570 xmldoc *this = impl_from_IPersistStreamInit(iface);
571 return IXMLDocument_Release((IXMLDocument *)this);
574 static HRESULT WINAPI xmldoc_IPersistStreamInit_GetClassID(
575 IPersistStreamInit *iface, CLSID *classid)
577 xmldoc *this = impl_from_IPersistStreamInit(iface);
578 TRACE("(%p,%p)\n", this, classid);
580 if (!classid) return E_POINTER;
582 *classid = CLSID_XMLDocument;
586 static HRESULT WINAPI xmldoc_IPersistStreamInit_IsDirty(
587 IPersistStreamInit *iface)
589 FIXME("(%p): stub!\n", iface);
593 xmlDocPtr parse_xml(char *ptr, int len)
595 #ifdef HAVE_XMLREADMEMORY
596 return xmlReadMemory(ptr, len, NULL, NULL,
597 XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NOBLANKS);
599 return xmlParseMemory(ptr, len);
603 static HRESULT WINAPI xmldoc_IPersistStreamInit_Load(
604 IPersistStreamInit *iface, LPSTREAM pStm)
606 xmldoc *This = impl_from_IPersistStreamInit(iface);
610 DWORD read, written, len;
614 TRACE("(%p, %p)\n", iface, pStm);
619 hr = CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
625 IStream_Read(pStm, buf, sizeof(buf), &read);
626 hr = IStream_Write(This->stream, buf, read, &written);
627 } while(SUCCEEDED(hr) && written != 0 && read != 0);
631 ERR("Failed to copy stream\n");
635 hr = GetHGlobalFromStream(This->stream, &hglobal);
639 len = GlobalSize(hglobal);
640 ptr = GlobalLock(hglobal);
642 This->xmldoc = parse_xml(ptr, len);
643 GlobalUnlock(hglobal);
647 ERR("Failed to parse xml\n");
651 xmlnode = xmlDocGetRootElement(This->xmldoc);
652 return XMLElement_create((IUnknown *)This, xmlnode, (LPVOID *)&This->root);
655 static HRESULT WINAPI xmldoc_IPersistStreamInit_Save(
656 IPersistStreamInit *iface, LPSTREAM pStm, BOOL fClearDirty)
658 FIXME("(%p, %p, %d): stub!\n", iface, pStm, fClearDirty);
662 static HRESULT WINAPI xmldoc_IPersistStreamInit_GetSizeMax(
663 IPersistStreamInit *iface, ULARGE_INTEGER *pcbSize)
665 xmldoc *This = impl_from_IPersistStreamInit(iface);
666 TRACE("(%p, %p)\n", This, pcbSize);
670 static HRESULT WINAPI xmldoc_IPersistStreamInit_InitNew(
671 IPersistStreamInit *iface)
673 FIXME("(%p): stub!\n", iface);
677 static const IPersistStreamInitVtbl xmldoc_IPersistStreamInit_VTable =
679 xmldoc_IPersistStreamInit_QueryInterface,
680 xmldoc_IPersistStreamInit_AddRef,
681 xmldoc_IPersistStreamInit_Release,
682 xmldoc_IPersistStreamInit_GetClassID,
683 xmldoc_IPersistStreamInit_IsDirty,
684 xmldoc_IPersistStreamInit_Load,
685 xmldoc_IPersistStreamInit_Save,
686 xmldoc_IPersistStreamInit_GetSizeMax,
687 xmldoc_IPersistStreamInit_InitNew
690 HRESULT XMLDocument_create(IUnknown *pUnkOuter, LPVOID *ppObj)
694 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
696 doc = HeapAlloc(GetProcessHeap(), 0, sizeof (*doc));
698 return E_OUTOFMEMORY;
700 doc->lpVtbl = &xmldoc_vtbl;
701 doc->lpvtblIPersistStreamInit = &xmldoc_IPersistStreamInit_VTable;
708 *ppObj = &doc->lpVtbl;
710 TRACE("returning iface %p\n", *ppObj);
716 HRESULT XMLDocument_create(IUnknown *pUnkOuter, LPVOID *ppObj)
718 MESSAGE("This program tried to use an XMLDocument object, but\n"
719 "libxml2 support was not present at compile time.\n");