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.
34 // Describe the state of a serialization ('save') operation.
35 // The 'load' equivalent is ImportContext.
38 // e.moon 28jun99 Begun
40 #ifndef __ExportContext_H__
41 #define __ExportContext_H__
51 #include "cortex_defs.h"
52 __BEGIN_CORTEX_NAMESPACE
59 inline BString
& _pad_with_spaces(BString
& out
, const char* text
,
60 ExportContext
& context
, uint16 column
);
66 ExportContext(BDataIO
* stream
);
67 virtual ~ExportContext();
73 struct element_entry
{
74 element_entry() : hasAttributes(false), hasContent(false) {}
81 typedef std::list
<element_entry
> element_list
;
82 element_list m_elementStack
;
84 public: // *** XML formatting helpers
86 // writes a start tag. should be called from
87 // IPersistent::xmlExportBegin()
88 // (or xmlExportContent(), if you're writing nested elements)
90 void beginElement(const char* name
);
92 // writes an end tag corresponding to the current element.
93 // should only be called from IPersistent::xmlExportEnd() or
94 // xmlExportContent().
97 // indicates that content follows (writes the end of the
98 // current element's start tag.)
101 // // writes an attribute.
102 // // should only be called from IPersistent::xmlExportAttributes().
103 // template <class T>
108 // if(!m_objectStack.size()) {
109 // reportError("writeAttr(): no object being written.\n");
112 // ASSERT(m_elementStack.size());
113 // if(m_state != WRITE_ATTRIBUTES &&
114 // m_state != WRITE_CONTENT) {
115 // reportError("writeAttr(): not allowed (state mismatch).\n");
119 // m_elementStack.back().hasAttributes = true;
122 // out << "\n" << indentString() << key;
123 // _pad_with_spaces(out, key, *this, m_attrColumn) << " = '" << value << '\'';
129 // non-template forms of writeAttr()
169 const BString
& value
);
175 // writes a child object.
176 // should only be called from IPersistent::xmlExportContent().
177 // returns B_OK on success, or B_ERROR if an error occurred.
178 status_t
writeObject(
179 IPersistent
* object
);
181 // writes an arbitrary string to the stream (calls reportError()
183 status_t
writeString(
184 const BString
& string
);
186 status_t
writeString(
190 public: // *** indentation helpers
192 // return a string padded with spaces to the current
194 const char* indentString() const;
196 // return the current indent level
197 uint16
indentLevel() const;
199 // decrease the indent level
202 // increase the indent level
205 // +++++ extra formatting controls needed [e.moon 1dec99]
206 // * attrColumn access
207 // * single vs. multi-line element formatting
210 public: // *** error operations
212 // register a fatal error; halts the write process
213 // as soon as possible.
218 const char* errorText() const { return m_error
.String(); }
222 // * Indentation/formatting
224 uint16 m_indentLevel
;
225 uint16 m_indentIncrement
;
229 BString m_indentString
;
247 struct object_entry
{
248 object_entry() : element(0), object(0) {}
254 typedef std::list
<object_entry
> object_list
;
255 object_list m_objectStack
;
258 void _dumpElementStack(
262 // ExportContext::writeAttr() helper
263 inline BString
& _pad_with_spaces(
266 ExportContext
& context
,
269 int16 spaces
= column
- (strlen(text
) + context
.indentLevel());
270 if(spaces
< 0) spaces
= 0;
271 while(spaces
--) out
<< ' ';
275 __END_CORTEX_NAMESPACE
277 #endif /*__ExportContext_H__*/