msxml3: Properly escape character data in text nodes.
[wine/multimedia.git] / dlls / msxml3 / xmldoc.c
blob1cf87855878271020264357e9200cc77cfc09c57
1 /*
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
21 #define COBJMACROS
23 #include "config.h"
25 #include <stdarg.h>
26 #ifdef HAVE_LIBXML2
27 # include <libxml/parser.h>
28 # include <libxml/xmlerror.h>
29 #endif
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "ole2.h"
35 #include "msxml6.h"
36 #include "wininet.h"
37 #include "winreg.h"
38 #include "shlwapi.h"
39 #include "ocidl.h"
41 #include "wine/debug.h"
43 #include "msxml_private.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
47 #ifdef HAVE_LIBXML2
49 /* FIXME: IXMLDocument needs to implement
50 * - IXMLError
51 * - IPersistMoniker
54 typedef struct _xmldoc
56 IXMLDocument IXMLDocument_iface;
57 IPersistStreamInit IPersistStreamInit_iface;
58 LONG ref;
59 HRESULT error;
61 /* IXMLDocument */
62 xmlDocPtr xmldoc;
64 /* IPersistStream */
65 IStream *stream;
66 } xmldoc;
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))
88 *ppvObject = iface;
90 else if (IsEqualGUID(&IID_IPersistStreamInit, riid) ||
91 IsEqualGUID(&IID_IPersistStream, riid))
93 *ppvObject = &This->IPersistStreamInit_iface;
95 else
97 FIXME("interface %s not implemented\n", debugstr_guid(riid));
98 *ppvObject = NULL;
99 return E_NOINTERFACE;
102 IXMLDocument_AddRef(iface);
104 return S_OK;
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);
112 return 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);
122 if (ref == 0)
124 xmlFreeDoc(This->xmldoc);
125 if (This->stream) IStream_Release(This->stream);
126 heap_free(This);
129 return ref;
132 static HRESULT WINAPI xmldoc_GetTypeInfoCount(IXMLDocument *iface, UINT* pctinfo)
134 xmldoc *This = impl_from_IXMLDocument(iface);
136 TRACE("(%p)->(%p)\n", This, pctinfo);
138 *pctinfo = 1;
140 return S_OK;
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);
158 ITypeInfo *typeinfo;
159 HRESULT hr;
161 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
162 lcid, rgDispId);
164 if(!rgszNames || cNames == 0 || !rgDispId)
165 return E_INVALIDARG;
167 hr = get_typeinfo(IXMLDocument_tid, &typeinfo);
168 if(SUCCEEDED(hr))
170 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
171 ITypeInfo_Release(typeinfo);
174 return hr;
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);
183 ITypeInfo *typeinfo;
184 HRESULT hr;
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);
190 if(SUCCEEDED(hr))
192 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLDocument_iface, dispIdMember, wFlags,
193 pDispParams, pVarResult, pExcepInfo, puArgErr);
194 ITypeInfo_Release(typeinfo);
197 return hr;
200 static HRESULT WINAPI xmldoc_get_root(IXMLDocument *iface, IXMLElement **p)
202 xmldoc *This = impl_from_IXMLDocument(iface);
203 xmlNodePtr root;
205 TRACE("(%p, %p)\n", iface, p);
207 if (!p)
208 return E_INVALIDARG;
210 *p = NULL;
212 if (!(root = xmlDocGetRootElement(This->xmldoc)))
213 return E_FAIL;
215 return XMLElement_create((IUnknown *)This, root, (LPVOID *)p, FALSE);
218 static HRESULT WINAPI xmldoc_get_fileSize(IXMLDocument *iface, BSTR *p)
220 FIXME("(%p, %p): stub\n", iface, p);
221 return E_NOTIMPL;
224 static HRESULT WINAPI xmldoc_put_fileModifiedDate(IXMLDocument *iface, BSTR *p)
226 FIXME("(%p, %p): stub\n", iface, p);
227 return E_NOTIMPL;
230 static HRESULT WINAPI xmldoc_get_fileUpdatedDate(IXMLDocument *iface, BSTR *p)
232 FIXME("(%p, %p): stub\n", iface, p);
233 return E_NOTIMPL;
236 static HRESULT WINAPI xmldoc_get_URL(IXMLDocument *iface, BSTR *p)
238 FIXME("(%p, %p): stub\n", iface, p);
239 return E_NOTIMPL;
242 typedef struct {
243 IBindStatusCallback IBindStatusCallback_iface;
244 } bsc;
246 static HRESULT WINAPI bsc_QueryInterface(
247 IBindStatusCallback *iface,
248 REFIID riid,
249 LPVOID *ppobj )
251 if (IsEqualGUID(riid, &IID_IUnknown) ||
252 IsEqualGUID(riid, &IID_IBindStatusCallback))
254 IBindStatusCallback_AddRef( iface );
255 *ppobj = iface;
256 return S_OK;
259 TRACE("interface %s not implemented\n", debugstr_guid(riid));
260 return E_NOINTERFACE;
263 static ULONG WINAPI bsc_AddRef(
264 IBindStatusCallback *iface )
266 return 2;
269 static ULONG WINAPI bsc_Release(
270 IBindStatusCallback *iface )
272 return 1;
275 static HRESULT WINAPI bsc_OnStartBinding(
276 IBindStatusCallback* iface,
277 DWORD dwReserved,
278 IBinding* pib)
280 return S_OK;
283 static HRESULT WINAPI bsc_GetPriority(
284 IBindStatusCallback* iface,
285 LONG* pnPriority)
287 return S_OK;
290 static HRESULT WINAPI bsc_OnLowResource(
291 IBindStatusCallback* iface,
292 DWORD reserved)
294 return S_OK;
297 static HRESULT WINAPI bsc_OnProgress(
298 IBindStatusCallback* iface,
299 ULONG ulProgress,
300 ULONG ulProgressMax,
301 ULONG ulStatusCode,
302 LPCWSTR szStatusText)
304 return S_OK;
307 static HRESULT WINAPI bsc_OnStopBinding(
308 IBindStatusCallback* iface,
309 HRESULT hresult,
310 LPCWSTR szError)
312 return S_OK;
315 static HRESULT WINAPI bsc_GetBindInfo(
316 IBindStatusCallback* iface,
317 DWORD* grfBINDF,
318 BINDINFO* pbindinfo)
320 *grfBINDF = BINDF_RESYNCHRONIZE;
322 return S_OK;
325 static HRESULT WINAPI bsc_OnDataAvailable(
326 IBindStatusCallback* iface,
327 DWORD grfBSCF,
328 DWORD dwSize,
329 FORMATETC* pformatetc,
330 STGMEDIUM* pstgmed)
332 return S_OK;
335 static HRESULT WINAPI bsc_OnObjectAvailable(
336 IBindStatusCallback* iface,
337 REFIID riid,
338 IUnknown* punk)
340 return S_OK;
343 static const struct IBindStatusCallbackVtbl bsc_vtbl =
345 bsc_QueryInterface,
346 bsc_AddRef,
347 bsc_Release,
348 bsc_OnStartBinding,
349 bsc_GetPriority,
350 bsc_OnLowResource,
351 bsc_OnProgress,
352 bsc_OnStopBinding,
353 bsc_GetBindInfo,
354 bsc_OnDataAvailable,
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];
363 IStream *stream;
364 IBindCtx *bctx;
365 IMoniker *moniker;
366 IPersistStreamInit *persist;
367 HRESULT hr;
369 TRACE("(%p, %s)\n", iface, debugstr_w(p));
371 if (!p)
372 return E_INVALIDARG;
374 if (!PathIsURLW(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");
382 return E_FAIL;
385 if (FAILED(UrlCreateFromPathW(fullpath, url, &needed, 0)))
387 ERR("can't create url from path\n");
388 return E_FAIL;
391 p = url;
394 hr = CreateURLMoniker(NULL, p, &moniker);
395 if (FAILED(hr))
396 return hr;
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);
403 if (FAILED(hr))
404 return hr;
406 hr = IXMLDocument_QueryInterface(iface, &IID_IPersistStreamInit, (LPVOID *)&persist);
407 if (FAILED(hr))
409 IStream_Release(stream);
410 return hr;
413 hr = IPersistStreamInit_Load(persist, stream);
414 IPersistStreamInit_Release(persist);
415 IStream_Release(stream);
417 return hr;
420 static HRESULT WINAPI xmldoc_get_mimeType(IXMLDocument *iface, BSTR *p)
422 FIXME("(%p, %p): stub\n", iface, p);
423 return E_NOTIMPL;
426 static HRESULT WINAPI xmldoc_get_readyState(IXMLDocument *iface, LONG *p)
428 FIXME("(%p, %p): stub\n", iface, p);
429 return E_NOTIMPL;
432 static HRESULT WINAPI xmldoc_get_charset(IXMLDocument *iface, BSTR *p)
434 FIXME("(%p, %p): stub\n", iface, p);
435 return E_NOTIMPL;
438 static HRESULT WINAPI xmldoc_put_charset(IXMLDocument *iface, BSTR p)
440 FIXME("(%p, %p): stub\n", iface, p);
441 return E_NOTIMPL;
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);
453 return S_OK;
456 static HRESULT WINAPI xmldoc_get_doctype(IXMLDocument *iface, BSTR *p)
458 xmldoc *This = impl_from_IXMLDocument(iface);
459 xmlDtd *dtd;
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));
471 return S_OK;
474 static HRESULT WINAPI xmldoc_get_dtdURl(IXMLDocument *iface, BSTR *p)
476 FIXME("(%p, %p): stub\n", iface, p);
477 return E_NOTIMPL;
480 static xmlElementType type_msxml_to_libxml(LONG type)
482 switch (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:
493 return XML_DTD_NODE;
494 case XMLELEMTYPE_PI:
495 return XML_PI_NODE;
496 default:
497 break;
500 return -1; /* FIXME: what is OTHER in msxml? */
503 static HRESULT WINAPI xmldoc_createElement(IXMLDocument *iface, VARIANT vType,
504 VARIANT var1, IXMLElement **ppElem)
506 xmlNodePtr node;
507 static const xmlChar empty[] = "\0";
509 TRACE("(%p)->(%s %s %p)\n", iface, debugstr_variant(&vType),
510 debugstr_variant(&var1), ppElem);
512 if (!ppElem)
513 return E_INVALIDARG;
515 *ppElem = NULL;
517 if (V_VT(&vType) != VT_I4)
518 return E_INVALIDARG;
520 if(type_msxml_to_libxml(V_I4(&vType)) == -1)
521 return E_NOTIMPL;
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((IUnknown *)iface, node, (LPVOID *)ppElem, TRUE);
530 static const struct IXMLDocumentVtbl xmldoc_vtbl =
532 xmldoc_QueryInterface,
533 xmldoc_AddRef,
534 xmldoc_Release,
535 xmldoc_GetTypeInfoCount,
536 xmldoc_GetTypeInfo,
537 xmldoc_GetIDsOfNames,
538 xmldoc_Invoke,
539 xmldoc_get_root,
540 xmldoc_get_fileSize,
541 xmldoc_put_fileModifiedDate,
542 xmldoc_get_fileUpdatedDate,
543 xmldoc_get_URL,
544 xmldoc_put_URL,
545 xmldoc_get_mimeType,
546 xmldoc_get_readyState,
547 xmldoc_get_charset,
548 xmldoc_put_charset,
549 xmldoc_get_version,
550 xmldoc_get_doctype,
551 xmldoc_get_dtdURl,
552 xmldoc_createElement
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;
588 return S_OK;
591 static HRESULT WINAPI xmldoc_IPersistStreamInit_IsDirty(
592 IPersistStreamInit *iface)
594 FIXME("(%p): stub!\n", iface);
595 return E_NOTIMPL;
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);
603 #else
604 return xmlParseMemory(ptr, len);
605 #endif
608 static HRESULT WINAPI xmldoc_IPersistStreamInit_Load(
609 IPersistStreamInit *iface, LPSTREAM pStm)
611 xmldoc *This = impl_from_IPersistStreamInit(iface);
612 HRESULT hr;
613 HGLOBAL hglobal;
614 DWORD read, written, len;
615 BYTE buf[4096];
616 char *ptr;
618 TRACE("(%p, %p)\n", iface, pStm);
620 if (!pStm)
621 return E_INVALIDARG;
623 /* release previously allocated stream */
624 if (This->stream) IStream_Release(This->stream);
625 hr = CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
626 if (FAILED(hr))
627 return hr;
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);
635 if (FAILED(hr))
637 ERR("Failed to copy stream\n");
638 return hr;
641 hr = GetHGlobalFromStream(This->stream, &hglobal);
642 if (FAILED(hr))
643 return hr;
645 len = GlobalSize(hglobal);
646 ptr = GlobalLock(hglobal);
647 if (len != 0)
649 xmlFreeDoc(This->xmldoc);
650 This->xmldoc = parse_xml(ptr, len);
652 GlobalUnlock(hglobal);
654 if (!This->xmldoc)
656 ERR("Failed to parse xml\n");
657 return E_FAIL;
660 return S_OK;
663 static HRESULT WINAPI xmldoc_IPersistStreamInit_Save(
664 IPersistStreamInit *iface, LPSTREAM pStm, BOOL fClearDirty)
666 FIXME("(%p, %p, %d): stub!\n", iface, pStm, fClearDirty);
667 return E_NOTIMPL;
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);
675 return E_NOTIMPL;
678 static HRESULT WINAPI xmldoc_IPersistStreamInit_InitNew(
679 IPersistStreamInit *iface)
681 xmldoc *This = impl_from_IPersistStreamInit(iface);
682 TRACE("(%p)\n", This);
683 return S_OK;
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(IUnknown *pUnkOuter, LPVOID *ppObj)
701 xmldoc *doc;
703 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
705 doc = heap_alloc(sizeof (*doc));
706 if(!doc)
707 return E_OUTOFMEMORY;
709 doc->IXMLDocument_iface.lpVtbl = &xmldoc_vtbl;
710 doc->IPersistStreamInit_iface.lpVtbl = &xmldoc_IPersistStreamInit_VTable;
711 doc->ref = 1;
712 doc->error = S_OK;
713 doc->xmldoc = NULL;
714 doc->stream = NULL;
716 *ppObj = &doc->IXMLDocument_iface;
718 TRACE("returning iface %p\n", *ppObj);
719 return S_OK;
722 #else
724 HRESULT XMLDocument_create(IUnknown *pUnkOuter, LPVOID *ppObj)
726 MESSAGE("This program tried to use an XMLDocument object, but\n"
727 "libxml2 support was not present at compile time.\n");
728 return E_NOTIMPL;
731 #endif