mirror of
https://github.com/reactos/reactos.git
synced 2025-08-05 21:42:57 +00:00
fix XML formating
svn path=/trunk/; revision=9045
This commit is contained in:
parent
37bfcac65a
commit
7f72303d64
2 changed files with 53 additions and 47 deletions
|
@ -75,7 +75,11 @@ XML_Status XMLReader::read(std::istream& in)
|
||||||
cerr << get_error_string();
|
cerr << get_error_string();
|
||||||
*/
|
*/
|
||||||
|
|
||||||
_pos->append_trailing(_content.c_str(), _content.length());
|
if (_pos->_children.empty())
|
||||||
|
_pos->_trailing.append(_content);
|
||||||
|
else
|
||||||
|
_pos->_children.back()->_trailing.append(_content);
|
||||||
|
|
||||||
_content.erase();
|
_content.erase();
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
|
@ -85,23 +89,24 @@ 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 XMLReader::XML_XmlDeclHandler(void* userData, const XML_Char* version, const XML_Char* encoding, int standalone)
|
||||||
{
|
{
|
||||||
XMLReader* pThis = (XMLReader*) userData;
|
XMLReader* pReader = (XMLReader*) userData;
|
||||||
|
|
||||||
if (version) {
|
if (version) {
|
||||||
pThis->_xml_version = version;
|
pReader->_xml_version = version;
|
||||||
pThis->_encoding = encoding;
|
pReader->_encoding = encoding;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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 XMLReader::XML_StartElementHandler(void* userData, const XML_Char* name, const XML_Char** atts)
|
||||||
{
|
{
|
||||||
XMLReader* pThis = (XMLReader*) userData;
|
XMLReader* pReader = (XMLReader*) userData;
|
||||||
|
XMLPos& pos = pReader->_pos;
|
||||||
|
|
||||||
// search for end of first line
|
// search for end of first line
|
||||||
const char* s = pThis->_content.c_str();
|
const char* s = pReader->_content.c_str();
|
||||||
const char* p = s;
|
const char* p = s;
|
||||||
const char* e = p + pThis->_content.length();
|
const char* e = p + pReader->_content.length();
|
||||||
|
|
||||||
for(; p<e; ++p)
|
for(; p<e; ++p)
|
||||||
if (*p == '\n') {
|
if (*p == '\n') {
|
||||||
|
@ -110,7 +115,14 @@ void XMLCALL XMLReader::XML_StartElementHandler(void* userData, const XML_Char*
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p != s)
|
if (p != s)
|
||||||
pThis->_pos->append_trailing(s, p-s);
|
if (pos->_children.empty()) { // no children in last node?
|
||||||
|
if (pReader->_last_tag == TAG_START)
|
||||||
|
pos->_content.append(s, p-s);
|
||||||
|
else if (pReader->_last_tag == TAG_END)
|
||||||
|
pos->_trailing.append(s, p-s);
|
||||||
|
// else TAG_NONE -> don't store white space in root node
|
||||||
|
} else
|
||||||
|
pos->_children.back()->_trailing.append(s, p-s);
|
||||||
|
|
||||||
std::string leading;
|
std::string leading;
|
||||||
|
|
||||||
|
@ -119,7 +131,7 @@ void XMLCALL XMLReader::XML_StartElementHandler(void* userData, const XML_Char*
|
||||||
|
|
||||||
XMLNode* node = new XMLNode(String_from_XML_Char(name), leading);
|
XMLNode* node = new XMLNode(String_from_XML_Char(name), leading);
|
||||||
|
|
||||||
pThis->_pos.add_down(node);
|
pos.add_down(node);
|
||||||
|
|
||||||
while(*atts) {
|
while(*atts) {
|
||||||
const XML_Char* attr_name = *atts++;
|
const XML_Char* attr_name = *atts++;
|
||||||
|
@ -128,19 +140,20 @@ void XMLCALL XMLReader::XML_StartElementHandler(void* userData, const XML_Char*
|
||||||
(*node)[String_from_XML_Char(attr_name)] = String_from_XML_Char(attr_value);
|
(*node)[String_from_XML_Char(attr_name)] = String_from_XML_Char(attr_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
pThis->_in_node = true;
|
pReader->_last_tag = TAG_START;
|
||||||
pThis->_content.erase();
|
pReader->_content.erase();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// notifications about XML end tag
|
/// notifications about XML end tag
|
||||||
void XMLCALL XMLReader::XML_EndElementHandler(void* userData, const XML_Char* name)
|
void XMLCALL XMLReader::XML_EndElementHandler(void* userData, const XML_Char* name)
|
||||||
{
|
{
|
||||||
XMLReader* pThis = (XMLReader*) userData;
|
XMLReader* pReader = (XMLReader*) userData;
|
||||||
|
XMLPos& pos = pReader->_pos;
|
||||||
|
|
||||||
// search for end of first line
|
// search for end of first line
|
||||||
const char* s = pThis->_content.c_str();
|
const char* s = pReader->_content.c_str();
|
||||||
const char* p = s;
|
const char* p = s;
|
||||||
const char* e = p + pThis->_content.length();
|
const char* e = p + pReader->_content.length();
|
||||||
|
|
||||||
for(; p<e; ++p)
|
for(; p<e; ++p)
|
||||||
if (*p == '\n') {
|
if (*p == '\n') {
|
||||||
|
@ -149,23 +162,29 @@ void XMLCALL XMLReader::XML_EndElementHandler(void* userData, const XML_Char* na
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p != s)
|
if (p != s)
|
||||||
pThis->_pos->append_content(s, p-s);
|
if (pos->_children.empty()) // no children in current node?
|
||||||
|
pos->_content.append(s, p-s);
|
||||||
|
else
|
||||||
|
if (pReader->_last_tag == TAG_START)
|
||||||
|
pos->_content.append(s, p-s);
|
||||||
|
else
|
||||||
|
pos->_children.back()->_trailing.append(s, p-s);
|
||||||
|
|
||||||
if (p != e)
|
if (p != e)
|
||||||
pThis->_pos->_end_leading.assign(p, e-p);
|
pos->_end_leading.assign(p, e-p);
|
||||||
|
|
||||||
pThis->_pos.back();
|
pos.back();
|
||||||
|
|
||||||
pThis->_in_node = false;
|
pReader->_last_tag = TAG_END;
|
||||||
pThis->_content.erase();
|
pReader->_content.erase();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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 XMLReader::XML_DefaultHandler(void* userData, const XML_Char* s, int len)
|
||||||
{
|
{
|
||||||
XMLReader* pThis = (XMLReader*) userData;
|
XMLReader* pReader = (XMLReader*) userData;
|
||||||
|
|
||||||
pThis->_content.append(s, len);
|
pReader->_content.append(s, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -282,7 +301,9 @@ void XMLNode::smart_write_worker(std::ostream& out, int indent) const
|
||||||
for(AttributeMap::const_iterator it=_attributes.begin(); it!=_attributes.end(); ++it)
|
for(AttributeMap::const_iterator it=_attributes.begin(); it!=_attributes.end(); ++it)
|
||||||
out << ' ' << EncodeXMLString(it->first) << "=\"" << EncodeXMLString(it->second) << "\"";
|
out << ' ' << EncodeXMLString(it->first) << "=\"" << EncodeXMLString(it->second) << "\"";
|
||||||
|
|
||||||
if (!_children.empty() || !_content.empty()) {
|
if (_children.empty() && _content.empty())
|
||||||
|
out << "/>";
|
||||||
|
else {
|
||||||
out << '>';
|
out << '>';
|
||||||
|
|
||||||
if (_content.empty())
|
if (_content.empty())
|
||||||
|
@ -292,7 +313,7 @@ void XMLNode::smart_write_worker(std::ostream& out, int indent) const
|
||||||
|
|
||||||
Children::const_iterator it = _children.begin();
|
Children::const_iterator it = _children.begin();
|
||||||
|
|
||||||
if (it != _children.end())
|
if (it != _children.end()) {
|
||||||
for(; it!=_children.end(); ++it)
|
for(; it!=_children.end(); ++it)
|
||||||
(*it)->smart_write_worker(out, indent+1);
|
(*it)->smart_write_worker(out, indent+1);
|
||||||
|
|
||||||
|
@ -301,10 +322,11 @@ void XMLNode::smart_write_worker(std::ostream& out, int indent) const
|
||||||
out << XML_INDENT_SPACE;
|
out << XML_INDENT_SPACE;
|
||||||
else
|
else
|
||||||
out << _end_leading;
|
out << _end_leading;
|
||||||
|
} else
|
||||||
|
out << _end_leading;
|
||||||
|
|
||||||
out << "</" << EncodeXMLString(*this) << '>';
|
out << "</" << EncodeXMLString(*this) << '>';
|
||||||
} else
|
}
|
||||||
out << "/>";
|
|
||||||
|
|
||||||
if (_trailing.empty())
|
if (_trailing.empty())
|
||||||
out << '\n';
|
out << '\n';
|
||||||
|
|
|
@ -576,22 +576,6 @@ protected:
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void append_content(const char* s, int l)
|
|
||||||
{
|
|
||||||
//if (_children.empty())
|
|
||||||
_content.append(s, l);
|
|
||||||
//else
|
|
||||||
// _children.back()->_content.append(s, l);
|
|
||||||
}
|
|
||||||
|
|
||||||
void append_trailing(const char* s, int l)
|
|
||||||
{
|
|
||||||
if (_children.empty())
|
|
||||||
_content.append(s, l);
|
|
||||||
else
|
|
||||||
_children.back()->_trailing.append(s, l);
|
|
||||||
}
|
|
||||||
|
|
||||||
void write_worker(std::ostream& out, int indent) const;
|
void write_worker(std::ostream& out, int indent) const;
|
||||||
void pretty_write_worker(std::ostream& out, int indent) const;
|
void pretty_write_worker(std::ostream& out, int indent) const;
|
||||||
void smart_write_worker(std::ostream& out, int indent) const;
|
void smart_write_worker(std::ostream& out, int indent) const;
|
||||||
|
@ -1227,7 +1211,7 @@ struct XMLReader
|
||||||
XML_SetElementHandler(_parser, XML_StartElementHandler, XML_EndElementHandler);
|
XML_SetElementHandler(_parser, XML_StartElementHandler, XML_EndElementHandler);
|
||||||
XML_SetDefaultHandler(_parser, XML_DefaultHandler);
|
XML_SetDefaultHandler(_parser, XML_DefaultHandler);
|
||||||
|
|
||||||
_in_node = false;
|
_last_tag = TAG_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
~XMLReader()
|
~XMLReader()
|
||||||
|
@ -1265,7 +1249,7 @@ protected:
|
||||||
std::string _encoding;
|
std::string _encoding;
|
||||||
|
|
||||||
std::string _content;
|
std::string _content;
|
||||||
bool _in_node;
|
enum {TAG_NONE, TAG_START, TAG_END} _last_tag;
|
||||||
|
|
||||||
static void XMLCALL XML_XmlDeclHandler(void* userData, const XML_Char* version, const XML_Char* encoding, int standalone);
|
static void XMLCALL XML_XmlDeclHandler(void* userData, const XML_Char* version, const XML_Char* encoding, int standalone);
|
||||||
static void XMLCALL XML_StartElementHandler(void* userData, const XML_Char* name, const XML_Char** atts);
|
static void XMLCALL XML_StartElementHandler(void* userData, const XML_Char* name, const XML_Char** atts);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue