xref: /haiku/src/apps/cortex/Persistence/ImportContext.cpp (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 /*
2  * Copyright (c) 1999-2000, Eric Moon.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions, and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 // ImportContext.cpp
33 // e.moon 1jul99
34 
35 #include "ImportContext.h"
36 #include "expat.h"
37 
38 using namespace std;
39 
40 __USE_CORTEX_NAMESPACE
41 
42 // -------------------------------------------------------- //
43 // ctor/dtor
44 // -------------------------------------------------------- //
45 
46 ImportContext::~ImportContext() {}
47 ImportContext::ImportContext(list<BString>& errors) :
48 	m_state(PARSING),
49 	m_errors(errors),
50 	m_pParser(0) {}
51 
52 // -------------------------------------------------------- //
53 // accessors
54 // -------------------------------------------------------- //
55 
56 // fetch the current element (tag)
57 // (returns 0 if the stack is empty)
58 const char* ImportContext::element() const {
59 	return (m_elementStack.size()) ?
60 		m_elementStack.back().String() :
61 		0;
62 }
63 
64 const char* ImportContext::parentElement() const {
65 	if(m_elementStack.size() < 2)
66 		return 0;
67 	list<BString>::const_reverse_iterator it = m_elementStack.rbegin();
68 	++it;
69 	return (*it).String();
70 }
71 
72 list<BString>& ImportContext::errors() const {
73 	return m_errors;
74 }
75 const ImportContext::state_t ImportContext::state() const {
76 	return m_state;
77 }
78 
79 // -------------------------------------------------------- //
80 // error-reporting operations
81 // -------------------------------------------------------- //
82 
83 // register a warning to be returned once the deserialization
84 // process is complete.
85 void ImportContext::reportWarning(
86 	const char*			pText) {
87 
88 	XML_Parser p = (XML_Parser)m_pParser;
89 
90 	BString err = "Warning: ";
91 	err << pText;
92 	if(p) {
93 		err << "\n         (line " <<
94 			(uint32)XML_GetCurrentLineNumber(p) << ", column " <<
95 			(uint32)XML_GetCurrentColumnNumber(p) << ", element '" <<
96 			(element() ? element() : "(none)") << "')\n";
97 	} else
98 		err << "\n";
99 	m_errors.push_back(err);
100 }
101 
102 // register a fatal error; halts the deserialization process
103 // as soon as possible.
104 void ImportContext::reportError(
105 	const char*			pText) {
106 
107 	XML_Parser p = (XML_Parser)m_pParser;
108 
109 	BString err = "FATAL ERROR: ";
110 	err << pText;
111 	if(p) {
112 		err << "\n             (line " <<
113 			(uint32)XML_GetCurrentLineNumber(p) << ", column " <<
114 			(uint32)XML_GetCurrentColumnNumber(p) << ", element '" <<
115 			(element() ? element() : "(none)") << "')\n";
116 	} else
117 		err << "\n";
118 	m_errors.push_back(err);
119 
120 	m_state = ABORT;
121 }
122 
123 // -------------------------------------------------------- //
124 // internal operations
125 // -------------------------------------------------------- //
126 
127 void ImportContext::reset() {
128 	m_state = PARSING;
129 	m_elementStack.clear();
130 	// +++++ potential for memory leaks; reset() is currently
131 	//       only to be called after an identify cycle, during
132 	//       which no objects are created anyway, but this still
133 	//       gives me the shivers...
134 	m_objectStack.clear();
135 }
136 
137 // END -- ImportContext.cpp --
138