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 */
67 IXmlWriterOutput IXmlWriterOutput_iface
;
70 ISequentialStream
*stream
;
72 xml_encoding encoding
;
73 struct output_buffer buffer
;
76 static const struct IUnknownVtbl xmlwriteroutputvtbl
;
82 unsigned int len
; /* qname length in chars */
85 typedef struct _xmlwriter
87 IXmlWriter IXmlWriter_iface
;
90 xmlwriteroutput
*output
;
94 XmlConformanceLevel conformance
;
101 static inline xmlwriter
*impl_from_IXmlWriter(IXmlWriter
*iface
)
103 return CONTAINING_RECORD(iface
, xmlwriter
, IXmlWriter_iface
);
106 static inline xmlwriteroutput
*impl_from_IXmlWriterOutput(IXmlWriterOutput
*iface
)
108 return CONTAINING_RECORD(iface
, xmlwriteroutput
, IXmlWriterOutput_iface
);
111 static const char *debugstr_writer_prop(XmlWriterProperty prop
)
113 static const char * const prop_names
[] =
118 "OmitXmlDeclaration",
122 if (prop
> _XmlWriterProperty_Last
)
123 return wine_dbg_sprintf("unknown property=%d", prop
);
125 return prop_names
[prop
];
128 /* writer output memory allocation functions */
129 static inline void *writeroutput_alloc(xmlwriteroutput
*output
, size_t len
)
131 return m_alloc(output
->imalloc
, len
);
134 static inline void writeroutput_free(xmlwriteroutput
*output
, void *mem
)
136 m_free(output
->imalloc
, mem
);
139 static inline void *writeroutput_realloc(xmlwriteroutput
*output
, void *mem
, size_t len
)
141 return m_realloc(output
->imalloc
, mem
, len
);
144 /* writer memory allocation functions */
145 static inline void *writer_alloc(xmlwriter
*writer
, size_t len
)
147 return m_alloc(writer
->imalloc
, len
);
150 static inline void writer_free(xmlwriter
*writer
, void *mem
)
152 m_free(writer
->imalloc
, mem
);
155 static struct element
*alloc_element(xmlwriter
*writer
, const WCHAR
*prefix
, const WCHAR
*local
)
160 ret
= writer_alloc(writer
, sizeof(*ret
));
161 if (!ret
) return ret
;
163 len
= prefix
? strlenW(prefix
) + 1 /* ':' */ : 0;
164 len
+= strlenW(local
);
166 ret
->qname
= writer_alloc(writer
, (len
+ 1)*sizeof(WCHAR
));
169 static const WCHAR colonW
[] = {':',0};
170 strcpyW(ret
->qname
, prefix
);
171 strcatW(ret
->qname
, colonW
);
175 strcatW(ret
->qname
, local
);
180 static void free_element(xmlwriter
*writer
, struct element
*element
)
182 writer_free(writer
, element
->qname
);
183 writer_free(writer
, element
);
186 static void push_element(xmlwriter
*writer
, struct element
*element
)
188 list_add_head(&writer
->elements
, &element
->entry
);
191 static struct element
*pop_element(xmlwriter
*writer
)
193 struct element
*element
= LIST_ENTRY(list_head(&writer
->elements
), struct element
, entry
);
196 list_remove(&element
->entry
);
201 static HRESULT
init_output_buffer(xmlwriteroutput
*output
)
203 struct output_buffer
*buffer
= &output
->buffer
;
204 const int initial_len
= 0x2000;
208 hr
= get_code_page(output
->encoding
, &cp
);
209 if (FAILED(hr
)) return hr
;
211 buffer
->data
= writeroutput_alloc(output
, initial_len
);
212 if (!buffer
->data
) return E_OUTOFMEMORY
;
214 memset(buffer
->data
, 0, 4);
215 buffer
->allocated
= initial_len
;
217 buffer
->codepage
= cp
;
222 static void free_output_buffer(xmlwriteroutput
*output
)
224 struct output_buffer
*buffer
= &output
->buffer
;
225 writeroutput_free(output
, buffer
->data
);
227 buffer
->allocated
= 0;
231 static HRESULT
grow_output_buffer(xmlwriteroutput
*output
, int length
)
233 struct output_buffer
*buffer
= &output
->buffer
;
234 /* grow if needed, plus 4 bytes to be sure null terminator will fit in */
235 if (buffer
->allocated
< buffer
->written
+ length
+ 4) {
236 int grown_size
= max(2*buffer
->allocated
, buffer
->allocated
+ length
);
237 char *ptr
= writeroutput_realloc(output
, buffer
->data
, grown_size
);
238 if (!ptr
) return E_OUTOFMEMORY
;
240 buffer
->allocated
= grown_size
;
246 static HRESULT
write_output_buffer(xmlwriteroutput
*output
, const WCHAR
*data
, int len
)
248 struct output_buffer
*buffer
= &output
->buffer
;
253 if (buffer
->codepage
!= ~0) {
254 length
= WideCharToMultiByte(buffer
->codepage
, 0, data
, len
, NULL
, 0, NULL
, NULL
);
255 hr
= grow_output_buffer(output
, length
);
256 if (FAILED(hr
)) return hr
;
257 ptr
= buffer
->data
+ buffer
->written
;
258 length
= WideCharToMultiByte(buffer
->codepage
, 0, data
, len
, ptr
, length
, NULL
, NULL
);
259 buffer
->written
+= len
== -1 ? length
-1 : length
;
262 /* WCHAR data just copied */
263 length
= len
== -1 ? strlenW(data
) : len
;
265 length
*= sizeof(WCHAR
);
267 hr
= grow_output_buffer(output
, length
);
268 if (FAILED(hr
)) return hr
;
269 ptr
= buffer
->data
+ buffer
->written
;
271 memcpy(ptr
, data
, length
);
272 buffer
->written
+= length
;
274 /* null termination */
282 static HRESULT
write_output_buffer_quoted(xmlwriteroutput
*output
, const WCHAR
*data
, int len
)
284 static const WCHAR quoteW
[] = {'"'};
285 write_output_buffer(output
, quoteW
, ARRAY_SIZE(quoteW
));
286 write_output_buffer(output
, data
, len
);
287 write_output_buffer(output
, quoteW
, ARRAY_SIZE(quoteW
));
291 /* TODO: test if we need to validate char range */
292 static HRESULT
write_output_qname(xmlwriteroutput
*output
, const WCHAR
*prefix
, const WCHAR
*local_name
)
295 static const WCHAR colW
[] = {':'};
296 write_output_buffer(output
, prefix
, -1);
297 write_output_buffer(output
, colW
, ARRAY_SIZE(colW
));
300 write_output_buffer(output
, local_name
, -1);
305 static void writeroutput_release_stream(xmlwriteroutput
*writeroutput
)
307 if (writeroutput
->stream
) {
308 ISequentialStream_Release(writeroutput
->stream
);
309 writeroutput
->stream
= NULL
;
313 static inline HRESULT
writeroutput_query_for_stream(xmlwriteroutput
*writeroutput
)
317 writeroutput_release_stream(writeroutput
);
318 hr
= IUnknown_QueryInterface(writeroutput
->output
, &IID_IStream
, (void**)&writeroutput
->stream
);
320 hr
= IUnknown_QueryInterface(writeroutput
->output
, &IID_ISequentialStream
, (void**)&writeroutput
->stream
);
325 static HRESULT
writeroutput_flush_stream(xmlwriteroutput
*output
)
327 struct output_buffer
*buffer
;
328 ULONG written
, offset
= 0;
331 if (!output
|| !output
->stream
)
334 buffer
= &output
->buffer
;
336 /* It will loop forever until everything is written or an error occured. */
339 hr
= ISequentialStream_Write(output
->stream
, buffer
->data
+ offset
, buffer
->written
, &written
);
341 WARN("write to stream failed (0x%08x)\n", hr
);
347 buffer
->written
-= written
;
348 } while (buffer
->written
> 0);
353 static HRESULT
write_encoding_bom(xmlwriter
*writer
)
355 if (!writer
->bom
|| writer
->bomwritten
) return S_OK
;
357 if (writer
->output
->encoding
== XmlEncoding_UTF16
) {
358 static const char utf16bom
[] = {0xff, 0xfe};
359 struct output_buffer
*buffer
= &writer
->output
->buffer
;
360 int len
= sizeof(utf16bom
);
363 hr
= grow_output_buffer(writer
->output
, len
);
364 if (FAILED(hr
)) return hr
;
365 memcpy(buffer
->data
+ buffer
->written
, utf16bom
, len
);
366 buffer
->written
+= len
;
369 writer
->bomwritten
= TRUE
;
373 static HRESULT
writer_close_starttag(xmlwriter
*writer
)
377 if (!writer
->starttagopen
) return S_OK
;
378 hr
= write_output_buffer(writer
->output
, gtW
, ARRAY_SIZE(gtW
));
379 writer
->starttagopen
= FALSE
;
380 writer
->state
= XmlWriterState_Content
;
384 static HRESULT WINAPI
xmlwriter_QueryInterface(IXmlWriter
*iface
, REFIID riid
, void **ppvObject
)
386 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
388 TRACE("%p %s %p\n", This
, debugstr_guid(riid
), ppvObject
);
390 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
391 IsEqualGUID(riid
, &IID_IXmlWriter
))
396 IXmlWriter_AddRef(iface
);
401 static ULONG WINAPI
xmlwriter_AddRef(IXmlWriter
*iface
)
403 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
405 return InterlockedIncrement(&This
->ref
);
408 static ULONG WINAPI
xmlwriter_Release(IXmlWriter
*iface
)
410 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
415 ref
= InterlockedDecrement(&This
->ref
);
417 struct element
*element
, *element2
;
418 IMalloc
*imalloc
= This
->imalloc
;
420 IXmlWriter_Flush(iface
);
421 if (This
->output
) IUnknown_Release(&This
->output
->IXmlWriterOutput_iface
);
424 LIST_FOR_EACH_ENTRY_SAFE(element
, element2
, &This
->elements
, struct element
, entry
) {
425 list_remove(&element
->entry
);
426 free_element(This
, element
);
429 writer_free(This
, This
);
430 if (imalloc
) IMalloc_Release(imalloc
);
436 /*** IXmlWriter methods ***/
437 static HRESULT WINAPI
xmlwriter_SetOutput(IXmlWriter
*iface
, IUnknown
*output
)
439 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
440 IXmlWriterOutput
*writeroutput
;
443 TRACE("(%p)->(%p)\n", This
, output
);
446 writeroutput_release_stream(This
->output
);
447 IUnknown_Release(&This
->output
->IXmlWriterOutput_iface
);
449 This
->bomwritten
= FALSE
;
452 /* just reset current output */
454 This
->state
= XmlWriterState_Initial
;
458 /* now try IXmlWriterOutput, ISequentialStream, IStream */
459 hr
= IUnknown_QueryInterface(output
, &IID_IXmlWriterOutput
, (void**)&writeroutput
);
461 if (writeroutput
->lpVtbl
== &xmlwriteroutputvtbl
)
462 This
->output
= impl_from_IXmlWriterOutput(writeroutput
);
464 ERR("got external IXmlWriterOutput implementation: %p, vtbl=%p\n",
465 writeroutput
, writeroutput
->lpVtbl
);
466 IUnknown_Release(writeroutput
);
472 if (hr
!= S_OK
|| !writeroutput
) {
473 /* create IXmlWriterOutput basing on supplied interface */
474 hr
= CreateXmlWriterOutputWithEncodingName(output
, This
->imalloc
, NULL
, &writeroutput
);
475 if (hr
!= S_OK
) return hr
;
476 This
->output
= impl_from_IXmlWriterOutput(writeroutput
);
479 This
->state
= XmlWriterState_Ready
;
480 return writeroutput_query_for_stream(This
->output
);
483 static HRESULT WINAPI
xmlwriter_GetProperty(IXmlWriter
*iface
, UINT property
, LONG_PTR
*value
)
485 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
487 TRACE("(%p)->(%s %p)\n", This
, debugstr_writer_prop(property
), value
);
489 if (!value
) return E_INVALIDARG
;
493 case XmlWriterProperty_Indent
:
494 *value
= This
->indent
;
496 case XmlWriterProperty_ByteOrderMark
:
499 case XmlWriterProperty_OmitXmlDeclaration
:
500 *value
= This
->omitxmldecl
;
502 case XmlWriterProperty_ConformanceLevel
:
503 *value
= This
->conformance
;
506 FIXME("Unimplemented property (%u)\n", property
);
513 static HRESULT WINAPI
xmlwriter_SetProperty(IXmlWriter
*iface
, UINT property
, LONG_PTR value
)
515 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
517 TRACE("(%p)->(%s %lu)\n", This
, debugstr_writer_prop(property
), value
);
521 case XmlWriterProperty_ByteOrderMark
:
524 case XmlWriterProperty_OmitXmlDeclaration
:
525 This
->omitxmldecl
= !!value
;
528 FIXME("Unimplemented property (%u)\n", property
);
535 static HRESULT WINAPI
xmlwriter_WriteAttributes(IXmlWriter
*iface
, IXmlReader
*pReader
,
536 BOOL fWriteDefaultAttributes
)
538 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
540 FIXME("%p %p %d\n", This
, pReader
, fWriteDefaultAttributes
);
545 static HRESULT WINAPI
xmlwriter_WriteAttributeString(IXmlWriter
*iface
, LPCWSTR pwszPrefix
,
546 LPCWSTR pwszLocalName
, LPCWSTR pwszNamespaceUri
,
549 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
551 FIXME("%p %s %s %s %s\n", This
, wine_dbgstr_w(pwszPrefix
), wine_dbgstr_w(pwszLocalName
),
552 wine_dbgstr_w(pwszNamespaceUri
), wine_dbgstr_w(pwszValue
));
557 static HRESULT WINAPI
xmlwriter_WriteCData(IXmlWriter
*iface
, LPCWSTR pwszText
)
559 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
561 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszText
));
566 static HRESULT WINAPI
xmlwriter_WriteCharEntity(IXmlWriter
*iface
, WCHAR wch
)
571 static HRESULT WINAPI
xmlwriter_WriteChars(IXmlWriter
*iface
, const WCHAR
*pwch
, UINT cwch
)
573 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
575 FIXME("%p %s %d\n", This
, wine_dbgstr_w(pwch
), cwch
);
580 static HRESULT WINAPI
xmlwriter_WriteComment(IXmlWriter
*iface
, LPCWSTR pwszComment
)
585 static HRESULT WINAPI
xmlwriter_WriteDocType(IXmlWriter
*iface
, LPCWSTR pwszName
, LPCWSTR pwszPublicId
,
586 LPCWSTR pwszSystemId
, LPCWSTR pwszSubset
)
588 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
590 FIXME("%p %s %s %s %s\n", This
, wine_dbgstr_w(pwszName
), wine_dbgstr_w(pwszPublicId
),
591 wine_dbgstr_w(pwszSystemId
), wine_dbgstr_w(pwszSubset
));
596 static HRESULT WINAPI
xmlwriter_WriteElementString(IXmlWriter
*iface
, LPCWSTR prefix
,
597 LPCWSTR local_name
, LPCWSTR uri
, LPCWSTR value
)
599 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
601 TRACE("(%p)->(%s %s %s %s)\n", This
, wine_dbgstr_w(prefix
), wine_dbgstr_w(local_name
),
602 wine_dbgstr_w(uri
), wine_dbgstr_w(value
));
606 case XmlWriterState_Initial
:
608 case XmlWriterState_ElemStarted
:
609 writer_close_starttag(This
);
615 write_encoding_bom(This
);
616 write_output_buffer(This
->output
, ltW
, ARRAY_SIZE(ltW
));
617 write_output_qname(This
->output
, prefix
, local_name
);
618 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
621 write_output_buffer(This
->output
, value
, -1);
623 write_output_buffer(This
->output
, closeelementW
, ARRAY_SIZE(closeelementW
));
624 write_output_qname(This
->output
, prefix
, local_name
);
625 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
626 This
->state
= XmlWriterState_Content
;
631 static HRESULT WINAPI
xmlwriter_WriteEndDocument(IXmlWriter
*iface
)
633 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
640 static HRESULT WINAPI
xmlwriter_WriteEndElement(IXmlWriter
*iface
)
642 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
643 struct element
*element
;
647 element
= pop_element(This
);
649 return WR_E_INVALIDACTION
;
651 if (This
->starttagopen
) {
652 static WCHAR closetagW
[] = {' ','/','>'};
653 write_output_buffer(This
->output
, closetagW
, ARRAY_SIZE(closetagW
));
654 This
->starttagopen
= FALSE
;
657 /* write full end tag */
658 write_output_buffer(This
->output
, closeelementW
, ARRAY_SIZE(closeelementW
));
659 write_output_buffer(This
->output
, element
->qname
, element
->len
);
660 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
666 static HRESULT WINAPI
xmlwriter_WriteEntityRef(IXmlWriter
*iface
, LPCWSTR pwszName
)
668 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
670 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszName
));
675 static HRESULT WINAPI
xmlwriter_WriteFullEndElement(IXmlWriter
*iface
)
677 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
678 struct element
*element
;
682 element
= pop_element(This
);
684 return WR_E_INVALIDACTION
;
686 /* write full end tag */
687 write_output_buffer(This
->output
, closeelementW
, ARRAY_SIZE(closeelementW
));
688 write_output_buffer(This
->output
, element
->qname
, element
->len
);
689 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
690 This
->starttagopen
= FALSE
;
695 static HRESULT WINAPI
xmlwriter_WriteName(IXmlWriter
*iface
, LPCWSTR pwszName
)
697 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
699 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszName
));
704 static HRESULT WINAPI
xmlwriter_WriteNmToken(IXmlWriter
*iface
, LPCWSTR pwszNmToken
)
706 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
708 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszNmToken
));
713 static HRESULT WINAPI
xmlwriter_WriteNode(IXmlWriter
*iface
, IXmlReader
*pReader
,
714 BOOL fWriteDefaultAttributes
)
716 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
718 FIXME("%p %p %d\n", This
, pReader
, fWriteDefaultAttributes
);
723 static HRESULT WINAPI
xmlwriter_WriteNodeShallow(IXmlWriter
*iface
, IXmlReader
*pReader
,
724 BOOL fWriteDefaultAttributes
)
726 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
728 FIXME("%p %p %d\n", This
, pReader
, fWriteDefaultAttributes
);
733 static HRESULT WINAPI
xmlwriter_WriteProcessingInstruction(IXmlWriter
*iface
, LPCWSTR name
,
736 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
737 static const WCHAR xmlW
[] = {'x','m','l',0};
738 static const WCHAR openpiW
[] = {'<','?'};
739 static const WCHAR spaceW
[] = {' '};
741 TRACE("(%p)->(%s %s)\n", This
, wine_dbgstr_w(name
), wine_dbgstr_w(text
));
745 case XmlWriterState_Initial
:
747 case XmlWriterState_DocStarted
:
748 if (!strcmpW(name
, xmlW
))
749 return WR_E_INVALIDACTION
;
751 case XmlWriterState_ElemStarted
:
752 return WR_E_INVALIDACTION
;
757 write_encoding_bom(This
);
758 write_output_buffer(This
->output
, openpiW
, ARRAY_SIZE(openpiW
));
759 write_output_buffer(This
->output
, name
, -1);
760 write_output_buffer(This
->output
, spaceW
, ARRAY_SIZE(spaceW
));
761 write_output_buffer(This
->output
, text
, -1);
762 write_output_buffer(This
->output
, closepiW
, ARRAY_SIZE(closepiW
));
764 if (!strcmpW(name
, xmlW
))
765 This
->state
= XmlWriterState_PIDocStarted
;
770 static HRESULT WINAPI
xmlwriter_WriteQualifiedName(IXmlWriter
*iface
, LPCWSTR pwszLocalName
,
771 LPCWSTR pwszNamespaceUri
)
773 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
775 FIXME("%p %s %s\n", This
, wine_dbgstr_w(pwszLocalName
), wine_dbgstr_w(pwszNamespaceUri
));
780 static HRESULT WINAPI
xmlwriter_WriteRaw(IXmlWriter
*iface
, LPCWSTR pwszData
)
782 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
784 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszData
));
789 static HRESULT WINAPI
xmlwriter_WriteRawChars(IXmlWriter
*iface
, const WCHAR
*pwch
, UINT cwch
)
791 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
793 FIXME("%p %s %d\n", This
, wine_dbgstr_w(pwch
), cwch
);
798 static HRESULT WINAPI
xmlwriter_WriteStartDocument(IXmlWriter
*iface
, XmlStandalone standalone
)
800 static const WCHAR versionW
[] = {'<','?','x','m','l',' ','v','e','r','s','i','o','n','=','"','1','.','0','"'};
801 static const WCHAR encodingW
[] = {' ','e','n','c','o','d','i','n','g','='};
802 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
804 TRACE("(%p)->(%d)\n", This
, standalone
);
808 case XmlWriterState_Initial
:
810 case XmlWriterState_PIDocStarted
:
811 This
->state
= XmlWriterState_DocStarted
;
813 case XmlWriterState_DocStarted
:
814 case XmlWriterState_ElemStarted
:
815 return WR_E_INVALIDACTION
;
820 write_encoding_bom(This
);
821 This
->state
= XmlWriterState_DocStarted
;
822 if (This
->omitxmldecl
) return S_OK
;
825 write_output_buffer(This
->output
, versionW
, ARRAY_SIZE(versionW
));
828 write_output_buffer(This
->output
, encodingW
, ARRAY_SIZE(encodingW
));
829 write_output_buffer_quoted(This
->output
, get_encoding_name(This
->output
->encoding
), -1);
832 if (standalone
== XmlStandalone_Omit
)
833 write_output_buffer(This
->output
, closepiW
, ARRAY_SIZE(closepiW
));
835 static const WCHAR standaloneW
[] = {' ','s','t','a','n','d','a','l','o','n','e','=','\"'};
836 static const WCHAR yesW
[] = {'y','e','s','\"','?','>'};
837 static const WCHAR noW
[] = {'n','o','\"','?','>'};
839 write_output_buffer(This
->output
, standaloneW
, ARRAY_SIZE(standaloneW
));
840 if (standalone
== XmlStandalone_Yes
)
841 write_output_buffer(This
->output
, yesW
, ARRAY_SIZE(yesW
));
843 write_output_buffer(This
->output
, noW
, ARRAY_SIZE(noW
));
849 static HRESULT WINAPI
xmlwriter_WriteStartElement(IXmlWriter
*iface
, LPCWSTR prefix
, LPCWSTR local_name
, LPCWSTR uri
)
851 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
852 struct element
*element
;
854 TRACE("(%p)->(%s %s %s)\n", This
, wine_dbgstr_w(prefix
), wine_dbgstr_w(local_name
), wine_dbgstr_w(uri
));
856 if (This
->state
== XmlWriterState_Initial
)
862 /* close pending element */
863 if (This
->starttagopen
)
864 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
866 element
= alloc_element(This
, prefix
, local_name
);
868 return E_OUTOFMEMORY
;
870 write_encoding_bom(This
);
871 This
->state
= XmlWriterState_ElemStarted
;
872 This
->starttagopen
= TRUE
;
874 push_element(This
, element
);
876 write_output_buffer(This
->output
, ltW
, ARRAY_SIZE(ltW
));
877 write_output_qname(This
->output
, prefix
, local_name
);
882 static HRESULT WINAPI
xmlwriter_WriteString(IXmlWriter
*iface
, LPCWSTR pwszText
)
884 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
886 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszText
));
891 static HRESULT WINAPI
xmlwriter_WriteSurrogateCharEntity(IXmlWriter
*iface
, WCHAR wchLow
, WCHAR wchHigh
)
893 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
895 FIXME("%p %d %d\n", This
, wchLow
, wchHigh
);
900 static HRESULT WINAPI
xmlwriter_WriteWhitespace(IXmlWriter
*iface
, LPCWSTR pwszWhitespace
)
902 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
904 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszWhitespace
));
909 static HRESULT WINAPI
xmlwriter_Flush(IXmlWriter
*iface
)
911 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
915 return writeroutput_flush_stream(This
->output
);
918 static const struct IXmlWriterVtbl xmlwriter_vtbl
=
920 xmlwriter_QueryInterface
,
924 xmlwriter_GetProperty
,
925 xmlwriter_SetProperty
,
926 xmlwriter_WriteAttributes
,
927 xmlwriter_WriteAttributeString
,
928 xmlwriter_WriteCData
,
929 xmlwriter_WriteCharEntity
,
930 xmlwriter_WriteChars
,
931 xmlwriter_WriteComment
,
932 xmlwriter_WriteDocType
,
933 xmlwriter_WriteElementString
,
934 xmlwriter_WriteEndDocument
,
935 xmlwriter_WriteEndElement
,
936 xmlwriter_WriteEntityRef
,
937 xmlwriter_WriteFullEndElement
,
939 xmlwriter_WriteNmToken
,
941 xmlwriter_WriteNodeShallow
,
942 xmlwriter_WriteProcessingInstruction
,
943 xmlwriter_WriteQualifiedName
,
945 xmlwriter_WriteRawChars
,
946 xmlwriter_WriteStartDocument
,
947 xmlwriter_WriteStartElement
,
948 xmlwriter_WriteString
,
949 xmlwriter_WriteSurrogateCharEntity
,
950 xmlwriter_WriteWhitespace
,
954 /** IXmlWriterOutput **/
955 static HRESULT WINAPI
xmlwriteroutput_QueryInterface(IXmlWriterOutput
*iface
, REFIID riid
, void** ppvObject
)
957 xmlwriteroutput
*This
= impl_from_IXmlWriterOutput(iface
);
959 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppvObject
);
961 if (IsEqualGUID(riid
, &IID_IXmlWriterOutput
) ||
962 IsEqualGUID(riid
, &IID_IUnknown
))
968 WARN("interface %s not implemented\n", debugstr_guid(riid
));
970 return E_NOINTERFACE
;
973 IUnknown_AddRef(iface
);
978 static ULONG WINAPI
xmlwriteroutput_AddRef(IXmlWriterOutput
*iface
)
980 xmlwriteroutput
*This
= impl_from_IXmlWriterOutput(iface
);
981 ULONG ref
= InterlockedIncrement(&This
->ref
);
982 TRACE("(%p)->(%d)\n", This
, ref
);
986 static ULONG WINAPI
xmlwriteroutput_Release(IXmlWriterOutput
*iface
)
988 xmlwriteroutput
*This
= impl_from_IXmlWriterOutput(iface
);
989 LONG ref
= InterlockedDecrement(&This
->ref
);
991 TRACE("(%p)->(%d)\n", This
, ref
);
995 IMalloc
*imalloc
= This
->imalloc
;
996 if (This
->output
) IUnknown_Release(This
->output
);
997 if (This
->stream
) ISequentialStream_Release(This
->stream
);
998 free_output_buffer(This
);
999 writeroutput_free(This
, This
);
1000 if (imalloc
) IMalloc_Release(imalloc
);
1006 static const struct IUnknownVtbl xmlwriteroutputvtbl
=
1008 xmlwriteroutput_QueryInterface
,
1009 xmlwriteroutput_AddRef
,
1010 xmlwriteroutput_Release
1013 HRESULT WINAPI
CreateXmlWriter(REFIID riid
, void **obj
, IMalloc
*imalloc
)
1017 TRACE("(%s, %p, %p)\n", wine_dbgstr_guid(riid
), obj
, imalloc
);
1019 if (!IsEqualGUID(riid
, &IID_IXmlWriter
))
1021 ERR("Unexpected IID requested -> (%s)\n", wine_dbgstr_guid(riid
));
1026 writer
= IMalloc_Alloc(imalloc
, sizeof(*writer
));
1028 writer
= heap_alloc(sizeof(*writer
));
1029 if(!writer
) return E_OUTOFMEMORY
;
1031 writer
->IXmlWriter_iface
.lpVtbl
= &xmlwriter_vtbl
;
1033 writer
->imalloc
= imalloc
;
1034 if (imalloc
) IMalloc_AddRef(imalloc
);
1035 writer
->output
= NULL
;
1036 writer
->indent
= FALSE
;
1038 writer
->omitxmldecl
= FALSE
;
1039 writer
->conformance
= XmlConformanceLevel_Document
;
1040 writer
->state
= XmlWriterState_Initial
;
1041 writer
->bomwritten
= FALSE
;
1042 writer
->starttagopen
= FALSE
;
1043 list_init(&writer
->elements
);
1045 *obj
= &writer
->IXmlWriter_iface
;
1047 TRACE("returning iface %p\n", *obj
);
1052 HRESULT WINAPI
CreateXmlWriterOutputWithEncodingName(IUnknown
*stream
,
1055 IXmlWriterOutput
**output
)
1057 static const WCHAR utf8W
[] = {'U','T','F','-','8',0};
1058 xmlwriteroutput
*writeroutput
;
1061 TRACE("%p %p %s %p\n", stream
, imalloc
, debugstr_w(encoding
), output
);
1063 if (!stream
|| !output
) return E_INVALIDARG
;
1068 writeroutput
= IMalloc_Alloc(imalloc
, sizeof(*writeroutput
));
1070 writeroutput
= heap_alloc(sizeof(*writeroutput
));
1071 if(!writeroutput
) return E_OUTOFMEMORY
;
1073 writeroutput
->IXmlWriterOutput_iface
.lpVtbl
= &xmlwriteroutputvtbl
;
1074 writeroutput
->ref
= 1;
1075 writeroutput
->imalloc
= imalloc
;
1076 if (imalloc
) IMalloc_AddRef(imalloc
);
1077 writeroutput
->encoding
= parse_encoding_name(encoding
? encoding
: utf8W
, -1);
1078 writeroutput
->stream
= NULL
;
1079 hr
= init_output_buffer(writeroutput
);
1081 IUnknown_Release(&writeroutput
->IXmlWriterOutput_iface
);
1085 IUnknown_QueryInterface(stream
, &IID_IUnknown
, (void**)&writeroutput
->output
);
1087 *output
= &writeroutput
->IXmlWriterOutput_iface
;
1089 TRACE("returning iface %p\n", *output
);