2 * IXmlWriter implementation
4 * Copyright 2011 Alistair Leslie-Hughes
5 * Copyright 2014 Nikolay Sivov for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "xmllite_private.h"
31 #include "wine/debug.h"
32 #include "wine/list.h"
33 #include "wine/unicode.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(xmllite
);
37 /* not defined in public headers */
38 DEFINE_GUID(IID_IXmlWriterOutput
, 0xc1131708, 0x0f59, 0x477f, 0x93, 0x59, 0x7d, 0x33, 0x24, 0x51, 0xbc, 0x1a);
40 #define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0]))
42 static const WCHAR closeelementW
[] = {'<','/'};
43 static const WCHAR closepiW
[] = {'?','>'};
44 static const WCHAR ltW
[] = {'<'};
45 static const WCHAR gtW
[] = {'>'};
50 unsigned int allocated
;
57 XmlWriterState_Initial
, /* output is not set yet */
58 XmlWriterState_Ready
, /* SetOutput() was called, ready to start */
59 XmlWriterState_PIDocStarted
, /* document was started with manually added 'xml' PI */
60 XmlWriterState_DocStarted
, /* document was started with WriteStartDocument() */
61 XmlWriterState_ElemStarted
, /* writing element */
62 XmlWriterState_Content
, /* content is accepted at this point */
63 XmlWriterState_DocClosed
/* WriteEndDocument was called */
68 IXmlWriterOutput IXmlWriterOutput_iface
;
71 ISequentialStream
*stream
;
73 xml_encoding encoding
;
74 struct output_buffer buffer
;
77 static const struct IUnknownVtbl xmlwriteroutputvtbl
;
83 unsigned int len
; /* qname length in chars */
86 typedef struct _xmlwriter
88 IXmlWriter IXmlWriter_iface
;
91 xmlwriteroutput
*output
;
95 XmlConformanceLevel conformance
;
102 static inline xmlwriter
*impl_from_IXmlWriter(IXmlWriter
*iface
)
104 return CONTAINING_RECORD(iface
, xmlwriter
, IXmlWriter_iface
);
107 static inline xmlwriteroutput
*impl_from_IXmlWriterOutput(IXmlWriterOutput
*iface
)
109 return CONTAINING_RECORD(iface
, xmlwriteroutput
, IXmlWriterOutput_iface
);
112 static const char *debugstr_writer_prop(XmlWriterProperty prop
)
114 static const char * const prop_names
[] =
119 "OmitXmlDeclaration",
123 if (prop
> _XmlWriterProperty_Last
)
124 return wine_dbg_sprintf("unknown property=%d", prop
);
126 return prop_names
[prop
];
129 /* writer output memory allocation functions */
130 static inline void *writeroutput_alloc(xmlwriteroutput
*output
, size_t len
)
132 return m_alloc(output
->imalloc
, len
);
135 static inline void writeroutput_free(xmlwriteroutput
*output
, void *mem
)
137 m_free(output
->imalloc
, mem
);
140 static inline void *writeroutput_realloc(xmlwriteroutput
*output
, void *mem
, size_t len
)
142 return m_realloc(output
->imalloc
, mem
, len
);
145 /* writer memory allocation functions */
146 static inline void *writer_alloc(xmlwriter
*writer
, size_t len
)
148 return m_alloc(writer
->imalloc
, len
);
151 static inline void writer_free(xmlwriter
*writer
, void *mem
)
153 m_free(writer
->imalloc
, mem
);
156 static struct element
*alloc_element(xmlwriter
*writer
, const WCHAR
*prefix
, const WCHAR
*local
)
161 ret
= writer_alloc(writer
, sizeof(*ret
));
162 if (!ret
) return ret
;
164 len
= prefix
? strlenW(prefix
) + 1 /* ':' */ : 0;
165 len
+= strlenW(local
);
167 ret
->qname
= writer_alloc(writer
, (len
+ 1)*sizeof(WCHAR
));
170 static const WCHAR colonW
[] = {':',0};
171 strcpyW(ret
->qname
, prefix
);
172 strcatW(ret
->qname
, colonW
);
176 strcatW(ret
->qname
, local
);
181 static void free_element(xmlwriter
*writer
, struct element
*element
)
183 writer_free(writer
, element
->qname
);
184 writer_free(writer
, element
);
187 static void push_element(xmlwriter
*writer
, struct element
*element
)
189 list_add_head(&writer
->elements
, &element
->entry
);
192 static struct element
*pop_element(xmlwriter
*writer
)
194 struct element
*element
= LIST_ENTRY(list_head(&writer
->elements
), struct element
, entry
);
197 list_remove(&element
->entry
);
202 static HRESULT
init_output_buffer(xmlwriteroutput
*output
)
204 struct output_buffer
*buffer
= &output
->buffer
;
205 const int initial_len
= 0x2000;
209 hr
= get_code_page(output
->encoding
, &cp
);
210 if (FAILED(hr
)) return hr
;
212 buffer
->data
= writeroutput_alloc(output
, initial_len
);
213 if (!buffer
->data
) return E_OUTOFMEMORY
;
215 memset(buffer
->data
, 0, 4);
216 buffer
->allocated
= initial_len
;
218 buffer
->codepage
= cp
;
223 static void free_output_buffer(xmlwriteroutput
*output
)
225 struct output_buffer
*buffer
= &output
->buffer
;
226 writeroutput_free(output
, buffer
->data
);
228 buffer
->allocated
= 0;
232 static HRESULT
grow_output_buffer(xmlwriteroutput
*output
, int length
)
234 struct output_buffer
*buffer
= &output
->buffer
;
235 /* grow if needed, plus 4 bytes to be sure null terminator will fit in */
236 if (buffer
->allocated
< buffer
->written
+ length
+ 4) {
237 int grown_size
= max(2*buffer
->allocated
, buffer
->allocated
+ length
);
238 char *ptr
= writeroutput_realloc(output
, buffer
->data
, grown_size
);
239 if (!ptr
) return E_OUTOFMEMORY
;
241 buffer
->allocated
= grown_size
;
247 static HRESULT
write_output_buffer(xmlwriteroutput
*output
, const WCHAR
*data
, int len
)
249 struct output_buffer
*buffer
= &output
->buffer
;
254 if (buffer
->codepage
!= ~0) {
255 length
= WideCharToMultiByte(buffer
->codepage
, 0, data
, len
, NULL
, 0, NULL
, NULL
);
256 hr
= grow_output_buffer(output
, length
);
257 if (FAILED(hr
)) return hr
;
258 ptr
= buffer
->data
+ buffer
->written
;
259 length
= WideCharToMultiByte(buffer
->codepage
, 0, data
, len
, ptr
, length
, NULL
, NULL
);
260 buffer
->written
+= len
== -1 ? length
-1 : length
;
263 /* WCHAR data just copied */
264 length
= len
== -1 ? strlenW(data
) : len
;
266 length
*= sizeof(WCHAR
);
268 hr
= grow_output_buffer(output
, length
);
269 if (FAILED(hr
)) return hr
;
270 ptr
= buffer
->data
+ buffer
->written
;
272 memcpy(ptr
, data
, length
);
273 buffer
->written
+= length
;
275 /* null termination */
283 static HRESULT
write_output_buffer_quoted(xmlwriteroutput
*output
, const WCHAR
*data
, int len
)
285 static const WCHAR quoteW
[] = {'"'};
286 write_output_buffer(output
, quoteW
, ARRAY_SIZE(quoteW
));
287 write_output_buffer(output
, data
, len
);
288 write_output_buffer(output
, quoteW
, ARRAY_SIZE(quoteW
));
292 /* TODO: test if we need to validate char range */
293 static HRESULT
write_output_qname(xmlwriteroutput
*output
, const WCHAR
*prefix
, const WCHAR
*local_name
)
296 static const WCHAR colW
[] = {':'};
297 write_output_buffer(output
, prefix
, -1);
298 write_output_buffer(output
, colW
, ARRAY_SIZE(colW
));
301 write_output_buffer(output
, local_name
, -1);
306 static void writeroutput_release_stream(xmlwriteroutput
*writeroutput
)
308 if (writeroutput
->stream
) {
309 ISequentialStream_Release(writeroutput
->stream
);
310 writeroutput
->stream
= NULL
;
314 static inline HRESULT
writeroutput_query_for_stream(xmlwriteroutput
*writeroutput
)
318 writeroutput_release_stream(writeroutput
);
319 hr
= IUnknown_QueryInterface(writeroutput
->output
, &IID_IStream
, (void**)&writeroutput
->stream
);
321 hr
= IUnknown_QueryInterface(writeroutput
->output
, &IID_ISequentialStream
, (void**)&writeroutput
->stream
);
326 static HRESULT
writeroutput_flush_stream(xmlwriteroutput
*output
)
328 struct output_buffer
*buffer
;
329 ULONG written
, offset
= 0;
332 if (!output
|| !output
->stream
)
335 buffer
= &output
->buffer
;
337 /* It will loop forever until everything is written or an error occurred. */
340 hr
= ISequentialStream_Write(output
->stream
, buffer
->data
+ offset
, buffer
->written
, &written
);
342 WARN("write to stream failed (0x%08x)\n", hr
);
348 buffer
->written
-= written
;
349 } while (buffer
->written
> 0);
354 static HRESULT
write_encoding_bom(xmlwriter
*writer
)
356 if (!writer
->bom
|| writer
->bomwritten
) return S_OK
;
358 if (writer
->output
->encoding
== XmlEncoding_UTF16
) {
359 static const char utf16bom
[] = {0xff, 0xfe};
360 struct output_buffer
*buffer
= &writer
->output
->buffer
;
361 int len
= sizeof(utf16bom
);
364 hr
= grow_output_buffer(writer
->output
, len
);
365 if (FAILED(hr
)) return hr
;
366 memcpy(buffer
->data
+ buffer
->written
, utf16bom
, len
);
367 buffer
->written
+= len
;
370 writer
->bomwritten
= TRUE
;
374 static HRESULT
writer_close_starttag(xmlwriter
*writer
)
378 if (!writer
->starttagopen
) return S_OK
;
379 hr
= write_output_buffer(writer
->output
, gtW
, ARRAY_SIZE(gtW
));
380 writer
->starttagopen
= FALSE
;
381 writer
->state
= XmlWriterState_Content
;
385 static HRESULT WINAPI
xmlwriter_QueryInterface(IXmlWriter
*iface
, REFIID riid
, void **ppvObject
)
387 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
389 TRACE("%p %s %p\n", This
, debugstr_guid(riid
), ppvObject
);
391 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
392 IsEqualGUID(riid
, &IID_IXmlWriter
))
397 IXmlWriter_AddRef(iface
);
402 static ULONG WINAPI
xmlwriter_AddRef(IXmlWriter
*iface
)
404 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
406 return InterlockedIncrement(&This
->ref
);
409 static ULONG WINAPI
xmlwriter_Release(IXmlWriter
*iface
)
411 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
416 ref
= InterlockedDecrement(&This
->ref
);
418 struct element
*element
, *element2
;
419 IMalloc
*imalloc
= This
->imalloc
;
421 IXmlWriter_Flush(iface
);
422 if (This
->output
) IUnknown_Release(&This
->output
->IXmlWriterOutput_iface
);
425 LIST_FOR_EACH_ENTRY_SAFE(element
, element2
, &This
->elements
, struct element
, entry
) {
426 list_remove(&element
->entry
);
427 free_element(This
, element
);
430 writer_free(This
, This
);
431 if (imalloc
) IMalloc_Release(imalloc
);
437 /*** IXmlWriter methods ***/
438 static HRESULT WINAPI
xmlwriter_SetOutput(IXmlWriter
*iface
, IUnknown
*output
)
440 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
441 IXmlWriterOutput
*writeroutput
;
444 TRACE("(%p)->(%p)\n", This
, output
);
447 writeroutput_release_stream(This
->output
);
448 IUnknown_Release(&This
->output
->IXmlWriterOutput_iface
);
450 This
->bomwritten
= FALSE
;
453 /* just reset current output */
455 This
->state
= XmlWriterState_Initial
;
459 /* now try IXmlWriterOutput, ISequentialStream, IStream */
460 hr
= IUnknown_QueryInterface(output
, &IID_IXmlWriterOutput
, (void**)&writeroutput
);
462 if (writeroutput
->lpVtbl
== &xmlwriteroutputvtbl
)
463 This
->output
= impl_from_IXmlWriterOutput(writeroutput
);
465 ERR("got external IXmlWriterOutput implementation: %p, vtbl=%p\n",
466 writeroutput
, writeroutput
->lpVtbl
);
467 IUnknown_Release(writeroutput
);
473 if (hr
!= S_OK
|| !writeroutput
) {
474 /* create IXmlWriterOutput basing on supplied interface */
475 hr
= CreateXmlWriterOutputWithEncodingName(output
, This
->imalloc
, NULL
, &writeroutput
);
476 if (hr
!= S_OK
) return hr
;
477 This
->output
= impl_from_IXmlWriterOutput(writeroutput
);
480 This
->state
= XmlWriterState_Ready
;
481 return writeroutput_query_for_stream(This
->output
);
484 static HRESULT WINAPI
xmlwriter_GetProperty(IXmlWriter
*iface
, UINT property
, LONG_PTR
*value
)
486 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
488 TRACE("(%p)->(%s %p)\n", This
, debugstr_writer_prop(property
), value
);
490 if (!value
) return E_INVALIDARG
;
494 case XmlWriterProperty_Indent
:
495 *value
= This
->indent
;
497 case XmlWriterProperty_ByteOrderMark
:
500 case XmlWriterProperty_OmitXmlDeclaration
:
501 *value
= This
->omitxmldecl
;
503 case XmlWriterProperty_ConformanceLevel
:
504 *value
= This
->conformance
;
507 FIXME("Unimplemented property (%u)\n", property
);
514 static HRESULT WINAPI
xmlwriter_SetProperty(IXmlWriter
*iface
, UINT property
, LONG_PTR value
)
516 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
518 TRACE("(%p)->(%s %lu)\n", This
, debugstr_writer_prop(property
), value
);
522 case XmlWriterProperty_ByteOrderMark
:
525 case XmlWriterProperty_OmitXmlDeclaration
:
526 This
->omitxmldecl
= !!value
;
529 FIXME("Unimplemented property (%u)\n", property
);
536 static HRESULT WINAPI
xmlwriter_WriteAttributes(IXmlWriter
*iface
, IXmlReader
*pReader
,
537 BOOL fWriteDefaultAttributes
)
539 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
541 FIXME("%p %p %d\n", This
, pReader
, fWriteDefaultAttributes
);
546 static HRESULT WINAPI
xmlwriter_WriteAttributeString(IXmlWriter
*iface
, LPCWSTR pwszPrefix
,
547 LPCWSTR pwszLocalName
, LPCWSTR pwszNamespaceUri
,
550 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
552 FIXME("%p %s %s %s %s\n", This
, wine_dbgstr_w(pwszPrefix
), wine_dbgstr_w(pwszLocalName
),
553 wine_dbgstr_w(pwszNamespaceUri
), wine_dbgstr_w(pwszValue
));
558 static HRESULT WINAPI
xmlwriter_WriteCData(IXmlWriter
*iface
, LPCWSTR pwszText
)
560 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
562 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszText
));
567 static HRESULT WINAPI
xmlwriter_WriteCharEntity(IXmlWriter
*iface
, WCHAR wch
)
572 static HRESULT WINAPI
xmlwriter_WriteChars(IXmlWriter
*iface
, const WCHAR
*pwch
, UINT cwch
)
574 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
576 FIXME("%p %s %d\n", This
, wine_dbgstr_w(pwch
), cwch
);
581 static HRESULT WINAPI
xmlwriter_WriteComment(IXmlWriter
*iface
, LPCWSTR pwszComment
)
586 static HRESULT WINAPI
xmlwriter_WriteDocType(IXmlWriter
*iface
, LPCWSTR pwszName
, LPCWSTR pwszPublicId
,
587 LPCWSTR pwszSystemId
, LPCWSTR pwszSubset
)
589 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
591 FIXME("%p %s %s %s %s\n", This
, wine_dbgstr_w(pwszName
), wine_dbgstr_w(pwszPublicId
),
592 wine_dbgstr_w(pwszSystemId
), wine_dbgstr_w(pwszSubset
));
597 static HRESULT WINAPI
xmlwriter_WriteElementString(IXmlWriter
*iface
, LPCWSTR prefix
,
598 LPCWSTR local_name
, LPCWSTR uri
, LPCWSTR value
)
600 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
602 TRACE("(%p)->(%s %s %s %s)\n", This
, wine_dbgstr_w(prefix
), wine_dbgstr_w(local_name
),
603 wine_dbgstr_w(uri
), wine_dbgstr_w(value
));
607 case XmlWriterState_Initial
:
609 case XmlWriterState_ElemStarted
:
610 writer_close_starttag(This
);
612 case XmlWriterState_DocClosed
:
613 return WR_E_INVALIDACTION
;
618 write_encoding_bom(This
);
619 write_output_buffer(This
->output
, ltW
, ARRAY_SIZE(ltW
));
620 write_output_qname(This
->output
, prefix
, local_name
);
621 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
624 write_output_buffer(This
->output
, value
, -1);
626 write_output_buffer(This
->output
, closeelementW
, ARRAY_SIZE(closeelementW
));
627 write_output_qname(This
->output
, prefix
, local_name
);
628 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
629 This
->state
= XmlWriterState_Content
;
634 static HRESULT WINAPI
xmlwriter_WriteEndDocument(IXmlWriter
*iface
)
636 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
643 case XmlWriterState_Initial
:
646 case XmlWriterState_Ready
:
647 case XmlWriterState_DocClosed
:
648 hr
= WR_E_INVALIDACTION
;
655 This
->state
= XmlWriterState_DocClosed
;
659 /* empty element stack */
660 while (IXmlWriter_WriteEndElement(iface
) == S_OK
)
663 This
->state
= XmlWriterState_DocClosed
;
667 static HRESULT WINAPI
xmlwriter_WriteEndElement(IXmlWriter
*iface
)
669 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
670 struct element
*element
;
674 element
= pop_element(This
);
676 return WR_E_INVALIDACTION
;
678 if (This
->starttagopen
) {
679 static WCHAR closetagW
[] = {' ','/','>'};
680 write_output_buffer(This
->output
, closetagW
, ARRAY_SIZE(closetagW
));
681 This
->starttagopen
= FALSE
;
684 /* write full end tag */
685 write_output_buffer(This
->output
, closeelementW
, ARRAY_SIZE(closeelementW
));
686 write_output_buffer(This
->output
, element
->qname
, element
->len
);
687 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
693 static HRESULT WINAPI
xmlwriter_WriteEntityRef(IXmlWriter
*iface
, LPCWSTR pwszName
)
695 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
697 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszName
));
702 static HRESULT WINAPI
xmlwriter_WriteFullEndElement(IXmlWriter
*iface
)
704 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
705 struct element
*element
;
709 element
= pop_element(This
);
711 return WR_E_INVALIDACTION
;
713 /* write full end tag */
714 write_output_buffer(This
->output
, closeelementW
, ARRAY_SIZE(closeelementW
));
715 write_output_buffer(This
->output
, element
->qname
, element
->len
);
716 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
717 This
->starttagopen
= FALSE
;
722 static HRESULT WINAPI
xmlwriter_WriteName(IXmlWriter
*iface
, LPCWSTR pwszName
)
724 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
726 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszName
));
731 static HRESULT WINAPI
xmlwriter_WriteNmToken(IXmlWriter
*iface
, LPCWSTR pwszNmToken
)
733 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
735 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszNmToken
));
740 static HRESULT WINAPI
xmlwriter_WriteNode(IXmlWriter
*iface
, IXmlReader
*pReader
,
741 BOOL fWriteDefaultAttributes
)
743 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
745 FIXME("%p %p %d\n", This
, pReader
, fWriteDefaultAttributes
);
750 static HRESULT WINAPI
xmlwriter_WriteNodeShallow(IXmlWriter
*iface
, IXmlReader
*pReader
,
751 BOOL fWriteDefaultAttributes
)
753 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
755 FIXME("%p %p %d\n", This
, pReader
, fWriteDefaultAttributes
);
760 static HRESULT WINAPI
xmlwriter_WriteProcessingInstruction(IXmlWriter
*iface
, LPCWSTR name
,
763 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
764 static const WCHAR xmlW
[] = {'x','m','l',0};
765 static const WCHAR openpiW
[] = {'<','?'};
766 static const WCHAR spaceW
[] = {' '};
768 TRACE("(%p)->(%s %s)\n", This
, wine_dbgstr_w(name
), wine_dbgstr_w(text
));
772 case XmlWriterState_Initial
:
774 case XmlWriterState_DocStarted
:
775 if (!strcmpW(name
, xmlW
))
776 return WR_E_INVALIDACTION
;
778 case XmlWriterState_ElemStarted
:
779 case XmlWriterState_DocClosed
:
780 return WR_E_INVALIDACTION
;
785 write_encoding_bom(This
);
786 write_output_buffer(This
->output
, openpiW
, ARRAY_SIZE(openpiW
));
787 write_output_buffer(This
->output
, name
, -1);
788 write_output_buffer(This
->output
, spaceW
, ARRAY_SIZE(spaceW
));
789 write_output_buffer(This
->output
, text
, -1);
790 write_output_buffer(This
->output
, closepiW
, ARRAY_SIZE(closepiW
));
792 if (!strcmpW(name
, xmlW
))
793 This
->state
= XmlWriterState_PIDocStarted
;
798 static HRESULT WINAPI
xmlwriter_WriteQualifiedName(IXmlWriter
*iface
, LPCWSTR pwszLocalName
,
799 LPCWSTR pwszNamespaceUri
)
801 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
803 FIXME("%p %s %s\n", This
, wine_dbgstr_w(pwszLocalName
), wine_dbgstr_w(pwszNamespaceUri
));
808 static HRESULT WINAPI
xmlwriter_WriteRaw(IXmlWriter
*iface
, LPCWSTR pwszData
)
810 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
812 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszData
));
817 static HRESULT WINAPI
xmlwriter_WriteRawChars(IXmlWriter
*iface
, const WCHAR
*pwch
, UINT cwch
)
819 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
821 FIXME("%p %s %d\n", This
, wine_dbgstr_w(pwch
), cwch
);
826 static HRESULT WINAPI
xmlwriter_WriteStartDocument(IXmlWriter
*iface
, XmlStandalone standalone
)
828 static const WCHAR versionW
[] = {'<','?','x','m','l',' ','v','e','r','s','i','o','n','=','"','1','.','0','"'};
829 static const WCHAR encodingW
[] = {' ','e','n','c','o','d','i','n','g','='};
830 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
832 TRACE("(%p)->(%d)\n", This
, standalone
);
836 case XmlWriterState_Initial
:
838 case XmlWriterState_PIDocStarted
:
839 This
->state
= XmlWriterState_DocStarted
;
841 case XmlWriterState_DocStarted
:
842 case XmlWriterState_ElemStarted
:
843 case XmlWriterState_DocClosed
:
844 return WR_E_INVALIDACTION
;
849 write_encoding_bom(This
);
850 This
->state
= XmlWriterState_DocStarted
;
851 if (This
->omitxmldecl
) return S_OK
;
854 write_output_buffer(This
->output
, versionW
, ARRAY_SIZE(versionW
));
857 write_output_buffer(This
->output
, encodingW
, ARRAY_SIZE(encodingW
));
858 write_output_buffer_quoted(This
->output
, get_encoding_name(This
->output
->encoding
), -1);
861 if (standalone
== XmlStandalone_Omit
)
862 write_output_buffer(This
->output
, closepiW
, ARRAY_SIZE(closepiW
));
864 static const WCHAR standaloneW
[] = {' ','s','t','a','n','d','a','l','o','n','e','=','\"'};
865 static const WCHAR yesW
[] = {'y','e','s','\"','?','>'};
866 static const WCHAR noW
[] = {'n','o','\"','?','>'};
868 write_output_buffer(This
->output
, standaloneW
, ARRAY_SIZE(standaloneW
));
869 if (standalone
== XmlStandalone_Yes
)
870 write_output_buffer(This
->output
, yesW
, ARRAY_SIZE(yesW
));
872 write_output_buffer(This
->output
, noW
, ARRAY_SIZE(noW
));
878 static HRESULT WINAPI
xmlwriter_WriteStartElement(IXmlWriter
*iface
, LPCWSTR prefix
, LPCWSTR local_name
, LPCWSTR uri
)
880 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
881 struct element
*element
;
883 TRACE("(%p)->(%s %s %s)\n", This
, wine_dbgstr_w(prefix
), wine_dbgstr_w(local_name
), wine_dbgstr_w(uri
));
887 case XmlWriterState_Initial
:
889 case XmlWriterState_DocClosed
:
890 return WR_E_INVALIDACTION
;
898 /* close pending element */
899 if (This
->starttagopen
)
900 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
902 element
= alloc_element(This
, prefix
, local_name
);
904 return E_OUTOFMEMORY
;
906 write_encoding_bom(This
);
907 This
->state
= XmlWriterState_ElemStarted
;
908 This
->starttagopen
= TRUE
;
910 push_element(This
, element
);
912 write_output_buffer(This
->output
, ltW
, ARRAY_SIZE(ltW
));
913 write_output_qname(This
->output
, prefix
, local_name
);
918 static HRESULT WINAPI
xmlwriter_WriteString(IXmlWriter
*iface
, LPCWSTR pwszText
)
920 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
922 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszText
));
927 static HRESULT WINAPI
xmlwriter_WriteSurrogateCharEntity(IXmlWriter
*iface
, WCHAR wchLow
, WCHAR wchHigh
)
929 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
931 FIXME("%p %d %d\n", This
, wchLow
, wchHigh
);
936 static HRESULT WINAPI
xmlwriter_WriteWhitespace(IXmlWriter
*iface
, LPCWSTR pwszWhitespace
)
938 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
940 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszWhitespace
));
945 static HRESULT WINAPI
xmlwriter_Flush(IXmlWriter
*iface
)
947 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
951 return writeroutput_flush_stream(This
->output
);
954 static const struct IXmlWriterVtbl xmlwriter_vtbl
=
956 xmlwriter_QueryInterface
,
960 xmlwriter_GetProperty
,
961 xmlwriter_SetProperty
,
962 xmlwriter_WriteAttributes
,
963 xmlwriter_WriteAttributeString
,
964 xmlwriter_WriteCData
,
965 xmlwriter_WriteCharEntity
,
966 xmlwriter_WriteChars
,
967 xmlwriter_WriteComment
,
968 xmlwriter_WriteDocType
,
969 xmlwriter_WriteElementString
,
970 xmlwriter_WriteEndDocument
,
971 xmlwriter_WriteEndElement
,
972 xmlwriter_WriteEntityRef
,
973 xmlwriter_WriteFullEndElement
,
975 xmlwriter_WriteNmToken
,
977 xmlwriter_WriteNodeShallow
,
978 xmlwriter_WriteProcessingInstruction
,
979 xmlwriter_WriteQualifiedName
,
981 xmlwriter_WriteRawChars
,
982 xmlwriter_WriteStartDocument
,
983 xmlwriter_WriteStartElement
,
984 xmlwriter_WriteString
,
985 xmlwriter_WriteSurrogateCharEntity
,
986 xmlwriter_WriteWhitespace
,
990 /** IXmlWriterOutput **/
991 static HRESULT WINAPI
xmlwriteroutput_QueryInterface(IXmlWriterOutput
*iface
, REFIID riid
, void** ppvObject
)
993 xmlwriteroutput
*This
= impl_from_IXmlWriterOutput(iface
);
995 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppvObject
);
997 if (IsEqualGUID(riid
, &IID_IXmlWriterOutput
) ||
998 IsEqualGUID(riid
, &IID_IUnknown
))
1004 WARN("interface %s not implemented\n", debugstr_guid(riid
));
1006 return E_NOINTERFACE
;
1009 IUnknown_AddRef(iface
);
1014 static ULONG WINAPI
xmlwriteroutput_AddRef(IXmlWriterOutput
*iface
)
1016 xmlwriteroutput
*This
= impl_from_IXmlWriterOutput(iface
);
1017 ULONG ref
= InterlockedIncrement(&This
->ref
);
1018 TRACE("(%p)->(%d)\n", This
, ref
);
1022 static ULONG WINAPI
xmlwriteroutput_Release(IXmlWriterOutput
*iface
)
1024 xmlwriteroutput
*This
= impl_from_IXmlWriterOutput(iface
);
1025 LONG ref
= InterlockedDecrement(&This
->ref
);
1027 TRACE("(%p)->(%d)\n", This
, ref
);
1031 IMalloc
*imalloc
= This
->imalloc
;
1032 if (This
->output
) IUnknown_Release(This
->output
);
1033 if (This
->stream
) ISequentialStream_Release(This
->stream
);
1034 free_output_buffer(This
);
1035 writeroutput_free(This
, This
);
1036 if (imalloc
) IMalloc_Release(imalloc
);
1042 static const struct IUnknownVtbl xmlwriteroutputvtbl
=
1044 xmlwriteroutput_QueryInterface
,
1045 xmlwriteroutput_AddRef
,
1046 xmlwriteroutput_Release
1049 HRESULT WINAPI
CreateXmlWriter(REFIID riid
, void **obj
, IMalloc
*imalloc
)
1053 TRACE("(%s, %p, %p)\n", wine_dbgstr_guid(riid
), obj
, imalloc
);
1055 if (!IsEqualGUID(riid
, &IID_IXmlWriter
))
1057 ERR("Unexpected IID requested -> (%s)\n", wine_dbgstr_guid(riid
));
1062 writer
= IMalloc_Alloc(imalloc
, sizeof(*writer
));
1064 writer
= heap_alloc(sizeof(*writer
));
1065 if(!writer
) return E_OUTOFMEMORY
;
1067 writer
->IXmlWriter_iface
.lpVtbl
= &xmlwriter_vtbl
;
1069 writer
->imalloc
= imalloc
;
1070 if (imalloc
) IMalloc_AddRef(imalloc
);
1071 writer
->output
= NULL
;
1072 writer
->indent
= FALSE
;
1074 writer
->omitxmldecl
= FALSE
;
1075 writer
->conformance
= XmlConformanceLevel_Document
;
1076 writer
->state
= XmlWriterState_Initial
;
1077 writer
->bomwritten
= FALSE
;
1078 writer
->starttagopen
= FALSE
;
1079 list_init(&writer
->elements
);
1081 *obj
= &writer
->IXmlWriter_iface
;
1083 TRACE("returning iface %p\n", *obj
);
1088 HRESULT WINAPI
CreateXmlWriterOutputWithEncodingName(IUnknown
*stream
,
1091 IXmlWriterOutput
**output
)
1093 static const WCHAR utf8W
[] = {'U','T','F','-','8',0};
1094 xmlwriteroutput
*writeroutput
;
1097 TRACE("%p %p %s %p\n", stream
, imalloc
, debugstr_w(encoding
), output
);
1099 if (!stream
|| !output
) return E_INVALIDARG
;
1104 writeroutput
= IMalloc_Alloc(imalloc
, sizeof(*writeroutput
));
1106 writeroutput
= heap_alloc(sizeof(*writeroutput
));
1107 if(!writeroutput
) return E_OUTOFMEMORY
;
1109 writeroutput
->IXmlWriterOutput_iface
.lpVtbl
= &xmlwriteroutputvtbl
;
1110 writeroutput
->ref
= 1;
1111 writeroutput
->imalloc
= imalloc
;
1112 if (imalloc
) IMalloc_AddRef(imalloc
);
1113 writeroutput
->encoding
= parse_encoding_name(encoding
? encoding
: utf8W
, -1);
1114 writeroutput
->stream
= NULL
;
1115 hr
= init_output_buffer(writeroutput
);
1117 IUnknown_Release(&writeroutput
->IXmlWriterOutput_iface
);
1121 IUnknown_QueryInterface(stream
, &IID_IUnknown
, (void**)&writeroutput
->output
);
1123 *output
= &writeroutput
->IXmlWriterOutput_iface
;
1125 TRACE("returning iface %p\n", *output
);