* IXmlWriter implementation
*
* Copyright 2011 Alistair Leslie-Hughes
- * Copyright 2014, 2016 Nikolay Sivov for CodeWeavers
+ * Copyright 2014-2018 Nikolay Sivov for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
*/
#define COBJMACROS
+#include <assert.h>
#include <stdarg.h>
+
#include "windef.h"
#include "winbase.h"
#include "objbase.h"
#include "xmllite.h"
#include "xmllite_private.h"
+#ifdef __REACTOS__
+#include <wchar.h>
+#include <winnls.h>
+#endif
#include "initguid.h"
#include "wine/debug.h"
#include "wine/list.h"
-#include "wine/unicode.h"
WINE_DEFAULT_DEBUG_CHANNEL(xmllite);
static const WCHAR closeelementW[] = {'<','/'};
static const WCHAR closetagW[] = {' ','/','>'};
static const WCHAR closepiW[] = {'?','>'};
-static const WCHAR ltW[] = {'<'};
-static const WCHAR gtW[] = {'>'};
-static const WCHAR spaceW[] = {' '};
-static const WCHAR quoteW[] = {'"'};
+static const WCHAR xmlnsW[] = {' ','x','m','l','n','s'};
+static const WCHAR xmlnsuriW[] = {'h','t','t','p',':','/','/','w','w','w','.','w','3','.','o','r','g','/','2','0','0','0','/','x','m','l','n','s','/',0};
struct output_buffer
{
xml_encoding encoding;
WCHAR *encoding_name; /* exactly as specified on output creation */
struct output_buffer buffer;
+ DWORD written : 1;
} xmlwriteroutput;
static const struct IUnknownVtbl xmlwriteroutputvtbl;
struct list entry;
WCHAR *qname;
unsigned int len; /* qname length in chars */
+ struct list ns;
+};
+
+struct ns
+{
+ struct list entry;
+ WCHAR *prefix;
+ int prefix_len;
+ WCHAR *uri;
+ BOOL emitted;
+ struct element *element;
};
typedef struct _xmlwriter
BOOL omitxmldecl;
XmlConformanceLevel conformance;
XmlWriterState state;
- BOOL bomwritten;
- BOOL starttagopen;
struct list elements;
+ DWORD bomwritten : 1;
+ DWORD starttagopen : 1;
+ DWORD textnode : 1;
} xmlwriter;
static inline xmlwriter *impl_from_IXmlWriter(IXmlWriter *iface)
return prop_names[prop];
}
+static HRESULT create_writer_output(IUnknown *stream, IMalloc *imalloc, xml_encoding encoding,
+ const WCHAR *encoding_name, xmlwriteroutput **out);
+
/* writer output memory allocation functions */
static inline void *writeroutput_alloc(xmlwriteroutput *output, size_t len)
{
}
/* writer memory allocation functions */
-static inline void *writer_alloc(xmlwriter *writer, size_t len)
+static inline void *writer_alloc(const xmlwriter *writer, size_t len)
{
return m_alloc(writer->imalloc, len);
}
-static inline void writer_free(xmlwriter *writer, void *mem)
+static inline void writer_free(const xmlwriter *writer, void *mem)
{
m_free(writer->imalloc, mem);
}
ret = writer_alloc(writer, sizeof(*ret));
if (!ret) return ret;
- len = prefix ? strlenW(prefix) + 1 /* ':' */ : 0;
- len += strlenW(local);
+ len = prefix ? lstrlenW(prefix) + 1 /* ':' */ : 0;
+ len += lstrlenW(local);
ret->qname = writer_alloc(writer, (len + 1)*sizeof(WCHAR));
ret->len = len;
if (prefix) {
static const WCHAR colonW[] = {':',0};
- strcpyW(ret->qname, prefix);
- strcatW(ret->qname, colonW);
+ lstrcpyW(ret->qname, prefix);
+ lstrcatW(ret->qname, colonW);
}
else
ret->qname[0] = 0;
- strcatW(ret->qname, local);
+ lstrcatW(ret->qname, local);
+ list_init(&ret->ns);
return ret;
}
-static void free_element(xmlwriter *writer, struct element *element)
+static void writer_free_element(xmlwriter *writer, struct element *element)
{
+ struct ns *ns, *ns2;
+
+ LIST_FOR_EACH_ENTRY_SAFE(ns, ns2, &element->ns, struct ns, entry)
+ {
+ list_remove(&ns->entry);
+ writer_free(writer, ns->prefix);
+ writer_free(writer, ns->uri);
+ writer_free(writer, ns);
+ }
+
writer_free(writer, element->qname);
writer_free(writer, element);
}
-static void push_element(xmlwriter *writer, struct element *element)
+static void writer_free_element_stack(xmlwriter *writer)
+{
+ struct element *element, *element2;
+
+ LIST_FOR_EACH_ENTRY_SAFE(element, element2, &writer->elements, struct element, entry)
+ {
+ list_remove(&element->entry);
+ writer_free_element(writer, element);
+ }
+}
+
+static void writer_push_element(xmlwriter *writer, struct element *element)
{
list_add_head(&writer->elements, &element->entry);
}
return element;
}
+static WCHAR *writer_strndupW(const xmlwriter *writer, const WCHAR *str, int len)
+{
+ size_t size;
+ WCHAR *ret;
+
+ if (!str)
+ return NULL;
+
+ if (len == -1)
+ len = lstrlenW(str);
+
+ size = (len + 1) * sizeof(WCHAR);
+ ret = writer_alloc(writer, size);
+ memcpy(ret, str, size);
+ return ret;
+}
+
+static WCHAR *writer_strdupW(const xmlwriter *writer, const WCHAR *str)
+{
+ return writer_strndupW(writer, str, -1);
+}
+
+static struct ns *writer_push_ns(xmlwriter *writer, const WCHAR *prefix, int prefix_len, const WCHAR *uri)
+{
+ struct element *element;
+ struct ns *ns;
+
+ element = LIST_ENTRY(list_head(&writer->elements), struct element, entry);
+ if (!element)
+ return NULL;
+
+ if ((ns = writer_alloc(writer, sizeof(*ns))))
+ {
+ ns->prefix = writer_strndupW(writer, prefix, prefix_len);
+ ns->prefix_len = prefix_len;
+ ns->uri = writer_strdupW(writer, uri);
+ ns->emitted = FALSE;
+ ns->element = element;
+ list_add_tail(&element->ns, &ns->entry);
+ }
+
+ return ns;
+}
+
+static BOOL is_empty_string(const WCHAR *str)
+{
+ return !str || !*str;
+}
+
+static struct ns *writer_find_ns_current(const xmlwriter *writer, const WCHAR *prefix, const WCHAR *uri)
+{
+ struct element *element;
+ struct ns *ns;
+
+ if (is_empty_string(prefix) || is_empty_string(uri))
+ return NULL;
+
+ element = LIST_ENTRY(list_head(&writer->elements), struct element, entry);
+
+ LIST_FOR_EACH_ENTRY(ns, &element->ns, struct ns, entry)
+ {
+ if (!wcscmp(uri, ns->uri) && !wcscmp(prefix, ns->prefix))
+ return ns;
+ }
+
+ return NULL;
+}
+
+static struct ns *writer_find_ns(const xmlwriter *writer, const WCHAR *prefix, const WCHAR *uri)
+{
+ struct element *element;
+ struct ns *ns;
+
+ if (is_empty_string(prefix) && is_empty_string(uri))
+ return NULL;
+
+ LIST_FOR_EACH_ENTRY(element, &writer->elements, struct element, entry)
+ {
+ LIST_FOR_EACH_ENTRY(ns, &element->ns, struct ns, entry)
+ {
+ if (!uri)
+ {
+ if (!ns->prefix) continue;
+ if (!wcscmp(ns->prefix, prefix))
+ return ns;
+ }
+ else if (!wcscmp(uri, ns->uri))
+ {
+ if (prefix && !*prefix)
+ return NULL;
+ if (!prefix || !wcscmp(prefix, ns->prefix))
+ return ns;
+ }
+ }
+ }
+
+ return NULL;
+}
+
+static HRESULT is_valid_ncname(const WCHAR *str, int *out)
+{
+ int len = 0;
+
+ *out = 0;
+
+ if (!str || !*str)
+ return S_OK;
+
+ while (*str)
+ {
+ if (!is_ncnamechar(*str))
+ return WC_E_NAMECHARACTER;
+ len++;
+ str++;
+ }
+
+ *out = len;
+ return S_OK;
+}
+
+static HRESULT is_valid_name(const WCHAR *str, unsigned int *out)
+{
+ unsigned int len = 1;
+
+ *out = 0;
+
+ if (!str || !*str)
+ return S_OK;
+
+ if (!is_namestartchar(*str++))
+ return WC_E_NAMECHARACTER;
+
+ while (*str++)
+ {
+ if (!is_namechar(*str))
+ return WC_E_NAMECHARACTER;
+ len++;
+ }
+
+ *out = len;
+ return S_OK;
+}
+
+static HRESULT is_valid_pubid(const WCHAR *str, unsigned int *out)
+{
+ unsigned int len = 0;
+
+ *out = 0;
+
+ if (!str || !*str)
+ return S_OK;
+
+ while (*str)
+ {
+ if (!is_pubchar(*str++))
+ return WC_E_PUBLICID;
+ len++;
+ }
+
+ *out = len;
+
+ return S_OK;
+}
+
static HRESULT init_output_buffer(xmlwriteroutput *output)
{
struct output_buffer *buffer = &output->buffer;
if (buffer->codepage == 1200) {
/* For UTF-16 encoding just copy. */
- length = len == -1 ? strlenW(data) : len;
+ length = len == -1 ? lstrlenW(data) : len;
if (length) {
length *= sizeof(WCHAR);
length = WideCharToMultiByte(buffer->codepage, 0, data, len, ptr, length, NULL, NULL);
buffer->written += len == -1 ? length-1 : length;
}
+ output->written = length != 0;
return S_OK;
}
+static HRESULT write_output_buffer_char(xmlwriteroutput *output, WCHAR ch)
+{
+ return write_output_buffer(output, &ch, 1);
+}
+
static HRESULT write_output_buffer_quoted(xmlwriteroutput *output, const WCHAR *data, int len)
{
- write_output_buffer(output, quoteW, ARRAY_SIZE(quoteW));
- write_output_buffer(output, data, len);
- write_output_buffer(output, quoteW, ARRAY_SIZE(quoteW));
+ write_output_buffer_char(output, '"');
+ if (!is_empty_string(data))
+ write_output_buffer(output, data, len);
+ write_output_buffer_char(output, '"');
return S_OK;
}
/* TODO: test if we need to validate char range */
-static HRESULT write_output_qname(xmlwriteroutput *output, const WCHAR *prefix, const WCHAR *local_name)
+static HRESULT write_output_qname(xmlwriteroutput *output, const WCHAR *prefix, int prefix_len,
+ const WCHAR *local_name, int local_len)
{
- if (prefix) {
- static const WCHAR colW[] = {':'};
- write_output_buffer(output, prefix, -1);
- write_output_buffer(output, colW, ARRAY_SIZE(colW));
- }
+ assert(prefix_len >= 0 && local_len >= 0);
+
+ if (prefix_len)
+ write_output_buffer(output, prefix, prefix_len);
+
+ if (prefix_len && local_len)
+ write_output_buffer_char(output, ':');
- write_output_buffer(output, local_name, -1);
+ write_output_buffer(output, local_name, local_len);
return S_OK;
}
return S_OK;
}
+static void writer_output_ns(xmlwriter *writer, struct element *element)
+{
+ struct ns *ns;
+
+ LIST_FOR_EACH_ENTRY(ns, &element->ns, struct ns, entry)
+ {
+ if (ns->emitted)
+ continue;
+
+ write_output_qname(writer->output, xmlnsW, ARRAY_SIZE(xmlnsW), ns->prefix, ns->prefix_len);
+ write_output_buffer_char(writer->output, '=');
+ write_output_buffer_quoted(writer->output, ns->uri, -1);
+ }
+}
+
static HRESULT writer_close_starttag(xmlwriter *writer)
{
HRESULT hr;
if (!writer->starttagopen) return S_OK;
- hr = write_output_buffer(writer->output, gtW, ARRAY_SIZE(gtW));
- writer->starttagopen = FALSE;
+
+ writer_output_ns(writer, LIST_ENTRY(list_head(&writer->elements), struct element, entry));
+ hr = write_output_buffer_char(writer->output, '>');
+ writer->starttagopen = 0;
return hr;
}
static const WCHAR crlfW[] = {'\r','\n'};
unsigned int indent_level = writer->indent_level;
- if (!writer->indent)
+ if (!writer->indent || writer->textnode)
+ {
+ writer->textnode = 0;
return;
+ }
/* Do state check to prevent newline inserted after BOM. It is assumed that
state does not change between writing BOM and inserting indentation. */
- if (writer->output->buffer.written && writer->state != XmlWriterState_Ready)
+ if (writer->output->written && writer->state != XmlWriterState_Ready)
write_output_buffer(writer->output, crlfW, ARRAY_SIZE(crlfW));
while (indent_level--)
write_output_buffer(writer->output, dblspaceW, ARRAY_SIZE(dblspaceW));
+
+ writer->textnode = 0;
}
static HRESULT WINAPI xmlwriter_QueryInterface(IXmlWriter *iface, REFIID riid, void **ppvObject)
TRACE("(%p)->(%u)\n", This, ref);
if (ref == 0) {
- struct element *element, *element2;
IMalloc *imalloc = This->imalloc;
writeroutput_flush_stream(This->output);
if (This->output) IUnknown_Release(&This->output->IXmlWriterOutput_iface);
- /* element stack */
- LIST_FOR_EACH_ENTRY_SAFE(element, element2, &This->elements, struct element, entry) {
- list_remove(&element->entry);
- free_element(This, element);
- }
+ writer_free_element_stack(This);
writer_free(This, This);
if (imalloc) IMalloc_Release(imalloc);
writeroutput_release_stream(This->output);
IUnknown_Release(&This->output->IXmlWriterOutput_iface);
This->output = NULL;
- This->bomwritten = FALSE;
+ This->bomwritten = 0;
+ This->textnode = 0;
This->indent_level = 0;
+ writer_free_element_stack(This);
}
/* just reset current output */
}
if (hr != S_OK || !writeroutput) {
- /* create IXmlWriterOutput basing on supplied interface */
- hr = CreateXmlWriterOutputWithEncodingName(output, This->imalloc, NULL, &writeroutput);
- if (hr != S_OK) return hr;
- This->output = impl_from_IXmlWriterOutput(writeroutput);
+ /* Create output for given stream. */
+ hr = create_writer_output(output, This->imalloc, XmlEncoding_UTF8, NULL, &This->output);
+ if (hr != S_OK)
+ return hr;
}
if (This->output->encoding == XmlEncoding_Unknown)
return E_NOTIMPL;
}
-static HRESULT WINAPI xmlwriter_WriteAttributeString(IXmlWriter *iface, LPCWSTR ns_prefix,
- LPCWSTR local_name, LPCWSTR ns_uri, LPCWSTR value)
+static void write_output_attribute(xmlwriter *writer, const WCHAR *prefix, int prefix_len,
+ const WCHAR *local, int local_len, const WCHAR *value)
+{
+ write_output_buffer_char(writer->output, ' ');
+ write_output_qname(writer->output, prefix, prefix_len, local, local_len);
+ write_output_buffer_char(writer->output, '=');
+ write_output_buffer_quoted(writer->output, value, -1);
+}
+
+static BOOL is_valid_xml_space_value(const WCHAR *value)
{
- static const WCHAR eqW[] = {'=','"'};
+ static const WCHAR preserveW[] = {'p','r','e','s','e','r','v','e',0};
+ static const WCHAR defaultW[] = {'d','e','f','a','u','l','t',0};
+
+ if (!value)
+ return FALSE;
+
+ return !wcscmp(value, preserveW) || !wcscmp(value, defaultW);
+}
+
+static HRESULT WINAPI xmlwriter_WriteAttributeString(IXmlWriter *iface, LPCWSTR prefix,
+ LPCWSTR local, LPCWSTR uri, LPCWSTR value)
+{
+ static const WCHAR spaceattrW[] = {'s','p','a','c','e',0};
+ static const WCHAR xmlnsW[] = {'x','m','l','n','s',0};
+ static const WCHAR xmlW[] = {'x','m','l',0};
xmlwriter *This = impl_from_IXmlWriter(iface);
+ BOOL is_xmlns_prefix, is_xmlns_local;
+ int prefix_len, local_len;
+ struct ns *ns;
+ HRESULT hr;
- TRACE("%p %s %s %s %s\n", This, debugstr_w(ns_prefix), debugstr_w(local_name),
- debugstr_w(ns_uri), debugstr_w(value));
+ TRACE("%p %s %s %s %s\n", This, debugstr_w(prefix), debugstr_w(local), debugstr_w(uri), debugstr_w(value));
switch (This->state)
{
;
}
- if (ns_prefix || ns_uri)
+ /* Prefix "xmlns" */
+ is_xmlns_prefix = prefix && !wcscmp(prefix, xmlnsW);
+ if (is_xmlns_prefix && is_empty_string(uri) && is_empty_string(local))
+ return WR_E_NSPREFIXDECLARED;
+
+ if (!local)
+ return E_INVALIDARG;
+
+ /* Validate prefix and local name */
+ if (FAILED(hr = is_valid_ncname(prefix, &prefix_len)))
+ return hr;
+
+ if (FAILED(hr = is_valid_ncname(local, &local_len)))
+ return hr;
+
+ is_xmlns_local = !wcscmp(local, xmlnsW);
+
+ /* Trivial case, no prefix. */
+ if (prefix_len == 0 && is_empty_string(uri))
+ {
+ write_output_attribute(This, prefix, prefix_len, local, local_len, value);
+ return S_OK;
+ }
+
+ /* Predefined "xml" prefix. */
+ if (prefix_len && !wcscmp(prefix, xmlW))
{
- FIXME("namespaces are not supported.\n");
- return E_NOTIMPL;
+ /* Valid "space" value is enforced. */
+ if (!wcscmp(local, spaceattrW) && !is_valid_xml_space_value(value))
+ return WR_E_INVALIDXMLSPACE;
+
+ /* Redefinition is not allowed. */
+ if (!is_empty_string(uri))
+ return WR_E_XMLPREFIXDECLARATION;
+
+ write_output_attribute(This, prefix, prefix_len, local, local_len, value);
+
+ return S_OK;
+ }
+
+ if (is_xmlns_prefix || (prefix_len == 0 && uri && !wcscmp(uri, xmlnsuriW)))
+ {
+ if (prefix_len && !is_empty_string(uri))
+ return WR_E_XMLNSPREFIXDECLARATION;
+
+ /* Look for exact match defined in current element, and write it out. */
+ if (!(ns = writer_find_ns_current(This, prefix, value)))
+ ns = writer_push_ns(This, local, local_len, value);
+ ns->emitted = TRUE;
+
+ write_output_attribute(This, xmlnsW, ARRAY_SIZE(xmlnsW) - 1, local, local_len, value);
+
+ return S_OK;
}
- write_output_buffer(This->output, spaceW, ARRAY_SIZE(spaceW));
- write_output_buffer(This->output, local_name, -1);
- write_output_buffer(This->output, eqW, ARRAY_SIZE(eqW));
- write_output_buffer(This->output, value, -1);
- write_output_buffer(This->output, quoteW, ARRAY_SIZE(quoteW));
+ /* Ignore prefix is URI wasn't specified. */
+ if (is_xmlns_local && is_empty_string(uri))
+ {
+ write_output_attribute(This, NULL, 0, xmlnsW, ARRAY_SIZE(xmlnsW) - 1, value);
+ return S_OK;
+ }
+
+ if (!(ns = writer_find_ns(This, prefix, uri)))
+ {
+ if (is_empty_string(prefix) && !is_empty_string(uri))
+ {
+ FIXME("Prefix autogeneration is not implemented.\n");
+ return E_NOTIMPL;
+ }
+ if (!is_empty_string(uri))
+ ns = writer_push_ns(This, prefix, prefix_len, uri);
+ }
+
+ if (ns)
+ write_output_attribute(This, ns->prefix, ns->prefix_len, local, local_len, value);
+ else
+ write_output_attribute(This, prefix, prefix_len, local, local_len, value);
return S_OK;
}
;
}
- len = data ? strlenW(data) : 0;
+ len = data ? lstrlenW(data) : 0;
write_node_indent(This);
if (!len)
else {
static const WCHAR cdatacloseW[] = {']',']','>',0};
while (len) {
- const WCHAR *str = strstrW(data, cdatacloseW);
+ const WCHAR *str = wcsstr(data, cdatacloseW);
if (str) {
str += 2;
write_cdata_section(This->output, data, str - data);
;
}
- sprintfW(bufW, fmtW, ch);
+ swprintf(bufW, fmtW, ch);
write_output_buffer(This->output, bufW, -1);
return S_OK;
write_node_indent(This);
write_output_buffer(This->output, copenW, ARRAY_SIZE(copenW));
if (comment) {
- int len = strlenW(comment), i;
+ int len = lstrlenW(comment), i;
/* Make sure there's no two hyphen sequences in a string, space is used as a separator to produce compliant
comment string */
for (i = 0; i < len; i++) {
write_output_buffer(This->output, comment + i, 1);
if (comment[i] == '-' && (i + 1 < len) && comment[i+1] == '-')
- write_output_buffer(This->output, spaceW, ARRAY_SIZE(spaceW));
+ write_output_buffer_char(This->output, ' ');
}
}
else
write_output_buffer(This->output, comment, len);
if (len && comment[len-1] == '-')
- write_output_buffer(This->output, spaceW, ARRAY_SIZE(spaceW));
+ write_output_buffer_char(This->output, ' ');
}
write_output_buffer(This->output, ccloseW, ARRAY_SIZE(ccloseW));
return S_OK;
}
-static HRESULT WINAPI xmlwriter_WriteDocType(IXmlWriter *iface, LPCWSTR pwszName, LPCWSTR pwszPublicId,
- LPCWSTR pwszSystemId, LPCWSTR pwszSubset)
+static HRESULT WINAPI xmlwriter_WriteDocType(IXmlWriter *iface, LPCWSTR name, LPCWSTR pubid,
+ LPCWSTR sysid, LPCWSTR subset)
{
+ static const WCHAR doctypeW[] = {'<','!','D','O','C','T','Y','P','E',' '};
+ static const WCHAR publicW[] = {' ','P','U','B','L','I','C',' '};
+ static const WCHAR systemW[] = {' ','S','Y','S','T','E','M',' '};
xmlwriter *This = impl_from_IXmlWriter(iface);
+ unsigned int name_len, pubid_len;
+ HRESULT hr;
- FIXME("%p %s %s %s %s\n", This, wine_dbgstr_w(pwszName), wine_dbgstr_w(pwszPublicId),
- wine_dbgstr_w(pwszSystemId), wine_dbgstr_w(pwszSubset));
+ TRACE("(%p)->(%s %s %s %s)\n", This, wine_dbgstr_w(name), wine_dbgstr_w(pubid), wine_dbgstr_w(sysid),
+ wine_dbgstr_w(subset));
- return E_NOTIMPL;
+ switch (This->state)
+ {
+ case XmlWriterState_Initial:
+ return E_UNEXPECTED;
+ case XmlWriterState_InvalidEncoding:
+ return MX_E_ENCODING;
+ case XmlWriterState_Content:
+ case XmlWriterState_DocClosed:
+ return WR_E_INVALIDACTION;
+ default:
+ ;
+ }
+
+ if (is_empty_string(name))
+ return E_INVALIDARG;
+
+ if (FAILED(hr = is_valid_name(name, &name_len)))
+ return hr;
+
+ if (FAILED(hr = is_valid_pubid(pubid, &pubid_len)))
+ return hr;
+
+ write_output_buffer(This->output, doctypeW, ARRAY_SIZE(doctypeW));
+ write_output_buffer(This->output, name, name_len);
+
+ if (pubid)
+ {
+ write_output_buffer(This->output, publicW, ARRAY_SIZE(publicW));
+ write_output_buffer_quoted(This->output, pubid, pubid_len);
+ write_output_buffer_char(This->output, ' ');
+ write_output_buffer_quoted(This->output, sysid, -1);
+ }
+ else if (sysid)
+ {
+ write_output_buffer(This->output, systemW, ARRAY_SIZE(systemW));
+ write_output_buffer_quoted(This->output, sysid, -1);
+ }
+
+ if (subset)
+ {
+ write_output_buffer_char(This->output, ' ');
+ write_output_buffer_char(This->output, '[');
+ write_output_buffer(This->output, subset, -1);
+ write_output_buffer_char(This->output, ']');
+ }
+ write_output_buffer_char(This->output, '>');
+
+ This->state = XmlWriterState_Content;
+
+ return S_OK;
}
static HRESULT WINAPI xmlwriter_WriteElementString(IXmlWriter *iface, LPCWSTR prefix,
LPCWSTR local_name, LPCWSTR uri, LPCWSTR value)
{
xmlwriter *This = impl_from_IXmlWriter(iface);
+ int prefix_len, local_len;
+ struct ns *ns;
+ HRESULT hr;
TRACE("(%p)->(%s %s %s %s)\n", This, wine_dbgstr_w(prefix), wine_dbgstr_w(local_name),
wine_dbgstr_w(uri), wine_dbgstr_w(value));
;
}
+ if (!local_name)
+ return E_INVALIDARG;
+
+ /* Validate prefix and local name */
+ if (FAILED(hr = is_valid_ncname(prefix, &prefix_len)))
+ return hr;
+
+ if (FAILED(hr = is_valid_ncname(local_name, &local_len)))
+ return hr;
+
+ ns = writer_find_ns(This, prefix, uri);
+ if (!ns && !is_empty_string(prefix) && is_empty_string(uri))
+ return WR_E_NSPREFIXWITHEMPTYNSURI;
+
+ if (uri && !wcscmp(uri, xmlnsuriW))
+ {
+ if (!prefix)
+ return WR_E_XMLNSPREFIXDECLARATION;
+
+ if (!is_empty_string(prefix))
+ return WR_E_XMLNSURIDECLARATION;
+ }
+
write_encoding_bom(This);
write_node_indent(This);
- write_output_buffer(This->output, ltW, ARRAY_SIZE(ltW));
- write_output_qname(This->output, prefix, local_name);
+
+ write_output_buffer_char(This->output, '<');
+ if (ns)
+ write_output_qname(This->output, ns->prefix, ns->prefix_len, local_name, local_len);
+ else
+ write_output_qname(This->output, prefix, prefix_len, local_name, local_len);
+
+ if (!ns && (prefix_len || !is_empty_string(uri)))
+ {
+ write_output_qname(This->output, xmlnsW, ARRAY_SIZE(xmlnsW), prefix, prefix_len);
+ write_output_buffer_char(This->output, '=');
+ write_output_buffer_quoted(This->output, uri, -1);
+ }
if (value)
{
- write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
+ write_output_buffer_char(This->output, '>');
write_output_buffer(This->output, value, -1);
write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW));
- write_output_qname(This->output, prefix, local_name);
- write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
+ write_output_qname(This->output, prefix, prefix_len, local_name, local_len);
+ write_output_buffer_char(This->output, '>');
}
else
write_output_buffer(This->output, closetagW, ARRAY_SIZE(closetagW));
if (This->starttagopen)
{
+ writer_output_ns(This, element);
write_output_buffer(This->output, closetagW, ARRAY_SIZE(closetagW));
- This->starttagopen = FALSE;
+ This->starttagopen = 0;
}
- else {
- /* write full end tag */
+ else
+ {
+ /* Write full end tag. */
write_node_indent(This);
write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW));
write_output_buffer(This->output, element->qname, element->len);
- write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
+ write_output_buffer_char(This->output, '>');
}
+ writer_free_element(This, element);
return S_OK;
}
return WR_E_INVALIDACTION;
case XmlWriterState_InvalidEncoding:
return MX_E_ENCODING;
+ case XmlWriterState_ElemStarted:
+ writer_close_starttag(This);
+ break;
default:
;
}
if (!element)
return WR_E_INVALIDACTION;
- writer_close_starttag(This);
writer_dec_indent(This);
/* don't force full end tag to the next line */
if (This->state == XmlWriterState_ElemStarted)
+ {
This->state = XmlWriterState_Content;
+ This->textnode = 0;
+ }
else
write_node_indent(This);
/* write full end tag */
write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW));
write_output_buffer(This->output, element->qname, element->len);
- write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
+ write_output_buffer_char(This->output, '>');
+
+ writer_free_element(This, element);
return S_OK;
}
case XmlWriterState_InvalidEncoding:
return MX_E_ENCODING;
case XmlWriterState_DocStarted:
- if (!strcmpW(name, xmlW))
+ if (!wcscmp(name, xmlW))
return WR_E_INVALIDACTION;
break;
case XmlWriterState_ElemStarted:
write_node_indent(This);
write_output_buffer(This->output, openpiW, ARRAY_SIZE(openpiW));
write_output_buffer(This->output, name, -1);
- write_output_buffer(This->output, spaceW, ARRAY_SIZE(spaceW));
+ write_output_buffer_char(This->output, ' ');
write_output_buffer(This->output, text, -1);
write_output_buffer(This->output, closepiW, ARRAY_SIZE(closepiW));
- if (!strcmpW(name, xmlW))
+ if (!wcscmp(name, xmlW))
This->state = XmlWriterState_PIDocStarted;
return S_OK;
static HRESULT WINAPI xmlwriter_WriteStartElement(IXmlWriter *iface, LPCWSTR prefix, LPCWSTR local_name, LPCWSTR uri)
{
xmlwriter *This = impl_from_IXmlWriter(iface);
+ int prefix_len, local_len;
struct element *element;
+ struct ns *ns;
+ HRESULT hr;
TRACE("(%p)->(%s %s %s)\n", This, wine_dbgstr_w(prefix), wine_dbgstr_w(local_name), wine_dbgstr_w(uri));
return MX_E_ENCODING;
case XmlWriterState_DocClosed:
return WR_E_INVALIDACTION;
+ case XmlWriterState_ElemStarted:
+ writer_close_starttag(This);
+ break;
default:
;
}
- /* close pending element */
- if (This->starttagopen)
- write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
+ /* Validate prefix and local name */
+ if (FAILED(hr = is_valid_ncname(prefix, &prefix_len)))
+ return hr;
+
+ if (FAILED(hr = is_valid_ncname(local_name, &local_len)))
+ return hr;
+
+ if (uri && !wcscmp(uri, xmlnsuriW))
+ {
+ if (!prefix)
+ return WR_E_XMLNSPREFIXDECLARATION;
+
+ if (!is_empty_string(prefix))
+ return WR_E_XMLNSURIDECLARATION;
+ }
+
+ ns = writer_find_ns(This, prefix, uri);
element = alloc_element(This, prefix, local_name);
if (!element)
write_node_indent(This);
This->state = XmlWriterState_ElemStarted;
- This->starttagopen = TRUE;
+ This->starttagopen = 1;
+
+ writer_push_element(This, element);
- push_element(This, element);
+ if (!ns && uri)
+ writer_push_ns(This, prefix, prefix_len, uri);
- write_output_buffer(This->output, ltW, ARRAY_SIZE(ltW));
- write_output_qname(This->output, prefix, local_name);
+ write_output_buffer_char(This->output, '<');
+ if (ns)
+ write_output_qname(This->output, ns->prefix, ns->prefix_len, local_name, local_len);
+ else
+ write_output_qname(This->output, prefix, prefix_len, local_name, local_len);
writer_inc_indent(This);
return S_OK;
;
}
+ This->textnode = 1;
write_escaped_string(This, string);
return S_OK;
}
writer = IMalloc_Alloc(imalloc, sizeof(*writer));
else
writer = heap_alloc(sizeof(*writer));
- if(!writer) return E_OUTOFMEMORY;
+ if (!writer)
+ return E_OUTOFMEMORY;
+
+ memset(writer, 0, sizeof(*writer));
writer->IXmlWriter_iface.lpVtbl = &xmlwriter_vtbl;
writer->ref = 1;
writer->imalloc = imalloc;
if (imalloc) IMalloc_AddRef(imalloc);
- writer->output = NULL;
- writer->indent_level = 0;
- writer->indent = FALSE;
writer->bom = TRUE;
- writer->omitxmldecl = FALSE;
writer->conformance = XmlConformanceLevel_Document;
writer->state = XmlWriterState_Initial;
- writer->bomwritten = FALSE;
- writer->starttagopen = FALSE;
list_init(&writer->elements);
hr = IXmlWriter_QueryInterface(&writer->IXmlWriter_iface, riid, obj);
}
static HRESULT create_writer_output(IUnknown *stream, IMalloc *imalloc, xml_encoding encoding,
- const WCHAR *encoding_name, IXmlWriterOutput **output)
+ const WCHAR *encoding_name, xmlwriteroutput **out)
{
xmlwriteroutput *writeroutput;
HRESULT hr;
- *output = NULL;
+ *out = NULL;
if (imalloc)
writeroutput = IMalloc_Alloc(imalloc, sizeof(*writeroutput));
}
if (encoding_name) {
- unsigned int size = (strlenW(encoding_name) + 1) * sizeof(WCHAR);
+ unsigned int size = (lstrlenW(encoding_name) + 1) * sizeof(WCHAR);
writeroutput->encoding_name = writeroutput_alloc(writeroutput, size);
memcpy(writeroutput->encoding_name, encoding_name, size);
}
else
writeroutput->encoding_name = NULL;
+ writeroutput->written = 0;
IUnknown_QueryInterface(stream, &IID_IUnknown, (void**)&writeroutput->output);
- *output = &writeroutput->IXmlWriterOutput_iface;
+ *out = writeroutput;
- TRACE("returning iface %p\n", *output);
+ TRACE("Created writer output %p\n", *out);
return S_OK;
}
-HRESULT WINAPI CreateXmlWriterOutputWithEncodingName(IUnknown *stream,
- IMalloc *imalloc,
- LPCWSTR encoding,
- IXmlWriterOutput **output)
+HRESULT WINAPI CreateXmlWriterOutputWithEncodingName(IUnknown *stream, IMalloc *imalloc, const WCHAR *encoding,
+ IXmlWriterOutput **out)
{
- static const WCHAR utf8W[] = {'U','T','F','-','8',0};
+ xmlwriteroutput *output;
xml_encoding xml_enc;
+ HRESULT hr;
+
+ TRACE("%p %p %s %p\n", stream, imalloc, debugstr_w(encoding), out);
+
+ if (!stream || !out)
+ return E_INVALIDARG;
- TRACE("%p %p %s %p\n", stream, imalloc, debugstr_w(encoding), output);
+ *out = NULL;
- if (!stream || !output) return E_INVALIDARG;
+ xml_enc = encoding ? parse_encoding_name(encoding, -1) : XmlEncoding_UTF8;
+ if (SUCCEEDED(hr = create_writer_output(stream, imalloc, xml_enc, encoding, &output)))
+ *out = &output->IXmlWriterOutput_iface;
- xml_enc = parse_encoding_name(encoding ? encoding : utf8W, -1);
- return create_writer_output(stream, imalloc, xml_enc, encoding, output);
+ return hr;
}
-HRESULT WINAPI CreateXmlWriterOutputWithEncodingCodePage(IUnknown *stream,
- IMalloc *imalloc,
- UINT codepage,
- IXmlWriterOutput **output)
+HRESULT WINAPI CreateXmlWriterOutputWithEncodingCodePage(IUnknown *stream, IMalloc *imalloc, UINT codepage,
+ IXmlWriterOutput **out)
{
+ xmlwriteroutput *output;
xml_encoding xml_enc;
+ HRESULT hr;
- TRACE("%p %p %u %p\n", stream, imalloc, codepage, output);
+ TRACE("%p %p %u %p\n", stream, imalloc, codepage, out);
- if (!stream || !output) return E_INVALIDARG;
+ if (!stream || !out)
+ return E_INVALIDARG;
+
+ *out = NULL;
xml_enc = get_encoding_from_codepage(codepage);
- return create_writer_output(stream, imalloc, xml_enc, NULL, output);
+ if (SUCCEEDED(hr = create_writer_output(stream, imalloc, xml_enc, NULL, &output)))
+ *out = &output->IXmlWriterOutput_iface;
+
+ return hr;
}