xref: /haiku/src/apps/cortex/Persistence/Importer.h (revision 23e6780695c03d940ce1c75492185e30ea89bdc6)
1 // Importer.h
2 //
3 // * PURPOSE
4 //   Given a stream of XML parser events, produce the object[s]
5 //   represented by the markup.
6 //
7 // * HISTORY
8 //   e.moon		30jun99   Moved actual object-building responsibility
9 //                      to IPersistent; this class is now internal
10 //                      to Cortex::XML.
11 //   e.moon		28jun99		Begun.  [was 'Importer']
12 
13 #ifndef __Importer_H__
14 #define __Importer_H__
15 
16 #include <list>
17 #include <String.h>
18 
19 #include "ImportContext.h"
20 #include "XML.h"
21 #include "xmlparse.h"
22 
23 #include "cortex_defs.h"
24 __BEGIN_CORTEX_NAMESPACE
25 
26 class Importer {
27 
28 public:													// *** ctor/dtor
29 	virtual ~Importer();
30 
31 	// constructs a format-guessing Importer (uses the
32 	// DocumentType registry)
33 	Importer(
34 		std::list<BString>&						errors);
35 
36 	// the Importer takes ownership of the given context!
37 	Importer(
38 		ImportContext*							context);
39 
40 	// constructs a manual Importer; the given root
41 	// object is populated
42 	Importer(
43 		std::list<BString>&						errors,
44 		IPersistent*								rootObject,
45 		XML::DocumentType*					docType);
46 
47 	// the Importer takes ownership of the given context!
48 	Importer(
49 		ImportContext*							context,
50 		IPersistent*								rootObject,
51 		XML::DocumentType*					docType);
52 
53 public:													// *** accessors
54 
55 	// the import context
56 	const ImportContext& context() const;
57 
58 	// matched document type
59 	XML::DocumentType* docType() const;
60 
61 	// completed object (available if
62 	// context().state() == ImportContext::COMPLETE, or
63 	// if a root object was provided to the ctor)
64 	IPersistent* target() const;
65 
66 public:													// *** operations
67 
68 	// put the importer into 'identify mode'
69 	// (disengaged once the first element is encountered)
70 	void setIdentifyMode();
71 
72 	// prepare to read the document after an identify cycle
73 	void reset();
74 
75 	// handle a buffer; return false if an error occurs
76 	bool parseBuffer(
77 		const char*									buffer,
78 		uint32											length,
79 		bool												last);
80 
81 public:													// *** internal operations
82 
83 	// create & initialize parser
84 	void initParser();
85 
86 	// clean up the parser
87 	void freeParser();
88 
89 public:													// *** XML parser event hooks
90 
91 	virtual void xmlElementStart(
92 		const char*									name,
93 		const char**								attributes);
94 
95 	virtual void xmlElementEnd(
96 		const char*									name);
97 
98 	virtual void xmlProcessingInstruction(
99 		const char*									target,
100 		const char*									data);
101 
102 	// not 0-terminated
103 	virtual void xmlCharacterData(
104 		const char*									data,
105 		int32												length);
106 
107 	// not 0-terminated
108 	virtual void xmlDefaultData(
109 		const char*									data,
110 		int32												length);
111 
112 private:												// *** implementation
113 
114 	XML_Parser										m_parser;
115 	XML::DocumentType*						m_docType;
116 
117 	// if true, the importer is being used to identify the
118 	// document type -- it should halt as soon as the first
119 	// element is encountered.
120 	bool													m_identify;
121 
122 	ImportContext* const					m_context;
123 
124 	// the constructed object: if no rootObject was provided
125 	// in the ctor, this is only filled in once the document
126 	// end tag has been encountered.
127 	IPersistent*									m_rootObject;
128 };
129 
130 __END_CORTEX_NAMESPACE
131 #endif /*__Importer_H__*/
132