2 * Copyright (c) 1999-2000, Eric Moon.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer.
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.
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.
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.
35 #include "FlatMessageIO.h"
36 //#include "xml_export_utils.h"
38 #include "ExportContext.h"
42 // base64 encoding tools
48 __USE_CORTEX_NAMESPACE
50 // -------------------------------------------------------- //
52 // -------------------------------------------------------- //
54 const char* const FlatMessageIO::s_element
= "flat-BMessage";
56 // -------------------------------------------------------- //
57 // *** ctor/dtor/accessor
58 // -------------------------------------------------------- //
60 FlatMessageIO::~FlatMessageIO() {
61 if(m_ownMessage
&& m_message
)
64 FlatMessageIO::FlatMessageIO() :
67 FlatMessageIO::FlatMessageIO(const BMessage
* message
) :
69 m_message(const_cast<BMessage
*>(message
)) {}
71 void FlatMessageIO::setMessage(BMessage
* message
) {
72 if(m_ownMessage
&& m_message
)
78 // -------------------------------------------------------- //
79 // *** static setup method
80 // -------------------------------------------------------- //
82 // call this method to install hooks for the tags needed by
83 // FlatMessageIO into the given document type
85 void FlatMessageIO::AddTo(XML::DocumentType
* pDocType
) {
86 pDocType
->addMapping(new Mapping
<FlatMessageIO
>(s_element
));
89 // -------------------------------------------------------- //
90 // *** IPersistent impl.
91 // -------------------------------------------------------- //
94 void FlatMessageIO::xmlExportBegin(
95 ExportContext
& context
) const {
97 context
.beginElement(s_element
);
100 void FlatMessageIO::xmlExportAttributes(
101 ExportContext
& context
) const {
103 context
.writeAttr("encoding", "base64");
106 void FlatMessageIO::xmlExportContent(
107 ExportContext
& context
) const {
109 context
.beginContent();
111 // convert message to base64
113 ssize_t flatSize
= m_message
->FlattenedSize();
115 char* flatData
= new char[flatSize
];
116 status_t err
= m_message
->Flatten(flatData
, flatSize
);
119 // make plenty of room for encoded content (encode_base64 adds newlines)
120 ssize_t base64Size
= ((flatSize
* 3) / 2);
121 char* base64Data
= new char[base64Size
];
122 ssize_t base64Used
= encode_base64(base64Data
, flatData
, flatSize
);
123 base64Data
[base64Used
] = '\0';
127 const char* pos
= base64Data
;
130 const char* nextBreak
= strchr(pos
, '\n');
134 chunk
= nextBreak
- pos
;
136 context
.writeString(context
.indentString());
137 context
.writeString(pos
, chunk
);
138 context
.writeString("\n");
147 delete [] base64Data
;
150 void FlatMessageIO::xmlExportEnd(
151 ExportContext
& context
) const {
152 context
.endElement();
155 // -------------------------------------------------------- //
157 void FlatMessageIO::xmlImportBegin(
158 ImportContext
& context
) {
163 void FlatMessageIO::xmlImportAttribute(
166 ImportContext
& context
) {
167 if(!strcmp(key
, "encoding")) {
168 if(strcmp(value
, "base64") != 0)
169 context
.reportError("Unexpected value of 'encoding'.");
173 void FlatMessageIO::xmlImportContent(
176 ImportContext
& context
) {
177 m_base64Data
.Append(data
, length
);
180 void FlatMessageIO::xmlImportChild(
182 ImportContext
& context
) {
184 context
.reportError("Unexpected child object.");
187 void FlatMessageIO::xmlImportComplete(
188 ImportContext
& context
) {
190 if(m_ownMessage
&& m_message
)
193 // decode the message
194 ssize_t decodedSize
= m_base64Data
.Length();
195 char* decodedData
= new char[decodedSize
];
196 decodedSize
= decode_base64(
197 decodedData
, const_cast<char*>(m_base64Data
.String()),
198 m_base64Data
.Length(), false);
200 m_message
= new BMessage();
203 status_t err
= m_message
->Unflatten(decodedData
);
205 // decode failed; report error & clean up
206 BString error
= "Unflatten(): ";
207 error
<< strerror(err
);
208 context
.reportError(error
.String());
215 delete [] decodedData
;
218 // END -- FlatMessageIO.cpp --