xref: /haiku/src/apps/cortex/Persistence/Wrappers/MessageIO.h (revision a4ef4a49150f118d47324242917a596a3f8f8bd5)
1 // MessageIO.h
2 // * PURPOSE
3 //   Export/import of BMessages to and from
4 //   XML using the Cortex persistence library.
5 //   Messages are stored in a user-readable form.
6 //
7 //   TO DO +++++
8 //   - sanity-check string values (filter/escape single quotes)
9 //
10 // * HISTORY
11 //   e.moon		1dec99		Begun.
12 
13 #ifndef __MessageIO_H__
14 #define __MessageIO_H__
15 
16 #include <Message.h>
17 #include <String.h>
18 #include "XML.h"
19 
20 #include "cortex_defs.h"
21 __BEGIN_CORTEX_NAMESPACE
22 
23 class MessageIO :
24 	public		IPersistent {
25 
26 public:												// *** ctor/dtor/accessor
27 	virtual ~MessageIO();
28 
29 	MessageIO(); //nyi
30 
31 	// When given a message to export, this object does NOT take
32 	// responsibility for deleting it.  It will, however, handle
33 	// deletion of an imported BMessage.
34 
35 	MessageIO(
36 		const BMessage*						message);
37 	void setMessage(
38 		BMessage*									message);
39 
40 	// Returns 0 if no message has been set, and if no message has
41 	// been imported.
42 
43 	const BMessage* message() const { return m_message; }
44 
45 	// Returns true if the message will be automatically deleted.
46 
47 	bool ownsMessage() const { return m_ownMessage; }
48 
49 public:												// *** static setup method
50 	// call this method to install hooks for the tags needed by
51 	// MessageIO into the given document type
52 	static void AddTo(
53 		XML::DocumentType*				docType);
54 
55 public:												// *** XML formatting
56 	static const char* const		s_element;
57 
58 public:												// *** IPersistent impl.
59 
60 	// EXPORT:
61 
62 	void xmlExportBegin(
63 		ExportContext&						context) const;
64 
65 	void xmlExportAttributes(
66 		ExportContext&						context) const;
67 
68 	void xmlExportContent(
69 		ExportContext&						context) const;
70 
71 	void xmlExportEnd(
72 		ExportContext&						context) const;
73 
74 
75 	// IMPORT:
76 
77 	virtual void xmlImportBegin(
78 		ImportContext&						context);
79 
80 	virtual void xmlImportAttribute(
81 		const char*								key,
82 		const char*								value,
83 		ImportContext&						context);
84 
85 	virtual void xmlImportContent(
86 		const char*								data,
87 		uint32										length,
88 		ImportContext&						context);
89 
90 	virtual void xmlImportChild(
91 		IPersistent*							child,
92 		ImportContext&						context);
93 
94 	virtual void xmlImportComplete(
95 		ImportContext&						context);
96 
97 	virtual void xmlImportChildBegin(
98 		const char*								name,
99 		ImportContext&						context);
100 
101 	virtual void xmlImportChildAttribute(
102 		const char*								key,
103 		const char*								value,
104 		ImportContext&						context);
105 
106 	virtual void xmlImportChildContent(
107 		const char*								data,
108 		uint32										length,
109 		ImportContext&						context);
110 
111 	virtual void xmlImportChildComplete(
112 		const char*								name,
113 		ImportContext&						context);
114 
115 private:											// *** members
116 	bool												m_ownMessage;
117 	BMessage*										m_message;
118 
119 	// name of the message (if used to import a nested BMessage)
120 	BString											m_name;
121 
122 	// current field
123 	BString											m_fieldName;
124 	BString											m_fieldData;
125 
126 	bool _isValidMessageElement(
127 		const char*								element) const;
128 
129 	status_t _importField(
130 		BMessage*									message,
131 		const char*								element,
132 		const char*								name,
133 		const char*								data);
134 
135 	status_t _exportField(
136 		ExportContext&						context,
137 		BMessage*									message,
138 		type_code									type,
139 		const char*								name,
140 		int32											index) const;
141 };
142 
143 __END_CORTEX_NAMESPACE
144 
145 #endif /*__MessageIO_H__*/
146 
147