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 class Importer; 39 40 // -------------------------------------------------------- // 41 // class XML 42 // -------------------------------------------------------- // 43 44 class XML { 45 // internal import helper 46 friend class Importer; 47 48 public: // *** types 49 class DocumentType; 50 51 public: // *** document type operations 52 53 // takes responsibility for the given type object 54 static void AddDocumentType( 55 XML::DocumentType* type); 56 57 public: // *** import/export operations 58 59 // identify object in stream. 60 // returns: 61 // - B_OK on success, or 62 // - B_BAD_TYPE if no document type matches the root 63 // element of the stream, or 64 // - B_IO_ERROR if the document is malformed, or if a 65 // read error occurs. 66 67 static status_t Identify( 68 BDataIO* stream, 69 DocumentType** outType, 70 std::list<BString>* outErrors); 71 72 // create & populate the root object from the given 73 // XML stream. 74 // returns: 75 // - B_OK on success, or 76 // - B_IO_ERROR if the document is malformed, or if a 77 // read error occurs, or 78 // - B_ERROR 79 80 static status_t Read( 81 BDataIO* stream, 82 IPersistent** outObject, 83 std::list<BString>* outErrors); 84 85 static status_t Read( 86 BDataIO* stream, 87 IPersistent** outObject, 88 ImportContext* context); //nyi 89 90 // [e.moon 26nov99] 91 // populate the provided root object from the given 92 // XML stream. you need to provide a document type 93 // that corresponds to the given object. 94 // returns: 95 // - B_OK on success, or 96 // - B_IO_ERROR if the document is malformed, or if a 97 // read error occurs, or 98 // - B_ERROR 99 100 static status_t Read( 101 BDataIO* stream, 102 IPersistent* rootObject, 103 XML::DocumentType* documentType, 104 std::list<BString>* outErrors); 105 106 static status_t Read( 107 BDataIO* stream, 108 IPersistent* rootObject, 109 XML::DocumentType* documentType, 110 ImportContext* context); 111 112 // create an ExportContext and use it to write the given object 113 // to the given stream 114 115 static status_t Write( 116 BDataIO* stream, 117 IPersistent* object, 118 BString* outError); 119 120 private: // static members 121 122 typedef std::map<BString, DocumentType*> doc_type_map; 123 124 static doc_type_map s_docTypeMap; 125 static BLocker s_docTypeLock; 126 127 private: // implementation 128 static status_t _DoRead( 129 BDataIO* stream, 130 Importer& i, 131 std::list<BString>* outErrors); //nyi 132 }; 133 134 // -------------------------------------------------------- // 135 // class XML::DocumentType 136 // -------------------------------------------------------- // 137 138 class XML::DocumentType { 139 public: // *** constant members 140 const BString rootElement; 141 const BMimeType mimeType; 142 143 static const BMimeType s_defaultMimeType; 144 145 public: // *** ctor/dtors 146 virtual ~DocumentType(); 147 148 DocumentType( 149 const char* _rootElement, 150 const char* _mimeType=0); 151 152 public: // *** 'factory' interface 153 154 // The DocumentType takes ownership of the given mapping 155 // object. If a mapping for the element already exists, 156 // the provided object is deleted and the method returns 157 // B_NAME_IN_USE. 158 virtual status_t addMapping( 159 XMLElementMapping* mapping); 160 161 // returns 0 if no mapping found for the given element 162 virtual IPersistent* objectFor( 163 const char* element); 164 165 private: // implementation 166 167 typedef std::set<XMLElementMapping*, _mapping_ptr_less> mapping_set; 168 mapping_set m_mappingSet; 169 }; 170 171 __END_CORTEX_NAMESPACE 172 173 #endif /*__XML_H__*/ 174