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
22 #include "xmllite_private.h"
24 #include <wine/list.h>
25 #include <wine/unicode.h>
27 /* not defined in public headers */
28 DEFINE_GUID(IID_IXmlWriterOutput
, 0xc1131708, 0x0f59, 0x477f, 0x93, 0x59, 0x7d, 0x33, 0x24, 0x51, 0xbc, 0x1a);
30 #define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0]))
32 static const WCHAR closeelementW
[] = {'<','/'};
33 static const WCHAR closepiW
[] = {'?','>'};
34 static const WCHAR ltW
[] = {'<'};
35 static const WCHAR gtW
[] = {'>'};
36 static const WCHAR spaceW
[] = {' '};
41 unsigned int allocated
;
48 XmlWriterState_Initial
, /* output is not set yet */
49 XmlWriterState_Ready
, /* SetOutput() was called, ready to start */
50 XmlWriterState_PIDocStarted
, /* document was started with manually added 'xml' PI */
51 XmlWriterState_DocStarted
, /* document was started with WriteStartDocument() */
52 XmlWriterState_ElemStarted
, /* writing element */
53 XmlWriterState_Content
, /* content is accepted at this point */
54 XmlWriterState_DocClosed
/* WriteEndDocument was called */
59 IXmlWriterOutput IXmlWriterOutput_iface
;
62 ISequentialStream
*stream
;
64 xml_encoding encoding
;
65 struct output_buffer buffer
;
68 static const struct IUnknownVtbl xmlwriteroutputvtbl
;
74 unsigned int len
; /* qname length in chars */
77 typedef struct _xmlwriter
79 IXmlWriter IXmlWriter_iface
;
82 xmlwriteroutput
*output
;
86 XmlConformanceLevel conformance
;
93 static inline xmlwriter
*impl_from_IXmlWriter(IXmlWriter
*iface
)
95 return CONTAINING_RECORD(iface
, xmlwriter
, IXmlWriter_iface
);
98 static inline xmlwriteroutput
*impl_from_IXmlWriterOutput(IXmlWriterOutput
*iface
)
100 return CONTAINING_RECORD(iface
, xmlwriteroutput
, IXmlWriterOutput_iface
);
103 static const char *debugstr_writer_prop(XmlWriterProperty prop
)
105 static const char * const prop_names
[] =
110 "OmitXmlDeclaration",
114 if (prop
> _XmlWriterProperty_Last
)
115 return wine_dbg_sprintf("unknown property=%d", prop
);
117 return prop_names
[prop
];
120 /* writer output memory allocation functions */
121 static inline void *writeroutput_alloc(xmlwriteroutput
*output
, size_t len
)
123 return m_alloc(output
->imalloc
, len
);
126 static inline void writeroutput_free(xmlwriteroutput
*output
, void *mem
)
128 m_free(output
->imalloc
, mem
);
131 static inline void *writeroutput_realloc(xmlwriteroutput
*output
, void *mem
, size_t len
)
133 return m_realloc(output
->imalloc
, mem
, len
);
136 /* writer memory allocation functions */
137 static inline void *writer_alloc(xmlwriter
*writer
, size_t len
)
139 return m_alloc(writer
->imalloc
, len
);
142 static inline void writer_free(xmlwriter
*writer
, void *mem
)
144 m_free(writer
->imalloc
, mem
);
147 static struct element
*alloc_element(xmlwriter
*writer
, const WCHAR
*prefix
, const WCHAR
*local
)
152 ret
= writer_alloc(writer
, sizeof(*ret
));
153 if (!ret
) return ret
;
155 len
= prefix
? strlenW(prefix
) + 1 /* ':' */ : 0;
156 len
+= strlenW(local
);
158 ret
->qname
= writer_alloc(writer
, (len
+ 1)*sizeof(WCHAR
));
161 static const WCHAR colonW
[] = {':',0};
162 strcpyW(ret
->qname
, prefix
);
163 strcatW(ret
->qname
, colonW
);
167 strcatW(ret
->qname
, local
);
172 static void free_element(xmlwriter
*writer
, struct element
*element
)
174 writer_free(writer
, element
->qname
);
175 writer_free(writer
, element
);
178 static void push_element(xmlwriter
*writer
, struct element
*element
)
180 list_add_head(&writer
->elements
, &element
->entry
);
183 static struct element
*pop_element(xmlwriter
*writer
)
185 struct element
*element
= LIST_ENTRY(list_head(&writer
->elements
), struct element
, entry
);
188 list_remove(&element
->entry
);
193 static HRESULT
init_output_buffer(xmlwriteroutput
*output
)
195 struct output_buffer
*buffer
= &output
->buffer
;
196 const int initial_len
= 0x2000;
200 hr
= get_code_page(output
->encoding
, &cp
);
201 if (FAILED(hr
)) return hr
;
203 buffer
->data
= writeroutput_alloc(output
, initial_len
);
204 if (!buffer
->data
) return E_OUTOFMEMORY
;
206 memset(buffer
->data
, 0, 4);
207 buffer
->allocated
= initial_len
;
209 buffer
->codepage
= cp
;
214 static void free_output_buffer(xmlwriteroutput
*output
)
216 struct output_buffer
*buffer
= &output
->buffer
;
217 writeroutput_free(output
, buffer
->data
);
219 buffer
->allocated
= 0;
223 static HRESULT
grow_output_buffer(xmlwriteroutput
*output
, int length
)
225 struct output_buffer
*buffer
= &output
->buffer
;
226 /* grow if needed, plus 4 bytes to be sure null terminator will fit in */
227 if (buffer
->allocated
< buffer
->written
+ length
+ 4) {
228 int grown_size
= max(2*buffer
->allocated
, buffer
->allocated
+ length
);
229 char *ptr
= writeroutput_realloc(output
, buffer
->data
, grown_size
);
230 if (!ptr
) return E_OUTOFMEMORY
;
232 buffer
->allocated
= grown_size
;
238 static HRESULT
write_output_buffer(xmlwriteroutput
*output
, const WCHAR
*data
, int len
)
240 struct output_buffer
*buffer
= &output
->buffer
;
245 if (buffer
->codepage
!= ~0) {
246 length
= WideCharToMultiByte(buffer
->codepage
, 0, data
, len
, NULL
, 0, NULL
, NULL
);
247 hr
= grow_output_buffer(output
, length
);
248 if (FAILED(hr
)) return hr
;
249 ptr
= buffer
->data
+ buffer
->written
;
250 length
= WideCharToMultiByte(buffer
->codepage
, 0, data
, len
, ptr
, length
, NULL
, NULL
);
251 buffer
->written
+= len
== -1 ? length
-1 : length
;
254 /* WCHAR data just copied */
255 length
= len
== -1 ? strlenW(data
) : len
;
257 length
*= sizeof(WCHAR
);
259 hr
= grow_output_buffer(output
, length
);
260 if (FAILED(hr
)) return hr
;
261 ptr
= buffer
->data
+ buffer
->written
;
263 memcpy(ptr
, data
, length
);
264 buffer
->written
+= length
;
266 /* null termination */
274 static HRESULT
write_output_buffer_quoted(xmlwriteroutput
*output
, const WCHAR
*data
, int len
)
276 static const WCHAR quoteW
[] = {'"'};
277 write_output_buffer(output
, quoteW
, ARRAY_SIZE(quoteW
));
278 write_output_buffer(output
, data
, len
);
279 write_output_buffer(output
, quoteW
, ARRAY_SIZE(quoteW
));
283 /* TODO: test if we need to validate char range */
284 static HRESULT
write_output_qname(xmlwriteroutput
*output
, const WCHAR
*prefix
, const WCHAR
*local_name
)
287 static const WCHAR colW
[] = {':'};
288 write_output_buffer(output
, prefix
, -1);
289 write_output_buffer(output
, colW
, ARRAY_SIZE(colW
));
292 write_output_buffer(output
, local_name
, -1);
297 static void writeroutput_release_stream(xmlwriteroutput
*writeroutput
)
299 if (writeroutput
->stream
) {
300 ISequentialStream_Release(writeroutput
->stream
);
301 writeroutput
->stream
= NULL
;
305 static inline HRESULT
writeroutput_query_for_stream(xmlwriteroutput
*writeroutput
)
309 writeroutput_release_stream(writeroutput
);
310 hr
= IUnknown_QueryInterface(writeroutput
->output
, &IID_IStream
, (void**)&writeroutput
->stream
);
312 hr
= IUnknown_QueryInterface(writeroutput
->output
, &IID_ISequentialStream
, (void**)&writeroutput
->stream
);
317 static HRESULT
writeroutput_flush_stream(xmlwriteroutput
*output
)
319 struct output_buffer
*buffer
;
320 ULONG written
, offset
= 0;
323 if (!output
|| !output
->stream
)
326 buffer
= &output
->buffer
;
328 /* It will loop forever until everything is written or an error occurred. */
331 hr
= ISequentialStream_Write(output
->stream
, buffer
->data
+ offset
, buffer
->written
, &written
);
333 WARN("write to stream failed (0x%08x)\n", hr
);
339 buffer
->written
-= written
;
340 } while (buffer
->written
> 0);
345 static HRESULT
write_encoding_bom(xmlwriter
*writer
)
347 if (!writer
->bom
|| writer
->bomwritten
) return S_OK
;
349 if (writer
->output
->encoding
== XmlEncoding_UTF16
) {
350 static const char utf16bom
[] = {0xff, 0xfe};
351 struct output_buffer
*buffer
= &writer
->output
->buffer
;
352 int len
= sizeof(utf16bom
);
355 hr
= grow_output_buffer(writer
->output
, len
);
356 if (FAILED(hr
)) return hr
;
357 memcpy(buffer
->data
+ buffer
->written
, utf16bom
, len
);
358 buffer
->written
+= len
;
361 writer
->bomwritten
= TRUE
;
365 static HRESULT
write_xmldecl(xmlwriter
*writer
, XmlStandalone standalone
)
367 static const WCHAR versionW
[] = {'<','?','x','m','l',' ','v','e','r','s','i','o','n','=','"','1','.','0','"'};
368 static const WCHAR encodingW
[] = {' ','e','n','c','o','d','i','n','g','='};
370 write_encoding_bom(writer
);
371 writer
->state
= XmlWriterState_DocStarted
;
372 if (writer
->omitxmldecl
) return S_OK
;
375 write_output_buffer(writer
->output
, versionW
, ARRAY_SIZE(versionW
));
378 write_output_buffer(writer
->output
, encodingW
, ARRAY_SIZE(encodingW
));
379 write_output_buffer_quoted(writer
->output
, get_encoding_name(writer
->output
->encoding
), -1);
382 if (standalone
== XmlStandalone_Omit
)
383 write_output_buffer(writer
->output
, closepiW
, ARRAY_SIZE(closepiW
));
385 static const WCHAR standaloneW
[] = {' ','s','t','a','n','d','a','l','o','n','e','=','\"'};
386 static const WCHAR yesW
[] = {'y','e','s','\"','?','>'};
387 static const WCHAR noW
[] = {'n','o','\"','?','>'};
389 write_output_buffer(writer
->output
, standaloneW
, ARRAY_SIZE(standaloneW
));
390 if (standalone
== XmlStandalone_Yes
)
391 write_output_buffer(writer
->output
, yesW
, ARRAY_SIZE(yesW
));
393 write_output_buffer(writer
->output
, noW
, ARRAY_SIZE(noW
));
399 static HRESULT
writer_close_starttag(xmlwriter
*writer
)
403 if (!writer
->starttagopen
) return S_OK
;
404 hr
= write_output_buffer(writer
->output
, gtW
, ARRAY_SIZE(gtW
));
405 writer
->starttagopen
= FALSE
;
406 writer
->state
= XmlWriterState_Content
;
410 static HRESULT WINAPI
xmlwriter_QueryInterface(IXmlWriter
*iface
, REFIID riid
, void **ppvObject
)
412 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
414 TRACE("%p %s %p\n", This
, debugstr_guid(riid
), ppvObject
);
416 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
417 IsEqualGUID(riid
, &IID_IXmlWriter
))
422 IXmlWriter_AddRef(iface
);
427 static ULONG WINAPI
xmlwriter_AddRef(IXmlWriter
*iface
)
429 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
431 return InterlockedIncrement(&This
->ref
);
434 static ULONG WINAPI
xmlwriter_Release(IXmlWriter
*iface
)
436 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
441 ref
= InterlockedDecrement(&This
->ref
);
443 struct element
*element
, *element2
;
444 IMalloc
*imalloc
= This
->imalloc
;
446 IXmlWriter_Flush(iface
);
447 if (This
->output
) IUnknown_Release(&This
->output
->IXmlWriterOutput_iface
);
450 LIST_FOR_EACH_ENTRY_SAFE(element
, element2
, &This
->elements
, struct element
, entry
) {
451 list_remove(&element
->entry
);
452 free_element(This
, element
);
455 writer_free(This
, This
);
456 if (imalloc
) IMalloc_Release(imalloc
);
462 /*** IXmlWriter methods ***/
463 static HRESULT WINAPI
xmlwriter_SetOutput(IXmlWriter
*iface
, IUnknown
*output
)
465 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
466 IXmlWriterOutput
*writeroutput
;
469 TRACE("(%p)->(%p)\n", This
, output
);
472 writeroutput_release_stream(This
->output
);
473 IUnknown_Release(&This
->output
->IXmlWriterOutput_iface
);
475 This
->bomwritten
= FALSE
;
478 /* just reset current output */
480 This
->state
= XmlWriterState_Initial
;
484 /* now try IXmlWriterOutput, ISequentialStream, IStream */
485 hr
= IUnknown_QueryInterface(output
, &IID_IXmlWriterOutput
, (void**)&writeroutput
);
487 if (writeroutput
->lpVtbl
== &xmlwriteroutputvtbl
)
488 This
->output
= impl_from_IXmlWriterOutput(writeroutput
);
490 ERR("got external IXmlWriterOutput implementation: %p, vtbl=%p\n",
491 writeroutput
, writeroutput
->lpVtbl
);
492 IUnknown_Release(writeroutput
);
498 if (hr
!= S_OK
|| !writeroutput
) {
499 /* create IXmlWriterOutput basing on supplied interface */
500 hr
= CreateXmlWriterOutputWithEncodingName(output
, This
->imalloc
, NULL
, &writeroutput
);
501 if (hr
!= S_OK
) return hr
;
502 This
->output
= impl_from_IXmlWriterOutput(writeroutput
);
505 This
->state
= XmlWriterState_Ready
;
506 return writeroutput_query_for_stream(This
->output
);
509 static HRESULT WINAPI
xmlwriter_GetProperty(IXmlWriter
*iface
, UINT property
, LONG_PTR
*value
)
511 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
513 TRACE("(%p)->(%s %p)\n", This
, debugstr_writer_prop(property
), value
);
515 if (!value
) return E_INVALIDARG
;
519 case XmlWriterProperty_Indent
:
520 *value
= This
->indent
;
522 case XmlWriterProperty_ByteOrderMark
:
525 case XmlWriterProperty_OmitXmlDeclaration
:
526 *value
= This
->omitxmldecl
;
528 case XmlWriterProperty_ConformanceLevel
:
529 *value
= This
->conformance
;
532 FIXME("Unimplemented property (%u)\n", property
);
539 static HRESULT WINAPI
xmlwriter_SetProperty(IXmlWriter
*iface
, UINT property
, LONG_PTR value
)
541 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
543 TRACE("(%p)->(%s %lu)\n", This
, debugstr_writer_prop(property
), value
);
547 case XmlWriterProperty_ByteOrderMark
:
550 case XmlWriterProperty_OmitXmlDeclaration
:
551 This
->omitxmldecl
= !!value
;
554 FIXME("Unimplemented property (%u)\n", property
);
561 static HRESULT WINAPI
xmlwriter_WriteAttributes(IXmlWriter
*iface
, IXmlReader
*pReader
,
562 BOOL fWriteDefaultAttributes
)
564 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
566 FIXME("%p %p %d\n", This
, pReader
, fWriteDefaultAttributes
);
571 static HRESULT WINAPI
xmlwriter_WriteAttributeString(IXmlWriter
*iface
, LPCWSTR pwszPrefix
,
572 LPCWSTR pwszLocalName
, LPCWSTR pwszNamespaceUri
,
575 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
577 FIXME("%p %s %s %s %s\n", This
, wine_dbgstr_w(pwszPrefix
), wine_dbgstr_w(pwszLocalName
),
578 wine_dbgstr_w(pwszNamespaceUri
), wine_dbgstr_w(pwszValue
));
582 case XmlWriterState_Initial
:
584 case XmlWriterState_Ready
:
585 case XmlWriterState_DocClosed
:
586 This
->state
= XmlWriterState_DocClosed
;
587 return WR_E_INVALIDACTION
;
595 static void write_cdata_section(xmlwriteroutput
*output
, const WCHAR
*data
, int len
)
597 static const WCHAR cdataopenW
[] = {'<','!','[','C','D','A','T','A','['};
598 static const WCHAR cdatacloseW
[] = {']',']','>'};
599 write_output_buffer(output
, cdataopenW
, ARRAY_SIZE(cdataopenW
));
601 write_output_buffer(output
, data
, len
);
602 write_output_buffer(output
, cdatacloseW
, ARRAY_SIZE(cdatacloseW
));
605 static HRESULT WINAPI
xmlwriter_WriteCData(IXmlWriter
*iface
, LPCWSTR data
)
607 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
610 TRACE("%p %s\n", This
, debugstr_w(data
));
614 case XmlWriterState_Initial
:
616 case XmlWriterState_ElemStarted
:
617 writer_close_starttag(This
);
619 case XmlWriterState_Ready
:
620 case XmlWriterState_DocClosed
:
621 This
->state
= XmlWriterState_DocClosed
;
622 return WR_E_INVALIDACTION
;
627 len
= data
? strlenW(data
) : 0;
630 write_cdata_section(This
->output
, NULL
, 0);
632 static const WCHAR cdatacloseW
[] = {']',']','>',0};
634 const WCHAR
*str
= strstrW(data
, cdatacloseW
);
637 write_cdata_section(This
->output
, data
, str
- data
);
642 write_cdata_section(This
->output
, data
, len
);
651 static HRESULT WINAPI
xmlwriter_WriteCharEntity(IXmlWriter
*iface
, WCHAR ch
)
653 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
655 FIXME("%p %x\n", This
, ch
);
659 case XmlWriterState_Initial
:
661 case XmlWriterState_DocClosed
:
662 return WR_E_INVALIDACTION
;
670 static HRESULT WINAPI
xmlwriter_WriteChars(IXmlWriter
*iface
, const WCHAR
*pwch
, UINT cwch
)
672 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
674 FIXME("%p %s %d\n", This
, wine_dbgstr_w(pwch
), cwch
);
678 case XmlWriterState_Initial
:
680 case XmlWriterState_DocClosed
:
681 return WR_E_INVALIDACTION
;
689 static HRESULT WINAPI
xmlwriter_WriteComment(IXmlWriter
*iface
, LPCWSTR comment
)
691 static const WCHAR copenW
[] = {'<','!','-','-'};
692 static const WCHAR ccloseW
[] = {'-','-','>'};
693 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
695 TRACE("%p %s\n", This
, debugstr_w(comment
));
699 case XmlWriterState_Initial
:
701 case XmlWriterState_ElemStarted
:
702 writer_close_starttag(This
);
704 case XmlWriterState_DocClosed
:
705 return WR_E_INVALIDACTION
;
710 write_output_buffer(This
->output
, copenW
, ARRAY_SIZE(copenW
));
712 int len
= strlenW(comment
), i
;
714 /* Make sure there's no two hyphen sequences in a string, space is used as a separator to produce compliant
717 for (i
= 0; i
< len
; i
++) {
718 write_output_buffer(This
->output
, comment
+ i
, 1);
719 if (comment
[i
] == '-' && (i
+ 1 < len
) && comment
[i
+1] == '-')
720 write_output_buffer(This
->output
, spaceW
, ARRAY_SIZE(spaceW
));
724 write_output_buffer(This
->output
, comment
, len
);
726 if (len
&& comment
[len
-1] == '-')
727 write_output_buffer(This
->output
, spaceW
, ARRAY_SIZE(spaceW
));
729 write_output_buffer(This
->output
, ccloseW
, ARRAY_SIZE(ccloseW
));
734 static HRESULT WINAPI
xmlwriter_WriteDocType(IXmlWriter
*iface
, LPCWSTR pwszName
, LPCWSTR pwszPublicId
,
735 LPCWSTR pwszSystemId
, LPCWSTR pwszSubset
)
737 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
739 FIXME("%p %s %s %s %s\n", This
, wine_dbgstr_w(pwszName
), wine_dbgstr_w(pwszPublicId
),
740 wine_dbgstr_w(pwszSystemId
), wine_dbgstr_w(pwszSubset
));
745 static HRESULT WINAPI
xmlwriter_WriteElementString(IXmlWriter
*iface
, LPCWSTR prefix
,
746 LPCWSTR local_name
, LPCWSTR uri
, LPCWSTR value
)
748 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
750 TRACE("(%p)->(%s %s %s %s)\n", This
, wine_dbgstr_w(prefix
), wine_dbgstr_w(local_name
),
751 wine_dbgstr_w(uri
), wine_dbgstr_w(value
));
755 case XmlWriterState_Initial
:
757 case XmlWriterState_ElemStarted
:
758 writer_close_starttag(This
);
760 case XmlWriterState_Ready
:
761 case XmlWriterState_DocStarted
:
762 case XmlWriterState_PIDocStarted
:
765 This
->state
= XmlWriterState_DocClosed
;
766 return WR_E_INVALIDACTION
;
769 write_encoding_bom(This
);
770 write_output_buffer(This
->output
, ltW
, ARRAY_SIZE(ltW
));
771 write_output_qname(This
->output
, prefix
, local_name
);
772 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
775 write_output_buffer(This
->output
, value
, -1);
777 write_output_buffer(This
->output
, closeelementW
, ARRAY_SIZE(closeelementW
));
778 write_output_qname(This
->output
, prefix
, local_name
);
779 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
780 This
->state
= XmlWriterState_Content
;
785 static HRESULT WINAPI
xmlwriter_WriteEndDocument(IXmlWriter
*iface
)
787 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
793 case XmlWriterState_Initial
:
795 case XmlWriterState_Ready
:
796 case XmlWriterState_DocClosed
:
797 This
->state
= XmlWriterState_DocClosed
;
798 return WR_E_INVALIDACTION
;
803 /* empty element stack */
804 while (IXmlWriter_WriteEndElement(iface
) == S_OK
)
807 This
->state
= XmlWriterState_DocClosed
;
811 static HRESULT WINAPI
xmlwriter_WriteEndElement(IXmlWriter
*iface
)
813 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
814 struct element
*element
;
820 case XmlWriterState_Initial
:
822 case XmlWriterState_Ready
:
823 case XmlWriterState_DocClosed
:
824 This
->state
= XmlWriterState_DocClosed
;
825 return WR_E_INVALIDACTION
;
830 element
= pop_element(This
);
832 return WR_E_INVALIDACTION
;
834 if (This
->starttagopen
) {
835 static WCHAR closetagW
[] = {' ','/','>'};
836 write_output_buffer(This
->output
, closetagW
, ARRAY_SIZE(closetagW
));
837 This
->starttagopen
= FALSE
;
840 /* write full end tag */
841 write_output_buffer(This
->output
, closeelementW
, ARRAY_SIZE(closeelementW
));
842 write_output_buffer(This
->output
, element
->qname
, element
->len
);
843 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
849 static HRESULT WINAPI
xmlwriter_WriteEntityRef(IXmlWriter
*iface
, LPCWSTR pwszName
)
851 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
853 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszName
));
857 case XmlWriterState_Initial
:
859 case XmlWriterState_DocClosed
:
860 return WR_E_INVALIDACTION
;
868 static HRESULT WINAPI
xmlwriter_WriteFullEndElement(IXmlWriter
*iface
)
870 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
871 struct element
*element
;
877 case XmlWriterState_Initial
:
879 case XmlWriterState_Ready
:
880 case XmlWriterState_DocClosed
:
881 This
->state
= XmlWriterState_DocClosed
;
882 return WR_E_INVALIDACTION
;
887 element
= pop_element(This
);
889 return WR_E_INVALIDACTION
;
891 /* write full end tag */
892 write_output_buffer(This
->output
, closeelementW
, ARRAY_SIZE(closeelementW
));
893 write_output_buffer(This
->output
, element
->qname
, element
->len
);
894 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
895 This
->starttagopen
= FALSE
;
900 static HRESULT WINAPI
xmlwriter_WriteName(IXmlWriter
*iface
, LPCWSTR pwszName
)
902 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
904 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszName
));
908 case XmlWriterState_Initial
:
910 case XmlWriterState_Ready
:
911 case XmlWriterState_DocClosed
:
912 This
->state
= XmlWriterState_DocClosed
;
913 return WR_E_INVALIDACTION
;
921 static HRESULT WINAPI
xmlwriter_WriteNmToken(IXmlWriter
*iface
, LPCWSTR pwszNmToken
)
923 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
925 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszNmToken
));
929 case XmlWriterState_Initial
:
931 case XmlWriterState_Ready
:
932 case XmlWriterState_DocClosed
:
933 This
->state
= XmlWriterState_DocClosed
;
934 return WR_E_INVALIDACTION
;
942 static HRESULT WINAPI
xmlwriter_WriteNode(IXmlWriter
*iface
, IXmlReader
*pReader
,
943 BOOL fWriteDefaultAttributes
)
945 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
947 FIXME("%p %p %d\n", This
, pReader
, fWriteDefaultAttributes
);
952 static HRESULT WINAPI
xmlwriter_WriteNodeShallow(IXmlWriter
*iface
, IXmlReader
*pReader
,
953 BOOL fWriteDefaultAttributes
)
955 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
957 FIXME("%p %p %d\n", This
, pReader
, fWriteDefaultAttributes
);
962 static HRESULT WINAPI
xmlwriter_WriteProcessingInstruction(IXmlWriter
*iface
, LPCWSTR name
,
965 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
966 static const WCHAR xmlW
[] = {'x','m','l',0};
967 static const WCHAR openpiW
[] = {'<','?'};
969 TRACE("(%p)->(%s %s)\n", This
, wine_dbgstr_w(name
), wine_dbgstr_w(text
));
973 case XmlWriterState_Initial
:
975 case XmlWriterState_DocStarted
:
976 if (!strcmpW(name
, xmlW
))
977 return WR_E_INVALIDACTION
;
979 case XmlWriterState_ElemStarted
:
980 case XmlWriterState_DocClosed
:
981 return WR_E_INVALIDACTION
;
986 write_encoding_bom(This
);
987 write_output_buffer(This
->output
, openpiW
, ARRAY_SIZE(openpiW
));
988 write_output_buffer(This
->output
, name
, -1);
989 write_output_buffer(This
->output
, spaceW
, ARRAY_SIZE(spaceW
));
990 write_output_buffer(This
->output
, text
, -1);
991 write_output_buffer(This
->output
, closepiW
, ARRAY_SIZE(closepiW
));
993 if (!strcmpW(name
, xmlW
))
994 This
->state
= XmlWriterState_PIDocStarted
;
999 static HRESULT WINAPI
xmlwriter_WriteQualifiedName(IXmlWriter
*iface
, LPCWSTR pwszLocalName
,
1000 LPCWSTR pwszNamespaceUri
)
1002 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
1004 FIXME("%p %s %s\n", This
, wine_dbgstr_w(pwszLocalName
), wine_dbgstr_w(pwszNamespaceUri
));
1006 switch (This
->state
)
1008 case XmlWriterState_Initial
:
1009 return E_UNEXPECTED
;
1010 case XmlWriterState_DocClosed
:
1011 return WR_E_INVALIDACTION
;
1019 static HRESULT WINAPI
xmlwriter_WriteRaw(IXmlWriter
*iface
, LPCWSTR data
)
1021 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
1023 TRACE("%p %s\n", This
, debugstr_w(data
));
1028 switch (This
->state
)
1030 case XmlWriterState_Initial
:
1031 return E_UNEXPECTED
;
1032 case XmlWriterState_Ready
:
1033 write_xmldecl(This
, XmlStandalone_Omit
);
1035 case XmlWriterState_DocStarted
:
1036 case XmlWriterState_PIDocStarted
:
1039 This
->state
= XmlWriterState_DocClosed
;
1040 return WR_E_INVALIDACTION
;
1043 write_output_buffer(This
->output
, data
, -1);
1047 static HRESULT WINAPI
xmlwriter_WriteRawChars(IXmlWriter
*iface
, const WCHAR
*pwch
, UINT cwch
)
1049 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
1051 FIXME("%p %s %d\n", This
, wine_dbgstr_w(pwch
), cwch
);
1053 switch (This
->state
)
1055 case XmlWriterState_Initial
:
1056 return E_UNEXPECTED
;
1057 case XmlWriterState_DocClosed
:
1058 return WR_E_INVALIDACTION
;
1066 static HRESULT WINAPI
xmlwriter_WriteStartDocument(IXmlWriter
*iface
, XmlStandalone standalone
)
1068 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
1070 TRACE("(%p)->(%d)\n", This
, standalone
);
1072 switch (This
->state
)
1074 case XmlWriterState_Initial
:
1075 return E_UNEXPECTED
;
1076 case XmlWriterState_PIDocStarted
:
1077 This
->state
= XmlWriterState_DocStarted
;
1079 case XmlWriterState_Ready
:
1082 This
->state
= XmlWriterState_DocClosed
;
1083 return WR_E_INVALIDACTION
;
1086 return write_xmldecl(This
, standalone
);
1089 static HRESULT WINAPI
xmlwriter_WriteStartElement(IXmlWriter
*iface
, LPCWSTR prefix
, LPCWSTR local_name
, LPCWSTR uri
)
1091 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
1092 struct element
*element
;
1094 TRACE("(%p)->(%s %s %s)\n", This
, wine_dbgstr_w(prefix
), wine_dbgstr_w(local_name
), wine_dbgstr_w(uri
));
1097 return E_INVALIDARG
;
1099 switch (This
->state
)
1101 case XmlWriterState_Initial
:
1102 return E_UNEXPECTED
;
1103 case XmlWriterState_DocClosed
:
1104 return WR_E_INVALIDACTION
;
1109 /* close pending element */
1110 if (This
->starttagopen
)
1111 write_output_buffer(This
->output
, gtW
, ARRAY_SIZE(gtW
));
1113 element
= alloc_element(This
, prefix
, local_name
);
1115 return E_OUTOFMEMORY
;
1117 write_encoding_bom(This
);
1118 This
->state
= XmlWriterState_ElemStarted
;
1119 This
->starttagopen
= TRUE
;
1121 push_element(This
, element
);
1123 write_output_buffer(This
->output
, ltW
, ARRAY_SIZE(ltW
));
1124 write_output_qname(This
->output
, prefix
, local_name
);
1129 static HRESULT WINAPI
xmlwriter_WriteString(IXmlWriter
*iface
, LPCWSTR pwszText
)
1131 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
1133 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszText
));
1135 switch (This
->state
)
1137 case XmlWriterState_Initial
:
1138 return E_UNEXPECTED
;
1139 case XmlWriterState_Ready
:
1140 case XmlWriterState_DocClosed
:
1141 This
->state
= XmlWriterState_DocClosed
;
1142 return WR_E_INVALIDACTION
;
1150 static HRESULT WINAPI
xmlwriter_WriteSurrogateCharEntity(IXmlWriter
*iface
, WCHAR wchLow
, WCHAR wchHigh
)
1152 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
1154 FIXME("%p %d %d\n", This
, wchLow
, wchHigh
);
1159 static HRESULT WINAPI
xmlwriter_WriteWhitespace(IXmlWriter
*iface
, LPCWSTR pwszWhitespace
)
1161 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
1163 FIXME("%p %s\n", This
, wine_dbgstr_w(pwszWhitespace
));
1168 static HRESULT WINAPI
xmlwriter_Flush(IXmlWriter
*iface
)
1170 xmlwriter
*This
= impl_from_IXmlWriter(iface
);
1172 TRACE("%p\n", This
);
1174 return writeroutput_flush_stream(This
->output
);
1177 static const struct IXmlWriterVtbl xmlwriter_vtbl
=
1179 xmlwriter_QueryInterface
,
1182 xmlwriter_SetOutput
,
1183 xmlwriter_GetProperty
,
1184 xmlwriter_SetProperty
,
1185 xmlwriter_WriteAttributes
,
1186 xmlwriter_WriteAttributeString
,
1187 xmlwriter_WriteCData
,
1188 xmlwriter_WriteCharEntity
,
1189 xmlwriter_WriteChars
,
1190 xmlwriter_WriteComment
,
1191 xmlwriter_WriteDocType
,
1192 xmlwriter_WriteElementString
,
1193 xmlwriter_WriteEndDocument
,
1194 xmlwriter_WriteEndElement
,
1195 xmlwriter_WriteEntityRef
,
1196 xmlwriter_WriteFullEndElement
,
1197 xmlwriter_WriteName
,
1198 xmlwriter_WriteNmToken
,
1199 xmlwriter_WriteNode
,
1200 xmlwriter_WriteNodeShallow
,
1201 xmlwriter_WriteProcessingInstruction
,
1202 xmlwriter_WriteQualifiedName
,
1204 xmlwriter_WriteRawChars
,
1205 xmlwriter_WriteStartDocument
,
1206 xmlwriter_WriteStartElement
,
1207 xmlwriter_WriteString
,
1208 xmlwriter_WriteSurrogateCharEntity
,
1209 xmlwriter_WriteWhitespace
,
1213 /** IXmlWriterOutput **/
1214 static HRESULT WINAPI
xmlwriteroutput_QueryInterface(IXmlWriterOutput
*iface
, REFIID riid
, void** ppvObject
)
1216 xmlwriteroutput
*This
= impl_from_IXmlWriterOutput(iface
);
1218 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppvObject
);
1220 if (IsEqualGUID(riid
, &IID_IXmlWriterOutput
) ||
1221 IsEqualGUID(riid
, &IID_IUnknown
))
1227 WARN("interface %s not implemented\n", debugstr_guid(riid
));
1229 return E_NOINTERFACE
;
1232 IUnknown_AddRef(iface
);
1237 static ULONG WINAPI
xmlwriteroutput_AddRef(IXmlWriterOutput
*iface
)
1239 xmlwriteroutput
*This
= impl_from_IXmlWriterOutput(iface
);
1240 ULONG ref
= InterlockedIncrement(&This
->ref
);
1241 TRACE("(%p)->(%d)\n", This
, ref
);
1245 static ULONG WINAPI
xmlwriteroutput_Release(IXmlWriterOutput
*iface
)
1247 xmlwriteroutput
*This
= impl_from_IXmlWriterOutput(iface
);
1248 LONG ref
= InterlockedDecrement(&This
->ref
);
1250 TRACE("(%p)->(%d)\n", This
, ref
);
1254 IMalloc
*imalloc
= This
->imalloc
;
1255 if (This
->output
) IUnknown_Release(This
->output
);
1256 if (This
->stream
) ISequentialStream_Release(This
->stream
);
1257 free_output_buffer(This
);
1258 writeroutput_free(This
, This
);
1259 if (imalloc
) IMalloc_Release(imalloc
);
1265 static const struct IUnknownVtbl xmlwriteroutputvtbl
=
1267 xmlwriteroutput_QueryInterface
,
1268 xmlwriteroutput_AddRef
,
1269 xmlwriteroutput_Release
1272 HRESULT WINAPI
CreateXmlWriter(REFIID riid
, void **obj
, IMalloc
*imalloc
)
1276 TRACE("(%s, %p, %p)\n", wine_dbgstr_guid(riid
), obj
, imalloc
);
1278 if (!IsEqualGUID(riid
, &IID_IXmlWriter
))
1280 ERR("Unexpected IID requested -> (%s)\n", wine_dbgstr_guid(riid
));
1285 writer
= IMalloc_Alloc(imalloc
, sizeof(*writer
));
1287 writer
= heap_alloc(sizeof(*writer
));
1288 if(!writer
) return E_OUTOFMEMORY
;
1290 writer
->IXmlWriter_iface
.lpVtbl
= &xmlwriter_vtbl
;
1292 writer
->imalloc
= imalloc
;
1293 if (imalloc
) IMalloc_AddRef(imalloc
);
1294 writer
->output
= NULL
;
1295 writer
->indent
= FALSE
;
1297 writer
->omitxmldecl
= FALSE
;
1298 writer
->conformance
= XmlConformanceLevel_Document
;
1299 writer
->state
= XmlWriterState_Initial
;
1300 writer
->bomwritten
= FALSE
;
1301 writer
->starttagopen
= FALSE
;
1302 list_init(&writer
->elements
);
1304 *obj
= &writer
->IXmlWriter_iface
;
1306 TRACE("returning iface %p\n", *obj
);
1311 static HRESULT
create_writer(IUnknown
*stream
, IMalloc
*imalloc
, xml_encoding encoding
,
1312 IXmlWriterOutput
**output
)
1314 xmlwriteroutput
*writeroutput
;
1320 writeroutput
= IMalloc_Alloc(imalloc
, sizeof(*writeroutput
));
1322 writeroutput
= heap_alloc(sizeof(*writeroutput
));
1323 if(!writeroutput
) return E_OUTOFMEMORY
;
1325 writeroutput
->IXmlWriterOutput_iface
.lpVtbl
= &xmlwriteroutputvtbl
;
1326 writeroutput
->ref
= 1;
1327 writeroutput
->imalloc
= imalloc
;
1328 if (imalloc
) IMalloc_AddRef(imalloc
);
1329 writeroutput
->encoding
= encoding
;
1330 writeroutput
->stream
= NULL
;
1331 hr
= init_output_buffer(writeroutput
);
1333 IUnknown_Release(&writeroutput
->IXmlWriterOutput_iface
);
1337 IUnknown_QueryInterface(stream
, &IID_IUnknown
, (void**)&writeroutput
->output
);
1339 *output
= &writeroutput
->IXmlWriterOutput_iface
;
1341 TRACE("returning iface %p\n", *output
);
1346 HRESULT WINAPI
CreateXmlWriterOutputWithEncodingName(IUnknown
*stream
,
1349 IXmlWriterOutput
**output
)
1351 static const WCHAR utf8W
[] = {'U','T','F','-','8',0};
1352 xml_encoding xml_enc
;
1354 TRACE("%p %p %s %p\n", stream
, imalloc
, debugstr_w(encoding
), output
);
1356 if (!stream
|| !output
) return E_INVALIDARG
;
1358 xml_enc
= parse_encoding_name(encoding
? encoding
: utf8W
, -1);
1359 return create_writer(stream
, imalloc
, xml_enc
, output
);
1362 HRESULT WINAPI
CreateXmlWriterOutputWithEncodingCodePage(IUnknown
*stream
,
1365 IXmlWriterOutput
**output
)
1367 xml_encoding xml_enc
;
1369 TRACE("%p %p %u %p\n", stream
, imalloc
, codepage
, output
);
1371 if (!stream
|| !output
) return E_INVALIDARG
;
1373 xml_enc
= get_encoding_from_codepage(codepage
);
1374 return create_writer(stream
, imalloc
, xml_enc
, output
);