From 2177403b8ebefcf8641505f8946e429f9960cb6f Mon Sep 17 00:00:00 2001 From: Nikolay Sivov Date: Wed, 21 May 2014 00:24:49 +0400 Subject: [PATCH] xmllite/writer: Initial implementation of WriteStartElement(). --- dlls/xmllite/tests/writer.c | 94 ++++++++++++++++++++++++++++++++++++- dlls/xmllite/writer.c | 49 +++++++++++++++---- 2 files changed, 133 insertions(+), 10 deletions(-) diff --git a/dlls/xmllite/tests/writer.c b/dlls/xmllite/tests/writer.c index 6800cd236ae..7631b458684 100644 --- a/dlls/xmllite/tests/writer.c +++ b/dlls/xmllite/tests/writer.c @@ -410,6 +410,7 @@ static void test_bom(void) static const WCHAR versionW[] = {'v','e','r','s','i','o','n','=','"','1','.','0','"',0}; static const WCHAR utf16W[] = {'u','t','f','-','1','6',0}; static const WCHAR xmlW[] = {'x','m','l',0}; + static const WCHAR aW[] = {'a',0}; IXmlWriterOutput *output; unsigned char *ptr; IXmlWriter *writer; @@ -473,8 +474,98 @@ static void test_bom(void) GlobalUnlock(hglobal); IUnknown_Release(output); - IXmlWriter_Release(writer); IStream_Release(stream); + + /* start with element */ + hr = CreateStreamOnHGlobal(NULL, TRUE, &stream); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = pCreateXmlWriterOutputWithEncodingName((IUnknown*)stream, NULL, utf16W, &output); + ok(hr == S_OK, "got %08x\n", hr); + + hr = IXmlWriter_SetOutput(writer, output); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = IXmlWriter_Flush(writer); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = GetHGlobalFromStream(stream, &hglobal); + ok(hr == S_OK, "got 0x%08x\n", hr); + + ptr = GlobalLock(hglobal); + ok(ptr[0] == 0xff && ptr[1] == 0xfe, "got %x,%x\n", ptr[0], ptr[1]); + GlobalUnlock(hglobal); + + IUnknown_Release(output); + IStream_Release(stream); + + IXmlWriter_Release(writer); +} + +static void test_writestartelement(void) +{ + static const WCHAR aW[] = {'a',0}; + char *ptr; + IXmlWriter *writer; + IStream *stream; + HGLOBAL hglobal; + HRESULT hr; + + hr = CreateStreamOnHGlobal(NULL, TRUE, &stream); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL); + ok(hr == S_OK, "Expected S_OK, got %08x\n", hr); + + hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL); + ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr); + + hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteStartElement(writer, aW, NULL, NULL); + ok(hr == E_INVALIDARG, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteStartElement(writer, NULL, NULL, NULL); + ok(hr == E_INVALIDARG, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteStartElement(writer, NULL, NULL, aW); + ok(hr == E_INVALIDARG, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = GetHGlobalFromStream(stream, &hglobal); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes); + ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr); + + hr = IXmlWriter_Flush(writer); + ok(hr == S_OK, "got 0x%08x\n", hr); + + ptr = GlobalLock(hglobal); + ok(!strncmp(ptr, "(%s %s)\n", This, wine_dbgstr_w(name), wine_dbgstr_w(text)); - if (This->state == XmlWriterState_Initial) + switch (This->state) + { + case XmlWriterState_Initial: return E_UNEXPECTED; - - if (This->state == XmlWriterState_DocStarted && !strcmpW(name, xmlW)) + case XmlWriterState_DocStarted: + if (!strcmpW(name, xmlW)) + return WR_E_INVALIDACTION; + break; + case XmlWriterState_ElemStarted: return WR_E_INVALIDACTION; + default: + ; + } write_encoding_bom(This); write_output_buffer(This->output, openpiW, sizeof(openpiW)/sizeof(WCHAR)); @@ -656,6 +665,7 @@ static HRESULT WINAPI xmlwriter_WriteStartDocument(IXmlWriter *iface, XmlStandal This->state = XmlWriterState_DocStarted; return S_OK; case XmlWriterState_DocStarted: + case XmlWriterState_ElemStarted: return WR_E_INVALIDACTION; default: ; @@ -690,15 +700,36 @@ static HRESULT WINAPI xmlwriter_WriteStartDocument(IXmlWriter *iface, XmlStandal return S_OK; } -static HRESULT WINAPI xmlwriter_WriteStartElement(IXmlWriter *iface, LPCWSTR pwszPrefix, - LPCWSTR pwszLocalName, LPCWSTR pwszNamespaceUri) +static HRESULT WINAPI xmlwriter_WriteStartElement(IXmlWriter *iface, LPCWSTR prefix, LPCWSTR local_name, LPCWSTR uri) { xmlwriter *This = impl_from_IXmlWriter(iface); + static const WCHAR ltW[] = {'<'}; - FIXME("%p %s %s %s\n", This, wine_dbgstr_w(pwszPrefix), wine_dbgstr_w(pwszLocalName), - wine_dbgstr_w(pwszNamespaceUri)); + TRACE("(%p)->(%s %s %s)\n", This, wine_dbgstr_w(prefix), wine_dbgstr_w(local_name), wine_dbgstr_w(uri)); - return E_NOTIMPL; + if (This->state == XmlWriterState_Initial) + return E_UNEXPECTED; + + if (!local_name) + return E_INVALIDARG; + + if (This->state == XmlWriterState_ElemStarted) + return WR_E_INVALIDACTION; + + write_encoding_bom(This); + This->state = XmlWriterState_ElemStarted; + + write_output_buffer(This->output, ltW, 1); + + if (prefix) { + static const WCHAR colW[] = {':'}; + write_output_buffer(This->output, prefix, -1); + write_output_buffer(This->output, colW, 1); + } + + write_output_buffer(This->output, local_name, -1); + + return S_OK; } static HRESULT WINAPI xmlwriter_WriteString(IXmlWriter *iface, LPCWSTR pwszText)