1 // XML.h 2 // * PURPOSE 3 // A central access point for Cortex's XML import/export 4 // services. A completely static class. 5 // 6 // * RESPONSIBILITIES 7 // - Maintain a set of XML::DocumentType objects, each 8 // containing the information needed to import a particular 9 // kind of XML document into native objects. 10 // 11 // - Provide a simple API for importing and exporting 12 // IPersistent objects from/to streams. 13 // 14 // * HISTORY 15 // e.moon 4oct99 API changes: 16 // - BDataIO used in place of standard streams. 17 // - Factory folded into XML::DocumentType 18 // 19 // e.moon 29jun99 Begun 20 21 #ifndef __XML_H__ 22 #define __XML_H__ 23 24 #include "IPersistent.h" 25 #include "XMLElementMapping.h" 26 27 #include <map> 28 #include <set> 29 30 #include <DataIO.h> 31 #include <Locker.h> 32 #include <Mime.h> 33 #include <String.h> 34 35 #include "cortex_defs.h" 36 __BEGIN_CORTEX_NAMESPACE 37 38 // -------------------------------------------------------- // 39 // class XML 40 // -------------------------------------------------------- // 41 42 class XML { 43 // internal import helper 44 friend class Importer; 45 46 public: // *** types 47 class DocumentType; 48 49 public: // *** document type operations 50 51 // takes responsibility for the given type object 52 static void AddDocumentType( 53 XML::DocumentType* type); 54 55 public: // *** import/export operations 56 57 // identify object in stream. 58 // returns: 59 // - B_OK on success, or 60 // - B_BAD_TYPE if no document type matches the root 61 // element of the stream, or 62 // - B_IO_ERROR if the document is malformed, or if a 63 // read error occurs. 64 65 static status_t Identify( 66 BDataIO* stream, 67 DocumentType** outType, 68 list<BString>* outErrors); 69 70 // create & populate the root object from the given 71 // XML stream. 72 // returns: 73 // - B_OK on success, or 74 // - B_IO_ERROR if the document is malformed, or if a 75 // read error occurs, or 76 // - B_ERROR 77 78 static status_t Read( 79 BDataIO* stream, 80 IPersistent** outObject, 81 list<BString>* outErrors); 82 83 static status_t Read( 84 BDataIO* stream, 85 IPersistent** outObject, 86 ImportContext* context); //nyi 87 88 // [e.moon 26nov99] 89 // populate the provided root object from the given 90 // XML stream. you need to provide a document type 91 // that corresponds to the given object. 92 // returns: 93 // - B_OK on success, or 94 // - B_IO_ERROR if the document is malformed, or if a 95 // read error occurs, or 96 // - B_ERROR 97 98 static status_t Read( 99 BDataIO* stream, 100 IPersistent* rootObject, 101 XML::DocumentType* documentType, 102 list<BString>* outErrors); 103 104 static status_t Read( 105 BDataIO* stream, 106 IPersistent* rootObject, 107 XML::DocumentType* documentType, 108 ImportContext* context); 109 110 // create an ExportContext and use it to write the given object 111 // to the given stream 112 113 static status_t Write( 114 BDataIO* stream, 115 IPersistent* object, 116 BString* outError); 117 118 private: // static members 119 120 typedef map<BString, DocumentType*> doc_type_map; 121 122 static doc_type_map s_docTypeMap; 123 static BLocker s_docTypeLock; 124 125 private: // implementation 126 static status_t _DoRead( 127 BDataIO* stream, 128 Importer& i, 129 list<BString>* outErrors); //nyi 130 }; 131 132 // -------------------------------------------------------- // 133 // class XML::DocumentType 134 // -------------------------------------------------------- // 135 136 class XML::DocumentType { 137 public: // *** constant members 138 const BString rootElement; 139 const BMimeType mimeType; 140 141 static const BMimeType s_defaultMimeType; 142 143 public: // *** ctor/dtors 144 virtual ~DocumentType(); 145 146 DocumentType( 147 const char* _rootElement, 148 const char* _mimeType=0); 149 150 public: // *** 'factory' interface 151 152 // The DocumentType takes ownership of the given mapping 153 // object. If a mapping for the element already exists, 154 // the provided object is deleted and the method returns 155 // B_NAME_IN_USE. 156 virtual status_t addMapping( 157 XMLElementMapping* mapping); 158 159 // returns 0 if no mapping found for the given element 160 virtual IPersistent* objectFor( 161 const char* element); 162 163 private: // implementation 164 165 typedef set<XMLElementMapping*, _mapping_ptr_less> mapping_set; 166 mapping_set m_mappingSet; 167 }; 168 169 __END_CORTEX_NAMESPACE 170 171 #endif /*__XML_H__*/ 172