mirror of
https://github.com/reactos/reactos.git
synced 2025-01-02 12:32:47 +00:00
XMLStorage update: XMLReaderBase
svn path=/trunk/; revision=9661
This commit is contained in:
parent
ebb7ad1e4a
commit
9ca2c517db
2 changed files with 122 additions and 41 deletions
|
@ -54,16 +54,18 @@ bool XMLPos::go(const char* path)
|
||||||
|
|
||||||
|
|
||||||
/// read XML stream into XML tree below _pos
|
/// read XML stream into XML tree below _pos
|
||||||
XML_Status XMLReader::read(std::istream& in)
|
XML_Status XMLReaderBase::read()
|
||||||
{
|
{
|
||||||
XML_Status status = XML_STATUS_OK;
|
XML_Status status = XML_STATUS_OK;
|
||||||
|
|
||||||
while(in.good() && status==XML_STATUS_OK) {
|
while(status == XML_STATUS_OK) {
|
||||||
char* buffer = (char*) XML_GetBuffer(_parser, BUFFER_LEN);
|
char* buffer = (char*) XML_GetBuffer(_parser, BUFFER_LEN);
|
||||||
|
|
||||||
in.read(buffer, BUFFER_LEN);
|
int l = read_buffer(buffer, BUFFER_LEN);
|
||||||
|
if (l < 0)
|
||||||
|
break;
|
||||||
|
|
||||||
status = XML_ParseBuffer(_parser, in.gcount(), false);
|
status = XML_ParseBuffer(_parser, l, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status != XML_STATUS_ERROR)
|
if (status != XML_STATUS_ERROR)
|
||||||
|
@ -81,9 +83,9 @@ XML_Status XMLReader::read(std::istream& in)
|
||||||
|
|
||||||
|
|
||||||
/// store XML version and encoding into XML reader
|
/// store XML version and encoding into XML reader
|
||||||
void XMLCALL XMLReader::XML_XmlDeclHandler(void* userData, const XML_Char* version, const XML_Char* encoding, int standalone)
|
void XMLCALL XMLReaderBase::XML_XmlDeclHandler(void* userData, const XML_Char* version, const XML_Char* encoding, int standalone)
|
||||||
{
|
{
|
||||||
XMLReader* pReader = (XMLReader*) userData;
|
XMLReaderBase* pReader = (XMLReaderBase*) userData;
|
||||||
|
|
||||||
if (version) {
|
if (version) {
|
||||||
pReader->_xml_version = version;
|
pReader->_xml_version = version;
|
||||||
|
@ -92,9 +94,9 @@ void XMLCALL XMLReader::XML_XmlDeclHandler(void* userData, const XML_Char* versi
|
||||||
}
|
}
|
||||||
|
|
||||||
/// notifications about XML start tag
|
/// notifications about XML start tag
|
||||||
void XMLCALL XMLReader::XML_StartElementHandler(void* userData, const XML_Char* name, const XML_Char** atts)
|
void XMLCALL XMLReaderBase::XML_StartElementHandler(void* userData, const XML_Char* name, const XML_Char** atts)
|
||||||
{
|
{
|
||||||
XMLReader* pReader = (XMLReader*) userData;
|
XMLReaderBase* pReader = (XMLReaderBase*) userData;
|
||||||
XMLPos& pos = pReader->_pos;
|
XMLPos& pos = pReader->_pos;
|
||||||
|
|
||||||
// search for end of first line
|
// search for end of first line
|
||||||
|
@ -139,9 +141,9 @@ void XMLCALL XMLReader::XML_StartElementHandler(void* userData, const XML_Char*
|
||||||
}
|
}
|
||||||
|
|
||||||
/// notifications about XML end tag
|
/// notifications about XML end tag
|
||||||
void XMLCALL XMLReader::XML_EndElementHandler(void* userData, const XML_Char* name)
|
void XMLCALL XMLReaderBase::XML_EndElementHandler(void* userData, const XML_Char* name)
|
||||||
{
|
{
|
||||||
XMLReader* pReader = (XMLReader*) userData;
|
XMLReaderBase* pReader = (XMLReaderBase*) userData;
|
||||||
XMLPos& pos = pReader->_pos;
|
XMLPos& pos = pReader->_pos;
|
||||||
|
|
||||||
// search for end of first line
|
// search for end of first line
|
||||||
|
@ -174,15 +176,15 @@ void XMLCALL XMLReader::XML_EndElementHandler(void* userData, const XML_Char* na
|
||||||
}
|
}
|
||||||
|
|
||||||
/// store content, white space and comments
|
/// store content, white space and comments
|
||||||
void XMLCALL XMLReader::XML_DefaultHandler(void* userData, const XML_Char* s, int len)
|
void XMLCALL XMLReaderBase::XML_DefaultHandler(void* userData, const XML_Char* s, int len)
|
||||||
{
|
{
|
||||||
XMLReader* pReader = (XMLReader*) userData;
|
XMLReaderBase* pReader = (XMLReaderBase*) userData;
|
||||||
|
|
||||||
pReader->_content.append(s, len);
|
pReader->_content.append(s, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string XMLReader::get_error_string() const
|
std::string XMLReaderBase::get_error_string() const
|
||||||
{
|
{
|
||||||
XML_Error error = XML_GetErrorCode(_parser);
|
XML_Error error = XML_GetErrorCode(_parser);
|
||||||
|
|
||||||
|
|
|
@ -345,7 +345,7 @@ struct XMLNode : public String
|
||||||
// access to protected class members for XMLPos and XMLReader
|
// access to protected class members for XMLPos and XMLReader
|
||||||
friend struct XMLPos;
|
friend struct XMLPos;
|
||||||
friend struct const_XMLPos;
|
friend struct const_XMLPos;
|
||||||
friend struct XMLReader;
|
friend struct XMLReaderBase;
|
||||||
|
|
||||||
XMLNode(const String& name)
|
XMLNode(const String& name)
|
||||||
: String(name)
|
: String(name)
|
||||||
|
@ -377,6 +377,26 @@ struct XMLNode : public String
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
_leading.erase();
|
||||||
|
_content.erase();
|
||||||
|
_end_leading.erase();
|
||||||
|
_trailing.erase();
|
||||||
|
|
||||||
|
_attributes.clear();
|
||||||
|
|
||||||
|
while(!_children.empty()) {
|
||||||
|
XMLNode* node = _children.back();
|
||||||
|
_children.pop_back();
|
||||||
|
|
||||||
|
node->clear();
|
||||||
|
delete node;
|
||||||
|
}
|
||||||
|
|
||||||
|
String::erase();
|
||||||
|
}
|
||||||
|
|
||||||
XMLNode& operator=(const XMLNode& other)
|
XMLNode& operator=(const XMLNode& other)
|
||||||
{
|
{
|
||||||
_children.clear();
|
_children.clear();
|
||||||
|
@ -926,6 +946,9 @@ struct XMLPos
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
String& str() {return *_cur;}
|
||||||
|
const String& str() const {return *_cur;}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
XMLNode* _root;
|
XMLNode* _root;
|
||||||
XMLNode* _cur;
|
XMLNode* _cur;
|
||||||
|
@ -1017,6 +1040,8 @@ struct const_XMLPos
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
const String& str() const {return *_cur;}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const XMLNode* _root;
|
const XMLNode* _root;
|
||||||
const XMLNode* _cur;
|
const XMLNode* _cur;
|
||||||
|
@ -1046,7 +1071,17 @@ struct XMLBool
|
||||||
_value = def;
|
_value = def;
|
||||||
}
|
}
|
||||||
|
|
||||||
XMLBool(XMLNode* node, const String& name, const String& attr_name, bool def=false)
|
XMLBool(const XMLNode* node, const String& attr_name, bool def=false)
|
||||||
|
{
|
||||||
|
const String& value = node->get(attr_name);
|
||||||
|
|
||||||
|
if (!value.empty())
|
||||||
|
_value = !_tcsicmp(value, TEXT("TRUE"));
|
||||||
|
else
|
||||||
|
_value = def;
|
||||||
|
}
|
||||||
|
|
||||||
|
XMLBool(const XMLNode* node, const String& name, const String& attr_name, bool def=false)
|
||||||
{
|
{
|
||||||
const String& value = node->value(name, attr_name);
|
const String& value = node->value(name, attr_name);
|
||||||
|
|
||||||
|
@ -1119,14 +1154,14 @@ protected:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct XMLNumber
|
struct XMLInt
|
||||||
{
|
{
|
||||||
XMLNumber(int value)
|
XMLInt(int value)
|
||||||
: _value(value)
|
: _value(value)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
XMLNumber(LPCTSTR value, int def=0)
|
XMLInt(LPCTSTR value, int def=0)
|
||||||
{
|
{
|
||||||
if (value && *value)
|
if (value && *value)
|
||||||
_value = _ttoi(value);
|
_value = _ttoi(value);
|
||||||
|
@ -1134,12 +1169,22 @@ struct XMLNumber
|
||||||
_value = def;
|
_value = def;
|
||||||
}
|
}
|
||||||
|
|
||||||
XMLNumber(XMLNode* node, const String& name, const String& attr_name, int def=0)
|
XMLInt(const XMLNode* node, const String& attr_name, int def=0)
|
||||||
|
{
|
||||||
|
const String& value = node->get(attr_name);
|
||||||
|
|
||||||
|
if (!value.empty())
|
||||||
|
_value = _ttoi(value);
|
||||||
|
else
|
||||||
|
_value = def;
|
||||||
|
}
|
||||||
|
|
||||||
|
XMLInt(const XMLNode* node, const String& name, const String& attr_name, int def=0)
|
||||||
{
|
{
|
||||||
const String& value = node->value(name, attr_name);
|
const String& value = node->value(name, attr_name);
|
||||||
|
|
||||||
if (!value.empty())
|
if (!value.empty())
|
||||||
_value = _ttoi(node->value(name, attr_name));
|
_value = _ttoi(value);
|
||||||
else
|
else
|
||||||
_value = def;
|
_value = def;
|
||||||
}
|
}
|
||||||
|
@ -1163,16 +1208,16 @@ private:
|
||||||
void operator=(const XMLBool&); // disallow assignment operations
|
void operator=(const XMLBool&); // disallow assignment operations
|
||||||
};
|
};
|
||||||
|
|
||||||
struct XMLNumberRef
|
struct XMLIntRef
|
||||||
{
|
{
|
||||||
XMLNumberRef(XMLNode* node, const String& name, const String& attr_name, int def=0)
|
XMLIntRef(XMLNode* node, const String& name, const String& attr_name, int def=0)
|
||||||
: _ref(node->value(name, attr_name))
|
: _ref(node->value(name, attr_name))
|
||||||
{
|
{
|
||||||
if (_ref.empty())
|
if (_ref.empty())
|
||||||
assign(def);
|
assign(def);
|
||||||
}
|
}
|
||||||
|
|
||||||
XMLNumberRef& operator=(int value)
|
XMLIntRef& operator=(int value)
|
||||||
{
|
{
|
||||||
assign(value);
|
assign(value);
|
||||||
|
|
||||||
|
@ -1202,9 +1247,9 @@ protected:
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// XML file reader
|
/// XML file reader
|
||||||
struct XMLReader
|
struct XMLReaderBase
|
||||||
{
|
{
|
||||||
XMLReader(XMLNode* node)
|
XMLReaderBase(XMLNode* node)
|
||||||
: _pos(node),
|
: _pos(node),
|
||||||
_parser(XML_ParserCreate(NULL))
|
_parser(XML_ParserCreate(NULL))
|
||||||
{
|
{
|
||||||
|
@ -1216,12 +1261,14 @@ struct XMLReader
|
||||||
_last_tag = TAG_NONE;
|
_last_tag = TAG_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
~XMLReader()
|
virtual ~XMLReaderBase()
|
||||||
{
|
{
|
||||||
XML_ParserFree(_parser);
|
XML_ParserFree(_parser);
|
||||||
}
|
}
|
||||||
|
|
||||||
XML_Status read(std::istream& in);
|
XML_Status read();
|
||||||
|
|
||||||
|
virtual int read_buffer(char* buffer, int len) = 0;
|
||||||
|
|
||||||
std::string get_position() const
|
std::string get_position() const
|
||||||
{
|
{
|
||||||
|
@ -1253,6 +1300,30 @@ protected:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct XMLReader : public XMLReaderBase
|
||||||
|
{
|
||||||
|
XMLReader(XMLNode* node, std::istream& in)
|
||||||
|
: XMLReaderBase(node),
|
||||||
|
_in(in)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// read XML stream into XML tree below _pos
|
||||||
|
int read_buffer(char* buffer, int len)
|
||||||
|
{
|
||||||
|
if (!_in.good())
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
_in.read(buffer, BUFFER_LEN);
|
||||||
|
|
||||||
|
return _in.gcount();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::istream& _in;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
struct XMLHeader : public std::string
|
struct XMLHeader : public std::string
|
||||||
{
|
{
|
||||||
XMLHeader(const std::string& xml_version="1.0", const std::string& encoding="UTF-8", const std::string& doctype="")
|
XMLHeader(const std::string& xml_version="1.0", const std::string& encoding="UTF-8", const std::string& doctype="")
|
||||||
|
@ -1293,18 +1364,9 @@ struct XMLDoc : public XMLNode
|
||||||
|
|
||||||
std::istream& read(std::istream& in)
|
std::istream& read(std::istream& in)
|
||||||
{
|
{
|
||||||
XMLReader reader(this);
|
XMLReader reader(this, in);
|
||||||
|
|
||||||
XML_Status status = reader.read(in);
|
read(reader);
|
||||||
|
|
||||||
if (status == XML_STATUS_ERROR) {
|
|
||||||
std::ostringstream out;
|
|
||||||
|
|
||||||
out << reader.get_position() << " " << reader.get_error_string();
|
|
||||||
|
|
||||||
_last_error = reader.get_error_code();
|
|
||||||
_last_error_msg = out.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
@ -1312,14 +1374,19 @@ struct XMLDoc : public XMLNode
|
||||||
bool read(LPCTSTR path)
|
bool read(LPCTSTR path)
|
||||||
{
|
{
|
||||||
tifstream in(path);
|
tifstream in(path);
|
||||||
XMLReader reader(this);
|
XMLReader reader(this, in);
|
||||||
|
|
||||||
XML_Status status = reader.read(in);
|
return read(reader, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool read(XMLReaderBase& reader, const char* display_path="")
|
||||||
|
{
|
||||||
|
XML_Status status = reader.read();
|
||||||
|
|
||||||
if (status == XML_STATUS_ERROR) {
|
if (status == XML_STATUS_ERROR) {
|
||||||
std::ostringstream out;
|
std::ostringstream out;
|
||||||
|
|
||||||
out << path << reader.get_position() << " " << reader.get_error_string();
|
out << display_path << reader.get_position() << " " << reader.get_error_string();
|
||||||
|
|
||||||
_last_error = reader.get_error_code();
|
_last_error = reader.get_error_code();
|
||||||
_last_error_msg = out.str();
|
_last_error_msg = out.str();
|
||||||
|
@ -1364,4 +1431,16 @@ struct XMLDoc : public XMLNode
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct XMLMessage : public XMLDoc
|
||||||
|
{
|
||||||
|
XMLMessage(const char* name)
|
||||||
|
: _pos(this)
|
||||||
|
{
|
||||||
|
_pos.create(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
XMLPos _pos;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
} // namespace XMLStorage
|
} // namespace XMLStorage
|
||||||
|
|
Loading…
Reference in a new issue