1/*
2 * Copyright (c) 1999-2000, Eric Moon.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions, and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions, and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * 3. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
32// FlatMessageIO.h
33// * PURPOSE
34//   Efficient export/import of BMessages to and from
35//   XML using the Cortex persistence library.
36//   Messages are stored in flattened form.
37// * HISTORY
38//   e.moon		6jul99		Begun.
39//
40// Example:
41//
42// <flat-BMessage
43//   encoding = 'base64'>
44//   nKJNFBlkn3lknbxlkfnbLKN/lknlknlDSLKn3lkn3l2k35234ljk234
45//   lkdsg23823nlknsdlkbNDSLKBNlkn3lk23n4kl23n423lknLKENL+==
46// </flat-BMessage>
47
48#ifndef __FlatMessageIO_H__
49#define __FlatMessageIO_H__
50
51#include <Message.h>
52#include <String.h>
53#include "XML.h"
54
55#include "cortex_defs.h"
56__BEGIN_CORTEX_NAMESPACE
57
58class FlatMessageIO :
59	public		IPersistent {
60
61public:					// *** ctor/dtor/accessor
62	virtual ~FlatMessageIO();
63
64	FlatMessageIO();
65
66	// When given a message to export, this object does NOT take
67	// responsibility for deleting it.  It will, however, handle
68	// deletion of an imported BMessage.
69
70	FlatMessageIO(const BMessage* message);
71	void setMessage(BMessage* message);
72
73	// Returns 0 if no message has been set, and if no message has
74	// been imported.
75
76	const BMessage* message() const { return m_message; }
77
78	// Returns true if the message will be automatically deleted.
79
80	bool ownsMessage() const { return m_ownMessage; }
81
82public:					// *** static setup method
83	// call this method to install hooks for the tags needed by
84	// FlatMessageIO into the given document type
85	static void AddTo(XML::DocumentType* pDocType);
86
87public:					// *** XML formatting
88	static const char* const			s_element;
89	static const uint16					s_encodeToMax			= 72;
90	static const uint16					s_encodeToMin			= 24;
91
92public:					// *** IPersistent impl.
93
94//	virtual void xmlExport(
95//		ostream& 					stream,
96//		ExportContext&		context) const;
97
98	// EXPORT:
99
100	void xmlExportBegin(
101		ExportContext& context) const;
102
103	void xmlExportAttributes(
104		ExportContext& context) const;
105
106	void xmlExportContent(
107		ExportContext& context) const;
108
109	void xmlExportEnd(
110		ExportContext& context) const;
111
112
113	// IMPORT:
114
115	virtual void xmlImportBegin(
116		ImportContext&		context);
117
118	virtual void xmlImportAttribute(
119		const char*					key,
120		const char*					value,
121		ImportContext&		context);
122
123	virtual void xmlImportContent(
124		const char*					data,
125		uint32						length,
126		ImportContext&		context);
127
128	virtual void xmlImportChild(
129		IPersistent*			child,
130		ImportContext&		context);
131
132	virtual void xmlImportComplete(
133		ImportContext&		context);
134
135private:					// *** members
136	bool						m_ownMessage;
137	BMessage*			m_message;
138
139	// encoded data is cached here during the import process
140	BString				m_base64Data;
141};
142
143__END_CORTEX_NAMESPACE
144
145#endif /*__FlatMessageIO_H__*/
146