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