2 * IMXNamespaceManager implementation
4 * Copyright 2011-2012 Nikolay Sivov for CodeWeavers
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>
29 # include <libxml/encoding.h>
38 #include "msxml_private.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msxml
);
59 #define DEFAULT_PREFIX_ALLOC_COUNT 16
61 static const WCHAR xmlW
[] = {'x','m','l',0};
62 static const WCHAR xmluriW
[] = {'h','t','t','p',':','/','/','w','w','w','.','w','3','.','o','r','g',
63 '/','X','M','L','/','1','9','9','8','/','n','a','m','e','s','p','a','c','e',0};
68 IMXNamespaceManager IMXNamespaceManager_iface
;
69 IVBMXNamespaceManager IVBMXNamespaceManager_iface
;
74 VARIANT_BOOL override
;
77 static inline namespacemanager
*impl_from_IMXNamespaceManager( IMXNamespaceManager
*iface
)
79 return CONTAINING_RECORD(iface
, namespacemanager
, IMXNamespaceManager_iface
);
82 static inline namespacemanager
*impl_from_IVBMXNamespaceManager( IVBMXNamespaceManager
*iface
)
84 return CONTAINING_RECORD(iface
, namespacemanager
, IVBMXNamespaceManager_iface
);
87 static HRESULT
declare_prefix(namespacemanager
*This
, const WCHAR
*prefix
, const WCHAR
*uri
)
89 struct nscontext
*ctxt
= LIST_ENTRY(list_head(&This
->ctxts
), struct nscontext
, entry
);
90 static const WCHAR emptyW
[] = {0};
94 if (ctxt
->count
== ctxt
->max_alloc
)
97 ctxt
->ns
= heap_realloc(ctxt
->ns
, ctxt
->max_alloc
*sizeof(*ctxt
->ns
));
100 if (!prefix
) prefix
= emptyW
;
103 for (i
= 0; i
< ctxt
->count
; i
++)
104 if (!strcmpW(ctxt
->ns
[i
].prefix
, prefix
))
112 if (This
->override
== VARIANT_TRUE
)
114 SysFreeString(ns
->uri
);
115 ns
->uri
= SysAllocString(uri
);
123 ctxt
->ns
[ctxt
->count
].prefix
= SysAllocString(prefix
);
124 ctxt
->ns
[ctxt
->count
].uri
= SysAllocString(uri
);
131 /* returned stored pointer, caller needs to copy it */
132 static HRESULT
get_declared_prefix_idx(const struct nscontext
*ctxt
, LONG index
, BSTR
*prefix
)
136 if (index
>= ctxt
->count
|| index
< 0) return E_FAIL
;
138 if (index
> 0) index
= ctxt
->count
- index
;
139 *prefix
= ctxt
->ns
[index
].prefix
;
144 /* returned stored pointer, caller needs to copy it */
145 static HRESULT
get_declared_prefix_uri(const struct list
*ctxts
, const WCHAR
*uri
, BSTR
*prefix
)
147 struct nscontext
*ctxt
;
149 LIST_FOR_EACH_ENTRY(ctxt
, ctxts
, struct nscontext
, entry
)
152 for (i
= 0; i
< ctxt
->count
; i
++)
153 if (!strcmpW(ctxt
->ns
[i
].uri
, uri
))
155 *prefix
= ctxt
->ns
[i
].prefix
;
164 static HRESULT
get_uri_from_prefix(const struct nscontext
*ctxt
, const WCHAR
*prefix
, BSTR
*uri
)
168 for (i
= 0; i
< ctxt
->count
; i
++)
169 if (!strcmpW(ctxt
->ns
[i
].prefix
, prefix
))
171 *uri
= ctxt
->ns
[i
].uri
;
179 static struct nscontext
* alloc_ns_context(void)
181 struct nscontext
*ctxt
;
183 ctxt
= heap_alloc(sizeof(*ctxt
));
184 if (!ctxt
) return NULL
;
187 ctxt
->max_alloc
= DEFAULT_PREFIX_ALLOC_COUNT
;
188 ctxt
->ns
= heap_alloc(ctxt
->max_alloc
*sizeof(*ctxt
->ns
));
195 /* first allocated prefix is always 'xml' */
196 ctxt
->ns
[0].prefix
= SysAllocString(xmlW
);
197 ctxt
->ns
[0].uri
= SysAllocString(xmluriW
);
199 if (!ctxt
->ns
[0].prefix
|| !ctxt
->ns
[0].uri
)
209 static void free_ns_context(struct nscontext
*ctxt
)
213 for (i
= 0; i
< ctxt
->count
; i
++)
215 SysFreeString(ctxt
->ns
[i
].prefix
);
216 SysFreeString(ctxt
->ns
[i
].uri
);
223 static HRESULT WINAPI
namespacemanager_QueryInterface(IMXNamespaceManager
*iface
, REFIID riid
, void **ppvObject
)
225 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
226 return IVBMXNamespaceManager_QueryInterface(&This
->IVBMXNamespaceManager_iface
, riid
, ppvObject
);
229 static ULONG WINAPI
namespacemanager_AddRef(IMXNamespaceManager
*iface
)
231 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
232 return IVBMXNamespaceManager_AddRef(&This
->IVBMXNamespaceManager_iface
);
235 static ULONG WINAPI
namespacemanager_Release(IMXNamespaceManager
*iface
)
237 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
238 return IVBMXNamespaceManager_Release(&This
->IVBMXNamespaceManager_iface
);
241 static HRESULT WINAPI
namespacemanager_putAllowOverride(IMXNamespaceManager
*iface
,
242 VARIANT_BOOL override
)
244 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
245 return IVBMXNamespaceManager_put_allowOverride(&This
->IVBMXNamespaceManager_iface
, override
);
248 static HRESULT WINAPI
namespacemanager_getAllowOverride(IMXNamespaceManager
*iface
,
249 VARIANT_BOOL
*override
)
251 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
252 return IVBMXNamespaceManager_get_allowOverride(&This
->IVBMXNamespaceManager_iface
, override
);
255 static HRESULT WINAPI
namespacemanager_reset(IMXNamespaceManager
*iface
)
257 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
258 return IVBMXNamespaceManager_reset(&This
->IVBMXNamespaceManager_iface
);
261 static HRESULT WINAPI
namespacemanager_pushContext(IMXNamespaceManager
*iface
)
263 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
264 return IVBMXNamespaceManager_pushContext(&This
->IVBMXNamespaceManager_iface
);
267 static HRESULT WINAPI
namespacemanager_pushNodeContext(IMXNamespaceManager
*iface
,
268 IXMLDOMNode
*node
, VARIANT_BOOL deep
)
270 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
271 return IVBMXNamespaceManager_pushNodeContext(&This
->IVBMXNamespaceManager_iface
, node
, deep
);
274 static HRESULT WINAPI
namespacemanager_popContext(IMXNamespaceManager
*iface
)
276 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
277 return IVBMXNamespaceManager_popContext(&This
->IVBMXNamespaceManager_iface
);
280 static HRESULT WINAPI
namespacemanager_declarePrefix(IMXNamespaceManager
*iface
,
281 const WCHAR
*prefix
, const WCHAR
*namespaceURI
)
283 static const WCHAR xmlnsW
[] = {'x','m','l','n','s',0};
285 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
287 TRACE("(%p)->(%s %s)\n", This
, debugstr_w(prefix
), debugstr_w(namespaceURI
));
289 if (prefix
&& (!strcmpW(prefix
, xmlW
) || !strcmpW(prefix
, xmlnsW
) || !namespaceURI
))
292 return declare_prefix(This
, prefix
, namespaceURI
);
295 static HRESULT WINAPI
namespacemanager_getDeclaredPrefix(IMXNamespaceManager
*iface
,
296 LONG index
, WCHAR
*prefix
, int *prefix_len
)
298 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
299 struct nscontext
*ctxt
;
303 TRACE("(%p)->(%d %p %p)\n", This
, index
, prefix
, prefix_len
);
305 if (!prefix_len
) return E_POINTER
;
307 ctxt
= LIST_ENTRY(list_head(&This
->ctxts
), struct nscontext
, entry
);
308 hr
= get_declared_prefix_idx(ctxt
, index
, &prfx
);
309 if (hr
!= S_OK
) return hr
;
313 if (*prefix_len
< (INT
)SysStringLen(prfx
)) return E_XML_BUFFERTOOSMALL
;
314 strcpyW(prefix
, prfx
);
317 *prefix_len
= SysStringLen(prfx
);
322 static HRESULT WINAPI
namespacemanager_getPrefix(IMXNamespaceManager
*iface
,
323 const WCHAR
*uri
, LONG index
, WCHAR
*prefix
, int *prefix_len
)
325 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
329 TRACE("(%p)->(%s %d %p %p)\n", This
, debugstr_w(uri
), index
, prefix
, prefix_len
);
331 if (!uri
|| !*uri
|| !prefix_len
) return E_INVALIDARG
;
333 hr
= get_declared_prefix_uri(&This
->ctxts
, uri
, &prfx
);
336 /* TODO: figure out what index argument is for */
337 if (index
) return E_FAIL
;
341 if (*prefix_len
< (INT
)SysStringLen(prfx
)) return E_XML_BUFFERTOOSMALL
;
342 strcpyW(prefix
, prfx
);
345 *prefix_len
= SysStringLen(prfx
);
346 TRACE("prefix=%s\n", debugstr_w(prfx
));
352 static HRESULT WINAPI
namespacemanager_getURI(IMXNamespaceManager
*iface
,
353 const WCHAR
*prefix
, IXMLDOMNode
*node
, WCHAR
*uri
, int *uri_len
)
355 namespacemanager
*This
= impl_from_IMXNamespaceManager( iface
);
356 struct nscontext
*ctxt
;
360 TRACE("(%p)->(%s %p %p %p)\n", This
, debugstr_w(prefix
), node
, uri
, uri_len
);
362 if (!prefix
) return E_INVALIDARG
;
363 if (!uri_len
) return E_POINTER
;
367 FIXME("namespaces from DOM node not supported\n");
371 ctxt
= LIST_ENTRY(list_head(&This
->ctxts
), struct nscontext
, entry
);
372 hr
= get_uri_from_prefix(ctxt
, prefix
, &urib
);
377 if (*uri_len
< (INT
)SysStringLen(urib
)) return E_XML_BUFFERTOOSMALL
;
384 *uri_len
= SysStringLen(urib
);
389 static const struct IMXNamespaceManagerVtbl MXNamespaceManagerVtbl
=
391 namespacemanager_QueryInterface
,
392 namespacemanager_AddRef
,
393 namespacemanager_Release
,
394 namespacemanager_putAllowOverride
,
395 namespacemanager_getAllowOverride
,
396 namespacemanager_reset
,
397 namespacemanager_pushContext
,
398 namespacemanager_pushNodeContext
,
399 namespacemanager_popContext
,
400 namespacemanager_declarePrefix
,
401 namespacemanager_getDeclaredPrefix
,
402 namespacemanager_getPrefix
,
403 namespacemanager_getURI
406 static HRESULT WINAPI
vbnamespacemanager_QueryInterface(IVBMXNamespaceManager
*iface
, REFIID riid
, void **obj
)
408 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
409 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), obj
);
411 if ( IsEqualGUID( riid
, &IID_IMXNamespaceManager
) ||
412 IsEqualGUID( riid
, &IID_IUnknown
) )
414 *obj
= &This
->IMXNamespaceManager_iface
;
416 else if ( IsEqualGUID( riid
, &IID_IVBMXNamespaceManager
) ||
417 IsEqualGUID( riid
, &IID_IDispatch
) )
419 *obj
= &This
->IVBMXNamespaceManager_iface
;
421 else if (dispex_query_interface(&This
->dispex
, riid
, obj
))
423 return *obj
? S_OK
: E_NOINTERFACE
;
427 TRACE("Unsupported interface %s\n", debugstr_guid(riid
));
429 return E_NOINTERFACE
;
432 IVBMXNamespaceManager_AddRef( iface
);
437 static ULONG WINAPI
vbnamespacemanager_AddRef(IVBMXNamespaceManager
*iface
)
439 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
440 ULONG ref
= InterlockedIncrement( &This
->ref
);
441 TRACE("(%p)->(%u)\n", This
, ref
);
445 static ULONG WINAPI
vbnamespacemanager_Release(IVBMXNamespaceManager
*iface
)
447 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
448 ULONG ref
= InterlockedDecrement( &This
->ref
);
450 TRACE("(%p)->(%u)\n", This
, ref
);
454 struct nscontext
*ctxt
, *ctxt2
;
456 LIST_FOR_EACH_ENTRY_SAFE(ctxt
, ctxt2
, &This
->ctxts
, struct nscontext
, entry
)
458 list_remove(&ctxt
->entry
);
459 free_ns_context(ctxt
);
468 static HRESULT WINAPI
vbnamespacemanager_GetTypeInfoCount(IVBMXNamespaceManager
*iface
, UINT
*pctinfo
)
470 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
471 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
474 static HRESULT WINAPI
vbnamespacemanager_GetTypeInfo(IVBMXNamespaceManager
*iface
, UINT iTInfo
,
475 LCID lcid
, ITypeInfo
**ppTInfo
)
477 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
478 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
,
479 iTInfo
, lcid
, ppTInfo
);
482 static HRESULT WINAPI
vbnamespacemanager_GetIDsOfNames(IVBMXNamespaceManager
*iface
, REFIID riid
,
483 LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
485 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
486 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
,
487 riid
, rgszNames
, cNames
, lcid
, rgDispId
);
490 static HRESULT WINAPI
vbnamespacemanager_Invoke(IVBMXNamespaceManager
*iface
, DISPID dispIdMember
, REFIID riid
,
491 LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
, VARIANT
*pVarResult
,
492 EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
494 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
495 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
,
496 dispIdMember
, riid
, lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
499 static HRESULT WINAPI
vbnamespacemanager_put_allowOverride(IVBMXNamespaceManager
*iface
,
500 VARIANT_BOOL override
)
502 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
504 TRACE("(%p)->(%d)\n", This
, override
);
505 This
->override
= override
;
510 static HRESULT WINAPI
vbnamespacemanager_get_allowOverride(IVBMXNamespaceManager
*iface
,
511 VARIANT_BOOL
*override
)
513 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
515 TRACE("(%p)->(%p)\n", This
, override
);
517 if (!override
) return E_POINTER
;
518 *override
= This
->override
;
523 static HRESULT WINAPI
vbnamespacemanager_reset(IVBMXNamespaceManager
*iface
)
525 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
526 FIXME("(%p): stub\n", This
);
530 static HRESULT WINAPI
vbnamespacemanager_pushContext(IVBMXNamespaceManager
*iface
)
532 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
533 struct nscontext
*ctxt
;
535 TRACE("(%p)\n", This
);
537 ctxt
= alloc_ns_context();
538 if (!ctxt
) return E_OUTOFMEMORY
;
540 list_add_head(&This
->ctxts
, &ctxt
->entry
);
545 static HRESULT WINAPI
vbnamespacemanager_pushNodeContext(IVBMXNamespaceManager
*iface
,
546 IXMLDOMNode
*node
, VARIANT_BOOL deep
)
548 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
549 FIXME("(%p)->(%p %d): stub\n", This
, node
, deep
);
553 static HRESULT WINAPI
vbnamespacemanager_popContext(IVBMXNamespaceManager
*iface
)
555 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
556 const struct list
*next
;
557 struct nscontext
*ctxt
;
559 TRACE("(%p)\n", This
);
561 next
= list_next(&This
->ctxts
, list_head(&This
->ctxts
));
562 if (!next
) return E_FAIL
;
564 ctxt
= LIST_ENTRY(list_head(&This
->ctxts
), struct nscontext
, entry
);
565 list_remove(list_head(&This
->ctxts
));
567 free_ns_context(ctxt
);
572 static HRESULT WINAPI
vbnamespacemanager_declarePrefix(IVBMXNamespaceManager
*iface
,
573 BSTR prefix
, BSTR namespaceURI
)
575 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
576 return IMXNamespaceManager_declarePrefix(&This
->IMXNamespaceManager_iface
, prefix
, namespaceURI
);
579 static HRESULT WINAPI
vbnamespacemanager_getDeclaredPrefixes(IVBMXNamespaceManager
*iface
,
580 IMXNamespacePrefixes
** prefixes
)
582 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
583 FIXME("(%p)->(%p): stub\n", This
, prefixes
);
587 static HRESULT WINAPI
vbnamespacemanager_getPrefixes(IVBMXNamespaceManager
*iface
,
588 BSTR namespaceURI
, IMXNamespacePrefixes
** prefixes
)
590 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
591 FIXME("(%p)->(%s %p): stub\n", This
, debugstr_w(namespaceURI
), prefixes
);
595 static HRESULT WINAPI
vbnamespacemanager_getURI(IVBMXNamespaceManager
*iface
,
596 BSTR prefix
, VARIANT
* uri
)
598 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
599 FIXME("(%p)->(%s %p): stub\n", This
, debugstr_w(prefix
), uri
);
603 static HRESULT WINAPI
vbnamespacemanager_getURIFromNode(IVBMXNamespaceManager
*iface
,
604 BSTR prefix
, IXMLDOMNode
*node
, VARIANT
*uri
)
606 namespacemanager
*This
= impl_from_IVBMXNamespaceManager( iface
);
607 FIXME("(%p)->(%s %p %p): stub\n", This
, debugstr_w(prefix
), node
, uri
);
611 static const struct IVBMXNamespaceManagerVtbl VBMXNamespaceManagerVtbl
=
613 vbnamespacemanager_QueryInterface
,
614 vbnamespacemanager_AddRef
,
615 vbnamespacemanager_Release
,
616 vbnamespacemanager_GetTypeInfoCount
,
617 vbnamespacemanager_GetTypeInfo
,
618 vbnamespacemanager_GetIDsOfNames
,
619 vbnamespacemanager_Invoke
,
620 vbnamespacemanager_put_allowOverride
,
621 vbnamespacemanager_get_allowOverride
,
622 vbnamespacemanager_reset
,
623 vbnamespacemanager_pushContext
,
624 vbnamespacemanager_pushNodeContext
,
625 vbnamespacemanager_popContext
,
626 vbnamespacemanager_declarePrefix
,
627 vbnamespacemanager_getDeclaredPrefixes
,
628 vbnamespacemanager_getPrefixes
,
629 vbnamespacemanager_getURI
,
630 vbnamespacemanager_getURIFromNode
633 static const tid_t namespacemanager_iface_tids
[] = {
634 IVBMXNamespaceManager_tid
,
638 static dispex_static_data_t namespacemanager_dispex
= {
640 IVBMXNamespaceManager_tid
,
642 namespacemanager_iface_tids
645 HRESULT
MXNamespaceManager_create(void **obj
)
647 namespacemanager
*This
;
648 struct nscontext
*ctxt
;
650 TRACE("(%p)\n", obj
);
652 This
= heap_alloc( sizeof (*This
) );
654 return E_OUTOFMEMORY
;
656 This
->IMXNamespaceManager_iface
.lpVtbl
= &MXNamespaceManagerVtbl
;
657 This
->IVBMXNamespaceManager_iface
.lpVtbl
= &VBMXNamespaceManagerVtbl
;
659 init_dispex(&This
->dispex
, (IUnknown
*)&This
->IVBMXNamespaceManager_iface
, &namespacemanager_dispex
);
661 list_init(&This
->ctxts
);
662 ctxt
= alloc_ns_context();
666 return E_OUTOFMEMORY
;
669 list_add_head(&This
->ctxts
, &ctxt
->entry
);
671 This
->override
= VARIANT_TRUE
;
673 *obj
= &This
->IMXNamespaceManager_iface
;
675 TRACE("returning iface %p\n", *obj
);