1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
//! Copyright (C) 2006 T. Zachary Laine
//!
//! This library is free software; you can redistribute it and/or
//! modify it under the terms of the GNU Lesser General Public License
//! as published by the Free Software Foundation; either version 2.1
//! of the License, or (at your option) any later version.
//!
//! This library is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//! Lesser General Public License for more details.
//!
//! You should have received a copy of the GNU Lesser General Public
//! License along with this library; if not, write to the Free
//! Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//! 02111-1307 USA
//!
//! If you do not wish to comply with the terms of the LGPL please
//! contact the author as other terms are available for a fee.
//!
//! ----
//!
//! Zach Laine
//! whatwasthataddress@hotmail.com/
//!
//! This notice came from the original file from which all the XML parsing code
//! was taken, part of the spirit distribution. The code was modified slightly
//! by me, and doesn't contain all the original code. Thanks to Daniel Nuffer
//! for his great work.
//!
//! ----
//!
//! simplexml.cpp
//!
//! Spirit V1.3
//! URL: http://spirit.sourceforge.net/
//!
//! Copyright (c) 2001, Daniel C. Nuffer
//!
//! This software is provided 'as-is', without any express or implied
//! warranty. In no event will the copyright holder be held liable for
//! any damages arising from the use of this software.
//!
//! Permission is granted to anyone to use this software for any purpose,
//! including commercial applications, and to alter it and redistribute
//! it freely, subject to the following restrictions:
//!
//! 1. The origin of this software must not be misrepresented; you must
//! not claim that you wrote the original software. If you use this
//! software in a product, an acknowledgment in the product documentation
//! would be appreciated but is not required.
//!
//! 2. Altered source versions must be plainly marked as such, and must
//! not be misrepresented as being the original software.
//!
//! 3. This notice may not be removed or altered from any source
//! distribution.
#include "XMLDoc.h"
#if defined(_MSC_VER) && _MSC_VER >= 1930
struct IUnknown; // Workaround for "combaseapi.h(229,21): error C2760: syntax error: 'identifier' was unexpected here; expected 'type specifier'"
#endif
#include <boost/spirit/include/classic.hpp>
#include <algorithm>
#include <stdexcept>
#include <sstream>
namespace {
using namespace boost::spirit::classic;
using chset_t = chset<uint8_t>;
//! XML grammar rules
rule<> document, prolog, element, Misc, Reference, CData, doctypedecl,
XMLDecl, SDDecl, VersionInfo, EncodingDecl, VersionNum, Eq,
EmptyElemTag, STag, content, ETag, Attribute, AttValue, CharData,
Comment, CDSect, CharRef, EntityRef, EncName, Name, Comment1, S;
//! XML Character classes
const chset_t Char("\x9\xA\xD\x20-\xFF");
const chset_t Letter("\x41-\x5A\x61-\x7A\xC0-\xD6\xD8-\xF6\xF8-\xFF");
const chset_t Digit("0-9");
const chset_t Extender(static_cast<uint8_t>('\xB7'));
const chset_t NameChar = Letter | Digit | chset_t("._:-") | Extender;
const chset_t Sch("\x20\x9\xD\xA");
}
//! Creates the XML parsing rules at static initialization time.
struct RuleDefiner {
RuleDefiner() {
// This is the start rule for XML parsing
document =
prolog >> element >> *Misc
;
S =
+(Sch)
;
Name =
(Letter | '_' | ':')
>> *(NameChar)
;
AttValue =
'"'
>> (
(*(anychar_p - (chset_t('<') | '&' | '"')))[&XMLDoc::AddAttribute]
| *(Reference)
)
>> '"'
| '\''
>> (
(*(anychar_p - (chset_t('<') | '&' | '\'')))[&XMLDoc::AddAttribute]
| *(Reference)
)
>> '\''
;
chset_t CharDataChar(anychar_p - (chset_t('<') | chset_t('&')));
CharData =
(*(CharDataChar - str_p("]]>")))[&XMLDoc::AppendToText]
;
Comment1 =
*(
(Char - ch_p('-'))
| (ch_p('-') >> (Char - ch_p('-')))
)
;
Comment =
str_p("<!--") >> Comment1 >> str_p("-->")
;
CDSect =
str_p("<![CDATA[") >> CData >> str_p("]]>")
;
CData =
(*(Char - str_p("]]>")))[&XMLDoc::AppendToText]
;
prolog =
!XMLDecl >> *Misc >> !(doctypedecl >> *Misc)
;
XMLDecl =
str_p("<?xml")
>> VersionInfo
>> !EncodingDecl
>> !SDDecl
>> !S
>> str_p("?>")
;
VersionInfo =
S
>> str_p("version")
>> Eq
>> (
ch_p('\'') >> VersionNum >> '\''
| ch_p('"') >> VersionNum >> '"'
)
;
Eq =
!S >> '=' >> !S
;
chset_t VersionNumCh("A-Za-z0-9_.:-");
VersionNum =
+(VersionNumCh)
;
Misc =
Comment | S
;
doctypedecl =
str_p("<!DOCTYPE")
>> *(Char - (chset_t('[') | '>'))
>> !('[' >> *(Char - ']') >> ']')
>> '>'
;
SDDecl =
S
>> str_p("standalone")
>> Eq
>> (
(ch_p('\'') >> (str_p("yes") | str_p("no")) >> '\'')
| (ch_p('"') >> (str_p("yes") | str_p("no")) >> '"')
)
;
element =
STag[&XMLDoc::PushElem1] >> content >> ETag
| EmptyElemTag[&XMLDoc::PushElem2]
;
STag =
'<'
>> Name[&XMLDoc::SetElemName]
>> *(S >> Attribute)
>> !S
>> '>'
;
Attribute =
Name[&XMLDoc::SetAttributeName] >> Eq >> AttValue
;
ETag =
str_p("</") >> Name[&XMLDoc::PopElem] >> !S >> '>'
;
content =
!CharData
>> *(
(
element
| Reference
| CDSect
| Comment
)
>> !CharData
)
;
EmptyElemTag =
'<'
>> Name[&XMLDoc::SetElemName]
>> *(S >> Attribute)
>> !S
>> str_p("/>")
;
CharRef =
str_p("&#") >> +digit_p >> ';'
| str_p("&#x") >> +xdigit_p >> ';'
;
Reference =
EntityRef
| CharRef
;
EntityRef =
'&' >> Name >> ';'
;
EncodingDecl =
S
>> str_p("encoding")
>> Eq
>> (
ch_p('"') >> EncName >> '"'
| ch_p('\'') >> EncName >> '\''
)
;
chset_t EncNameCh = VersionNumCh - chset_t(':');
EncName =
alpha_p >> *(EncNameCh)
;
}
};
namespace {
RuleDefiner s_rule_definer;
constinit XMLDoc* s_curr_parsing_doc = nullptr;
#if defined(__cpp_lib_constexpr_vector)
constinit
#endif
std::vector<XMLElement*> s_element_stack;
#if defined(__cpp_lib_constexpr_vector) && defined(__cpp_lib_constexpr_string) && ((!defined(__GNUC__) || (__GNUC__ > 12) || (__GNUC__ == 12 && __GNUC_MINOR__ >= 2))) && ((!defined(_MSC_VER) || (_MSC_VER >= 1934))) && ((!defined(__clang_major__) || (__clang_major__ >= 17)))
constinit
#endif
XMLElement s_temp_elem;
#if defined(__cpp_lib_constexpr_string) && ((!defined(__GNUC__) || (__GNUC__ > 12) || (__GNUC__ == 12 && __GNUC_MINOR__ >= 2))) && ((!defined(_MSC_VER) || (_MSC_VER >= 1934))) && ((!defined(__clang_major__) || (__clang_major__ >= 17)))
constinit
#endif
std::string s_temp_attr_name;
}
bool XMLElement::ContainsChild(const std::string& tag) const {
return children.end() != std::find_if(children.begin(), children.end(),
[&tag] (const XMLElement& e) { return e.m_tag == tag; });
}
const XMLElement& XMLElement::Child(const std::string& tag) const {
auto match = std::find_if(children.begin(), children.end(),
[&tag] (const XMLElement& e) { return e.m_tag == tag; });
if (match == children.end())
throw std::out_of_range("XMLElement::Child(): The XMLElement \"" + Tag() + "\" contains no child \"" + tag + "\".");
return *match;
}
std::string XMLElement::WriteElement(int indent, bool whitespace) const {
std::stringstream ss;
WriteElement(ss, indent, whitespace);
return ss.str();
}
std::ostream& XMLElement::WriteElement(std::ostream& os, int indent, bool whitespace) const {
if (whitespace)
os << std::string(indent * 2, ' ');
os << '<' << m_tag;
for (const auto& attribute : attributes)
os << ' ' << attribute.first << "=\"" << attribute.second << "\"";
if (children.empty() && m_text.empty() && !m_root) {
os << "/>";
if (whitespace)
os << "\n";
} else {
os << ">";
if (!m_text.empty() && m_text.find_first_of("<&") != std::string::npos)
os << "<![CDATA[" << m_text << "]]>";
else
os << m_text;
if (whitespace && !children.empty())
os << "\n";
for (const XMLElement& child : children)
child.WriteElement(os, indent + 1, whitespace);
if (whitespace && !children.empty())
os << std::string(static_cast<std::size_t>(indent) * 2, ' ');
os << "</" << m_tag << ">";
if (whitespace) os << "\n";
}
return os;
}
XMLElement& XMLElement::Child(const std::string& tag) {
auto match = std::find_if(children.begin(), children.end(),
[&tag] (const XMLElement& e) { return e.m_tag == tag; });
if (match == children.end())
throw std::out_of_range("XMLElement::Child(): The XMLElement \"" + Tag() + "\" contains no child \"" + tag + "\".");
return *match;
}
void XMLElement::SetTag(std::string tag)
{ m_tag = std::move(tag); }
void XMLElement::SetText(std::string text)
{ m_text = std::move(text); }
const std::string& XMLElement::Attribute(const std::string& name) const {
auto match = std::find_if(attributes.begin(), attributes.end(),
[&name] (const auto& p) { return p.first == name; });
if (match == attributes.end())
throw std::out_of_range("XMLElement::Attribute(): The XMLElement \"" + Tag() + "\" contains no attribute \"" + name + "\".");
return match->second;
}
bool XMLElement::HasAttribute(const std::string& name) const noexcept {
return std::any_of(attributes.begin(), attributes.end(),
[&name](const auto& a) { return a.first == name; });
}
XMLDoc::XMLDoc(std::string root_tag) :
root_node(XMLElement(std::move(root_tag), true))
{}
XMLDoc::XMLDoc(const std::istream& is) :
root_node(XMLElement())
{}
std::ostream& XMLDoc::WriteDoc(std::ostream& os, bool whitespace) const {
os << "<?xml version=\"1.0\"?>";
if (whitespace) os << "\n";
return root_node.WriteElement(os, 0, whitespace);
}
void XMLDoc::ReadDoc(const std::string& s) {
std::stringstream ss(s);
ReadDoc(ss);
}
std::istream& XMLDoc::ReadDoc(std::istream& is) {
root_node = XMLElement(); // clear doc contents
s_element_stack.clear(); // clear this to start a fresh read
s_curr_parsing_doc = this; // indicate where to add elements
std::string parse_str;
std::string temp_str;
while (is) {
getline(is, temp_str);
parse_str += temp_str + '\n';
}
parse(parse_str.c_str(), document);
s_curr_parsing_doc = nullptr;
return is;
}
void XMLDoc::SetElemName(const char* first, const char* last)
{ s_temp_elem = XMLElement(std::string(first, last)); }
void XMLDoc::SetAttributeName(const char* first, const char* last)
{ s_temp_attr_name = std::string(first, last); }
void XMLDoc::AddAttribute(const char* first, const char* last) {
auto& attribs = s_temp_elem.attributes;
auto it = std::find_if(attribs.begin(), attribs.end(),
[](const auto& a) { return a.first == s_temp_attr_name; });
if (it == attribs.end())
attribs.emplace_back(s_temp_attr_name, std::string{first, last});
else
it->second = std::string{first, last};
}
void XMLDoc::PushElem1(const char* first, const char* last) {
if (XMLDoc* this_ = s_curr_parsing_doc) {
if (s_element_stack.empty()) {
this_->root_node = s_temp_elem;
s_element_stack.emplace_back(&this_->root_node);
} else {
s_element_stack.back()->children.emplace_back(s_temp_elem);
s_element_stack.emplace_back(&s_element_stack.back()->children.back());
}
}
}
void XMLDoc::PushElem2(const char* first, const char* last) {
if (XMLDoc* this_ = s_curr_parsing_doc) {
if (s_element_stack.empty()) {
this_->root_node = s_temp_elem;
} else {
s_element_stack.back()->children.emplace_back(s_temp_elem);
}
}
}
void XMLDoc::PopElem(const char*, const char*) {
if (!s_element_stack.empty())
s_element_stack.pop_back();
}
void XMLDoc::AppendToText(const char* first, const char* last) {
if (!s_element_stack.empty()) {
std::string text(first, last);
std::string::size_type first_good_posn = (text[0] != '\"') ? 0 : 1;
std::string::size_type last_good_posn = text.find_last_not_of(" \t\n\"\r\f");
// strip of leading quote and/or trailing quote, and/or trailing whitespace
if (last_good_posn != std::string::npos)
s_element_stack.back()->m_text += text.substr(first_good_posn, (last_good_posn + 1) - first_good_posn);
}
}
|