xref: /haiku/src/apps/cortex/Persistence/ImportContext.cpp (revision 0e50eab75e25d0d82090e22dbff766dfaa6f5e86)
1 // ImportContext.cpp
2 // e.moon 1jul99
3 
4 #include "ImportContext.h"
5 #include "xmlparse.h"
6 
7 __USE_CORTEX_NAMESPACE
8 
9 // -------------------------------------------------------- //
10 // ctor/dtor
11 // -------------------------------------------------------- //
12 
13 ImportContext::~ImportContext() {}
14 ImportContext::ImportContext(list<BString>& errors) :
15 	m_state(PARSING),
16 	m_errors(errors),
17 	m_pParser(0) {}
18 
19 // -------------------------------------------------------- //
20 // accessors
21 // -------------------------------------------------------- //
22 
23 // fetch the current element (tag)
24 // (returns 0 if the stack is empty)
25 const char* ImportContext::element() const {
26 	return (m_elementStack.size()) ?
27 		m_elementStack.back().String() :
28 		0;
29 }
30 
31 const char* ImportContext::parentElement() const {
32 	if(m_elementStack.size() < 2)
33 		return 0;
34 	list<BString>::const_reverse_iterator it = m_elementStack.rbegin();
35 	++it;
36 	return (*it).String();
37 }
38 
39 list<BString>& ImportContext::errors() const {
40 	return m_errors;
41 }
42 const ImportContext::state_t ImportContext::state() const {
43 	return m_state;
44 }
45 
46 // -------------------------------------------------------- //
47 // error-reporting operations
48 // -------------------------------------------------------- //
49 
50 // register a warning to be returned once the deserialization
51 // process is complete.
52 void ImportContext::reportWarning(
53 	const char*			pText) {
54 
55 	XML_Parser p = (XML_Parser)m_pParser;
56 
57 	BString err = "Warning: ";
58 	err << pText;
59 	if(p) {
60 		err << "\n         (line " <<
61 			(uint32)XML_GetCurrentLineNumber(p) << ", column " <<
62 			(uint32)XML_GetCurrentColumnNumber(p) << ", element '" <<
63 			(element() ? element() : "(none)") << "')\n";
64 	} else
65 		err << "\n";
66 	m_errors.push_back(err);
67 }
68 
69 // register a fatal error; halts the deserialization process
70 // as soon as possible.
71 void ImportContext::reportError(
72 	const char*			pText) {
73 
74 	XML_Parser p = (XML_Parser)m_pParser;
75 
76 	BString err = "FATAL ERROR: ";
77 	err << pText;
78 	if(p) {
79 		err << "\n             (line " <<
80 			(uint32)XML_GetCurrentLineNumber(p) << ", column " <<
81 			(uint32)XML_GetCurrentColumnNumber(p) << ", element '" <<
82 			(element() ? element() : "(none)") << "')\n";
83 	} else
84 		err << "\n";
85 	m_errors.push_back(err);
86 
87 	m_state = ABORT;
88 }
89 
90 // -------------------------------------------------------- //
91 // internal operations
92 // -------------------------------------------------------- //
93 
94 void ImportContext::reset() {
95 	m_state = PARSING;
96 	m_elementStack.clear();
97 	// +++++ potential for memory leaks; reset() is currently
98 	//       only to be called after an identify cycle, during
99 	//       which no objects are created anyway, but this still
100 	//       gives me the shivers...
101 	m_objectStack.clear();
102 }
103 
104 // END -- ImportContext.cpp --
105