xref: /haiku/src/apps/cortex/Persistence/Wrappers/MessageIO.h (revision 19ae20e67e91fc09cc9fc5c0e60e21e24e7a53eb)
1*c284bb0fSMatt Madia /*
2*c284bb0fSMatt Madia  * Copyright (c) 1999-2000, Eric Moon.
3*c284bb0fSMatt Madia  * All rights reserved.
4*c284bb0fSMatt Madia  *
5*c284bb0fSMatt Madia  * Redistribution and use in source and binary forms, with or without
6*c284bb0fSMatt Madia  * modification, are permitted provided that the following conditions
7*c284bb0fSMatt Madia  * are met:
8*c284bb0fSMatt Madia  *
9*c284bb0fSMatt Madia  * 1. Redistributions of source code must retain the above copyright
10*c284bb0fSMatt Madia  *    notice, this list of conditions, and the following disclaimer.
11*c284bb0fSMatt Madia  *
12*c284bb0fSMatt Madia  * 2. Redistributions in binary form must reproduce the above copyright
13*c284bb0fSMatt Madia  *    notice, this list of conditions, and the following disclaimer in the
14*c284bb0fSMatt Madia  *    documentation and/or other materials provided with the distribution.
15*c284bb0fSMatt Madia  *
16*c284bb0fSMatt Madia  * 3. The name of the author may not be used to endorse or promote products
17*c284bb0fSMatt Madia  *    derived from this software without specific prior written permission.
18*c284bb0fSMatt Madia  *
19*c284bb0fSMatt Madia  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20*c284bb0fSMatt Madia  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21*c284bb0fSMatt Madia  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*c284bb0fSMatt Madia  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23*c284bb0fSMatt Madia  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24*c284bb0fSMatt Madia  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*c284bb0fSMatt Madia  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26*c284bb0fSMatt Madia  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27*c284bb0fSMatt Madia  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*c284bb0fSMatt Madia  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*c284bb0fSMatt Madia  */
30*c284bb0fSMatt Madia 
31*c284bb0fSMatt Madia 
32a0795c6fSMarcus Overhagen // MessageIO.h
33a0795c6fSMarcus Overhagen // * PURPOSE
34a0795c6fSMarcus Overhagen //   Export/import of BMessages to and from
35a0795c6fSMarcus Overhagen //   XML using the Cortex persistence library.
36a0795c6fSMarcus Overhagen //   Messages are stored in a user-readable form.
37a0795c6fSMarcus Overhagen //
38a0795c6fSMarcus Overhagen //   TO DO +++++
39a0795c6fSMarcus Overhagen //   - sanity-check string values (filter/escape single quotes)
40a0795c6fSMarcus Overhagen //
41a0795c6fSMarcus Overhagen // * HISTORY
42a0795c6fSMarcus Overhagen //   e.moon		1dec99		Begun.
43a0795c6fSMarcus Overhagen 
44a0795c6fSMarcus Overhagen #ifndef __MessageIO_H__
45a0795c6fSMarcus Overhagen #define __MessageIO_H__
46a0795c6fSMarcus Overhagen 
47a0795c6fSMarcus Overhagen #include <Message.h>
48a0795c6fSMarcus Overhagen #include <String.h>
49a0795c6fSMarcus Overhagen #include "XML.h"
50a0795c6fSMarcus Overhagen 
51a0795c6fSMarcus Overhagen #include "cortex_defs.h"
52a0795c6fSMarcus Overhagen __BEGIN_CORTEX_NAMESPACE
53a0795c6fSMarcus Overhagen 
54a0795c6fSMarcus Overhagen class MessageIO :
55a0795c6fSMarcus Overhagen 	public		IPersistent {
56a0795c6fSMarcus Overhagen 
57a0795c6fSMarcus Overhagen public:												// *** ctor/dtor/accessor
58a0795c6fSMarcus Overhagen 	virtual ~MessageIO();
59a0795c6fSMarcus Overhagen 
60a0795c6fSMarcus Overhagen 	MessageIO(); //nyi
61a0795c6fSMarcus Overhagen 
62a0795c6fSMarcus Overhagen 	// When given a message to export, this object does NOT take
63a0795c6fSMarcus Overhagen 	// responsibility for deleting it.  It will, however, handle
64a0795c6fSMarcus Overhagen 	// deletion of an imported BMessage.
65a0795c6fSMarcus Overhagen 
66a0795c6fSMarcus Overhagen 	MessageIO(
67a0795c6fSMarcus Overhagen 		const BMessage*						message);
68a0795c6fSMarcus Overhagen 	void setMessage(
69a0795c6fSMarcus Overhagen 		BMessage*									message);
70a0795c6fSMarcus Overhagen 
71a0795c6fSMarcus Overhagen 	// Returns 0 if no message has been set, and if no message has
72a0795c6fSMarcus Overhagen 	// been imported.
73a0795c6fSMarcus Overhagen 
message()74a0795c6fSMarcus Overhagen 	const BMessage* message() const { return m_message; }
75a0795c6fSMarcus Overhagen 
76a0795c6fSMarcus Overhagen 	// Returns true if the message will be automatically deleted.
77a0795c6fSMarcus Overhagen 
ownsMessage()78a0795c6fSMarcus Overhagen 	bool ownsMessage() const { return m_ownMessage; }
79a0795c6fSMarcus Overhagen 
80a0795c6fSMarcus Overhagen public:												// *** static setup method
81a0795c6fSMarcus Overhagen 	// call this method to install hooks for the tags needed by
82a0795c6fSMarcus Overhagen 	// MessageIO into the given document type
83a0795c6fSMarcus Overhagen 	static void AddTo(
84a0795c6fSMarcus Overhagen 		XML::DocumentType*				docType);
85a0795c6fSMarcus Overhagen 
86a0795c6fSMarcus Overhagen public:												// *** XML formatting
87a0795c6fSMarcus Overhagen 	static const char* const		s_element;
88a0795c6fSMarcus Overhagen 
89a0795c6fSMarcus Overhagen public:												// *** IPersistent impl.
90a0795c6fSMarcus Overhagen 
91a0795c6fSMarcus Overhagen 	// EXPORT:
92a0795c6fSMarcus Overhagen 
93a0795c6fSMarcus Overhagen 	void xmlExportBegin(
94a0795c6fSMarcus Overhagen 		ExportContext&						context) const;
95a0795c6fSMarcus Overhagen 
96a0795c6fSMarcus Overhagen 	void xmlExportAttributes(
97a0795c6fSMarcus Overhagen 		ExportContext&						context) const;
98a0795c6fSMarcus Overhagen 
99a0795c6fSMarcus Overhagen 	void xmlExportContent(
100a0795c6fSMarcus Overhagen 		ExportContext&						context) const;
101a0795c6fSMarcus Overhagen 
102a0795c6fSMarcus Overhagen 	void xmlExportEnd(
103a0795c6fSMarcus Overhagen 		ExportContext&						context) const;
104a0795c6fSMarcus Overhagen 
105a0795c6fSMarcus Overhagen 
106a0795c6fSMarcus Overhagen 	// IMPORT:
107a0795c6fSMarcus Overhagen 
108a0795c6fSMarcus Overhagen 	virtual void xmlImportBegin(
109a0795c6fSMarcus Overhagen 		ImportContext&						context);
110a0795c6fSMarcus Overhagen 
111a0795c6fSMarcus Overhagen 	virtual void xmlImportAttribute(
112a0795c6fSMarcus Overhagen 		const char*								key,
113a0795c6fSMarcus Overhagen 		const char*								value,
114a0795c6fSMarcus Overhagen 		ImportContext&						context);
115a0795c6fSMarcus Overhagen 
116a0795c6fSMarcus Overhagen 	virtual void xmlImportContent(
117a0795c6fSMarcus Overhagen 		const char*								data,
118a0795c6fSMarcus Overhagen 		uint32										length,
119a0795c6fSMarcus Overhagen 		ImportContext&						context);
120a0795c6fSMarcus Overhagen 
121a0795c6fSMarcus Overhagen 	virtual void xmlImportChild(
122a0795c6fSMarcus Overhagen 		IPersistent*							child,
123a0795c6fSMarcus Overhagen 		ImportContext&						context);
124a0795c6fSMarcus Overhagen 
125a0795c6fSMarcus Overhagen 	virtual void xmlImportComplete(
126a0795c6fSMarcus Overhagen 		ImportContext&						context);
127a0795c6fSMarcus Overhagen 
128a0795c6fSMarcus Overhagen 	virtual void xmlImportChildBegin(
129a0795c6fSMarcus Overhagen 		const char*								name,
130a0795c6fSMarcus Overhagen 		ImportContext&						context);
131a0795c6fSMarcus Overhagen 
132a0795c6fSMarcus Overhagen 	virtual void xmlImportChildAttribute(
133a0795c6fSMarcus Overhagen 		const char*								key,
134a0795c6fSMarcus Overhagen 		const char*								value,
135a0795c6fSMarcus Overhagen 		ImportContext&						context);
136a0795c6fSMarcus Overhagen 
137a0795c6fSMarcus Overhagen 	virtual void xmlImportChildContent(
138a0795c6fSMarcus Overhagen 		const char*								data,
139a0795c6fSMarcus Overhagen 		uint32										length,
140a0795c6fSMarcus Overhagen 		ImportContext&						context);
141a0795c6fSMarcus Overhagen 
142a0795c6fSMarcus Overhagen 	virtual void xmlImportChildComplete(
143a0795c6fSMarcus Overhagen 		const char*								name,
144a0795c6fSMarcus Overhagen 		ImportContext&						context);
145a0795c6fSMarcus Overhagen 
146a0795c6fSMarcus Overhagen private:											// *** members
147a0795c6fSMarcus Overhagen 	bool												m_ownMessage;
148a0795c6fSMarcus Overhagen 	BMessage*										m_message;
149a0795c6fSMarcus Overhagen 
150a0795c6fSMarcus Overhagen 	// name of the message (if used to import a nested BMessage)
151a0795c6fSMarcus Overhagen 	BString											m_name;
152a0795c6fSMarcus Overhagen 
153a0795c6fSMarcus Overhagen 	// current field
154a0795c6fSMarcus Overhagen 	BString											m_fieldName;
155a0795c6fSMarcus Overhagen 	BString											m_fieldData;
156a0795c6fSMarcus Overhagen 
157a0795c6fSMarcus Overhagen 	bool _isValidMessageElement(
158a0795c6fSMarcus Overhagen 		const char*								element) const;
159a0795c6fSMarcus Overhagen 
160a0795c6fSMarcus Overhagen 	status_t _importField(
161a0795c6fSMarcus Overhagen 		BMessage*									message,
162a0795c6fSMarcus Overhagen 		const char*								element,
163a0795c6fSMarcus Overhagen 		const char*								name,
164a0795c6fSMarcus Overhagen 		const char*								data);
165a0795c6fSMarcus Overhagen 
166a0795c6fSMarcus Overhagen 	status_t _exportField(
167a0795c6fSMarcus Overhagen 		ExportContext&						context,
168a0795c6fSMarcus Overhagen 		BMessage*									message,
169a0795c6fSMarcus Overhagen 		type_code									type,
170a0795c6fSMarcus Overhagen 		const char*								name,
171a0795c6fSMarcus Overhagen 		int32											index) const;
172a0795c6fSMarcus Overhagen };
173a0795c6fSMarcus Overhagen 
174a0795c6fSMarcus Overhagen __END_CORTEX_NAMESPACE
175a0795c6fSMarcus Overhagen 
176a0795c6fSMarcus Overhagen #endif /*__MessageIO_H__*/
1771fb778c8SJérôme Duval 
178