gdi32: Move internal path functions to the top of the file to avoid forward declarations.
[wine] / dlls / msxml3 / mxwriter.c
1 /*
2  *    MXWriter implementation
3  *
4  * Copyright 2011 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 #ifdef HAVE_LIBXML2
43
44 static const WCHAR utf16W[] = {'U','T','F','-','1','6',0};
45 static const WCHAR utf8W[]  = {'U','T','F','-','8',0};
46
47 static const char crlfA[] = "\r\n";
48 static const WCHAR emptyW[] = {0};
49
50 typedef enum
51 {
52     MXWriter_BOM = 0,
53     MXWriter_DisableEscaping,
54     MXWriter_Indent,
55     MXWriter_OmitXmlDecl,
56     MXWriter_Standalone,
57     MXWriter_LastProp
58 } MXWRITER_PROPS;
59
60 typedef struct _mxwriter
61 {
62     IMXWriter IMXWriter_iface;
63     ISAXContentHandler ISAXContentHandler_iface;
64
65     LONG ref;
66     MSXML_VERSION class_version;
67
68     VARIANT_BOOL props[MXWriter_LastProp];
69     BOOL prop_changed;
70     xmlCharEncoding encoding;
71     BSTR version;
72
73     /* contains a pending (or not closed yet) element name or NULL if
74        we don't have to close */
75     BSTR element;
76
77     IStream *dest;
78     ULONG   dest_written;
79
80     xmlOutputBufferPtr buffer;
81 } mxwriter;
82
83 static HRESULT bstr_from_xmlCharEncoding(xmlCharEncoding enc, BSTR *encoding)
84 {
85     const char *encodingA;
86
87     if (enc != XML_CHAR_ENCODING_UTF16LE && enc != XML_CHAR_ENCODING_UTF8) {
88         FIXME("Unsupported xmlCharEncoding: %d\n", enc);
89         *encoding = NULL;
90         return E_NOTIMPL;
91     }
92
93     encodingA = xmlGetCharEncodingName(enc);
94     if (encodingA) {
95         DWORD len = MultiByteToWideChar(CP_ACP, 0, encodingA, -1, NULL, 0);
96         *encoding = SysAllocStringLen(NULL, len-1);
97         if(*encoding)
98             MultiByteToWideChar( CP_ACP, 0, encodingA, -1, *encoding, len);
99     } else
100         *encoding = SysAllocStringLen(NULL, 0);
101
102     return *encoding ? S_OK : E_OUTOFMEMORY;
103 }
104
105 /* Attempts to the write data from the mxwriter's buffer to
106  * the destination stream (if there is one).
107  */
108 static HRESULT write_data_to_stream(mxwriter *This)
109 {
110     HRESULT hres;
111     ULONG written = 0;
112     xmlBufferPtr buffer = NULL;
113
114     if (!This->dest)
115         return S_OK;
116
117     /* The xmlOutputBuffer doesn't copy its contents from its 'buffer' to the
118      * 'conv' buffer when UTF8 encoding is used.
119      */
120     if (This->encoding == XML_CHAR_ENCODING_UTF8)
121         buffer = This->buffer->buffer;
122     else
123         buffer = This->buffer->conv;
124
125     if (This->dest_written > buffer->use) {
126         ERR("Failed sanity check! Not sure what to do... (%d > %d)\n", This->dest_written, buffer->use);
127         return E_FAIL;
128     } else if (This->dest_written == buffer->use && This->encoding != XML_CHAR_ENCODING_UTF8)
129         /* Windows seems to make an empty write call when the encoding is UTF-8 and
130          * all the data has been written to the stream. It doesn't seem make this call
131          * for any other encodings.
132          */
133         return S_OK;
134
135     /* Write the current content from the output buffer into 'dest'.
136      * TODO: Check what Windows does if the IStream doesn't write all of
137      *       the data we give it at once.
138      */
139     hres = IStream_Write(This->dest, buffer->content+This->dest_written,
140                          buffer->use-This->dest_written, &written);
141     if (FAILED(hres)) {
142         WARN("Failed to write data to IStream (%08x)\n", hres);
143         return hres;
144     }
145
146     This->dest_written += written;
147     return hres;
148 }
149
150 /* Newly added element start tag left unclosed cause for empty elements
151    we have to close it differently. */
152 static void close_element_starttag(const mxwriter *This)
153 {
154     if (!This->element) return;
155     xmlOutputBufferWriteString(This->buffer, ">");
156 }
157
158 static void set_element_name(mxwriter *This, const WCHAR *name, int len)
159 {
160     SysFreeString(This->element);
161     This->element = name ? SysAllocStringLen(name, len) : NULL;
162 }
163
164 static inline HRESULT flush_output_buffer(mxwriter *This)
165 {
166     close_element_starttag(This);
167     set_element_name(This, NULL, 0);
168     xmlOutputBufferFlush(This->buffer);
169     return write_data_to_stream(This);
170 }
171
172 /* Resets the mxwriter's output buffer by closing it, then creating a new
173  * output buffer using the given encoding.
174  */
175 static inline void reset_output_buffer(mxwriter *This)
176 {
177     xmlOutputBufferClose(This->buffer);
178     This->buffer = xmlAllocOutputBuffer(xmlGetCharEncodingHandler(This->encoding));
179     This->dest_written = 0;
180 }
181
182 static HRESULT writer_set_property(mxwriter *writer, MXWRITER_PROPS property, VARIANT_BOOL value)
183 {
184     writer->props[property] = value;
185     writer->prop_changed = TRUE;
186     return S_OK;
187 }
188
189 static HRESULT writer_get_property(const mxwriter *writer, MXWRITER_PROPS property, VARIANT_BOOL *value)
190 {
191     if (!value) return E_POINTER;
192     *value = writer->props[property];
193     return S_OK;
194 }
195
196 static inline mxwriter *impl_from_IMXWriter(IMXWriter *iface)
197 {
198     return CONTAINING_RECORD(iface, mxwriter, IMXWriter_iface);
199 }
200
201 static inline mxwriter *impl_from_ISAXContentHandler(ISAXContentHandler *iface)
202 {
203     return CONTAINING_RECORD(iface, mxwriter, ISAXContentHandler_iface);
204 }
205
206 static HRESULT WINAPI mxwriter_QueryInterface(IMXWriter *iface, REFIID riid, void **obj)
207 {
208     mxwriter *This = impl_from_IMXWriter( iface );
209
210     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
211
212     if ( IsEqualGUID( riid, &IID_IMXWriter ) ||
213          IsEqualGUID( riid, &IID_IDispatch ) ||
214          IsEqualGUID( riid, &IID_IUnknown ) )
215     {
216         *obj = &This->IMXWriter_iface;
217     }
218     else if ( IsEqualGUID( riid, &IID_ISAXContentHandler ) )
219     {
220         *obj = &This->ISAXContentHandler_iface;
221     }
222     else
223     {
224         ERR("interface %s not implemented\n", debugstr_guid(riid));
225         *obj = NULL;
226         return E_NOINTERFACE;
227     }
228
229     IMXWriter_AddRef(iface);
230     return S_OK;
231 }
232
233 static ULONG WINAPI mxwriter_AddRef(IMXWriter *iface)
234 {
235     mxwriter *This = impl_from_IMXWriter( iface );
236     LONG ref = InterlockedIncrement(&This->ref);
237
238     TRACE("(%p)->(%d)\n", This, ref);
239
240     return ref;
241 }
242
243 static ULONG WINAPI mxwriter_Release(IMXWriter *iface)
244 {
245     mxwriter *This = impl_from_IMXWriter( iface );
246     ULONG ref = InterlockedDecrement(&This->ref);
247
248     TRACE("(%p)->(%d)\n", This, ref);
249
250     if(!ref)
251     {
252         /* Windows flushes the buffer when the interface is destroyed. */
253         flush_output_buffer(This);
254
255         if (This->dest) IStream_Release(This->dest);
256         SysFreeString(This->version);
257
258         xmlOutputBufferClose(This->buffer);
259         SysFreeString(This->element);
260         heap_free(This);
261     }
262
263     return ref;
264 }
265
266 static HRESULT WINAPI mxwriter_GetTypeInfoCount(IMXWriter *iface, UINT* pctinfo)
267 {
268     mxwriter *This = impl_from_IMXWriter( iface );
269
270     TRACE("(%p)->(%p)\n", This, pctinfo);
271
272     *pctinfo = 1;
273
274     return S_OK;
275 }
276
277 static HRESULT WINAPI mxwriter_GetTypeInfo(
278     IMXWriter *iface,
279     UINT iTInfo, LCID lcid,
280     ITypeInfo** ppTInfo )
281 {
282     mxwriter *This = impl_from_IMXWriter( iface );
283
284     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
285
286     return get_typeinfo(IMXWriter_tid, ppTInfo);
287 }
288
289 static HRESULT WINAPI mxwriter_GetIDsOfNames(
290     IMXWriter *iface,
291     REFIID riid, LPOLESTR* rgszNames,
292     UINT cNames, LCID lcid, DISPID* rgDispId )
293 {
294     mxwriter *This = impl_from_IMXWriter( iface );
295     ITypeInfo *typeinfo;
296     HRESULT hr;
297
298     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
299           lcid, rgDispId);
300
301     if(!rgszNames || cNames == 0 || !rgDispId)
302         return E_INVALIDARG;
303
304     hr = get_typeinfo(IMXWriter_tid, &typeinfo);
305     if(SUCCEEDED(hr))
306     {
307         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
308         ITypeInfo_Release(typeinfo);
309     }
310
311     return hr;
312 }
313
314 static HRESULT WINAPI mxwriter_Invoke(
315     IMXWriter *iface,
316     DISPID dispIdMember, REFIID riid, LCID lcid,
317     WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
318     EXCEPINFO* pExcepInfo, UINT* puArgErr )
319 {
320     mxwriter *This = impl_from_IMXWriter( iface );
321     ITypeInfo *typeinfo;
322     HRESULT hr;
323
324     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
325           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
326
327     hr = get_typeinfo(IMXWriter_tid, &typeinfo);
328     if(SUCCEEDED(hr))
329     {
330         hr = ITypeInfo_Invoke(typeinfo, &This->IMXWriter_iface, dispIdMember, wFlags,
331                 pDispParams, pVarResult, pExcepInfo, puArgErr);
332         ITypeInfo_Release(typeinfo);
333     }
334
335     return hr;
336 }
337
338 static HRESULT WINAPI mxwriter_put_output(IMXWriter *iface, VARIANT dest)
339 {
340     mxwriter *This = impl_from_IMXWriter( iface );
341     HRESULT hr;
342
343     TRACE("(%p)->(%s)\n", This, debugstr_variant(&dest));
344
345     hr = flush_output_buffer(This);
346     if (FAILED(hr))
347         return hr;
348
349     switch (V_VT(&dest))
350     {
351     case VT_EMPTY:
352     {
353         if (This->dest) IStream_Release(This->dest);
354         This->dest = NULL;
355
356         /* We need to reset the output buffer to UTF-16, since the only way
357          * the content of the mxwriter can be accessed now is through a BSTR.
358          */
359         This->encoding = xmlParseCharEncoding("UTF-16");
360         reset_output_buffer(This);
361         break;
362     }
363     case VT_UNKNOWN:
364     {
365         IStream *stream;
366
367         hr = IUnknown_QueryInterface(V_UNKNOWN(&dest), &IID_IStream, (void**)&stream);
368         if (hr == S_OK)
369         {
370             /* Recreate the output buffer to make sure it's using the correct encoding. */
371             reset_output_buffer(This);
372
373             if (This->dest) IStream_Release(This->dest);
374             This->dest = stream;
375             break;
376         }
377
378         FIXME("unhandled interface type for VT_UNKNOWN destination\n");
379         return E_NOTIMPL;
380     }
381     default:
382         FIXME("unhandled destination type %s\n", debugstr_variant(&dest));
383         return E_NOTIMPL;
384     }
385
386     return S_OK;
387 }
388
389 static HRESULT WINAPI mxwriter_get_output(IMXWriter *iface, VARIANT *dest)
390 {
391     mxwriter *This = impl_from_IMXWriter( iface );
392
393     TRACE("(%p)->(%p)\n", This, dest);
394
395     if (!This->dest)
396     {
397         HRESULT hr = flush_output_buffer(This);
398         if (FAILED(hr))
399             return hr;
400
401         /* TODO: Windows always seems to re-encode the XML to UTF-16 (this includes
402          * updating the XML decl so it says "UTF-16" instead of "UTF-8"). We don't
403          * support this yet...
404          */
405         if (This->encoding == XML_CHAR_ENCODING_UTF8) {
406             FIXME("XML re-encoding not supported yet\n");
407             return E_NOTIMPL;
408         }
409
410         V_VT(dest)   = VT_BSTR;
411         V_BSTR(dest) = SysAllocStringLen((const WCHAR*)This->buffer->conv->content,
412                                          This->buffer->conv->use/sizeof(WCHAR));
413
414         return S_OK;
415     }
416     else
417         FIXME("not implemented when stream is set up\n");
418
419     return E_NOTIMPL;
420 }
421
422 static HRESULT WINAPI mxwriter_put_encoding(IMXWriter *iface, BSTR encoding)
423 {
424     mxwriter *This = impl_from_IMXWriter( iface );
425
426     TRACE("(%p)->(%s)\n", This, debugstr_w(encoding));
427
428     /* FIXME: filter all supported encodings */
429     if (!strcmpW(encoding, utf16W) || !strcmpW(encoding, utf8W))
430     {
431         HRESULT hr;
432         LPSTR enc;
433
434         hr = flush_output_buffer(This);
435         if (FAILED(hr))
436             return hr;
437
438         enc = heap_strdupWtoA(encoding);
439         if (!enc)
440             return E_OUTOFMEMORY;
441
442         This->encoding = xmlParseCharEncoding(enc);
443         heap_free(enc);
444
445         reset_output_buffer(This);
446         return S_OK;
447     }
448     else
449     {
450         FIXME("unsupported encoding %s\n", debugstr_w(encoding));
451         return E_INVALIDARG;
452     }
453 }
454
455 static HRESULT WINAPI mxwriter_get_encoding(IMXWriter *iface, BSTR *encoding)
456 {
457     mxwriter *This = impl_from_IMXWriter( iface );
458
459     TRACE("(%p)->(%p)\n", This, encoding);
460
461     if (!encoding) return E_POINTER;
462
463     return bstr_from_xmlCharEncoding(This->encoding, encoding);
464 }
465
466 static HRESULT WINAPI mxwriter_put_byteOrderMark(IMXWriter *iface, VARIANT_BOOL value)
467 {
468     mxwriter *This = impl_from_IMXWriter( iface );
469
470     TRACE("(%p)->(%d)\n", This, value);
471     return writer_set_property(This, MXWriter_BOM, value);
472 }
473
474 static HRESULT WINAPI mxwriter_get_byteOrderMark(IMXWriter *iface, VARIANT_BOOL *value)
475 {
476     mxwriter *This = impl_from_IMXWriter( iface );
477
478     TRACE("(%p)->(%p)\n", This, value);
479     return writer_get_property(This, MXWriter_BOM, value);
480 }
481
482 static HRESULT WINAPI mxwriter_put_indent(IMXWriter *iface, VARIANT_BOOL value)
483 {
484     mxwriter *This = impl_from_IMXWriter( iface );
485
486     TRACE("(%p)->(%d)\n", This, value);
487     return writer_set_property(This, MXWriter_Indent, value);
488 }
489
490 static HRESULT WINAPI mxwriter_get_indent(IMXWriter *iface, VARIANT_BOOL *value)
491 {
492     mxwriter *This = impl_from_IMXWriter( iface );
493
494     TRACE("(%p)->(%p)\n", This, value);
495     return writer_get_property(This, MXWriter_Indent, value);
496 }
497
498 static HRESULT WINAPI mxwriter_put_standalone(IMXWriter *iface, VARIANT_BOOL value)
499 {
500     mxwriter *This = impl_from_IMXWriter( iface );
501
502     TRACE("(%p)->(%d)\n", This, value);
503     return writer_set_property(This, MXWriter_Standalone, value);
504 }
505
506 static HRESULT WINAPI mxwriter_get_standalone(IMXWriter *iface, VARIANT_BOOL *value)
507 {
508     mxwriter *This = impl_from_IMXWriter( iface );
509
510     TRACE("(%p)->(%p)\n", This, value);
511     return writer_get_property(This, MXWriter_Standalone, value);
512 }
513
514 static HRESULT WINAPI mxwriter_put_omitXMLDeclaration(IMXWriter *iface, VARIANT_BOOL value)
515 {
516     mxwriter *This = impl_from_IMXWriter( iface );
517
518     TRACE("(%p)->(%d)\n", This, value);
519     return writer_set_property(This, MXWriter_OmitXmlDecl, value);
520 }
521
522 static HRESULT WINAPI mxwriter_get_omitXMLDeclaration(IMXWriter *iface, VARIANT_BOOL *value)
523 {
524     mxwriter *This = impl_from_IMXWriter( iface );
525
526     TRACE("(%p)->(%p)\n", This, value);
527     return writer_get_property(This, MXWriter_OmitXmlDecl, value);
528 }
529
530 static HRESULT WINAPI mxwriter_put_version(IMXWriter *iface, BSTR version)
531 {
532     mxwriter *This = impl_from_IMXWriter( iface );
533
534     TRACE("(%p)->(%s)\n", This, debugstr_w(version));
535
536     if (!version) return E_INVALIDARG;
537
538     SysFreeString(This->version);
539     This->version = SysAllocString(version);
540
541     return S_OK;
542 }
543
544 static HRESULT WINAPI mxwriter_get_version(IMXWriter *iface, BSTR *version)
545 {
546     mxwriter *This = impl_from_IMXWriter( iface );
547
548     TRACE("(%p)->(%p)\n", This, version);
549
550     if (!version) return E_POINTER;
551
552     return return_bstr(This->version, version);
553 }
554
555 static HRESULT WINAPI mxwriter_put_disableOutputEscaping(IMXWriter *iface, VARIANT_BOOL value)
556 {
557     mxwriter *This = impl_from_IMXWriter( iface );
558
559     TRACE("(%p)->(%d)\n", This, value);
560     return writer_set_property(This, MXWriter_DisableEscaping, value);
561 }
562
563 static HRESULT WINAPI mxwriter_get_disableOutputEscaping(IMXWriter *iface, VARIANT_BOOL *value)
564 {
565     mxwriter *This = impl_from_IMXWriter( iface );
566
567     TRACE("(%p)->(%p)\n", This, value);
568     return writer_get_property(This, MXWriter_DisableEscaping, value);
569 }
570
571 static HRESULT WINAPI mxwriter_flush(IMXWriter *iface)
572 {
573     mxwriter *This = impl_from_IMXWriter( iface );
574     TRACE("(%p)\n", This);
575     return flush_output_buffer(This);
576 }
577
578 static const struct IMXWriterVtbl mxwriter_vtbl =
579 {
580     mxwriter_QueryInterface,
581     mxwriter_AddRef,
582     mxwriter_Release,
583     mxwriter_GetTypeInfoCount,
584     mxwriter_GetTypeInfo,
585     mxwriter_GetIDsOfNames,
586     mxwriter_Invoke,
587     mxwriter_put_output,
588     mxwriter_get_output,
589     mxwriter_put_encoding,
590     mxwriter_get_encoding,
591     mxwriter_put_byteOrderMark,
592     mxwriter_get_byteOrderMark,
593     mxwriter_put_indent,
594     mxwriter_get_indent,
595     mxwriter_put_standalone,
596     mxwriter_get_standalone,
597     mxwriter_put_omitXMLDeclaration,
598     mxwriter_get_omitXMLDeclaration,
599     mxwriter_put_version,
600     mxwriter_get_version,
601     mxwriter_put_disableOutputEscaping,
602     mxwriter_get_disableOutputEscaping,
603     mxwriter_flush
604 };
605
606 /*** ISAXContentHandler ***/
607 static HRESULT WINAPI mxwriter_saxcontent_QueryInterface(
608     ISAXContentHandler *iface,
609     REFIID riid,
610     void **obj)
611 {
612     mxwriter *This = impl_from_ISAXContentHandler( iface );
613     return IMXWriter_QueryInterface(&This->IMXWriter_iface, riid, obj);
614 }
615
616 static ULONG WINAPI mxwriter_saxcontent_AddRef(ISAXContentHandler *iface)
617 {
618     mxwriter *This = impl_from_ISAXContentHandler( iface );
619     return IMXWriter_AddRef(&This->IMXWriter_iface);
620 }
621
622 static ULONG WINAPI mxwriter_saxcontent_Release(ISAXContentHandler *iface)
623 {
624     mxwriter *This = impl_from_ISAXContentHandler( iface );
625     return IMXWriter_Release(&This->IMXWriter_iface);
626 }
627
628 static HRESULT WINAPI mxwriter_saxcontent_putDocumentLocator(
629     ISAXContentHandler *iface,
630     ISAXLocator *locator)
631 {
632     mxwriter *This = impl_from_ISAXContentHandler( iface );
633     FIXME("(%p)->(%p)\n", This, locator);
634     return E_NOTIMPL;
635 }
636
637 static HRESULT WINAPI mxwriter_saxcontent_startDocument(ISAXContentHandler *iface)
638 {
639     mxwriter *This = impl_from_ISAXContentHandler( iface );
640     xmlChar *s;
641
642     TRACE("(%p)\n", This);
643
644     /* If properties have been changed since the last "endDocument" call
645      * we need to reset the output buffer. If we don't the output buffer
646      * could end up with multiple XML documents in it, plus this seems to
647      * be how Windows works.
648      */
649     if (This->prop_changed) {
650         reset_output_buffer(This);
651         This->prop_changed = FALSE;
652     }
653
654     if (This->props[MXWriter_OmitXmlDecl] == VARIANT_TRUE) return S_OK;
655
656     /* version */
657     xmlOutputBufferWriteString(This->buffer, "<?xml version=\"");
658     s = xmlchar_from_wchar(This->version);
659     xmlOutputBufferWriteString(This->buffer, (char*)s);
660     heap_free(s);
661     xmlOutputBufferWriteString(This->buffer, "\"");
662
663     /* encoding */
664     xmlOutputBufferWriteString(This->buffer, " encoding=\"");
665     xmlOutputBufferWriteString(This->buffer, xmlGetCharEncodingName(This->encoding));
666     xmlOutputBufferWriteString(This->buffer, "\"");
667
668     /* standalone */
669     xmlOutputBufferWriteString(This->buffer, " standalone=\"");
670     if (This->props[MXWriter_Standalone] == VARIANT_TRUE)
671         xmlOutputBufferWriteString(This->buffer, "yes\"?>");
672     else
673         xmlOutputBufferWriteString(This->buffer, "no\"?>");
674
675     xmlOutputBufferWriteString(This->buffer, crlfA);
676
677     if (This->dest && This->encoding == XML_CHAR_ENCODING_UTF16LE) {
678         static const CHAR utf16BOM[] = {0xff,0xfe};
679
680         if (This->props[MXWriter_BOM] == VARIANT_TRUE)
681             /* Windows passes a NULL pointer as the pcbWritten parameter and
682              * ignores any error codes returned from this Write call.
683              */
684             IStream_Write(This->dest, utf16BOM, sizeof(utf16BOM), NULL);
685     }
686
687     return S_OK;
688 }
689
690 static HRESULT WINAPI mxwriter_saxcontent_endDocument(ISAXContentHandler *iface)
691 {
692     mxwriter *This = impl_from_ISAXContentHandler( iface );
693     TRACE("(%p)\n", This);
694     This->prop_changed = FALSE;
695     return flush_output_buffer(This);
696 }
697
698 static HRESULT WINAPI mxwriter_saxcontent_startPrefixMapping(
699     ISAXContentHandler *iface,
700     const WCHAR *prefix,
701     int nprefix,
702     const WCHAR *uri,
703     int nuri)
704 {
705     mxwriter *This = impl_from_ISAXContentHandler( iface );
706     FIXME("(%p)->(%s %s)\n", This, debugstr_wn(prefix, nprefix), debugstr_wn(uri, nuri));
707     return E_NOTIMPL;
708 }
709
710 static HRESULT WINAPI mxwriter_saxcontent_endPrefixMapping(
711     ISAXContentHandler *iface,
712     const WCHAR *prefix,
713     int nprefix)
714 {
715     mxwriter *This = impl_from_ISAXContentHandler( iface );
716     FIXME("(%p)->(%s)\n", This, debugstr_wn(prefix, nprefix));
717     return E_NOTIMPL;
718 }
719
720 static HRESULT WINAPI mxwriter_saxcontent_startElement(
721     ISAXContentHandler *iface,
722     const WCHAR *namespaceUri,
723     int nnamespaceUri,
724     const WCHAR *local_name,
725     int nlocal_name,
726     const WCHAR *QName,
727     int nQName,
728     ISAXAttributes *attr)
729 {
730     mxwriter *This = impl_from_ISAXContentHandler( iface );
731     xmlChar *s;
732
733     TRACE("(%p)->(%s %s %s %p)\n", This, debugstr_wn(namespaceUri, nnamespaceUri),
734         debugstr_wn(local_name, nlocal_name), debugstr_wn(QName, nQName), attr);
735
736     if ((!namespaceUri || !local_name || !QName) && This->class_version != MSXML6)
737         return E_INVALIDARG;
738
739     close_element_starttag(This);
740     set_element_name(This, QName ? QName  : emptyW,
741                            QName ? nQName : 0);
742
743     xmlOutputBufferWriteString(This->buffer, "<");
744     s = xmlchar_from_wcharn(QName, nQName);
745     xmlOutputBufferWriteString(This->buffer, (char*)s);
746     heap_free(s);
747
748     if (attr)
749     {
750         HRESULT hr;
751         INT length;
752         INT i;
753
754         hr = ISAXAttributes_getLength(attr, &length);
755         if (FAILED(hr)) return hr;
756
757         for (i = 0; i < length; i++)
758         {
759             const WCHAR *str;
760             INT len = 0;
761
762             hr = ISAXAttributes_getQName(attr, i, &str, &len);
763             if (FAILED(hr)) return hr;
764
765             /* space separator in front of every attribute */
766             xmlOutputBufferWriteString(This->buffer, " ");
767
768             s = xmlchar_from_wcharn(str, len);
769             xmlOutputBufferWriteString(This->buffer, (char*)s);
770             heap_free(s);
771
772             xmlOutputBufferWriteString(This->buffer, "=\"");
773
774             len = 0;
775             hr = ISAXAttributes_getValue(attr, i, &str, &len);
776             if (FAILED(hr)) return hr;
777
778             s = xmlchar_from_wcharn(str, len);
779             xmlOutputBufferWriteString(This->buffer, (char*)s);
780             heap_free(s);
781
782             xmlOutputBufferWriteString(This->buffer, "\"");
783         }
784     }
785
786     return S_OK;
787 }
788
789 static HRESULT WINAPI mxwriter_saxcontent_endElement(
790     ISAXContentHandler *iface,
791     const WCHAR *namespaceUri,
792     int nnamespaceUri,
793     const WCHAR * local_name,
794     int nlocal_name,
795     const WCHAR *QName,
796     int nQName)
797 {
798     mxwriter *This = impl_from_ISAXContentHandler( iface );
799
800     TRACE("(%p)->(%s:%d %s:%d %s:%d)\n", This, debugstr_wn(namespaceUri, nnamespaceUri), nnamespaceUri,
801         debugstr_wn(local_name, nlocal_name), nlocal_name, debugstr_wn(QName, nQName), nQName);
802
803     if ((!namespaceUri || !local_name || !QName) && This->class_version != MSXML6)
804         return E_INVALIDARG;
805
806     if (This->element && QName && !strncmpW(This->element, QName, nQName))
807     {
808         xmlOutputBufferWriteString(This->buffer, "/>");
809     }
810     else
811     {
812         xmlChar *s = xmlchar_from_wcharn(QName, nQName);
813
814         xmlOutputBufferWriteString(This->buffer, "</");
815         xmlOutputBufferWriteString(This->buffer, (char*)s);
816         xmlOutputBufferWriteString(This->buffer, ">");
817
818         heap_free(s);
819     }
820
821     set_element_name(This, NULL, 0);
822
823     return S_OK;
824 }
825
826 static HRESULT WINAPI mxwriter_saxcontent_characters(
827     ISAXContentHandler *iface,
828     const WCHAR *chars,
829     int nchars)
830 {
831     mxwriter *This = impl_from_ISAXContentHandler( iface );
832
833     TRACE("(%p)->(%s:%d)\n", This, debugstr_wn(chars, nchars), nchars);
834
835     if (!chars) return E_INVALIDARG;
836
837     close_element_starttag(This);
838     set_element_name(This, NULL, 0);
839
840     if (nchars)
841     {
842         xmlChar *s = xmlchar_from_wcharn(chars, nchars);
843         xmlOutputBufferWriteString(This->buffer, (char*)s);
844         heap_free(s);
845     }
846
847     return S_OK;
848 }
849
850 static HRESULT WINAPI mxwriter_saxcontent_ignorableWhitespace(
851     ISAXContentHandler *iface,
852     const WCHAR *chars,
853     int nchars)
854 {
855     mxwriter *This = impl_from_ISAXContentHandler( iface );
856     FIXME("(%p)->(%s)\n", This, debugstr_wn(chars, nchars));
857     return E_NOTIMPL;
858 }
859
860 static HRESULT WINAPI mxwriter_saxcontent_processingInstruction(
861     ISAXContentHandler *iface,
862     const WCHAR *target,
863     int ntarget,
864     const WCHAR *data,
865     int ndata)
866 {
867     mxwriter *This = impl_from_ISAXContentHandler( iface );
868     FIXME("(%p)->(%s %s)\n", This, debugstr_wn(target, ntarget), debugstr_wn(data, ndata));
869     return E_NOTIMPL;
870 }
871
872 static HRESULT WINAPI mxwriter_saxcontent_skippedEntity(
873     ISAXContentHandler *iface,
874     const WCHAR *name,
875     int nname)
876 {
877     mxwriter *This = impl_from_ISAXContentHandler( iface );
878     FIXME("(%p)->(%s)\n", This, debugstr_wn(name, nname));
879     return E_NOTIMPL;
880 }
881
882 static const struct ISAXContentHandlerVtbl mxwriter_saxcontent_vtbl =
883 {
884     mxwriter_saxcontent_QueryInterface,
885     mxwriter_saxcontent_AddRef,
886     mxwriter_saxcontent_Release,
887     mxwriter_saxcontent_putDocumentLocator,
888     mxwriter_saxcontent_startDocument,
889     mxwriter_saxcontent_endDocument,
890     mxwriter_saxcontent_startPrefixMapping,
891     mxwriter_saxcontent_endPrefixMapping,
892     mxwriter_saxcontent_startElement,
893     mxwriter_saxcontent_endElement,
894     mxwriter_saxcontent_characters,
895     mxwriter_saxcontent_ignorableWhitespace,
896     mxwriter_saxcontent_processingInstruction,
897     mxwriter_saxcontent_skippedEntity
898 };
899
900 HRESULT MXWriter_create(MSXML_VERSION version, IUnknown *pUnkOuter, void **ppObj)
901 {
902     static const WCHAR version10W[] = {'1','.','0',0};
903     mxwriter *This;
904
905     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
906
907     if (pUnkOuter) FIXME("support aggregation, outer\n");
908
909     This = heap_alloc( sizeof (*This) );
910     if(!This)
911         return E_OUTOFMEMORY;
912
913     This->IMXWriter_iface.lpVtbl = &mxwriter_vtbl;
914     This->ISAXContentHandler_iface.lpVtbl = &mxwriter_saxcontent_vtbl;
915     This->ref = 1;
916     This->class_version = version;
917
918     This->props[MXWriter_BOM] = VARIANT_TRUE;
919     This->props[MXWriter_DisableEscaping] = VARIANT_FALSE;
920     This->props[MXWriter_Indent] = VARIANT_FALSE;
921     This->props[MXWriter_OmitXmlDecl] = VARIANT_FALSE;
922     This->props[MXWriter_Standalone] = VARIANT_FALSE;
923     This->prop_changed = FALSE;
924     This->encoding   = xmlParseCharEncoding("UTF-16");
925     This->version    = SysAllocString(version10W);
926
927     This->element = NULL;
928
929     This->dest = NULL;
930     This->dest_written = 0;
931
932     This->buffer = xmlAllocOutputBuffer(xmlGetCharEncodingHandler(This->encoding));
933
934     *ppObj = &This->IMXWriter_iface;
935
936     TRACE("returning iface %p\n", *ppObj);
937
938     return S_OK;
939 }
940
941 #else
942
943 HRESULT MXWriter_create(MSXML_VERSION version, IUnknown *pUnkOuter, void **obj)
944 {
945     MESSAGE("This program tried to use a MXXMLWriter object, but\n"
946             "libxml2 support was not present at compile time.\n");
947     return E_NOTIMPL;
948 }
949
950 #endif /* HAVE_LIBXML2 */