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// MessageIO.h
33// * PURPOSE
34//   Export/import of BMessages to and from
35//   XML using the Cortex persistence library.
36//   Messages are stored in a user-readable form.
37//
38//   TO DO +++++
39//   - sanity-check string values (filter/escape single quotes)
40//
41// * HISTORY
42//   e.moon		1dec99		Begun.
43
44#ifndef __MessageIO_H__
45#define __MessageIO_H__
46
47#include <Message.h>
48#include <String.h>
49#include "XML.h"
50
51#include "cortex_defs.h"
52__BEGIN_CORTEX_NAMESPACE
53
54class MessageIO :
55	public		IPersistent {
56
57public:												// *** ctor/dtor/accessor
58	virtual ~MessageIO();
59
60	MessageIO(); //nyi
61
62	// When given a message to export, this object does NOT take
63	// responsibility for deleting it.  It will, however, handle
64	// deletion of an imported BMessage.
65
66	MessageIO(
67		const BMessage*						message);
68	void setMessage(
69		BMessage*									message);
70
71	// Returns 0 if no message has been set, and if no message has
72	// been imported.
73
74	const BMessage* message() const { return m_message; }
75
76	// Returns true if the message will be automatically deleted.
77
78	bool ownsMessage() const { return m_ownMessage; }
79
80public:												// *** static setup method
81	// call this method to install hooks for the tags needed by
82	// MessageIO into the given document type
83	static void AddTo(
84		XML::DocumentType*				docType);
85
86public:												// *** XML formatting
87	static const char* const		s_element;
88
89public:												// *** IPersistent impl.
90
91	// EXPORT:
92
93	void xmlExportBegin(
94		ExportContext&						context) const;
95
96	void xmlExportAttributes(
97		ExportContext&						context) const;
98
99	void xmlExportContent(
100		ExportContext&						context) const;
101
102	void xmlExportEnd(
103		ExportContext&						context) const;
104
105
106	// IMPORT:
107
108	virtual void xmlImportBegin(
109		ImportContext&						context);
110
111	virtual void xmlImportAttribute(
112		const char*								key,
113		const char*								value,
114		ImportContext&						context);
115
116	virtual void xmlImportContent(
117		const char*								data,
118		uint32										length,
119		ImportContext&						context);
120
121	virtual void xmlImportChild(
122		IPersistent*							child,
123		ImportContext&						context);
124
125	virtual void xmlImportComplete(
126		ImportContext&						context);
127
128	virtual void xmlImportChildBegin(
129		const char*								name,
130		ImportContext&						context);
131
132	virtual void xmlImportChildAttribute(
133		const char*								key,
134		const char*								value,
135		ImportContext&						context);
136
137	virtual void xmlImportChildContent(
138		const char*								data,
139		uint32										length,
140		ImportContext&						context);
141
142	virtual void xmlImportChildComplete(
143		const char*								name,
144		ImportContext&						context);
145
146private:											// *** members
147	bool												m_ownMessage;
148	BMessage*										m_message;
149
150	// name of the message (if used to import a nested BMessage)
151	BString											m_name;
152
153	// current field
154	BString											m_fieldName;
155	BString											m_fieldData;
156
157	bool _isValidMessageElement(
158		const char*								element) const;
159
160	status_t _importField(
161		BMessage*									message,
162		const char*								element,
163		const char*								name,
164		const char*								data);
165
166	status_t _exportField(
167		ExportContext&						context,
168		BMessage*									message,
169		type_code									type,
170		const char*								name,
171		int32											index) const;
172};
173
174__END_CORTEX_NAMESPACE
175
176#endif /*__MessageIO_H__*/
177
178