From 3ac76fb2371bc386b29b3a72ad3f2740b3e1ed09 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sun, 13 Feb 2011 18:37:26 +0100 Subject: [PATCH] Add wocky_xmpp_writer_write_node() function. Allows serializing of a single node, and doesn't include the processing instruction. --- tests/wocky-xmpp-readwrite-test.c | 49 +++++++++++++++++++++++++++++++++++++ wocky/wocky-xmpp-writer.c | 47 +++++++++++++++++++++++++++++++++++ wocky/wocky-xmpp-writer.h | 5 ++++ 3 files changed, 101 insertions(+), 0 deletions(-) diff --git a/tests/wocky-xmpp-readwrite-test.c b/tests/wocky-xmpp-readwrite-test.c index 2a70ead..b5749d6 100644 --- a/tests/wocky-xmpp-readwrite-test.c +++ b/tests/wocky-xmpp-readwrite-test.c @@ -200,6 +200,53 @@ test_readwrite_nostream (void) g_object_unref (writer); } +static void +test_readwrite_nostream_node (void) +{ + WockyXmppReader *reader; + WockyXmppWriter *writer; + WockyNode *node; + WockyStanza *received = NULL, *sent; + const guint8 *data; + gsize length; + int i; + + writer = wocky_xmpp_writer_new_no_stream (); + reader = wocky_xmpp_reader_new_no_stream (); + + sent = create_stanza (); + + + for (i = 0 ; i < 3 ; i++) + { + g_assert (wocky_xmpp_reader_get_state (reader) + == WOCKY_XMPP_READER_STATE_OPENED); + + node = wocky_stanza_get_top_node (sent); + wocky_xmpp_writer_write_node (writer, node, &data, &length); + wocky_xmpp_reader_push (reader, data, length); + + g_assert (wocky_xmpp_reader_get_state (reader) + == WOCKY_XMPP_READER_STATE_OPENED); + + received = wocky_xmpp_reader_pop_stanza (reader); + + g_assert (received != NULL); + test_assert_stanzas_equal (sent, received); + + g_assert (wocky_xmpp_reader_get_state (reader) == + WOCKY_XMPP_READER_STATE_CLOSED); + + wocky_xmpp_reader_reset (reader); + + g_object_unref (received); + } + + g_object_unref (sent); + g_object_unref (reader); + g_object_unref (writer); +} + int main (int argc, char **argv) @@ -211,6 +258,8 @@ main (int argc, g_test_add_func ("/xmpp-readwrite/readwrite", test_readwrite); g_test_add_func ("/xmpp-readwrite/readwrite-nostream", test_readwrite_nostream); + g_test_add_func ("/xmpp-readwrite/readwrite-nostream-node", + test_readwrite_nostream_node); result = g_test_run (); test_deinit (); diff --git a/wocky/wocky-xmpp-writer.c b/wocky/wocky-xmpp-writer.c index 2bd2438..a4b2c5d 100644 --- a/wocky/wocky-xmpp-writer.c +++ b/wocky/wocky-xmpp-writer.c @@ -454,6 +454,28 @@ _write_node_tree (WockyXmppWriter *writer, #endif } +static void +_write_node (WockyXmppWriter *writer, + WockyNode *node, + const guint8 **data, + gsize *length) +{ + WockyXmppWriterPrivate *priv = writer->priv; + + xmlBufferEmpty (priv->buffer); + + _xml_write_node (writer, node); + + xmlTextWriterFlush (priv->xmlwriter); + + *data = (const guint8 *)priv->buffer->content; + *length = priv->buffer->use; + +#ifdef ENABLE_DEBUG + wocky_debug (DEBUG_NET, "Writing xml: %.*s", (int)*length, *data); +#endif +} + /** * wocky_xmpp_writer_write_stanza: * @writer: a WockyXmppWriter @@ -499,6 +521,31 @@ wocky_xmpp_writer_write_node_tree (WockyXmppWriter *writer, } /** + * wocky_xmpp_writer_write_node: + * @writer: a WockyXmppWriter + * @node: the node to serialize + * @data: location to store a pointer to the data buffer + * @length: length of the data buffer + * + * Serialize the @node to XML. The result is available in the + * @data buffer. The buffer is only valid until the next call to a function. + * This function may only be called in non-streaming mode. + */ +void +wocky_xmpp_writer_write_node (WockyXmppWriter *writer, + WockyNode *node, + const guint8 **data, + gsize *length) +{ + *data = NULL; + *length = 0; + + g_return_if_fail (!writer->priv->stream_mode); + + _write_node (writer, node, data, length); +} + +/** * wocky_xmpp_writer_flush: * @writer: a WockyXmppWriter * diff --git a/wocky/wocky-xmpp-writer.h b/wocky/wocky-xmpp-writer.h index 9f69b66..d99f7a8 100644 --- a/wocky/wocky-xmpp-writer.h +++ b/wocky/wocky-xmpp-writer.h @@ -88,6 +88,11 @@ void wocky_xmpp_writer_write_node_tree (WockyXmppWriter *writer, const guint8 **data, gsize *length); +void wocky_xmpp_writer_write_node (WockyXmppWriter *writer, + WockyNode *node, + const guint8 **data, + gsize *length); + void wocky_xmpp_writer_flush (WockyXmppWriter *writer); G_END_DECLS -- 1.7.1