BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / cortex / Persistence / XML.h
blobb83f8a750fa87c0a7b1680585231a0d54d022193
1 /*
2 * Copyright (c) 1999-2000, Eric Moon.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
32 // XML.h
33 // * PURPOSE
34 // A central access point for Cortex's XML import/export
35 // services. A completely static class.
37 // * RESPONSIBILITIES
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.
45 // * HISTORY
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
52 #ifndef __XML_H__
53 #define __XML_H__
55 #include "IPersistent.h"
56 #include "XMLElementMapping.h"
58 #include <map>
59 #include <set>
61 #include <DataIO.h>
62 #include <Locker.h>
63 #include <Mime.h>
64 #include <String.h>
66 #include "cortex_defs.h"
67 __BEGIN_CORTEX_NAMESPACE
69 class Importer;
71 // -------------------------------------------------------- //
72 // class XML
73 // -------------------------------------------------------- //
75 class XML {
76 // internal import helper
77 friend class Importer;
79 public: // *** types
80 class DocumentType;
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.
91 // returns:
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
96 // read error occurs.
98 static status_t Identify(
99 BDataIO* stream,
100 DocumentType** outType,
101 std::list<BString>* outErrors);
103 // create & populate the root object from the given
104 // XML stream.
105 // returns:
106 // - B_OK on success, or
107 // - B_IO_ERROR if the document is malformed, or if a
108 // read error occurs, or
109 // - B_ERROR
111 static status_t Read(
112 BDataIO* stream,
113 IPersistent** outObject,
114 std::list<BString>* outErrors);
116 static status_t Read(
117 BDataIO* stream,
118 IPersistent** outObject,
119 ImportContext* context); //nyi
121 // [e.moon 26nov99]
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.
125 // returns:
126 // - B_OK on success, or
127 // - B_IO_ERROR if the document is malformed, or if a
128 // read error occurs, or
129 // - B_ERROR
131 static status_t Read(
132 BDataIO* stream,
133 IPersistent* rootObject,
134 XML::DocumentType* documentType,
135 std::list<BString>* outErrors);
137 static status_t Read(
138 BDataIO* stream,
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(
147 BDataIO* stream,
148 IPersistent* object,
149 BString* outError);
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(
160 BDataIO* stream,
161 Importer& i,
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();
179 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
188 // B_NAME_IN_USE.
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
204 #endif /*__XML_H__*/