[XMLLITE] Sync with Wine Staging 1.7.47. CORE-9924
[reactos.git] / reactos / dll / win32 / xmllite / writer.c
1 /*
2 * IXmlWriter implementation
3 *
4 * Copyright 2011 Alistair Leslie-Hughes
5 * Copyright 2014 Nikolay Sivov for CodeWeavers
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "xmllite_private.h"
23
24 #include <wine/list.h>
25 #include <wine/unicode.h>
26
27 /* not defined in public headers */
28 DEFINE_GUID(IID_IXmlWriterOutput, 0xc1131708, 0x0f59, 0x477f, 0x93, 0x59, 0x7d, 0x33, 0x24, 0x51, 0xbc, 0x1a);
29
30 #define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0]))
31
32 static const WCHAR closeelementW[] = {'<','/'};
33 static const WCHAR closepiW[] = {'?','>'};
34 static const WCHAR ltW[] = {'<'};
35 static const WCHAR gtW[] = {'>'};
36
37 struct output_buffer
38 {
39 char *data;
40 unsigned int allocated;
41 unsigned int written;
42 UINT codepage;
43 };
44
45 typedef enum
46 {
47 XmlWriterState_Initial, /* output is not set yet */
48 XmlWriterState_Ready, /* SetOutput() was called, ready to start */
49 XmlWriterState_PIDocStarted, /* document was started with manually added 'xml' PI */
50 XmlWriterState_DocStarted, /* document was started with WriteStartDocument() */
51 XmlWriterState_ElemStarted, /* writing element */
52 XmlWriterState_Content, /* content is accepted at this point */
53 XmlWriterState_DocClosed /* WriteEndDocument was called */
54 } XmlWriterState;
55
56 typedef struct
57 {
58 IXmlWriterOutput IXmlWriterOutput_iface;
59 LONG ref;
60 IUnknown *output;
61 ISequentialStream *stream;
62 IMalloc *imalloc;
63 xml_encoding encoding;
64 struct output_buffer buffer;
65 } xmlwriteroutput;
66
67 static const struct IUnknownVtbl xmlwriteroutputvtbl;
68
69 struct element
70 {
71 struct list entry;
72 WCHAR *qname;
73 unsigned int len; /* qname length in chars */
74 };
75
76 typedef struct _xmlwriter
77 {
78 IXmlWriter IXmlWriter_iface;
79 LONG ref;
80 IMalloc *imalloc;
81 xmlwriteroutput *output;
82 BOOL indent;
83 BOOL bom;
84 BOOL omitxmldecl;
85 XmlConformanceLevel conformance;
86 XmlWriterState state;
87 BOOL bomwritten;
88 BOOL starttagopen;
89 struct list elements;
90 } xmlwriter;
91
92 static inline xmlwriter *impl_from_IXmlWriter(IXmlWriter *iface)
93 {
94 return CONTAINING_RECORD(iface, xmlwriter, IXmlWriter_iface);
95 }
96
97 static inline xmlwriteroutput *impl_from_IXmlWriterOutput(IXmlWriterOutput *iface)
98 {
99 return CONTAINING_RECORD(iface, xmlwriteroutput, IXmlWriterOutput_iface);
100 }
101
102 static const char *debugstr_writer_prop(XmlWriterProperty prop)
103 {
104 static const char * const prop_names[] =
105 {
106 "MultiLanguage",
107 "Indent",
108 "ByteOrderMark",
109 "OmitXmlDeclaration",
110 "ConformanceLevel"
111 };
112
113 if (prop > _XmlWriterProperty_Last)
114 return wine_dbg_sprintf("unknown property=%d", prop);
115
116 return prop_names[prop];
117 }
118
119 /* writer output memory allocation functions */
120 static inline void *writeroutput_alloc(xmlwriteroutput *output, size_t len)
121 {
122 return m_alloc(output->imalloc, len);
123 }
124
125 static inline void writeroutput_free(xmlwriteroutput *output, void *mem)
126 {
127 m_free(output->imalloc, mem);
128 }
129
130 static inline void *writeroutput_realloc(xmlwriteroutput *output, void *mem, size_t len)
131 {
132 return m_realloc(output->imalloc, mem, len);
133 }
134
135 /* writer memory allocation functions */
136 static inline void *writer_alloc(xmlwriter *writer, size_t len)
137 {
138 return m_alloc(writer->imalloc, len);
139 }
140
141 static inline void writer_free(xmlwriter *writer, void *mem)
142 {
143 m_free(writer->imalloc, mem);
144 }
145
146 static struct element *alloc_element(xmlwriter *writer, const WCHAR *prefix, const WCHAR *local)
147 {
148 struct element *ret;
149 int len;
150
151 ret = writer_alloc(writer, sizeof(*ret));
152 if (!ret) return ret;
153
154 len = prefix ? strlenW(prefix) + 1 /* ':' */ : 0;
155 len += strlenW(local);
156
157 ret->qname = writer_alloc(writer, (len + 1)*sizeof(WCHAR));
158 ret->len = len;
159 if (prefix) {
160 static const WCHAR colonW[] = {':',0};
161 strcpyW(ret->qname, prefix);
162 strcatW(ret->qname, colonW);
163 }
164 else
165 ret->qname[0] = 0;
166 strcatW(ret->qname, local);
167
168 return ret;
169 }
170
171 static void free_element(xmlwriter *writer, struct element *element)
172 {
173 writer_free(writer, element->qname);
174 writer_free(writer, element);
175 }
176
177 static void push_element(xmlwriter *writer, struct element *element)
178 {
179 list_add_head(&writer->elements, &element->entry);
180 }
181
182 static struct element *pop_element(xmlwriter *writer)
183 {
184 struct element *element = LIST_ENTRY(list_head(&writer->elements), struct element, entry);
185
186 if (element)
187 list_remove(&element->entry);
188
189 return element;
190 }
191
192 static HRESULT init_output_buffer(xmlwriteroutput *output)
193 {
194 struct output_buffer *buffer = &output->buffer;
195 const int initial_len = 0x2000;
196 HRESULT hr;
197 UINT cp;
198
199 hr = get_code_page(output->encoding, &cp);
200 if (FAILED(hr)) return hr;
201
202 buffer->data = writeroutput_alloc(output, initial_len);
203 if (!buffer->data) return E_OUTOFMEMORY;
204
205 memset(buffer->data, 0, 4);
206 buffer->allocated = initial_len;
207 buffer->written = 0;
208 buffer->codepage = cp;
209
210 return S_OK;
211 }
212
213 static void free_output_buffer(xmlwriteroutput *output)
214 {
215 struct output_buffer *buffer = &output->buffer;
216 writeroutput_free(output, buffer->data);
217 buffer->data = NULL;
218 buffer->allocated = 0;
219 buffer->written = 0;
220 }
221
222 static HRESULT grow_output_buffer(xmlwriteroutput *output, int length)
223 {
224 struct output_buffer *buffer = &output->buffer;
225 /* grow if needed, plus 4 bytes to be sure null terminator will fit in */
226 if (buffer->allocated < buffer->written + length + 4) {
227 int grown_size = max(2*buffer->allocated, buffer->allocated + length);
228 char *ptr = writeroutput_realloc(output, buffer->data, grown_size);
229 if (!ptr) return E_OUTOFMEMORY;
230 buffer->data = ptr;
231 buffer->allocated = grown_size;
232 }
233
234 return S_OK;
235 }
236
237 static HRESULT write_output_buffer(xmlwriteroutput *output, const WCHAR *data, int len)
238 {
239 struct output_buffer *buffer = &output->buffer;
240 int length;
241 HRESULT hr;
242 char *ptr;
243
244 if (buffer->codepage != ~0) {
245 length = WideCharToMultiByte(buffer->codepage, 0, data, len, NULL, 0, NULL, NULL);
246 hr = grow_output_buffer(output, length);
247 if (FAILED(hr)) return hr;
248 ptr = buffer->data + buffer->written;
249 length = WideCharToMultiByte(buffer->codepage, 0, data, len, ptr, length, NULL, NULL);
250 buffer->written += len == -1 ? length-1 : length;
251 }
252 else {
253 /* WCHAR data just copied */
254 length = len == -1 ? strlenW(data) : len;
255 if (length) {
256 length *= sizeof(WCHAR);
257
258 hr = grow_output_buffer(output, length);
259 if (FAILED(hr)) return hr;
260 ptr = buffer->data + buffer->written;
261
262 memcpy(ptr, data, length);
263 buffer->written += length;
264 ptr += length;
265 /* null termination */
266 *(WCHAR*)ptr = 0;
267 }
268 }
269
270 return S_OK;
271 }
272
273 static HRESULT write_output_buffer_quoted(xmlwriteroutput *output, const WCHAR *data, int len)
274 {
275 static const WCHAR quoteW[] = {'"'};
276 write_output_buffer(output, quoteW, ARRAY_SIZE(quoteW));
277 write_output_buffer(output, data, len);
278 write_output_buffer(output, quoteW, ARRAY_SIZE(quoteW));
279 return S_OK;
280 }
281
282 /* TODO: test if we need to validate char range */
283 static HRESULT write_output_qname(xmlwriteroutput *output, const WCHAR *prefix, const WCHAR *local_name)
284 {
285 if (prefix) {
286 static const WCHAR colW[] = {':'};
287 write_output_buffer(output, prefix, -1);
288 write_output_buffer(output, colW, ARRAY_SIZE(colW));
289 }
290
291 write_output_buffer(output, local_name, -1);
292
293 return S_OK;
294 }
295
296 static void writeroutput_release_stream(xmlwriteroutput *writeroutput)
297 {
298 if (writeroutput->stream) {
299 ISequentialStream_Release(writeroutput->stream);
300 writeroutput->stream = NULL;
301 }
302 }
303
304 static inline HRESULT writeroutput_query_for_stream(xmlwriteroutput *writeroutput)
305 {
306 HRESULT hr;
307
308 writeroutput_release_stream(writeroutput);
309 hr = IUnknown_QueryInterface(writeroutput->output, &IID_IStream, (void**)&writeroutput->stream);
310 if (hr != S_OK)
311 hr = IUnknown_QueryInterface(writeroutput->output, &IID_ISequentialStream, (void**)&writeroutput->stream);
312
313 return hr;
314 }
315
316 static HRESULT writeroutput_flush_stream(xmlwriteroutput *output)
317 {
318 struct output_buffer *buffer;
319 ULONG written, offset = 0;
320 HRESULT hr;
321
322 if (!output || !output->stream)
323 return S_OK;
324
325 buffer = &output->buffer;
326
327 /* It will loop forever until everything is written or an error occurred. */
328 do {
329 written = 0;
330 hr = ISequentialStream_Write(output->stream, buffer->data + offset, buffer->written, &written);
331 if (FAILED(hr)) {
332 WARN("write to stream failed (0x%08x)\n", hr);
333 buffer->written = 0;
334 return hr;
335 }
336
337 offset += written;
338 buffer->written -= written;
339 } while (buffer->written > 0);
340
341 return S_OK;
342 }
343
344 static HRESULT write_encoding_bom(xmlwriter *writer)
345 {
346 if (!writer->bom || writer->bomwritten) return S_OK;
347
348 if (writer->output->encoding == XmlEncoding_UTF16) {
349 static const char utf16bom[] = {0xff, 0xfe};
350 struct output_buffer *buffer = &writer->output->buffer;
351 int len = sizeof(utf16bom);
352 HRESULT hr;
353
354 hr = grow_output_buffer(writer->output, len);
355 if (FAILED(hr)) return hr;
356 memcpy(buffer->data + buffer->written, utf16bom, len);
357 buffer->written += len;
358 }
359
360 writer->bomwritten = TRUE;
361 return S_OK;
362 }
363
364 static HRESULT writer_close_starttag(xmlwriter *writer)
365 {
366 HRESULT hr;
367
368 if (!writer->starttagopen) return S_OK;
369 hr = write_output_buffer(writer->output, gtW, ARRAY_SIZE(gtW));
370 writer->starttagopen = FALSE;
371 writer->state = XmlWriterState_Content;
372 return hr;
373 }
374
375 static HRESULT WINAPI xmlwriter_QueryInterface(IXmlWriter *iface, REFIID riid, void **ppvObject)
376 {
377 xmlwriter *This = impl_from_IXmlWriter(iface);
378
379 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
380
381 if (IsEqualGUID(riid, &IID_IUnknown) ||
382 IsEqualGUID(riid, &IID_IXmlWriter))
383 {
384 *ppvObject = iface;
385 }
386
387 IXmlWriter_AddRef(iface);
388
389 return S_OK;
390 }
391
392 static ULONG WINAPI xmlwriter_AddRef(IXmlWriter *iface)
393 {
394 xmlwriter *This = impl_from_IXmlWriter(iface);
395 TRACE("%p\n", This);
396 return InterlockedIncrement(&This->ref);
397 }
398
399 static ULONG WINAPI xmlwriter_Release(IXmlWriter *iface)
400 {
401 xmlwriter *This = impl_from_IXmlWriter(iface);
402 LONG ref;
403
404 TRACE("%p\n", This);
405
406 ref = InterlockedDecrement(&This->ref);
407 if (ref == 0) {
408 struct element *element, *element2;
409 IMalloc *imalloc = This->imalloc;
410
411 IXmlWriter_Flush(iface);
412 if (This->output) IUnknown_Release(&This->output->IXmlWriterOutput_iface);
413
414 /* element stack */
415 LIST_FOR_EACH_ENTRY_SAFE(element, element2, &This->elements, struct element, entry) {
416 list_remove(&element->entry);
417 free_element(This, element);
418 }
419
420 writer_free(This, This);
421 if (imalloc) IMalloc_Release(imalloc);
422 }
423
424 return ref;
425 }
426
427 /*** IXmlWriter methods ***/
428 static HRESULT WINAPI xmlwriter_SetOutput(IXmlWriter *iface, IUnknown *output)
429 {
430 xmlwriter *This = impl_from_IXmlWriter(iface);
431 IXmlWriterOutput *writeroutput;
432 HRESULT hr;
433
434 TRACE("(%p)->(%p)\n", This, output);
435
436 if (This->output) {
437 writeroutput_release_stream(This->output);
438 IUnknown_Release(&This->output->IXmlWriterOutput_iface);
439 This->output = NULL;
440 This->bomwritten = FALSE;
441 }
442
443 /* just reset current output */
444 if (!output) {
445 This->state = XmlWriterState_Initial;
446 return S_OK;
447 }
448
449 /* now try IXmlWriterOutput, ISequentialStream, IStream */
450 hr = IUnknown_QueryInterface(output, &IID_IXmlWriterOutput, (void**)&writeroutput);
451 if (hr == S_OK) {
452 if (writeroutput->lpVtbl == &xmlwriteroutputvtbl)
453 This->output = impl_from_IXmlWriterOutput(writeroutput);
454 else {
455 ERR("got external IXmlWriterOutput implementation: %p, vtbl=%p\n",
456 writeroutput, writeroutput->lpVtbl);
457 IUnknown_Release(writeroutput);
458 return E_FAIL;
459
460 }
461 }
462
463 if (hr != S_OK || !writeroutput) {
464 /* create IXmlWriterOutput basing on supplied interface */
465 hr = CreateXmlWriterOutputWithEncodingName(output, This->imalloc, NULL, &writeroutput);
466 if (hr != S_OK) return hr;
467 This->output = impl_from_IXmlWriterOutput(writeroutput);
468 }
469
470 This->state = XmlWriterState_Ready;
471 return writeroutput_query_for_stream(This->output);
472 }
473
474 static HRESULT WINAPI xmlwriter_GetProperty(IXmlWriter *iface, UINT property, LONG_PTR *value)
475 {
476 xmlwriter *This = impl_from_IXmlWriter(iface);
477
478 TRACE("(%p)->(%s %p)\n", This, debugstr_writer_prop(property), value);
479
480 if (!value) return E_INVALIDARG;
481
482 switch (property)
483 {
484 case XmlWriterProperty_Indent:
485 *value = This->indent;
486 break;
487 case XmlWriterProperty_ByteOrderMark:
488 *value = This->bom;
489 break;
490 case XmlWriterProperty_OmitXmlDeclaration:
491 *value = This->omitxmldecl;
492 break;
493 case XmlWriterProperty_ConformanceLevel:
494 *value = This->conformance;
495 break;
496 default:
497 FIXME("Unimplemented property (%u)\n", property);
498 return E_NOTIMPL;
499 }
500
501 return S_OK;
502 }
503
504 static HRESULT WINAPI xmlwriter_SetProperty(IXmlWriter *iface, UINT property, LONG_PTR value)
505 {
506 xmlwriter *This = impl_from_IXmlWriter(iface);
507
508 TRACE("(%p)->(%s %lu)\n", This, debugstr_writer_prop(property), value);
509
510 switch (property)
511 {
512 case XmlWriterProperty_ByteOrderMark:
513 This->bom = !!value;
514 break;
515 case XmlWriterProperty_OmitXmlDeclaration:
516 This->omitxmldecl = !!value;
517 break;
518 default:
519 FIXME("Unimplemented property (%u)\n", property);
520 return E_NOTIMPL;
521 }
522
523 return S_OK;
524 }
525
526 static HRESULT WINAPI xmlwriter_WriteAttributes(IXmlWriter *iface, IXmlReader *pReader,
527 BOOL fWriteDefaultAttributes)
528 {
529 xmlwriter *This = impl_from_IXmlWriter(iface);
530
531 FIXME("%p %p %d\n", This, pReader, fWriteDefaultAttributes);
532
533 return E_NOTIMPL;
534 }
535
536 static HRESULT WINAPI xmlwriter_WriteAttributeString(IXmlWriter *iface, LPCWSTR pwszPrefix,
537 LPCWSTR pwszLocalName, LPCWSTR pwszNamespaceUri,
538 LPCWSTR pwszValue)
539 {
540 xmlwriter *This = impl_from_IXmlWriter(iface);
541
542 FIXME("%p %s %s %s %s\n", This, wine_dbgstr_w(pwszPrefix), wine_dbgstr_w(pwszLocalName),
543 wine_dbgstr_w(pwszNamespaceUri), wine_dbgstr_w(pwszValue));
544
545 return E_NOTIMPL;
546 }
547
548 static HRESULT WINAPI xmlwriter_WriteCData(IXmlWriter *iface, LPCWSTR pwszText)
549 {
550 xmlwriter *This = impl_from_IXmlWriter(iface);
551
552 FIXME("%p %s\n", This, wine_dbgstr_w(pwszText));
553
554 return E_NOTIMPL;
555 }
556
557 static HRESULT WINAPI xmlwriter_WriteCharEntity(IXmlWriter *iface, WCHAR wch)
558 {
559 return E_NOTIMPL;
560 }
561
562 static HRESULT WINAPI xmlwriter_WriteChars(IXmlWriter *iface, const WCHAR *pwch, UINT cwch)
563 {
564 xmlwriter *This = impl_from_IXmlWriter(iface);
565
566 FIXME("%p %s %d\n", This, wine_dbgstr_w(pwch), cwch);
567
568 return E_NOTIMPL;
569 }
570
571 static HRESULT WINAPI xmlwriter_WriteComment(IXmlWriter *iface, LPCWSTR pwszComment)
572 {
573 return E_NOTIMPL;
574 }
575
576 static HRESULT WINAPI xmlwriter_WriteDocType(IXmlWriter *iface, LPCWSTR pwszName, LPCWSTR pwszPublicId,
577 LPCWSTR pwszSystemId, LPCWSTR pwszSubset)
578 {
579 xmlwriter *This = impl_from_IXmlWriter(iface);
580
581 FIXME("%p %s %s %s %s\n", This, wine_dbgstr_w(pwszName), wine_dbgstr_w(pwszPublicId),
582 wine_dbgstr_w(pwszSystemId), wine_dbgstr_w(pwszSubset));
583
584 return E_NOTIMPL;
585 }
586
587 static HRESULT WINAPI xmlwriter_WriteElementString(IXmlWriter *iface, LPCWSTR prefix,
588 LPCWSTR local_name, LPCWSTR uri, LPCWSTR value)
589 {
590 xmlwriter *This = impl_from_IXmlWriter(iface);
591
592 TRACE("(%p)->(%s %s %s %s)\n", This, wine_dbgstr_w(prefix), wine_dbgstr_w(local_name),
593 wine_dbgstr_w(uri), wine_dbgstr_w(value));
594
595 switch (This->state)
596 {
597 case XmlWriterState_Initial:
598 return E_UNEXPECTED;
599 case XmlWriterState_ElemStarted:
600 writer_close_starttag(This);
601 break;
602 case XmlWriterState_DocClosed:
603 return WR_E_INVALIDACTION;
604 default:
605 ;
606 }
607
608 write_encoding_bom(This);
609 write_output_buffer(This->output, ltW, ARRAY_SIZE(ltW));
610 write_output_qname(This->output, prefix, local_name);
611 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
612
613 if (value)
614 write_output_buffer(This->output, value, -1);
615
616 write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW));
617 write_output_qname(This->output, prefix, local_name);
618 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
619 This->state = XmlWriterState_Content;
620
621 return S_OK;
622 }
623
624 static HRESULT WINAPI xmlwriter_WriteEndDocument(IXmlWriter *iface)
625 {
626 xmlwriter *This = impl_from_IXmlWriter(iface);
627 HRESULT hr = S_OK;
628
629 TRACE("%p\n", This);
630
631 switch (This->state)
632 {
633 case XmlWriterState_Initial:
634 hr = E_UNEXPECTED;
635 break;
636 case XmlWriterState_Ready:
637 case XmlWriterState_DocClosed:
638 hr = WR_E_INVALIDACTION;
639 break;
640 default:
641 ;
642 }
643
644 if (FAILED(hr)) {
645 This->state = XmlWriterState_DocClosed;
646 return hr;
647 }
648
649 /* empty element stack */
650 while (IXmlWriter_WriteEndElement(iface) == S_OK)
651 ;
652
653 This->state = XmlWriterState_DocClosed;
654 return S_OK;
655 }
656
657 static HRESULT WINAPI xmlwriter_WriteEndElement(IXmlWriter *iface)
658 {
659 xmlwriter *This = impl_from_IXmlWriter(iface);
660 struct element *element;
661
662 TRACE("%p\n", This);
663
664 element = pop_element(This);
665 if (!element)
666 return WR_E_INVALIDACTION;
667
668 if (This->starttagopen) {
669 static WCHAR closetagW[] = {' ','/','>'};
670 write_output_buffer(This->output, closetagW, ARRAY_SIZE(closetagW));
671 This->starttagopen = FALSE;
672 }
673 else {
674 /* write full end tag */
675 write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW));
676 write_output_buffer(This->output, element->qname, element->len);
677 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
678 }
679
680 return S_OK;
681 }
682
683 static HRESULT WINAPI xmlwriter_WriteEntityRef(IXmlWriter *iface, LPCWSTR pwszName)
684 {
685 xmlwriter *This = impl_from_IXmlWriter(iface);
686
687 FIXME("%p %s\n", This, wine_dbgstr_w(pwszName));
688
689 return E_NOTIMPL;
690 }
691
692 static HRESULT WINAPI xmlwriter_WriteFullEndElement(IXmlWriter *iface)
693 {
694 xmlwriter *This = impl_from_IXmlWriter(iface);
695 struct element *element;
696
697 TRACE("%p\n", This);
698
699 element = pop_element(This);
700 if (!element)
701 return WR_E_INVALIDACTION;
702
703 /* write full end tag */
704 write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW));
705 write_output_buffer(This->output, element->qname, element->len);
706 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
707 This->starttagopen = FALSE;
708
709 return S_OK;
710 }
711
712 static HRESULT WINAPI xmlwriter_WriteName(IXmlWriter *iface, LPCWSTR pwszName)
713 {
714 xmlwriter *This = impl_from_IXmlWriter(iface);
715
716 FIXME("%p %s\n", This, wine_dbgstr_w(pwszName));
717
718 return E_NOTIMPL;
719 }
720
721 static HRESULT WINAPI xmlwriter_WriteNmToken(IXmlWriter *iface, LPCWSTR pwszNmToken)
722 {
723 xmlwriter *This = impl_from_IXmlWriter(iface);
724
725 FIXME("%p %s\n", This, wine_dbgstr_w(pwszNmToken));
726
727 return E_NOTIMPL;
728 }
729
730 static HRESULT WINAPI xmlwriter_WriteNode(IXmlWriter *iface, IXmlReader *pReader,
731 BOOL fWriteDefaultAttributes)
732 {
733 xmlwriter *This = impl_from_IXmlWriter(iface);
734
735 FIXME("%p %p %d\n", This, pReader, fWriteDefaultAttributes);
736
737 return E_NOTIMPL;
738 }
739
740 static HRESULT WINAPI xmlwriter_WriteNodeShallow(IXmlWriter *iface, IXmlReader *pReader,
741 BOOL fWriteDefaultAttributes)
742 {
743 xmlwriter *This = impl_from_IXmlWriter(iface);
744
745 FIXME("%p %p %d\n", This, pReader, fWriteDefaultAttributes);
746
747 return E_NOTIMPL;
748 }
749
750 static HRESULT WINAPI xmlwriter_WriteProcessingInstruction(IXmlWriter *iface, LPCWSTR name,
751 LPCWSTR text)
752 {
753 xmlwriter *This = impl_from_IXmlWriter(iface);
754 static const WCHAR xmlW[] = {'x','m','l',0};
755 static const WCHAR openpiW[] = {'<','?'};
756 static const WCHAR spaceW[] = {' '};
757
758 TRACE("(%p)->(%s %s)\n", This, wine_dbgstr_w(name), wine_dbgstr_w(text));
759
760 switch (This->state)
761 {
762 case XmlWriterState_Initial:
763 return E_UNEXPECTED;
764 case XmlWriterState_DocStarted:
765 if (!strcmpW(name, xmlW))
766 return WR_E_INVALIDACTION;
767 break;
768 case XmlWriterState_ElemStarted:
769 case XmlWriterState_DocClosed:
770 return WR_E_INVALIDACTION;
771 default:
772 ;
773 }
774
775 write_encoding_bom(This);
776 write_output_buffer(This->output, openpiW, ARRAY_SIZE(openpiW));
777 write_output_buffer(This->output, name, -1);
778 write_output_buffer(This->output, spaceW, ARRAY_SIZE(spaceW));
779 write_output_buffer(This->output, text, -1);
780 write_output_buffer(This->output, closepiW, ARRAY_SIZE(closepiW));
781
782 if (!strcmpW(name, xmlW))
783 This->state = XmlWriterState_PIDocStarted;
784
785 return S_OK;
786 }
787
788 static HRESULT WINAPI xmlwriter_WriteQualifiedName(IXmlWriter *iface, LPCWSTR pwszLocalName,
789 LPCWSTR pwszNamespaceUri)
790 {
791 xmlwriter *This = impl_from_IXmlWriter(iface);
792
793 FIXME("%p %s %s\n", This, wine_dbgstr_w(pwszLocalName), wine_dbgstr_w(pwszNamespaceUri));
794
795 return E_NOTIMPL;
796 }
797
798 static HRESULT WINAPI xmlwriter_WriteRaw(IXmlWriter *iface, LPCWSTR pwszData)
799 {
800 xmlwriter *This = impl_from_IXmlWriter(iface);
801
802 FIXME("%p %s\n", This, wine_dbgstr_w(pwszData));
803
804 return E_NOTIMPL;
805 }
806
807 static HRESULT WINAPI xmlwriter_WriteRawChars(IXmlWriter *iface, const WCHAR *pwch, UINT cwch)
808 {
809 xmlwriter *This = impl_from_IXmlWriter(iface);
810
811 FIXME("%p %s %d\n", This, wine_dbgstr_w(pwch), cwch);
812
813 return E_NOTIMPL;
814 }
815
816 static HRESULT WINAPI xmlwriter_WriteStartDocument(IXmlWriter *iface, XmlStandalone standalone)
817 {
818 static const WCHAR versionW[] = {'<','?','x','m','l',' ','v','e','r','s','i','o','n','=','"','1','.','0','"'};
819 static const WCHAR encodingW[] = {' ','e','n','c','o','d','i','n','g','='};
820 xmlwriter *This = impl_from_IXmlWriter(iface);
821
822 TRACE("(%p)->(%d)\n", This, standalone);
823
824 switch (This->state)
825 {
826 case XmlWriterState_Initial:
827 return E_UNEXPECTED;
828 case XmlWriterState_PIDocStarted:
829 This->state = XmlWriterState_DocStarted;
830 return S_OK;
831 case XmlWriterState_DocStarted:
832 case XmlWriterState_ElemStarted:
833 case XmlWriterState_DocClosed:
834 return WR_E_INVALIDACTION;
835 default:
836 ;
837 }
838
839 write_encoding_bom(This);
840 This->state = XmlWriterState_DocStarted;
841 if (This->omitxmldecl) return S_OK;
842
843 /* version */
844 write_output_buffer(This->output, versionW, ARRAY_SIZE(versionW));
845
846 /* encoding */
847 write_output_buffer(This->output, encodingW, ARRAY_SIZE(encodingW));
848 write_output_buffer_quoted(This->output, get_encoding_name(This->output->encoding), -1);
849
850 /* standalone */
851 if (standalone == XmlStandalone_Omit)
852 write_output_buffer(This->output, closepiW, ARRAY_SIZE(closepiW));
853 else {
854 static const WCHAR standaloneW[] = {' ','s','t','a','n','d','a','l','o','n','e','=','\"'};
855 static const WCHAR yesW[] = {'y','e','s','\"','?','>'};
856 static const WCHAR noW[] = {'n','o','\"','?','>'};
857
858 write_output_buffer(This->output, standaloneW, ARRAY_SIZE(standaloneW));
859 if (standalone == XmlStandalone_Yes)
860 write_output_buffer(This->output, yesW, ARRAY_SIZE(yesW));
861 else
862 write_output_buffer(This->output, noW, ARRAY_SIZE(noW));
863 }
864
865 return S_OK;
866 }
867
868 static HRESULT WINAPI xmlwriter_WriteStartElement(IXmlWriter *iface, LPCWSTR prefix, LPCWSTR local_name, LPCWSTR uri)
869 {
870 xmlwriter *This = impl_from_IXmlWriter(iface);
871 struct element *element;
872
873 TRACE("(%p)->(%s %s %s)\n", This, wine_dbgstr_w(prefix), wine_dbgstr_w(local_name), wine_dbgstr_w(uri));
874
875 switch (This->state)
876 {
877 case XmlWriterState_Initial:
878 return E_UNEXPECTED;
879 case XmlWriterState_DocClosed:
880 return WR_E_INVALIDACTION;
881 default:
882 ;
883 }
884
885 if (!local_name)
886 return E_INVALIDARG;
887
888 /* close pending element */
889 if (This->starttagopen)
890 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
891
892 element = alloc_element(This, prefix, local_name);
893 if (!element)
894 return E_OUTOFMEMORY;
895
896 write_encoding_bom(This);
897 This->state = XmlWriterState_ElemStarted;
898 This->starttagopen = TRUE;
899
900 push_element(This, element);
901
902 write_output_buffer(This->output, ltW, ARRAY_SIZE(ltW));
903 write_output_qname(This->output, prefix, local_name);
904
905 return S_OK;
906 }
907
908 static HRESULT WINAPI xmlwriter_WriteString(IXmlWriter *iface, LPCWSTR pwszText)
909 {
910 xmlwriter *This = impl_from_IXmlWriter(iface);
911
912 FIXME("%p %s\n", This, wine_dbgstr_w(pwszText));
913
914 return E_NOTIMPL;
915 }
916
917 static HRESULT WINAPI xmlwriter_WriteSurrogateCharEntity(IXmlWriter *iface, WCHAR wchLow, WCHAR wchHigh)
918 {
919 xmlwriter *This = impl_from_IXmlWriter(iface);
920
921 FIXME("%p %d %d\n", This, wchLow, wchHigh);
922
923 return E_NOTIMPL;
924 }
925
926 static HRESULT WINAPI xmlwriter_WriteWhitespace(IXmlWriter *iface, LPCWSTR pwszWhitespace)
927 {
928 xmlwriter *This = impl_from_IXmlWriter(iface);
929
930 FIXME("%p %s\n", This, wine_dbgstr_w(pwszWhitespace));
931
932 return E_NOTIMPL;
933 }
934
935 static HRESULT WINAPI xmlwriter_Flush(IXmlWriter *iface)
936 {
937 xmlwriter *This = impl_from_IXmlWriter(iface);
938
939 TRACE("%p\n", This);
940
941 return writeroutput_flush_stream(This->output);
942 }
943
944 static const struct IXmlWriterVtbl xmlwriter_vtbl =
945 {
946 xmlwriter_QueryInterface,
947 xmlwriter_AddRef,
948 xmlwriter_Release,
949 xmlwriter_SetOutput,
950 xmlwriter_GetProperty,
951 xmlwriter_SetProperty,
952 xmlwriter_WriteAttributes,
953 xmlwriter_WriteAttributeString,
954 xmlwriter_WriteCData,
955 xmlwriter_WriteCharEntity,
956 xmlwriter_WriteChars,
957 xmlwriter_WriteComment,
958 xmlwriter_WriteDocType,
959 xmlwriter_WriteElementString,
960 xmlwriter_WriteEndDocument,
961 xmlwriter_WriteEndElement,
962 xmlwriter_WriteEntityRef,
963 xmlwriter_WriteFullEndElement,
964 xmlwriter_WriteName,
965 xmlwriter_WriteNmToken,
966 xmlwriter_WriteNode,
967 xmlwriter_WriteNodeShallow,
968 xmlwriter_WriteProcessingInstruction,
969 xmlwriter_WriteQualifiedName,
970 xmlwriter_WriteRaw,
971 xmlwriter_WriteRawChars,
972 xmlwriter_WriteStartDocument,
973 xmlwriter_WriteStartElement,
974 xmlwriter_WriteString,
975 xmlwriter_WriteSurrogateCharEntity,
976 xmlwriter_WriteWhitespace,
977 xmlwriter_Flush
978 };
979
980 /** IXmlWriterOutput **/
981 static HRESULT WINAPI xmlwriteroutput_QueryInterface(IXmlWriterOutput *iface, REFIID riid, void** ppvObject)
982 {
983 xmlwriteroutput *This = impl_from_IXmlWriterOutput(iface);
984
985 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
986
987 if (IsEqualGUID(riid, &IID_IXmlWriterOutput) ||
988 IsEqualGUID(riid, &IID_IUnknown))
989 {
990 *ppvObject = iface;
991 }
992 else
993 {
994 WARN("interface %s not implemented\n", debugstr_guid(riid));
995 *ppvObject = NULL;
996 return E_NOINTERFACE;
997 }
998
999 IUnknown_AddRef(iface);
1000
1001 return S_OK;
1002 }
1003
1004 static ULONG WINAPI xmlwriteroutput_AddRef(IXmlWriterOutput *iface)
1005 {
1006 xmlwriteroutput *This = impl_from_IXmlWriterOutput(iface);
1007 ULONG ref = InterlockedIncrement(&This->ref);
1008 TRACE("(%p)->(%d)\n", This, ref);
1009 return ref;
1010 }
1011
1012 static ULONG WINAPI xmlwriteroutput_Release(IXmlWriterOutput *iface)
1013 {
1014 xmlwriteroutput *This = impl_from_IXmlWriterOutput(iface);
1015 LONG ref = InterlockedDecrement(&This->ref);
1016
1017 TRACE("(%p)->(%d)\n", This, ref);
1018
1019 if (ref == 0)
1020 {
1021 IMalloc *imalloc = This->imalloc;
1022 if (This->output) IUnknown_Release(This->output);
1023 if (This->stream) ISequentialStream_Release(This->stream);
1024 free_output_buffer(This);
1025 writeroutput_free(This, This);
1026 if (imalloc) IMalloc_Release(imalloc);
1027 }
1028
1029 return ref;
1030 }
1031
1032 static const struct IUnknownVtbl xmlwriteroutputvtbl =
1033 {
1034 xmlwriteroutput_QueryInterface,
1035 xmlwriteroutput_AddRef,
1036 xmlwriteroutput_Release
1037 };
1038
1039 HRESULT WINAPI CreateXmlWriter(REFIID riid, void **obj, IMalloc *imalloc)
1040 {
1041 xmlwriter *writer;
1042
1043 TRACE("(%s, %p, %p)\n", wine_dbgstr_guid(riid), obj, imalloc);
1044
1045 if (!IsEqualGUID(riid, &IID_IXmlWriter))
1046 {
1047 ERR("Unexpected IID requested -> (%s)\n", wine_dbgstr_guid(riid));
1048 return E_FAIL;
1049 }
1050
1051 if (imalloc)
1052 writer = IMalloc_Alloc(imalloc, sizeof(*writer));
1053 else
1054 writer = heap_alloc(sizeof(*writer));
1055 if(!writer) return E_OUTOFMEMORY;
1056
1057 writer->IXmlWriter_iface.lpVtbl = &xmlwriter_vtbl;
1058 writer->ref = 1;
1059 writer->imalloc = imalloc;
1060 if (imalloc) IMalloc_AddRef(imalloc);
1061 writer->output = NULL;
1062 writer->indent = FALSE;
1063 writer->bom = TRUE;
1064 writer->omitxmldecl = FALSE;
1065 writer->conformance = XmlConformanceLevel_Document;
1066 writer->state = XmlWriterState_Initial;
1067 writer->bomwritten = FALSE;
1068 writer->starttagopen = FALSE;
1069 list_init(&writer->elements);
1070
1071 *obj = &writer->IXmlWriter_iface;
1072
1073 TRACE("returning iface %p\n", *obj);
1074
1075 return S_OK;
1076 }
1077
1078 HRESULT WINAPI CreateXmlWriterOutputWithEncodingName(IUnknown *stream,
1079 IMalloc *imalloc,
1080 LPCWSTR encoding,
1081 IXmlWriterOutput **output)
1082 {
1083 static const WCHAR utf8W[] = {'U','T','F','-','8',0};
1084 xmlwriteroutput *writeroutput;
1085 HRESULT hr;
1086
1087 TRACE("%p %p %s %p\n", stream, imalloc, debugstr_w(encoding), output);
1088
1089 if (!stream || !output) return E_INVALIDARG;
1090
1091 *output = NULL;
1092
1093 if (imalloc)
1094 writeroutput = IMalloc_Alloc(imalloc, sizeof(*writeroutput));
1095 else
1096 writeroutput = heap_alloc(sizeof(*writeroutput));
1097 if(!writeroutput) return E_OUTOFMEMORY;
1098
1099 writeroutput->IXmlWriterOutput_iface.lpVtbl = &xmlwriteroutputvtbl;
1100 writeroutput->ref = 1;
1101 writeroutput->imalloc = imalloc;
1102 if (imalloc) IMalloc_AddRef(imalloc);
1103 writeroutput->encoding = parse_encoding_name(encoding ? encoding : utf8W, -1);
1104 writeroutput->stream = NULL;
1105 hr = init_output_buffer(writeroutput);
1106 if (FAILED(hr)) {
1107 IUnknown_Release(&writeroutput->IXmlWriterOutput_iface);
1108 return hr;
1109 }
1110
1111 IUnknown_QueryInterface(stream, &IID_IUnknown, (void**)&writeroutput->output);
1112
1113 *output = &writeroutput->IXmlWriterOutput_iface;
1114
1115 TRACE("returning iface %p\n", *output);
1116
1117 return S_OK;
1118 }