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 // A central access point for Cortex's XML import/export
35 // services. A completely static class.
38 // - Maintain a set of XML::DocumentType objects, each
39 // containing the information needed to import a particular
40 // kind of XML document into native objects.
42 // - Provide a simple API for importing and exporting
43 // IPersistent objects from/to streams.
46 // e.moon 4oct99 API changes:
47 // - BDataIO used in place of standard streams.
48 // - Factory folded into XML::DocumentType
50 // e.moon 29jun99 Begun
55 #include "IPersistent.h"
56 #include "XMLElementMapping.h"
66 #include "cortex_defs.h"
67 __BEGIN_CORTEX_NAMESPACE
71 // -------------------------------------------------------- //
73 // -------------------------------------------------------- //
76 // internal import helper
77 friend class Importer
;
82 public: // *** document type operations
84 // takes responsibility for the given type object
85 static void AddDocumentType(
86 XML::DocumentType
* type
);
88 public: // *** import/export operations
90 // identify object in stream.
92 // - B_OK on success, or
93 // - B_BAD_TYPE if no document type matches the root
94 // element of the stream, or
95 // - B_IO_ERROR if the document is malformed, or if a
98 static status_t
Identify(
100 DocumentType
** outType
,
101 std::list
<BString
>* outErrors
);
103 // create & populate the root object from the given
106 // - B_OK on success, or
107 // - B_IO_ERROR if the document is malformed, or if a
108 // read error occurs, or
111 static status_t
Read(
113 IPersistent
** outObject
,
114 std::list
<BString
>* outErrors
);
116 static status_t
Read(
118 IPersistent
** outObject
,
119 ImportContext
* context
); //nyi
122 // populate the provided root object from the given
123 // XML stream. you need to provide a document type
124 // that corresponds to the given object.
126 // - B_OK on success, or
127 // - B_IO_ERROR if the document is malformed, or if a
128 // read error occurs, or
131 static status_t
Read(
133 IPersistent
* rootObject
,
134 XML::DocumentType
* documentType
,
135 std::list
<BString
>* outErrors
);
137 static status_t
Read(
139 IPersistent
* rootObject
,
140 XML::DocumentType
* documentType
,
141 ImportContext
* context
);
143 // create an ExportContext and use it to write the given object
144 // to the given stream
146 static status_t
Write(
151 private: // static members
153 typedef std::map
<BString
, DocumentType
*> doc_type_map
;
155 static doc_type_map s_docTypeMap
;
156 static BLocker s_docTypeLock
;
158 private: // implementation
159 static status_t
_DoRead(
162 std::list
<BString
>* outErrors
); //nyi
165 // -------------------------------------------------------- //
166 // class XML::DocumentType
167 // -------------------------------------------------------- //
169 class XML::DocumentType
{
170 public: // *** constant members
171 const BString rootElement
;
172 const BMimeType mimeType
;
174 static const BMimeType s_defaultMimeType
;
176 public: // *** ctor/dtors
177 virtual ~DocumentType();
180 const char* _rootElement
,
181 const char* _mimeType
=0);
183 public: // *** 'factory' interface
185 // The DocumentType takes ownership of the given mapping
186 // object. If a mapping for the element already exists,
187 // the provided object is deleted and the method returns
189 virtual status_t
addMapping(
190 XMLElementMapping
* mapping
);
192 // returns 0 if no mapping found for the given element
193 virtual IPersistent
* objectFor(
194 const char* element
);
196 private: // implementation
198 typedef std::set
<XMLElementMapping
*, _mapping_ptr_less
> mapping_set
;
199 mapping_set m_mappingSet
;
202 __END_CORTEX_NAMESPACE