msxml3: Stub support for IMXAttributes.
[wine] / dlls / msxml3 / mxwriter.c
1 /*
2  *    MXWriter implementation
3  *
4  * Copyright 2011-2012 Nikolay Sivov for CodeWeavers
5  * Copyright 2011 Thomas Mullaly
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 #define COBJMACROS
23 #include "config.h"
24
25 #include <stdarg.h>
26 #ifdef HAVE_LIBXML2
27 # include <libxml/parser.h>
28 #endif
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "ole2.h"
33
34 #include "msxml6.h"
35
36 #include "wine/debug.h"
37
38 #include "msxml_private.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
41
42 static const WCHAR utf16W[] = {'U','T','F','-','1','6',0};
43 static const WCHAR emptyW[] = {0};
44 static const WCHAR spaceW[] = {' '};
45 static const WCHAR quotW[]  = {'\"'};
46
47 typedef enum
48 {
49     XmlEncoding_UTF8,
50     XmlEncoding_UTF16,
51     XmlEncoding_Unknown
52 } xml_encoding;
53
54 typedef enum
55 {
56     OutputBuffer_Native  = 0x001,
57     OutputBuffer_Encoded = 0x010,
58     OutputBuffer_Both    = 0x100
59 } output_mode;
60
61 typedef enum
62 {
63     MXWriter_BOM = 0,
64     MXWriter_DisableEscaping,
65     MXWriter_Indent,
66     MXWriter_OmitXmlDecl,
67     MXWriter_Standalone,
68     MXWriter_LastProp
69 } mxwriter_prop;
70
71 typedef enum
72 {
73     EscapeValue,
74     EscapeText
75 } escape_mode;
76
77 typedef struct
78 {
79     char *data;
80     unsigned int allocated;
81     unsigned int written;
82 } encoded_buffer;
83
84 typedef struct
85 {
86     encoded_buffer utf16;
87     encoded_buffer encoded;
88     UINT code_page;
89 } output_buffer;
90
91 typedef struct
92 {
93     DispatchEx dispex;
94     IMXWriter IMXWriter_iface;
95     ISAXContentHandler ISAXContentHandler_iface;
96     ISAXLexicalHandler ISAXLexicalHandler_iface;
97     ISAXDeclHandler    ISAXDeclHandler_iface;
98
99     LONG ref;
100     MSXML_VERSION class_version;
101
102     VARIANT_BOOL props[MXWriter_LastProp];
103     BOOL prop_changed;
104     BOOL cdata;
105
106     BSTR version;
107
108     BSTR encoding; /* exact property value */
109     xml_encoding xml_enc;
110
111     /* contains a pending (or not closed yet) element name or NULL if
112        we don't have to close */
113     BSTR element;
114
115     IStream *dest;
116     ULONG dest_written;
117
118     output_buffer *buffer;
119 } mxwriter;
120
121 typedef struct
122 {
123     DispatchEx dispex;
124     IMXAttributes IMXAttributes_iface;
125     LONG ref;
126 } mxattributes;
127
128 static inline mxattributes *impl_from_IMXAttributes( IMXAttributes *iface )
129 {
130     return CONTAINING_RECORD(iface, mxattributes, IMXAttributes_iface);
131 }
132
133 static xml_encoding parse_encoding_name(const WCHAR *encoding)
134 {
135     static const WCHAR utf8W[]  = {'U','T','F','-','8',0};
136     if (!strcmpiW(encoding, utf8W))  return XmlEncoding_UTF8;
137     if (!strcmpiW(encoding, utf16W)) return XmlEncoding_UTF16;
138     return XmlEncoding_Unknown;
139 }
140
141 static HRESULT init_encoded_buffer(encoded_buffer *buffer)
142 {
143     const int initial_len = 0x2000;
144     buffer->data = heap_alloc(initial_len);
145     if (!buffer->data) return E_OUTOFMEMORY;
146
147     memset(buffer->data, 0, 4);
148     buffer->allocated = initial_len;
149     buffer->written = 0;
150
151     return S_OK;
152 }
153
154 static void free_encoded_buffer(encoded_buffer *buffer)
155 {
156     heap_free(buffer->data);
157 }
158
159 static HRESULT get_code_page(xml_encoding encoding, UINT *cp)
160 {
161     switch (encoding)
162     {
163     case XmlEncoding_UTF8:
164         *cp = CP_UTF8;
165         break;
166     case XmlEncoding_UTF16:
167         *cp = ~0;
168         break;
169     default:
170         FIXME("unsupported encoding %d\n", encoding);
171         return E_NOTIMPL;
172     }
173
174     return S_OK;
175 }
176
177 static HRESULT alloc_output_buffer(xml_encoding encoding, output_buffer **buffer)
178 {
179     output_buffer *ret;
180     HRESULT hr;
181
182     ret = heap_alloc(sizeof(*ret));
183     if (!ret) return E_OUTOFMEMORY;
184
185     hr = get_code_page(encoding, &ret->code_page);
186     if (hr != S_OK) {
187         heap_free(ret);
188         return hr;
189     }
190
191     hr = init_encoded_buffer(&ret->utf16);
192     if (hr != S_OK) {
193         heap_free(ret);
194         return hr;
195     }
196
197     if (ret->code_page == CP_UTF8) {
198         hr = init_encoded_buffer(&ret->encoded);
199         if (hr != S_OK) {
200             free_encoded_buffer(&ret->utf16);
201             heap_free(ret);
202             return hr;
203         }
204     }
205     else
206         memset(&ret->encoded, 0, sizeof(ret->encoded));
207
208     *buffer = ret;
209
210     return S_OK;
211 }
212
213 static void free_output_buffer(output_buffer *buffer)
214 {
215     free_encoded_buffer(&buffer->encoded);
216     free_encoded_buffer(&buffer->utf16);
217     heap_free(buffer);
218 }
219
220 static void grow_buffer(encoded_buffer *buffer, int length)
221 {
222     /* grow if needed, plus 4 bytes to be sure null terminator will fit in */
223     if (buffer->allocated < buffer->written + length + 4)
224     {
225         int grown_size = max(2*buffer->allocated, buffer->allocated + length);
226         buffer->data = heap_realloc(buffer->data, grown_size);
227         buffer->allocated = grown_size;
228     }
229 }
230
231 static HRESULT write_output_buffer_mode(output_buffer *buffer, output_mode mode, const WCHAR *data, int len)
232 {
233     int length;
234     char *ptr;
235
236     if (mode & (OutputBuffer_Encoded | OutputBuffer_Both)) {
237         if (buffer->code_page == CP_UTF8)
238         {
239             length = WideCharToMultiByte(buffer->code_page, 0, data, len, NULL, 0, NULL, NULL);
240             grow_buffer(&buffer->encoded, length);
241             ptr = buffer->encoded.data + buffer->encoded.written;
242             length = WideCharToMultiByte(buffer->code_page, 0, data, len, ptr, length, NULL, NULL);
243             buffer->encoded.written += len == -1 ? length-1 : length;
244         }
245     }
246
247     if (mode & (OutputBuffer_Native | OutputBuffer_Both)) {
248         /* WCHAR data just copied */
249         length = len == -1 ? strlenW(data) : len;
250         if (length)
251         {
252             length *= sizeof(WCHAR);
253
254             grow_buffer(&buffer->utf16, length);
255             ptr = buffer->utf16.data + buffer->utf16.written;
256
257             memcpy(ptr, data, length);
258             buffer->utf16.written += length;
259             ptr += length;
260             /* null termination */
261             memset(ptr, 0, sizeof(WCHAR));
262         }
263     }
264
265     return S_OK;
266 }
267
268 static HRESULT write_output_buffer(output_buffer *buffer, const WCHAR *data, int len)
269 {
270     return write_output_buffer_mode(buffer, OutputBuffer_Both, data, len);
271 }
272
273 static HRESULT write_output_buffer_quoted(output_buffer *buffer, const WCHAR *data, int len)
274 {
275     write_output_buffer(buffer, quotW, 1);
276     write_output_buffer(buffer, data, len);
277     write_output_buffer(buffer, quotW, 1);
278
279     return S_OK;
280 }
281
282 /* frees buffer data, reallocates with a default lengths */
283 static void close_output_buffer(mxwriter *This)
284 {
285     heap_free(This->buffer->utf16.data);
286     heap_free(This->buffer->encoded.data);
287     init_encoded_buffer(&This->buffer->utf16);
288     init_encoded_buffer(&This->buffer->encoded);
289     get_code_page(This->xml_enc, &This->buffer->code_page);
290 }
291
292 /* escapes special characters like:
293    '<' -> "&lt;"
294    '&' -> "&amp;"
295    '"' -> "&quot;"
296    '>' -> "&gt;"
297 */
298 static WCHAR *get_escaped_string(const WCHAR *str, escape_mode mode, int *len)
299 {
300     static const WCHAR ltW[]    = {'&','l','t',';'};
301     static const WCHAR ampW[]   = {'&','a','m','p',';'};
302     static const WCHAR equotW[] = {'&','q','u','o','t',';'};
303     static const WCHAR gtW[]    = {'&','g','t',';'};
304
305     const int default_alloc = 100;
306     const int grow_thresh = 10;
307     int p = *len, conv_len;
308     WCHAR *ptr, *ret;
309
310     /* default buffer size to something if length is unknown */
311     conv_len = *len == -1 ? default_alloc : max(2**len, default_alloc);
312     ptr = ret = heap_alloc(conv_len*sizeof(WCHAR));
313
314     while (*str && p)
315     {
316         if (ptr - ret > conv_len - grow_thresh)
317         {
318             int written = ptr - ret;
319             conv_len *= 2;
320             ptr = ret = heap_realloc(ret, conv_len*sizeof(WCHAR));
321             ptr += written;
322         }
323
324         switch (*str)
325         {
326         case '<':
327             memcpy(ptr, ltW, sizeof(ltW));
328             ptr += sizeof(ltW)/sizeof(WCHAR);
329             break;
330         case '&':
331             memcpy(ptr, ampW, sizeof(ampW));
332             ptr += sizeof(ampW)/sizeof(WCHAR);
333             break;
334         case '>':
335             memcpy(ptr, gtW, sizeof(gtW));
336             ptr += sizeof(gtW)/sizeof(WCHAR);
337             break;
338         case '"':
339             if (mode == EscapeValue)
340             {
341                 memcpy(ptr, equotW, sizeof(equotW));
342                 ptr += sizeof(equotW)/sizeof(WCHAR);
343                 break;
344             }
345             /* fallthrough for text mode */
346         default:
347             *ptr++ = *str;
348             break;
349         }
350
351         str++;
352         if (*len != -1) p--;
353     }
354
355     if (*len != -1) *len = ptr-ret;
356     *++ptr = 0;
357
358     return ret;
359 }
360
361 static void write_prolog_buffer(const mxwriter *This)
362 {
363     static const WCHAR versionW[] = {'<','?','x','m','l',' ','v','e','r','s','i','o','n','='};
364     static const WCHAR encodingW[] = {' ','e','n','c','o','d','i','n','g','=','\"'};
365     static const WCHAR standaloneW[] = {' ','s','t','a','n','d','a','l','o','n','e','=','\"'};
366     static const WCHAR yesW[] = {'y','e','s','\"','?','>'};
367     static const WCHAR noW[] = {'n','o','\"','?','>'};
368     static const WCHAR crlfW[] = {'\r','\n'};
369
370     /* version */
371     write_output_buffer(This->buffer, versionW, sizeof(versionW)/sizeof(WCHAR));
372     write_output_buffer_quoted(This->buffer, This->version, -1);
373
374     /* encoding */
375     write_output_buffer(This->buffer, encodingW, sizeof(encodingW)/sizeof(WCHAR));
376
377     /* always write UTF-16 to WCHAR buffer */
378     write_output_buffer_mode(This->buffer, OutputBuffer_Native, utf16W, sizeof(utf16W)/sizeof(WCHAR) - 1);
379     write_output_buffer_mode(This->buffer, OutputBuffer_Encoded, This->encoding, -1);
380     write_output_buffer(This->buffer, quotW, 1);
381
382     /* standalone */
383     write_output_buffer(This->buffer, standaloneW, sizeof(standaloneW)/sizeof(WCHAR));
384     if (This->props[MXWriter_Standalone] == VARIANT_TRUE)
385         write_output_buffer(This->buffer, yesW, sizeof(yesW)/sizeof(WCHAR));
386     else
387         write_output_buffer(This->buffer, noW, sizeof(noW)/sizeof(WCHAR));
388
389     write_output_buffer(This->buffer, crlfW, sizeof(crlfW)/sizeof(WCHAR));
390 }
391
392 /* Attempts to the write data from the mxwriter's buffer to
393  * the destination stream (if there is one).
394  */
395 static HRESULT write_data_to_stream(mxwriter *This)
396 {
397     encoded_buffer *buffer;
398     ULONG written = 0;
399     HRESULT hr;
400
401     if (!This->dest)
402         return S_OK;
403
404     if (This->xml_enc != XmlEncoding_UTF16)
405         buffer = &This->buffer->encoded;
406     else
407         buffer = &This->buffer->utf16;
408
409     if (This->dest_written > buffer->written) {
410         ERR("Failed sanity check! Not sure what to do... (%d > %d)\n", This->dest_written, buffer->written);
411         return E_FAIL;
412     } else if (This->dest_written == buffer->written && This->xml_enc != XmlEncoding_UTF8)
413         /* Windows seems to make an empty write call when the encoding is UTF-8 and
414          * all the data has been written to the stream. It doesn't seem make this call
415          * for any other encodings.
416          */
417         return S_OK;
418
419     /* Write the current content from the output buffer into 'dest'.
420      * TODO: Check what Windows does if the IStream doesn't write all of
421      *       the data we give it at once.
422      */
423     hr = IStream_Write(This->dest, buffer->data+This->dest_written,
424                          buffer->written-This->dest_written, &written);
425     if (FAILED(hr)) {
426         WARN("Failed to write data to IStream (0x%08x)\n", hr);
427         return hr;
428     }
429
430     This->dest_written += written;
431     return hr;
432 }
433
434 /* Newly added element start tag left unclosed cause for empty elements
435    we have to close it differently. */
436 static void close_element_starttag(const mxwriter *This)
437 {
438     static const WCHAR gtW[] = {'>'};
439     if (!This->element) return;
440     write_output_buffer(This->buffer, gtW, 1);
441 }
442
443 static void set_element_name(mxwriter *This, const WCHAR *name, int len)
444 {
445     SysFreeString(This->element);
446     This->element = name ? SysAllocStringLen(name, len) : NULL;
447 }
448
449 static inline HRESULT flush_output_buffer(mxwriter *This)
450 {
451     close_element_starttag(This);
452     set_element_name(This, NULL, 0);
453     This->cdata = FALSE;
454     return write_data_to_stream(This);
455 }
456
457 /* Resets the mxwriter's output buffer by closing it, then creating a new
458  * output buffer using the given encoding.
459  */
460 static inline void reset_output_buffer(mxwriter *This)
461 {
462     close_output_buffer(This);
463     This->dest_written = 0;
464 }
465
466 static HRESULT writer_set_property(mxwriter *writer, mxwriter_prop property, VARIANT_BOOL value)
467 {
468     writer->props[property] = value;
469     writer->prop_changed = TRUE;
470     return S_OK;
471 }
472
473 static HRESULT writer_get_property(const mxwriter *writer, mxwriter_prop property, VARIANT_BOOL *value)
474 {
475     if (!value) return E_POINTER;
476     *value = writer->props[property];
477     return S_OK;
478 }
479
480 static inline mxwriter *impl_from_IMXWriter(IMXWriter *iface)
481 {
482     return CONTAINING_RECORD(iface, mxwriter, IMXWriter_iface);
483 }
484
485 static inline mxwriter *impl_from_ISAXContentHandler(ISAXContentHandler *iface)
486 {
487     return CONTAINING_RECORD(iface, mxwriter, ISAXContentHandler_iface);
488 }
489
490 static inline mxwriter *impl_from_ISAXLexicalHandler(ISAXLexicalHandler *iface)
491 {
492     return CONTAINING_RECORD(iface, mxwriter, ISAXLexicalHandler_iface);
493 }
494
495 static inline mxwriter *impl_from_ISAXDeclHandler(ISAXDeclHandler *iface)
496 {
497     return CONTAINING_RECORD(iface, mxwriter, ISAXDeclHandler_iface);
498 }
499
500 static HRESULT WINAPI mxwriter_QueryInterface(IMXWriter *iface, REFIID riid, void **obj)
501 {
502     mxwriter *This = impl_from_IMXWriter( iface );
503
504     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
505
506     *obj = NULL;
507
508     if ( IsEqualGUID( riid, &IID_IMXWriter ) ||
509          IsEqualGUID( riid, &IID_IDispatch ) ||
510          IsEqualGUID( riid, &IID_IUnknown ) )
511     {
512         *obj = &This->IMXWriter_iface;
513     }
514     else if ( IsEqualGUID( riid, &IID_ISAXContentHandler ) )
515     {
516         *obj = &This->ISAXContentHandler_iface;
517     }
518     else if ( IsEqualGUID( riid, &IID_ISAXLexicalHandler ) )
519     {
520         *obj = &This->ISAXLexicalHandler_iface;
521     }
522     else if ( IsEqualGUID( riid, &IID_ISAXDeclHandler ) )
523     {
524         *obj = &This->ISAXDeclHandler_iface;
525     }
526     else if (dispex_query_interface(&This->dispex, riid, obj))
527     {
528         return *obj ? S_OK : E_NOINTERFACE;
529     }
530     else
531     {
532         ERR("interface %s not implemented\n", debugstr_guid(riid));
533         *obj = NULL;
534         return E_NOINTERFACE;
535     }
536
537     IMXWriter_AddRef(iface);
538     return S_OK;
539 }
540
541 static ULONG WINAPI mxwriter_AddRef(IMXWriter *iface)
542 {
543     mxwriter *This = impl_from_IMXWriter( iface );
544     LONG ref = InterlockedIncrement(&This->ref);
545
546     TRACE("(%p)->(%d)\n", This, ref);
547
548     return ref;
549 }
550
551 static ULONG WINAPI mxwriter_Release(IMXWriter *iface)
552 {
553     mxwriter *This = impl_from_IMXWriter( iface );
554     ULONG ref = InterlockedDecrement(&This->ref);
555
556     TRACE("(%p)->(%d)\n", This, ref);
557
558     if(!ref)
559     {
560         /* Windows flushes the buffer when the interface is destroyed. */
561         flush_output_buffer(This);
562         free_output_buffer(This->buffer);
563
564         if (This->dest) IStream_Release(This->dest);
565         SysFreeString(This->version);
566         SysFreeString(This->encoding);
567
568         SysFreeString(This->element);
569         release_dispex(&This->dispex);
570         heap_free(This);
571     }
572
573     return ref;
574 }
575
576 static HRESULT WINAPI mxwriter_GetTypeInfoCount(IMXWriter *iface, UINT* pctinfo)
577 {
578     mxwriter *This = impl_from_IMXWriter( iface );
579     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
580 }
581
582 static HRESULT WINAPI mxwriter_GetTypeInfo(
583     IMXWriter *iface,
584     UINT iTInfo, LCID lcid,
585     ITypeInfo** ppTInfo )
586 {
587     mxwriter *This = impl_from_IMXWriter( iface );
588     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface,
589         iTInfo, lcid, ppTInfo);
590 }
591
592 static HRESULT WINAPI mxwriter_GetIDsOfNames(
593     IMXWriter *iface,
594     REFIID riid, LPOLESTR* rgszNames,
595     UINT cNames, LCID lcid, DISPID* rgDispId )
596 {
597     mxwriter *This = impl_from_IMXWriter( iface );
598     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface,
599         riid, rgszNames, cNames, lcid, rgDispId);
600 }
601
602 static HRESULT WINAPI mxwriter_Invoke(
603     IMXWriter *iface,
604     DISPID dispIdMember, REFIID riid, LCID lcid,
605     WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
606     EXCEPINFO* pExcepInfo, UINT* puArgErr )
607 {
608     mxwriter *This = impl_from_IMXWriter( iface );
609     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface,
610         dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
611 }
612
613 static HRESULT WINAPI mxwriter_put_output(IMXWriter *iface, VARIANT dest)
614 {
615     mxwriter *This = impl_from_IMXWriter( iface );
616     HRESULT hr;
617
618     TRACE("(%p)->(%s)\n", This, debugstr_variant(&dest));
619
620     hr = flush_output_buffer(This);
621     if (FAILED(hr))
622         return hr;
623
624     switch (V_VT(&dest))
625     {
626     case VT_EMPTY:
627     {
628         if (This->dest) IStream_Release(This->dest);
629         This->dest = NULL;
630         reset_output_buffer(This);
631         break;
632     }
633     case VT_UNKNOWN:
634     {
635         IStream *stream;
636
637         hr = IUnknown_QueryInterface(V_UNKNOWN(&dest), &IID_IStream, (void**)&stream);
638         if (hr == S_OK)
639         {
640             /* Recreate the output buffer to make sure it's using the correct encoding. */
641             reset_output_buffer(This);
642
643             if (This->dest) IStream_Release(This->dest);
644             This->dest = stream;
645             break;
646         }
647
648         FIXME("unhandled interface type for VT_UNKNOWN destination\n");
649         return E_NOTIMPL;
650     }
651     default:
652         FIXME("unhandled destination type %s\n", debugstr_variant(&dest));
653         return E_NOTIMPL;
654     }
655
656     return S_OK;
657 }
658
659 static HRESULT WINAPI mxwriter_get_output(IMXWriter *iface, VARIANT *dest)
660 {
661     mxwriter *This = impl_from_IMXWriter( iface );
662
663     TRACE("(%p)->(%p)\n", This, dest);
664
665     if (!This->dest)
666     {
667         HRESULT hr = flush_output_buffer(This);
668         if (FAILED(hr))
669             return hr;
670
671         V_VT(dest)   = VT_BSTR;
672         V_BSTR(dest) = SysAllocString((WCHAR*)This->buffer->utf16.data);
673
674         return S_OK;
675     }
676     else
677         FIXME("not implemented when stream is set up\n");
678
679     return E_NOTIMPL;
680 }
681
682 static HRESULT WINAPI mxwriter_put_encoding(IMXWriter *iface, BSTR encoding)
683 {
684     mxwriter *This = impl_from_IMXWriter( iface );
685     xml_encoding enc;
686     HRESULT hr;
687
688     TRACE("(%p)->(%s)\n", This, debugstr_w(encoding));
689
690     enc = parse_encoding_name(encoding);
691     if (enc == XmlEncoding_Unknown)
692     {
693         FIXME("unsupported encoding %s\n", debugstr_w(encoding));
694         return E_INVALIDARG;
695     }
696
697     hr = flush_output_buffer(This);
698     if (FAILED(hr))
699         return hr;
700
701     SysReAllocString(&This->encoding, encoding);
702     This->xml_enc = enc;
703
704     TRACE("got encoding %d\n", This->xml_enc);
705     reset_output_buffer(This);
706     return S_OK;
707 }
708
709 static HRESULT WINAPI mxwriter_get_encoding(IMXWriter *iface, BSTR *encoding)
710 {
711     mxwriter *This = impl_from_IMXWriter( iface );
712
713     TRACE("(%p)->(%p)\n", This, encoding);
714
715     if (!encoding) return E_POINTER;
716
717     *encoding = SysAllocString(This->encoding);
718     if (!*encoding) return E_OUTOFMEMORY;
719
720     return S_OK;
721 }
722
723 static HRESULT WINAPI mxwriter_put_byteOrderMark(IMXWriter *iface, VARIANT_BOOL value)
724 {
725     mxwriter *This = impl_from_IMXWriter( iface );
726
727     TRACE("(%p)->(%d)\n", This, value);
728     return writer_set_property(This, MXWriter_BOM, value);
729 }
730
731 static HRESULT WINAPI mxwriter_get_byteOrderMark(IMXWriter *iface, VARIANT_BOOL *value)
732 {
733     mxwriter *This = impl_from_IMXWriter( iface );
734
735     TRACE("(%p)->(%p)\n", This, value);
736     return writer_get_property(This, MXWriter_BOM, value);
737 }
738
739 static HRESULT WINAPI mxwriter_put_indent(IMXWriter *iface, VARIANT_BOOL value)
740 {
741     mxwriter *This = impl_from_IMXWriter( iface );
742
743     TRACE("(%p)->(%d)\n", This, value);
744     return writer_set_property(This, MXWriter_Indent, value);
745 }
746
747 static HRESULT WINAPI mxwriter_get_indent(IMXWriter *iface, VARIANT_BOOL *value)
748 {
749     mxwriter *This = impl_from_IMXWriter( iface );
750
751     TRACE("(%p)->(%p)\n", This, value);
752     return writer_get_property(This, MXWriter_Indent, value);
753 }
754
755 static HRESULT WINAPI mxwriter_put_standalone(IMXWriter *iface, VARIANT_BOOL value)
756 {
757     mxwriter *This = impl_from_IMXWriter( iface );
758
759     TRACE("(%p)->(%d)\n", This, value);
760     return writer_set_property(This, MXWriter_Standalone, value);
761 }
762
763 static HRESULT WINAPI mxwriter_get_standalone(IMXWriter *iface, VARIANT_BOOL *value)
764 {
765     mxwriter *This = impl_from_IMXWriter( iface );
766
767     TRACE("(%p)->(%p)\n", This, value);
768     return writer_get_property(This, MXWriter_Standalone, value);
769 }
770
771 static HRESULT WINAPI mxwriter_put_omitXMLDeclaration(IMXWriter *iface, VARIANT_BOOL value)
772 {
773     mxwriter *This = impl_from_IMXWriter( iface );
774
775     TRACE("(%p)->(%d)\n", This, value);
776     return writer_set_property(This, MXWriter_OmitXmlDecl, value);
777 }
778
779 static HRESULT WINAPI mxwriter_get_omitXMLDeclaration(IMXWriter *iface, VARIANT_BOOL *value)
780 {
781     mxwriter *This = impl_from_IMXWriter( iface );
782
783     TRACE("(%p)->(%p)\n", This, value);
784     return writer_get_property(This, MXWriter_OmitXmlDecl, value);
785 }
786
787 static HRESULT WINAPI mxwriter_put_version(IMXWriter *iface, BSTR version)
788 {
789     mxwriter *This = impl_from_IMXWriter( iface );
790
791     TRACE("(%p)->(%s)\n", This, debugstr_w(version));
792
793     if (!version) return E_INVALIDARG;
794
795     SysFreeString(This->version);
796     This->version = SysAllocString(version);
797
798     return S_OK;
799 }
800
801 static HRESULT WINAPI mxwriter_get_version(IMXWriter *iface, BSTR *version)
802 {
803     mxwriter *This = impl_from_IMXWriter( iface );
804
805     TRACE("(%p)->(%p)\n", This, version);
806
807     if (!version) return E_POINTER;
808
809     return return_bstr(This->version, version);
810 }
811
812 static HRESULT WINAPI mxwriter_put_disableOutputEscaping(IMXWriter *iface, VARIANT_BOOL value)
813 {
814     mxwriter *This = impl_from_IMXWriter( iface );
815
816     TRACE("(%p)->(%d)\n", This, value);
817     return writer_set_property(This, MXWriter_DisableEscaping, value);
818 }
819
820 static HRESULT WINAPI mxwriter_get_disableOutputEscaping(IMXWriter *iface, VARIANT_BOOL *value)
821 {
822     mxwriter *This = impl_from_IMXWriter( iface );
823
824     TRACE("(%p)->(%p)\n", This, value);
825     return writer_get_property(This, MXWriter_DisableEscaping, value);
826 }
827
828 static HRESULT WINAPI mxwriter_flush(IMXWriter *iface)
829 {
830     mxwriter *This = impl_from_IMXWriter( iface );
831     TRACE("(%p)\n", This);
832     return flush_output_buffer(This);
833 }
834
835 static const struct IMXWriterVtbl MXWriterVtbl =
836 {
837     mxwriter_QueryInterface,
838     mxwriter_AddRef,
839     mxwriter_Release,
840     mxwriter_GetTypeInfoCount,
841     mxwriter_GetTypeInfo,
842     mxwriter_GetIDsOfNames,
843     mxwriter_Invoke,
844     mxwriter_put_output,
845     mxwriter_get_output,
846     mxwriter_put_encoding,
847     mxwriter_get_encoding,
848     mxwriter_put_byteOrderMark,
849     mxwriter_get_byteOrderMark,
850     mxwriter_put_indent,
851     mxwriter_get_indent,
852     mxwriter_put_standalone,
853     mxwriter_get_standalone,
854     mxwriter_put_omitXMLDeclaration,
855     mxwriter_get_omitXMLDeclaration,
856     mxwriter_put_version,
857     mxwriter_get_version,
858     mxwriter_put_disableOutputEscaping,
859     mxwriter_get_disableOutputEscaping,
860     mxwriter_flush
861 };
862
863 /*** ISAXContentHandler ***/
864 static HRESULT WINAPI SAXContentHandler_QueryInterface(
865     ISAXContentHandler *iface,
866     REFIID riid,
867     void **obj)
868 {
869     mxwriter *This = impl_from_ISAXContentHandler( iface );
870     return IMXWriter_QueryInterface(&This->IMXWriter_iface, riid, obj);
871 }
872
873 static ULONG WINAPI SAXContentHandler_AddRef(ISAXContentHandler *iface)
874 {
875     mxwriter *This = impl_from_ISAXContentHandler( iface );
876     return IMXWriter_AddRef(&This->IMXWriter_iface);
877 }
878
879 static ULONG WINAPI SAXContentHandler_Release(ISAXContentHandler *iface)
880 {
881     mxwriter *This = impl_from_ISAXContentHandler( iface );
882     return IMXWriter_Release(&This->IMXWriter_iface);
883 }
884
885 static HRESULT WINAPI SAXContentHandler_putDocumentLocator(
886     ISAXContentHandler *iface,
887     ISAXLocator *locator)
888 {
889     mxwriter *This = impl_from_ISAXContentHandler( iface );
890     FIXME("(%p)->(%p)\n", This, locator);
891     return E_NOTIMPL;
892 }
893
894 static HRESULT WINAPI SAXContentHandler_startDocument(ISAXContentHandler *iface)
895 {
896     mxwriter *This = impl_from_ISAXContentHandler( iface );
897
898     TRACE("(%p)\n", This);
899
900     /* If properties have been changed since the last "endDocument" call
901      * we need to reset the output buffer. If we don't the output buffer
902      * could end up with multiple XML documents in it, plus this seems to
903      * be how Windows works.
904      */
905     if (This->prop_changed) {
906         reset_output_buffer(This);
907         This->prop_changed = FALSE;
908     }
909
910     if (This->props[MXWriter_OmitXmlDecl] == VARIANT_TRUE) return S_OK;
911
912     write_prolog_buffer(This);
913
914     if (This->dest && This->xml_enc == XmlEncoding_UTF16) {
915         static const char utf16BOM[] = {0xff,0xfe};
916
917         if (This->props[MXWriter_BOM] == VARIANT_TRUE)
918             /* Windows passes a NULL pointer as the pcbWritten parameter and
919              * ignores any error codes returned from this Write call.
920              */
921             IStream_Write(This->dest, utf16BOM, sizeof(utf16BOM), NULL);
922     }
923
924     return S_OK;
925 }
926
927 static HRESULT WINAPI SAXContentHandler_endDocument(ISAXContentHandler *iface)
928 {
929     mxwriter *This = impl_from_ISAXContentHandler( iface );
930     TRACE("(%p)\n", This);
931     This->prop_changed = FALSE;
932     return flush_output_buffer(This);
933 }
934
935 static HRESULT WINAPI SAXContentHandler_startPrefixMapping(
936     ISAXContentHandler *iface,
937     const WCHAR *prefix,
938     int nprefix,
939     const WCHAR *uri,
940     int nuri)
941 {
942     mxwriter *This = impl_from_ISAXContentHandler( iface );
943     FIXME("(%p)->(%s %s)\n", This, debugstr_wn(prefix, nprefix), debugstr_wn(uri, nuri));
944     return E_NOTIMPL;
945 }
946
947 static HRESULT WINAPI SAXContentHandler_endPrefixMapping(
948     ISAXContentHandler *iface,
949     const WCHAR *prefix,
950     int nprefix)
951 {
952     mxwriter *This = impl_from_ISAXContentHandler( iface );
953     FIXME("(%p)->(%s)\n", This, debugstr_wn(prefix, nprefix));
954     return E_NOTIMPL;
955 }
956
957 static HRESULT WINAPI SAXContentHandler_startElement(
958     ISAXContentHandler *iface,
959     const WCHAR *namespaceUri,
960     int nnamespaceUri,
961     const WCHAR *local_name,
962     int nlocal_name,
963     const WCHAR *QName,
964     int nQName,
965     ISAXAttributes *attr)
966 {
967     mxwriter *This = impl_from_ISAXContentHandler( iface );
968     static const WCHAR ltW[] = {'<'};
969
970     TRACE("(%p)->(%s %s %s %p)\n", This, debugstr_wn(namespaceUri, nnamespaceUri),
971         debugstr_wn(local_name, nlocal_name), debugstr_wn(QName, nQName), attr);
972
973     if ((!namespaceUri || !local_name || !QName) && This->class_version != MSXML6)
974         return E_INVALIDARG;
975
976     close_element_starttag(This);
977     set_element_name(This, QName ? QName  : emptyW,
978                            QName ? nQName : 0);
979
980     write_output_buffer(This->buffer, ltW, 1);
981     write_output_buffer(This->buffer, QName, nQName);
982
983     if (attr)
984     {
985         HRESULT hr;
986         INT length;
987         INT i;
988
989         hr = ISAXAttributes_getLength(attr, &length);
990         if (FAILED(hr)) return hr;
991
992         for (i = 0; i < length; i++)
993         {
994             static const WCHAR eqW[] = {'='};
995             const WCHAR *str;
996             WCHAR *escaped;
997             INT len = 0;
998
999             hr = ISAXAttributes_getQName(attr, i, &str, &len);
1000             if (FAILED(hr)) return hr;
1001
1002             /* space separator in front of every attribute */
1003             write_output_buffer(This->buffer, spaceW, 1);
1004             write_output_buffer(This->buffer, str, len);
1005
1006             write_output_buffer(This->buffer, eqW, 1);
1007
1008             len = 0;
1009             hr = ISAXAttributes_getValue(attr, i, &str, &len);
1010             if (FAILED(hr)) return hr;
1011
1012             escaped = get_escaped_string(str, EscapeValue, &len);
1013             write_output_buffer_quoted(This->buffer, escaped, len);
1014             heap_free(escaped);
1015         }
1016     }
1017
1018     return S_OK;
1019 }
1020
1021 static HRESULT WINAPI SAXContentHandler_endElement(
1022     ISAXContentHandler *iface,
1023     const WCHAR *namespaceUri,
1024     int nnamespaceUri,
1025     const WCHAR * local_name,
1026     int nlocal_name,
1027     const WCHAR *QName,
1028     int nQName)
1029 {
1030     mxwriter *This = impl_from_ISAXContentHandler( iface );
1031
1032     TRACE("(%p)->(%s:%d %s:%d %s:%d)\n", This, debugstr_wn(namespaceUri, nnamespaceUri), nnamespaceUri,
1033         debugstr_wn(local_name, nlocal_name), nlocal_name, debugstr_wn(QName, nQName), nQName);
1034
1035     if ((!namespaceUri || !local_name || !QName) && This->class_version != MSXML6)
1036         return E_INVALIDARG;
1037
1038     if (This->element && QName && !strncmpW(This->element, QName, nQName))
1039     {
1040         static const WCHAR closeW[] = {'/','>'};
1041
1042         write_output_buffer(This->buffer, closeW, 2);
1043     }
1044     else
1045     {
1046         static const WCHAR closetagW[] = {'<','/'};
1047         static const WCHAR gtW[] = {'>'};
1048
1049         write_output_buffer(This->buffer, closetagW, 2);
1050         write_output_buffer(This->buffer, QName, nQName);
1051         write_output_buffer(This->buffer, gtW, 1);
1052     }
1053
1054     set_element_name(This, NULL, 0);
1055
1056     return S_OK;
1057 }
1058
1059 static HRESULT WINAPI SAXContentHandler_characters(
1060     ISAXContentHandler *iface,
1061     const WCHAR *chars,
1062     int nchars)
1063 {
1064     mxwriter *This = impl_from_ISAXContentHandler( iface );
1065
1066     TRACE("(%p)->(%s:%d)\n", This, debugstr_wn(chars, nchars), nchars);
1067
1068     if (!chars) return E_INVALIDARG;
1069
1070     close_element_starttag(This);
1071     set_element_name(This, NULL, 0);
1072
1073     if (nchars)
1074     {
1075         if (This->cdata)
1076             write_output_buffer(This->buffer, chars, nchars);
1077         else
1078         {
1079             int len = nchars;
1080             WCHAR *escaped;
1081
1082             escaped = get_escaped_string(chars, EscapeText, &len);
1083             write_output_buffer(This->buffer, escaped, len);
1084             heap_free(escaped);
1085         }
1086     }
1087
1088     return S_OK;
1089 }
1090
1091 static HRESULT WINAPI SAXContentHandler_ignorableWhitespace(
1092     ISAXContentHandler *iface,
1093     const WCHAR *chars,
1094     int nchars)
1095 {
1096     mxwriter *This = impl_from_ISAXContentHandler( iface );
1097     FIXME("(%p)->(%s)\n", This, debugstr_wn(chars, nchars));
1098     return E_NOTIMPL;
1099 }
1100
1101 static HRESULT WINAPI SAXContentHandler_processingInstruction(
1102     ISAXContentHandler *iface,
1103     const WCHAR *target,
1104     int ntarget,
1105     const WCHAR *data,
1106     int ndata)
1107 {
1108     mxwriter *This = impl_from_ISAXContentHandler( iface );
1109     FIXME("(%p)->(%s %s)\n", This, debugstr_wn(target, ntarget), debugstr_wn(data, ndata));
1110     return E_NOTIMPL;
1111 }
1112
1113 static HRESULT WINAPI SAXContentHandler_skippedEntity(
1114     ISAXContentHandler *iface,
1115     const WCHAR *name,
1116     int nname)
1117 {
1118     mxwriter *This = impl_from_ISAXContentHandler( iface );
1119     FIXME("(%p)->(%s)\n", This, debugstr_wn(name, nname));
1120     return E_NOTIMPL;
1121 }
1122
1123 static const struct ISAXContentHandlerVtbl SAXContentHandlerVtbl =
1124 {
1125     SAXContentHandler_QueryInterface,
1126     SAXContentHandler_AddRef,
1127     SAXContentHandler_Release,
1128     SAXContentHandler_putDocumentLocator,
1129     SAXContentHandler_startDocument,
1130     SAXContentHandler_endDocument,
1131     SAXContentHandler_startPrefixMapping,
1132     SAXContentHandler_endPrefixMapping,
1133     SAXContentHandler_startElement,
1134     SAXContentHandler_endElement,
1135     SAXContentHandler_characters,
1136     SAXContentHandler_ignorableWhitespace,
1137     SAXContentHandler_processingInstruction,
1138     SAXContentHandler_skippedEntity
1139 };
1140
1141 /*** ISAXLexicalHandler ***/
1142 static HRESULT WINAPI SAXLexicalHandler_QueryInterface(ISAXLexicalHandler *iface,
1143     REFIID riid, void **obj)
1144 {
1145     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1146     return IMXWriter_QueryInterface(&This->IMXWriter_iface, riid, obj);
1147 }
1148
1149 static ULONG WINAPI SAXLexicalHandler_AddRef(ISAXLexicalHandler *iface)
1150 {
1151     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1152     return IMXWriter_AddRef(&This->IMXWriter_iface);
1153 }
1154
1155 static ULONG WINAPI SAXLexicalHandler_Release(ISAXLexicalHandler *iface)
1156 {
1157     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1158     return IMXWriter_Release(&This->IMXWriter_iface);
1159 }
1160
1161 static HRESULT WINAPI SAXLexicalHandler_startDTD(ISAXLexicalHandler *iface,
1162     const WCHAR *name, int name_len, const WCHAR *publicId, int publicId_len,
1163     const WCHAR *systemId, int systemId_len)
1164 {
1165     static const WCHAR doctypeW[] = {'<','!','D','O','C','T','Y','P','E',' '};
1166     static const WCHAR openintW[] = {'[','\r','\n'};
1167
1168     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1169
1170     TRACE("(%p)->(%s %s %s)\n", This, debugstr_wn(name, name_len), debugstr_wn(publicId, publicId_len),
1171         debugstr_wn(systemId, systemId_len));
1172
1173     if (!name) return E_INVALIDARG;
1174
1175     write_output_buffer(This->buffer, doctypeW, sizeof(doctypeW)/sizeof(WCHAR));
1176
1177     if (*name)
1178     {
1179         write_output_buffer(This->buffer, name, name_len);
1180         write_output_buffer(This->buffer, spaceW, 1);
1181     }
1182
1183     if (publicId)
1184     {
1185         static const WCHAR publicW[] = {'P','U','B','L','I','C',' '};
1186
1187         write_output_buffer(This->buffer, publicW, sizeof(publicW)/sizeof(WCHAR));
1188         write_output_buffer_quoted(This->buffer, publicId, publicId_len);
1189
1190         if (!systemId) return E_INVALIDARG;
1191
1192         if (*publicId)
1193             write_output_buffer(This->buffer, spaceW, 1);
1194
1195         write_output_buffer_quoted(This->buffer, systemId, systemId_len);
1196
1197         if (*systemId)
1198             write_output_buffer(This->buffer, spaceW, 1);
1199     }
1200     else if (systemId)
1201     {
1202         static const WCHAR systemW[] = {'S','Y','S','T','E','M',' '};
1203
1204         write_output_buffer(This->buffer, systemW, sizeof(systemW)/sizeof(WCHAR));
1205         write_output_buffer_quoted(This->buffer, systemId, systemId_len);
1206         if (*systemId)
1207             write_output_buffer(This->buffer, spaceW, 1);
1208     }
1209
1210     write_output_buffer(This->buffer, openintW, sizeof(openintW)/sizeof(WCHAR));
1211
1212     return S_OK;
1213 }
1214
1215 static HRESULT WINAPI SAXLexicalHandler_endDTD(ISAXLexicalHandler *iface)
1216 {
1217     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1218     static const WCHAR closedtdW[] = {']','>','\r','\n'};
1219
1220     TRACE("(%p)\n", This);
1221
1222     write_output_buffer(This->buffer, closedtdW, sizeof(closedtdW)/sizeof(WCHAR));
1223
1224     return S_OK;
1225 }
1226
1227 static HRESULT WINAPI SAXLexicalHandler_startEntity(ISAXLexicalHandler *iface, const WCHAR *name, int len)
1228 {
1229     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1230     FIXME("(%p)->(%s): stub\n", This, debugstr_wn(name, len));
1231     return E_NOTIMPL;
1232 }
1233
1234 static HRESULT WINAPI SAXLexicalHandler_endEntity(ISAXLexicalHandler *iface, const WCHAR *name, int len)
1235 {
1236     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1237     FIXME("(%p)->(%s): stub\n", This, debugstr_wn(name, len));
1238     return E_NOTIMPL;
1239 }
1240
1241 static HRESULT WINAPI SAXLexicalHandler_startCDATA(ISAXLexicalHandler *iface)
1242 {
1243     static const WCHAR scdataW[] = {'<','!','[','C','D','A','T','A','['};
1244     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1245
1246     TRACE("(%p)\n", This);
1247
1248     write_output_buffer(This->buffer, scdataW, sizeof(scdataW)/sizeof(WCHAR));
1249     This->cdata = TRUE;
1250
1251     return S_OK;
1252 }
1253
1254 static HRESULT WINAPI SAXLexicalHandler_endCDATA(ISAXLexicalHandler *iface)
1255 {
1256     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1257     static const WCHAR ecdataW[] = {']',']','>'};
1258
1259     TRACE("(%p)\n", This);
1260
1261     write_output_buffer(This->buffer, ecdataW, sizeof(ecdataW)/sizeof(WCHAR));
1262     This->cdata = FALSE;
1263
1264     return S_OK;
1265 }
1266
1267 static HRESULT WINAPI SAXLexicalHandler_comment(ISAXLexicalHandler *iface, const WCHAR *chars, int nchars)
1268 {
1269     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1270     static const WCHAR copenW[] = {'<','!','-','-'};
1271     static const WCHAR ccloseW[] = {'-','-','>','\r','\n'};
1272
1273     TRACE("(%p)->(%s:%d)\n", This, debugstr_wn(chars, nchars), nchars);
1274
1275     if (!chars) return E_INVALIDARG;
1276
1277     close_element_starttag(This);
1278
1279     write_output_buffer(This->buffer, copenW, sizeof(copenW)/sizeof(WCHAR));
1280     if (nchars)
1281         write_output_buffer(This->buffer, chars, nchars);
1282     write_output_buffer(This->buffer, ccloseW, sizeof(ccloseW)/sizeof(WCHAR));
1283
1284     return S_OK;
1285 }
1286
1287 static const struct ISAXLexicalHandlerVtbl SAXLexicalHandlerVtbl =
1288 {
1289     SAXLexicalHandler_QueryInterface,
1290     SAXLexicalHandler_AddRef,
1291     SAXLexicalHandler_Release,
1292     SAXLexicalHandler_startDTD,
1293     SAXLexicalHandler_endDTD,
1294     SAXLexicalHandler_startEntity,
1295     SAXLexicalHandler_endEntity,
1296     SAXLexicalHandler_startCDATA,
1297     SAXLexicalHandler_endCDATA,
1298     SAXLexicalHandler_comment
1299 };
1300
1301 /*** ISAXDeclHandler ***/
1302 static HRESULT WINAPI SAXDeclHandler_QueryInterface(ISAXDeclHandler *iface,
1303     REFIID riid, void **obj)
1304 {
1305     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1306     return IMXWriter_QueryInterface(&This->IMXWriter_iface, riid, obj);
1307 }
1308
1309 static ULONG WINAPI SAXDeclHandler_AddRef(ISAXDeclHandler *iface)
1310 {
1311     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1312     return IMXWriter_AddRef(&This->IMXWriter_iface);
1313 }
1314
1315 static ULONG WINAPI SAXDeclHandler_Release(ISAXDeclHandler *iface)
1316 {
1317     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1318     return IMXWriter_Release(&This->IMXWriter_iface);
1319 }
1320
1321 static HRESULT WINAPI SAXDeclHandler_elementDecl(ISAXDeclHandler *iface,
1322     const WCHAR *name, int n_name, const WCHAR *model, int n_model)
1323 {
1324     static const WCHAR elementW[] = {'<','!','E','L','E','M','E','N','T',' '};
1325     static const WCHAR closeelementW[] = {'>','\r','\n'};
1326     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1327
1328     TRACE("(%p)->(%s:%d %s:%d)\n", This, debugstr_wn(name, n_name), n_name,
1329         debugstr_wn(model, n_model), n_model);
1330
1331     if (!name || !model) return E_INVALIDARG;
1332
1333     write_output_buffer(This->buffer, elementW, sizeof(elementW)/sizeof(WCHAR));
1334     if (n_name) {
1335         write_output_buffer(This->buffer, name, n_name);
1336         write_output_buffer(This->buffer, spaceW, sizeof(spaceW)/sizeof(WCHAR));
1337     }
1338     if (n_model)
1339         write_output_buffer(This->buffer, model, n_model);
1340     write_output_buffer(This->buffer, closeelementW, sizeof(closeelementW)/sizeof(WCHAR));
1341
1342     return S_OK;
1343 }
1344
1345 static HRESULT WINAPI SAXDeclHandler_attributeDecl(ISAXDeclHandler *iface,
1346     const WCHAR *element, int n_element, const WCHAR *attr, int n_attr,
1347     const WCHAR *type, int n_type, const WCHAR *Default, int n_default,
1348     const WCHAR *value, int n_value)
1349 {
1350     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1351     FIXME("(%p)->(%s:%d %s:%d %s:%d %s:%d %s:%d): stub\n", This, debugstr_wn(element, n_element), n_element,
1352         debugstr_wn(attr, n_attr), n_attr, debugstr_wn(type, n_type), n_type, debugstr_wn(Default, n_default), n_default,
1353         debugstr_wn(value, n_value), n_value);
1354     return E_NOTIMPL;
1355 }
1356
1357 static HRESULT WINAPI SAXDeclHandler_internalEntityDecl(ISAXDeclHandler *iface,
1358     const WCHAR *name, int n_name, const WCHAR *value, int n_value)
1359 {
1360     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1361     FIXME("(%p)->(%s:%d %s:%d): stub\n", This, debugstr_wn(name, n_name), n_name,
1362         debugstr_wn(value, n_value), n_value);
1363     return E_NOTIMPL;
1364 }
1365
1366 static HRESULT WINAPI SAXDeclHandler_externalEntityDecl(ISAXDeclHandler *iface,
1367     const WCHAR *name, int n_name, const WCHAR *publicId, int n_publicId,
1368     const WCHAR *systemId, int n_systemId)
1369 {
1370     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1371     FIXME("(%p)->(%s:%d %s:%d %s:%d): stub\n", This, debugstr_wn(name, n_name), n_name,
1372         debugstr_wn(publicId, n_publicId), n_publicId, debugstr_wn(systemId, n_systemId), n_systemId);
1373     return E_NOTIMPL;
1374 }
1375
1376 static const ISAXDeclHandlerVtbl SAXDeclHandlerVtbl = {
1377     SAXDeclHandler_QueryInterface,
1378     SAXDeclHandler_AddRef,
1379     SAXDeclHandler_Release,
1380     SAXDeclHandler_elementDecl,
1381     SAXDeclHandler_attributeDecl,
1382     SAXDeclHandler_internalEntityDecl,
1383     SAXDeclHandler_externalEntityDecl
1384 };
1385
1386 static const tid_t mxwriter_iface_tids[] = {
1387     IMXWriter_tid,
1388     0
1389 };
1390
1391 static dispex_static_data_t mxwriter_dispex = {
1392     NULL,
1393     IMXWriter_tid,
1394     NULL,
1395     mxwriter_iface_tids
1396 };
1397
1398 HRESULT MXWriter_create(MSXML_VERSION version, IUnknown *outer, void **ppObj)
1399 {
1400     static const WCHAR version10W[] = {'1','.','0',0};
1401     mxwriter *This;
1402     HRESULT hr;
1403
1404     TRACE("(%p, %p)\n", outer, ppObj);
1405
1406     if (outer) FIXME("support aggregation, outer\n");
1407
1408     This = heap_alloc( sizeof (*This) );
1409     if(!This)
1410         return E_OUTOFMEMORY;
1411
1412     This->IMXWriter_iface.lpVtbl = &MXWriterVtbl;
1413     This->ISAXContentHandler_iface.lpVtbl = &SAXContentHandlerVtbl;
1414     This->ISAXLexicalHandler_iface.lpVtbl = &SAXLexicalHandlerVtbl;
1415     This->ISAXDeclHandler_iface.lpVtbl = &SAXDeclHandlerVtbl;
1416     This->ref = 1;
1417     This->class_version = version;
1418
1419     This->props[MXWriter_BOM] = VARIANT_TRUE;
1420     This->props[MXWriter_DisableEscaping] = VARIANT_FALSE;
1421     This->props[MXWriter_Indent] = VARIANT_FALSE;
1422     This->props[MXWriter_OmitXmlDecl] = VARIANT_FALSE;
1423     This->props[MXWriter_Standalone] = VARIANT_FALSE;
1424     This->prop_changed = FALSE;
1425     This->encoding = SysAllocString(utf16W);
1426     This->version  = SysAllocString(version10W);
1427     This->xml_enc  = XmlEncoding_UTF16;
1428
1429     This->element = NULL;
1430     This->cdata = FALSE;
1431
1432     This->dest = NULL;
1433     This->dest_written = 0;
1434
1435     hr = alloc_output_buffer(This->xml_enc, &This->buffer);
1436     if (hr != S_OK) {
1437         SysFreeString(This->encoding);
1438         SysFreeString(This->version);
1439         heap_free(This);
1440         return hr;
1441     }
1442
1443     init_dispex(&This->dispex, (IUnknown*)&This->IMXWriter_iface, &mxwriter_dispex);
1444
1445     *ppObj = &This->IMXWriter_iface;
1446
1447     TRACE("returning iface %p\n", *ppObj);
1448
1449     return S_OK;
1450 }
1451
1452 static HRESULT WINAPI MXAttributes_QueryInterface(IMXAttributes *iface, REFIID riid, void **ppObj)
1453 {
1454     mxattributes *This = impl_from_IMXAttributes( iface );
1455
1456     TRACE("(%p)->(%s %p)\n", This, debugstr_guid( riid ), ppObj);
1457
1458     *ppObj = NULL;
1459
1460     if ( IsEqualGUID( riid, &IID_IUnknown )  ||
1461          IsEqualGUID( riid, &IID_IDispatch ) ||
1462          IsEqualGUID( riid, &IID_IMXAttributes ))
1463     {
1464         *ppObj = iface;
1465     }
1466     else if (dispex_query_interface(&This->dispex, riid, ppObj))
1467     {
1468         return *ppObj ? S_OK : E_NOINTERFACE;
1469     }
1470     else
1471     {
1472         FIXME("interface %s not implemented\n", debugstr_guid(riid));
1473         return E_NOINTERFACE;
1474     }
1475
1476     IMXAttributes_AddRef( iface );
1477
1478     return S_OK;
1479 }
1480
1481 static ULONG WINAPI MXAttributes_AddRef(IMXAttributes *iface)
1482 {
1483     mxattributes *This = impl_from_IMXAttributes( iface );
1484     ULONG ref = InterlockedIncrement( &This->ref );
1485     TRACE("(%p)->(%d)\n", This, ref );
1486     return ref;
1487 }
1488
1489 static ULONG WINAPI MXAttributes_Release(IMXAttributes *iface)
1490 {
1491     mxattributes *This = impl_from_IMXAttributes( iface );
1492     LONG ref = InterlockedDecrement( &This->ref );
1493
1494     TRACE("(%p)->(%d)\n", This, ref);
1495
1496     if (ref == 0)
1497     {
1498         release_dispex(&This->dispex);
1499         heap_free(This);
1500     }
1501
1502     return ref;
1503 }
1504
1505 static HRESULT WINAPI MXAttributes_GetTypeInfoCount(IMXAttributes *iface, UINT* pctinfo)
1506 {
1507     mxattributes *This = impl_from_IMXAttributes( iface );
1508     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
1509 }
1510
1511 static HRESULT WINAPI MXAttributes_GetTypeInfo(IMXAttributes *iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1512 {
1513     mxattributes *This = impl_from_IMXAttributes( iface );
1514     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1515 }
1516
1517 static HRESULT WINAPI MXAttributes_GetIDsOfNames(
1518     IMXAttributes *iface,
1519     REFIID riid,
1520     LPOLESTR* rgszNames,
1521     UINT cNames,
1522     LCID lcid,
1523     DISPID* rgDispId)
1524 {
1525     mxattributes *This = impl_from_IMXAttributes( iface );
1526     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface,
1527         riid, rgszNames, cNames, lcid, rgDispId);
1528 }
1529
1530 static HRESULT WINAPI MXAttributes_Invoke(
1531     IMXAttributes *iface,
1532     DISPID dispIdMember,
1533     REFIID riid,
1534     LCID lcid,
1535     WORD wFlags,
1536     DISPPARAMS* pDispParams,
1537     VARIANT* pVarResult,
1538     EXCEPINFO* pExcepInfo,
1539     UINT* puArgErr)
1540 {
1541     mxattributes *This = impl_from_IMXAttributes( iface );
1542     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface,
1543         dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1544 }
1545
1546 static HRESULT WINAPI MXAttributes_addAttribute(IMXAttributes *iface,
1547     BSTR uri, BSTR localName, BSTR QName, BSTR type, BSTR value)
1548 {
1549     mxattributes *This = impl_from_IMXAttributes( iface );
1550     FIXME("(%p)->(%s %s %s %s %s): stub\n", This, debugstr_w(uri), debugstr_w(localName),
1551         debugstr_w(QName), debugstr_w(type), debugstr_w(value));
1552     return E_NOTIMPL;
1553 }
1554
1555 static HRESULT WINAPI MXAttributes_addAttributeFromIndex(IMXAttributes *iface,
1556     VARIANT atts, int index)
1557 {
1558     mxattributes *This = impl_from_IMXAttributes( iface );
1559     FIXME("(%p)->(%s %d): stub\n", This, debugstr_variant(&atts), index);
1560     return E_NOTIMPL;
1561 }
1562
1563 static HRESULT WINAPI MXAttributes_clear(IMXAttributes *iface)
1564 {
1565     mxattributes *This = impl_from_IMXAttributes( iface );
1566     FIXME("(%p): stub\n", This);
1567     return E_NOTIMPL;
1568 }
1569
1570 static HRESULT WINAPI MXAttributes_removeAttribute(IMXAttributes *iface, int index)
1571 {
1572     mxattributes *This = impl_from_IMXAttributes( iface );
1573     FIXME("(%p)->(%d): stub\n", This, index);
1574     return E_NOTIMPL;
1575 }
1576
1577 static HRESULT WINAPI MXAttributes_setAttribute(IMXAttributes *iface, int index,
1578     BSTR uri, BSTR localName, BSTR QName, BSTR type, BSTR value)
1579 {
1580     mxattributes *This = impl_from_IMXAttributes( iface );
1581     FIXME("(%p)->(%d %s %s %s %s %s): stub\n", This, index, debugstr_w(uri),
1582         debugstr_w(localName), debugstr_w(QName), debugstr_w(type), debugstr_w(value));
1583     return E_NOTIMPL;
1584 }
1585
1586 static HRESULT WINAPI MXAttributes_setAttributes(IMXAttributes *iface, VARIANT atts)
1587 {
1588     mxattributes *This = impl_from_IMXAttributes( iface );
1589     FIXME("(%p)->(%s): stub\n", This, debugstr_variant(&atts));
1590     return E_NOTIMPL;
1591 }
1592
1593 static HRESULT WINAPI MXAttributes_setLocalName(IMXAttributes *iface, int index,
1594     BSTR localName)
1595 {
1596     mxattributes *This = impl_from_IMXAttributes( iface );
1597     FIXME("(%p)->(%d %s): stub\n", This, index, debugstr_w(localName));
1598     return E_NOTIMPL;
1599 }
1600
1601 static HRESULT WINAPI MXAttributes_setQName(IMXAttributes *iface, int index, BSTR QName)
1602 {
1603     mxattributes *This = impl_from_IMXAttributes( iface );
1604     FIXME("(%p)->(%d %s): stub\n", This, index, debugstr_w(QName));
1605     return E_NOTIMPL;
1606 }
1607
1608 static HRESULT WINAPI MXAttributes_setURI(IMXAttributes *iface, int index, BSTR uri)
1609 {
1610     mxattributes *This = impl_from_IMXAttributes( iface );
1611     FIXME("(%p)->(%d %s): stub\n", This, index, debugstr_w(uri));
1612     return E_NOTIMPL;
1613 }
1614
1615 static HRESULT WINAPI MXAttributes_setValue(IMXAttributes *iface, int index, BSTR value)
1616 {
1617     mxattributes *This = impl_from_IMXAttributes( iface );
1618     FIXME("(%p)->(%d %s): stub\n", This, index, debugstr_w(value));
1619     return E_NOTIMPL;
1620 }
1621
1622 static const IMXAttributesVtbl MXAttributesVtbl = {
1623     MXAttributes_QueryInterface,
1624     MXAttributes_AddRef,
1625     MXAttributes_Release,
1626     MXAttributes_GetTypeInfoCount,
1627     MXAttributes_GetTypeInfo,
1628     MXAttributes_GetIDsOfNames,
1629     MXAttributes_Invoke,
1630     MXAttributes_addAttribute,
1631     MXAttributes_addAttributeFromIndex,
1632     MXAttributes_clear,
1633     MXAttributes_removeAttribute,
1634     MXAttributes_setAttribute,
1635     MXAttributes_setAttributes,
1636     MXAttributes_setLocalName,
1637     MXAttributes_setQName,
1638     MXAttributes_setURI,
1639     MXAttributes_setValue
1640 };
1641
1642 static const tid_t mxattrs_iface_tids[] = {
1643     IMXAttributes_tid,
1644     0
1645 };
1646
1647 static dispex_static_data_t mxattrs_dispex = {
1648     NULL,
1649     IMXAttributes_tid,
1650     NULL,
1651     mxattrs_iface_tids
1652 };
1653
1654 HRESULT SAXAttributes_create(MSXML_VERSION version, IUnknown *outer, void **ppObj)
1655 {
1656     mxattributes *This;
1657
1658     TRACE("(%p, %p)\n", outer, ppObj);
1659
1660     This = heap_alloc( sizeof (*This) );
1661     if( !This )
1662         return E_OUTOFMEMORY;
1663
1664     This->IMXAttributes_iface.lpVtbl = &MXAttributesVtbl;
1665     This->ref = 1;
1666
1667     *ppObj = &This->IMXAttributes_iface;
1668
1669     init_dispex(&This->dispex, (IUnknown*)&This->IMXAttributes_iface, &mxattrs_dispex);
1670
1671     TRACE("returning iface %p\n", *ppObj);
1672
1673     return S_OK;
1674 }