| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2011 Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
#include <map> |
| 26 |
#include <vector> |
| 27 |
#include <iostream> |
| 28 |
#include <string> |
| 29 |
#include <xercesc/sax2/XMLReaderFactory.hpp> |
| 30 |
#include <xercesc/sax2/Attributes.hpp> |
| 31 |
#include <xercesc/util/XMLUni.hpp> |
| 32 |
|
| 33 |
#include <locale> |
| 34 |
|
| 35 |
#include "Reader.h" |
| 36 |
#include "Base64.h" |
| 37 |
#include "Base64Filter.h" |
| 38 |
#include "Console.h" |
| 39 |
#include "Exception.h" |
| 40 |
#include "InputSource.h" |
| 41 |
#include "Persistence.h" |
| 42 |
#include "Sequencer.h" |
| 43 |
#include "Stream.h" |
| 44 |
#include "XMLTools.h" |
| 45 |
|
| 46 |
#ifdef _MSC_VER |
| 47 |
# include <zipios++/zipios-config.h> |
| 48 |
#endif |
| 49 |
#include <zipios++/zipinputstream.h> |
| 50 |
#include <boost/iostreams/filtering_stream.hpp> |
| 51 |
|
| 52 |
#ifndef XERCES_CPP_NAMESPACE_BEGIN |
| 53 |
# define XERCES_CPP_NAMESPACE_QUALIFIER |
| 54 |
using namespace XERCES_CPP_NAMESPACE; |
| 55 |
#else |
| 56 |
XERCES_CPP_NAMESPACE_USE |
| 57 |
#endif |
| 58 |
|
| 59 |
using namespace std; |
| 60 |
|
| 61 |
|
| 62 |
// --------------------------------------------------------------------------- |
| 63 |
// Base::XMLReader: Constructors and Destructor |
| 64 |
// --------------------------------------------------------------------------- |
| 65 |
|
| 66 |
Base::XMLReader::XMLReader(const char* FileName, std::istream& str) |
| 67 |
: _File(FileName) |
| 68 |
{ |
| 69 |
str.imbue(std::locale::classic()); |
| 70 |
|
| 71 |
// create the parser |
| 72 |
parser = XMLReaderFactory::createXMLReader(); // NOLINT |
| 73 |
|
| 74 |
parser->setContentHandler(this); |
| 75 |
parser->setLexicalHandler(this); |
| 76 |
parser->setErrorHandler(this); |
| 77 |
parser->setFeature(XMLUni::fgXercesDisableDefaultEntityResolution, true); |
| 78 |
parser->setFeature(XMLUni::fgXercesLoadExternalDTD, false); |
| 79 |
|
| 80 |
try { |
| 81 |
StdInputSource file(str, _File.filePath().c_str()); |
| 82 |
_valid = parser->parseFirst(file, token); |
| 83 |
} |
| 84 |
catch (const XMLException& toCatch) { |
| 85 |
char* message = XMLString::transcode(toCatch.getMessage()); |
| 86 |
cerr << "Exception message is: \n" << message << "\n"; |
| 87 |
XMLString::release(&message); |
| 88 |
} |
| 89 |
catch (const SAXParseException& toCatch) { |
| 90 |
char* message = XMLString::transcode(toCatch.getMessage()); |
| 91 |
cerr << "Exception message is: \n" << message << "\n"; |
| 92 |
XMLString::release(&message); |
| 93 |
} |
| 94 |
#ifndef FC_DEBUG |
| 95 |
catch (...) { |
| 96 |
cerr << "Unexpected Exception \n"; |
| 97 |
} |
| 98 |
#endif |
| 99 |
} |
| 100 |
|
| 101 |
Base::XMLReader::~XMLReader() |
| 102 |
{ |
| 103 |
// Delete the parser itself. Must be done prior to calling Terminate, below. |
| 104 |
delete parser; |
| 105 |
} |
| 106 |
|
| 107 |
const char* Base::XMLReader::localName() const |
| 108 |
{ |
| 109 |
return LocalName.c_str(); |
| 110 |
} |
| 111 |
|
| 112 |
unsigned int Base::XMLReader::getAttributeCount() const |
| 113 |
{ |
| 114 |
return static_cast<unsigned int>(AttrMap.size()); |
| 115 |
} |
| 116 |
|
| 117 |
namespace |
| 118 |
{ |
| 119 |
template<typename T> |
| 120 |
T readerCast(const char* value) |
| 121 |
{ |
| 122 |
if constexpr (std::is_same_v<T, const char*>) { |
| 123 |
return value; |
| 124 |
} |
| 125 |
if constexpr (std::is_same_v<T, long>) { |
| 126 |
return stol(value); |
| 127 |
} |
| 128 |
if constexpr (std::is_same_v<T, int>) { |
| 129 |
return stoi(value); |
| 130 |
} |
| 131 |
if constexpr (std::is_same_v<T, unsigned long>) { |
| 132 |
return stoul(value, nullptr); |
| 133 |
} |
| 134 |
if constexpr (std::is_same_v<T, double>) { |
| 135 |
return stod(value, nullptr); |
| 136 |
} |
| 137 |
if constexpr (std::is_same_v<T, bool>) { |
| 138 |
return std::string_view(value) != "0"; |
| 139 |
} |
| 140 |
} |
| 141 |
} // anonymous namespace |
| 142 |
|
| 143 |
template<typename T> |
| 144 |
requires Base::XMLReader::instantiated<T> |
| 145 |
T Base::XMLReader::getAttribute(const char* AttrName, T defaultValue) const |
| 146 |
{ |
| 147 |
auto pos = AttrMap.find(AttrName); |
| 148 |
if (pos == AttrMap.end()) { |
| 149 |
return defaultValue; |
| 150 |
} |
| 151 |
const char* rawValue = pos->second.c_str(); |
| 152 |
return readerCast<T>(rawValue); |
| 153 |
} |
| 154 |
|
| 155 |
template<typename T> |
| 156 |
requires Base::XMLReader::instantiated<T> |
| 157 |
T Base::XMLReader::getAttribute(const char* AttrName) const |
| 158 |
{ |
| 159 |
auto pos = AttrMap.find(AttrName); |
| 160 |
if (pos == AttrMap.end()) { |
| 161 |
// wrong name, use hasAttribute if not sure! |
| 162 |
std::string msg = std::string("XML Attribute: \"") + AttrName + "\" not found"; |
| 163 |
throw Base::XMLAttributeError(msg); |
| 164 |
} |
| 165 |
const char* rawValue = pos->second.c_str(); |
| 166 |
return readerCast<T>(rawValue); |
| 167 |
} |
| 168 |
|
| 169 |
// Explicit template instantiation |
| 170 |
template BaseExport bool Base::XMLReader::getAttribute<bool>( |
| 171 |
const char* AttrName, |
| 172 |
bool defaultValue |
| 173 |
) const; |
| 174 |
template BaseExport bool Base::XMLReader::getAttribute<bool>(const char* AttrName) const; |
| 175 |
template BaseExport const char* Base::XMLReader::getAttribute<const char*>( |
| 176 |
const char* AttrName, |
| 177 |
const char* defaultValue |
| 178 |
) const; |
| 179 |
template BaseExport const char* Base::XMLReader::getAttribute<const char*>(const char* AttrName) const; |
| 180 |
template BaseExport double Base::XMLReader::getAttribute<double>( |
| 181 |
const char* AttrName, |
| 182 |
double defaultValue |
| 183 |
) const; |
| 184 |
template BaseExport double Base::XMLReader::getAttribute<double>(const char* AttrName) const; |
| 185 |
template BaseExport int Base::XMLReader::getAttribute<int>(const char* AttrName, int defaultValue) const; |
| 186 |
template BaseExport int Base::XMLReader::getAttribute<int>(const char* AttrName) const; |
| 187 |
template BaseExport long Base::XMLReader::getAttribute<long>( |
| 188 |
const char* AttrName, |
| 189 |
long defaultValue |
| 190 |
) const; |
| 191 |
template BaseExport long Base::XMLReader::getAttribute<long>(const char* AttrName) const; |
| 192 |
template BaseExport unsigned long Base::XMLReader::getAttribute<unsigned long>( |
| 193 |
const char* AttrName, |
| 194 |
unsigned long defaultValue |
| 195 |
) const; |
| 196 |
template BaseExport unsigned long Base::XMLReader::getAttribute<unsigned long>( |
| 197 |
const char* AttrName |
| 198 |
) const; |
| 199 |
|
| 200 |
bool Base::XMLReader::hasAttribute(const char* AttrName) const |
| 201 |
{ |
| 202 |
return AttrMap.find(AttrName) != AttrMap.end(); |
| 203 |
} |
| 204 |
|
| 205 |
bool Base::XMLReader::read() |
| 206 |
{ |
| 207 |
ReadType = None; |
| 208 |
|
| 209 |
try { |
| 210 |
parser->parseNext(token); |
| 211 |
} |
| 212 |
catch (const XMLException& toCatch) { |
| 213 |
|
| 214 |
char* message = XMLString::transcode(toCatch.getMessage()); |
| 215 |
std::string what = message; |
| 216 |
XMLString::release(&message); |
| 217 |
throw Base::XMLBaseException(what); |
| 218 |
} |
| 219 |
catch (const SAXParseException& toCatch) { |
| 220 |
|
| 221 |
char* message = XMLString::transcode(toCatch.getMessage()); |
| 222 |
std::string what = message; |
| 223 |
XMLString::release(&message); |
| 224 |
throw Base::XMLParseException(what); |
| 225 |
} |
| 226 |
catch (...) { |
| 227 |
throw Base::XMLBaseException("Unexpected XML exception"); |
| 228 |
} |
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
void Base::XMLReader::readElement(const char* ElementName) |
| 233 |
{ |
| 234 |
bool ok {}; |
| 235 |
|
| 236 |
endCharStream(); |
| 237 |
int currentLevel = Level; |
| 238 |
std::string currentName = LocalName; |
| 239 |
do { |
| 240 |
ok = read(); |
| 241 |
if (!ok) { |
| 242 |
break; |
| 243 |
} |
| 244 |
if (ReadType == EndElement && currentName == LocalName && currentLevel >= Level) { |
| 245 |
// we have reached the end of the element when calling this method |
| 246 |
// thus we must stop reading on. |
| 247 |
break; |
| 248 |
} |
| 249 |
if (ReadType == EndDocument) { |
| 250 |
// the end of the document has been reached but we still try to continue on reading |
| 251 |
throw Base::XMLParseException("End of document reached"); |
| 252 |
} |
| 253 |
} while ((ReadType != StartElement && ReadType != StartEndElement) |
| 254 |
|| (ElementName && LocalName != ElementName)); |
| 255 |
} |
| 256 |
|
| 257 |
bool Base::XMLReader::readNextElement() |
| 258 |
{ |
| 259 |
bool ok {}; |
| 260 |
while (true) { |
| 261 |
ok = read(); |
| 262 |
if (!ok) { |
| 263 |
break; |
| 264 |
} |
| 265 |
if (ReadType == StartElement) { |
| 266 |
break; |
| 267 |
} |
| 268 |
if (ReadType == StartEndElement) { |
| 269 |
break; |
| 270 |
} |
| 271 |
if (ReadType == EndElement) { |
| 272 |
break; |
| 273 |
} |
| 274 |
if (ReadType == EndDocument) { |
| 275 |
break; |
| 276 |
} |
| 277 |
}; |
| 278 |
|
| 279 |
return (ReadType == StartElement || ReadType == StartEndElement); |
| 280 |
} |
| 281 |
|
| 282 |
int Base::XMLReader::level() const |
| 283 |
{ |
| 284 |
return Level; |
| 285 |
} |
| 286 |
|
| 287 |
bool Base::XMLReader::isEndOfElement() const |
| 288 |
{ |
| 289 |
return (ReadType == EndElement); |
| 290 |
} |
| 291 |
|
| 292 |
bool Base::XMLReader::isStartOfDocument() const |
| 293 |
{ |
| 294 |
return (ReadType == StartDocument); |
| 295 |
} |
| 296 |
|
| 297 |
bool Base::XMLReader::isEndOfDocument() const |
| 298 |
{ |
| 299 |
return (ReadType == EndDocument); |
| 300 |
} |
| 301 |
|
| 302 |
void Base::XMLReader::readEndElement(const char* ElementName, int level) |
| 303 |
{ |
| 304 |
endCharStream(); |
| 305 |
|
| 306 |
// if we are already at the end of the current element |
| 307 |
if ((ReadType == EndElement || ReadType == StartEndElement) && ElementName |
| 308 |
&& LocalName == ElementName && (level < 0 || level == Level)) { |
| 309 |
return; |
| 310 |
} |
| 311 |
if (ReadType == EndDocument) { |
| 312 |
// the end of the document has been reached but we still try to continue on reading |
| 313 |
throw Base::XMLParseException("End of document reached"); |
| 314 |
} |
| 315 |
|
| 316 |
bool ok {}; |
| 317 |
do { |
| 318 |
ok = read(); |
| 319 |
if (!ok) { |
| 320 |
break; |
| 321 |
} |
| 322 |
if (ReadType == EndDocument) { |
| 323 |
break; |
| 324 |
} |
| 325 |
} while (ReadType != EndElement |
| 326 |
|| (ElementName && (LocalName != ElementName || (level >= 0 && level != Level)))); |
| 327 |
} |
| 328 |
|
| 329 |
void Base::XMLReader::readCharacters(const char* filename, CharStreamFormat format) |
| 330 |
{ |
| 331 |
Base::FileInfo fi(filename); |
| 332 |
Base::ofstream to(fi, std::ios::out | std::ios::binary | std::ios::trunc); |
| 333 |
if (!to) { |
| 334 |
throw Base::FileException("XMLReader::readCharacters() Could not open file!"); |
| 335 |
} |
| 336 |
|
| 337 |
beginCharStream(format) >> to.rdbuf(); |
| 338 |
to.close(); |
| 339 |
endCharStream(); |
| 340 |
} |
| 341 |
|
| 342 |
std::streamsize Base::XMLReader::read(char_type* s, std::streamsize n) |
| 343 |
{ |
| 344 |
|
| 345 |
char_type* buf = s; |
| 346 |
if (CharacterOffset < 0) { |
| 347 |
return -1; |
| 348 |
} |
| 349 |
|
| 350 |
for (;;) { |
| 351 |
std::streamsize copy_size = static_cast<std::streamsize>(Characters.size()) - CharacterOffset; |
| 352 |
if (n < copy_size) { |
| 353 |
copy_size = n; |
| 354 |
} |
| 355 |
std::memcpy(s, Characters.c_str() + CharacterOffset, copy_size); |
| 356 |
n -= copy_size; |
| 357 |
s += copy_size; |
| 358 |
CharacterOffset += copy_size; |
| 359 |
|
| 360 |
if (!n) { |
| 361 |
break; |
| 362 |
} |
| 363 |
|
| 364 |
if (ReadType == Chars) { |
| 365 |
read(); |
| 366 |
} |
| 367 |
else { |
| 368 |
CharacterOffset = -1; |
| 369 |
break; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
return s - buf; |
| 374 |
} |
| 375 |
|
| 376 |
void Base::XMLReader::endCharStream() |
| 377 |
{ |
| 378 |
CharacterOffset = -1; |
| 379 |
CharStream.reset(); |
| 380 |
} |
| 381 |
|
| 382 |
std::istream& Base::XMLReader::charStream() |
| 383 |
{ |
| 384 |
if (!CharStream) { |
| 385 |
throw Base::XMLParseException("no current character stream"); |
| 386 |
} |
| 387 |
return *CharStream; |
| 388 |
} |
| 389 |
|
| 390 |
std::istream& Base::XMLReader::beginCharStream(CharStreamFormat format) |
| 391 |
{ |
| 392 |
if (CharStream) { |
| 393 |
throw Base::XMLParseException("recursive character stream"); |
| 394 |
} |
| 395 |
|
| 396 |
// TODO: An XML element can actually contain a mix of child elements and |
| 397 |
// characters. So we should not actually demand 'StartElement' here. But |
| 398 |
// with the current implementation of character stream, we cannot track |
| 399 |
// child elements and character content at the same time. |
| 400 |
if (ReadType == StartElement) { |
| 401 |
CharacterOffset = 0; |
| 402 |
read(); |
| 403 |
} |
| 404 |
else if (ReadType == StartEndElement) { |
| 405 |
// If we are currently at a self-closing element, just leave the offset |
| 406 |
// as negative and do not read any characters. This will result in an |
| 407 |
// empty input stream for the caller. |
| 408 |
CharacterOffset = -1; |
| 409 |
} |
| 410 |
else { |
| 411 |
throw Base::XMLParseException("invalid state while reading character stream"); |
| 412 |
} |
| 413 |
|
| 414 |
CharStream = std::make_unique<boost::iostreams::filtering_istream>(); |
| 415 |
auto* filteringStream = dynamic_cast<boost::iostreams::filtering_istream*>(CharStream.get()); |
| 416 |
if (format == CharStreamFormat::Base64Encoded) { |
| 417 |
filteringStream->push( |
| 418 |
base64_decoder(Base::base64DefaultBufferSize, Base64ErrorHandling::silent) |
| 419 |
); |
| 420 |
} |
| 421 |
filteringStream->push(boost::ref(*this)); |
| 422 |
return *CharStream; |
| 423 |
} |
| 424 |
|
| 425 |
void Base::XMLReader::readBinFile(const char* filename) |
| 426 |
{ |
| 427 |
Base::FileInfo fi(filename); |
| 428 |
Base::ofstream to(fi, std::ios::out | std::ios::binary); |
| 429 |
if (!to) { |
| 430 |
throw Base::FileException("XMLReader::readBinFile() Could not open file!"); |
| 431 |
} |
| 432 |
|
| 433 |
bool ok {}; |
| 434 |
do { |
| 435 |
ok = read(); |
| 436 |
if (!ok) { |
| 437 |
break; |
| 438 |
} |
| 439 |
} while (ReadType != EndCDATA); |
| 440 |
|
| 441 |
to << Base::base64_decode(Characters); |
| 442 |
to.close(); |
| 443 |
} |
| 444 |
|
| 445 |
void Base::XMLReader::readFiles(zipios::ZipInputStream& zipstream) const |
| 446 |
{ |
| 447 |
// It's possible that not all objects inside the document could be created, e.g. if a module |
| 448 |
// is missing that would know these object types. So, there may be data files inside the zip |
| 449 |
// file that cannot be read. We simply ignore these files. |
| 450 |
// On the other hand, however, it could happen that a file should be read that is not part of |
| 451 |
// the zip file. This happens e.g. if a document is written without GUI up but is read with GUI |
| 452 |
// up. In this case the associated GUI document asks for its file which is not part of the ZIP |
| 453 |
// file, then. |
| 454 |
// In either case it's guaranteed that the order of the files is kept. |
| 455 |
zipios::ConstEntryPointer entry; |
| 456 |
try { |
| 457 |
entry = zipstream.getNextEntry(); |
| 458 |
} |
| 459 |
catch (const std::exception&) { |
| 460 |
// There is no further file at all. This can happen if the |
| 461 |
// project file was created without GUI |
| 462 |
return; |
| 463 |
} |
| 464 |
std::vector<FileEntry>::const_iterator it = FileList.begin(); |
| 465 |
Base::SequencerLauncher seq("Importing project files...", FileList.size()); |
| 466 |
while (entry->isValid() && it != FileList.end()) { |
| 467 |
std::vector<FileEntry>::const_iterator jt = it; |
| 468 |
// Check if the current entry is registered, otherwise check the next registered files as |
| 469 |
// soon as both file names match |
| 470 |
while (jt != FileList.end() && entry->getName() != jt->FileName) { |
| 471 |
++jt; |
| 472 |
} |
| 473 |
// If this condition is true both file names match and we can read-in the data, otherwise |
| 474 |
// no file name for the current entry in the zip was registered. |
| 475 |
if (jt != FileList.end()) { |
| 476 |
try { |
| 477 |
Base::Reader reader(zipstream, jt->FileName, FileVersion); |
| 478 |
jt->Object->RestoreDocFile(reader); |
| 479 |
if (reader.getLocalReader()) { |
| 480 |
reader.getLocalReader()->readFiles(zipstream); |
| 481 |
} |
| 482 |
} |
| 483 |
catch (...) { |
| 484 |
// For any exception we just continue with the next file. |
| 485 |
// It doesn't matter if the last reader has read more or |
| 486 |
// less data than the file size would allow. |
| 487 |
// All what we need to do is to notify the user about the |
| 488 |
// failure. |
| 489 |
Base::Console().error( |
| 490 |
"Reading failed from embedded file: %s\n", |
| 491 |
entry->toString().c_str() |
| 492 |
); |
| 493 |
FailedFiles.push_back(jt->FileName); |
| 494 |
} |
| 495 |
// Go to the next registered file name |
| 496 |
it = jt + 1; |
| 497 |
} |
| 498 |
|
| 499 |
seq.next(); |
| 500 |
|
| 501 |
// In either case we must go to the next entry |
| 502 |
try { |
| 503 |
entry = zipstream.getNextEntry(); |
| 504 |
} |
| 505 |
catch (const std::exception&) { |
| 506 |
// there is no further entry |
| 507 |
break; |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
const char* Base::XMLReader::addFile(const char* Name, Base::Persistence* Object) |
| 513 |
{ |
| 514 |
FileEntry temp; |
| 515 |
temp.FileName = Name; |
| 516 |
temp.Object = Object; |
| 517 |
|
| 518 |
FileList.push_back(temp); |
| 519 |
|
| 520 |
return Name; |
| 521 |
} |
| 522 |
|
| 523 |
bool Base::XMLReader::hasFilenames() const |
| 524 |
{ |
| 525 |
return !FileList.empty(); |
| 526 |
} |
| 527 |
|
| 528 |
bool Base::XMLReader::hasReadFailed(const std::string& filename) const |
| 529 |
{ |
| 530 |
return std::ranges::find(FailedFiles, filename) != FailedFiles.end(); |
| 531 |
} |
| 532 |
|
| 533 |
bool Base::XMLReader::isRegistered(Base::Persistence* Object) const |
| 534 |
{ |
| 535 |
if (Object) { |
| 536 |
for (const auto& it : FileList) { |
| 537 |
if (it.Object == Object) { |
| 538 |
return true; |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
return false; |
| 544 |
} |
| 545 |
|
| 546 |
void Base::XMLReader::addName(const char* /*unused*/, const char* /*unused*/) |
| 547 |
{} |
| 548 |
|
| 549 |
const char* Base::XMLReader::getName(const char* name) const |
| 550 |
{ |
| 551 |
return name; |
| 552 |
} |
| 553 |
|
| 554 |
bool Base::XMLReader::doNameMapping() const |
| 555 |
{ |
| 556 |
return false; |
| 557 |
} |
| 558 |
|
| 559 |
// --------------------------------------------------------------------------- |
| 560 |
// Base::XMLReader: Implementation of the SAX DocumentHandler interface |
| 561 |
// --------------------------------------------------------------------------- |
| 562 |
void Base::XMLReader::startDocument() |
| 563 |
{ |
| 564 |
ReadType = StartDocument; |
| 565 |
} |
| 566 |
|
| 567 |
void Base::XMLReader::endDocument() |
| 568 |
{ |
| 569 |
ReadType = EndDocument; |
| 570 |
} |
| 571 |
|
| 572 |
void Base::XMLReader::startElement( |
| 573 |
const XMLCh* const /*uri*/, |
| 574 |
const XMLCh* const localname, |
| 575 |
const XMLCh* const /*qname*/, |
| 576 |
const XERCES_CPP_NAMESPACE_QUALIFIER Attributes& attrs |
| 577 |
) |
| 578 |
{ |
| 579 |
Level++; // new scope |
| 580 |
LocalName = StrX(localname).c_str(); |
| 581 |
|
| 582 |
// saving attributes of the current scope, delete all previously stored ones |
| 583 |
AttrMap.clear(); |
| 584 |
for (unsigned int i = 0; i < attrs.getLength(); i++) { |
| 585 |
AttrMap[StrX(attrs.getQName(i)).c_str()] = StrXUTF8(attrs.getValue(i)).c_str(); |
| 586 |
} |
| 587 |
|
| 588 |
ReadType = StartElement; |
| 589 |
} |
| 590 |
|
| 591 |
void Base::XMLReader::endElement(const XMLCh* const /*uri*/, const XMLCh* const localname, const XMLCh* const /*qname*/) |
| 592 |
{ |
| 593 |
Level--; // end of scope |
| 594 |
LocalName = StrX(localname).c_str(); |
| 595 |
|
| 596 |
if (ReadType == StartElement) { |
| 597 |
ReadType = StartEndElement; |
| 598 |
} |
| 599 |
else { |
| 600 |
ReadType = EndElement; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
void Base::XMLReader::startCDATA() |
| 605 |
{ |
| 606 |
ReadType = StartCDATA; |
| 607 |
} |
| 608 |
|
| 609 |
void Base::XMLReader::endCDATA() |
| 610 |
{ |
| 611 |
ReadType = EndCDATA; |
| 612 |
} |
| 613 |
|
| 614 |
void Base::XMLReader::characters(const XMLCh* const chars, const XMLSize_t length) |
| 615 |
{ |
| 616 |
Characters = StrX(chars).c_str(); |
| 617 |
ReadType = Chars; |
| 618 |
CharacterCount += length; |
| 619 |
} |
| 620 |
|
| 621 |
void Base::XMLReader::ignorableWhitespace(const XMLCh* const /*chars*/, const XMLSize_t /*length*/) |
| 622 |
{ |
| 623 |
// fSpaceCount += length; |
| 624 |
} |
| 625 |
|
| 626 |
void Base::XMLReader::resetDocument() |
| 627 |
{ |
| 628 |
// fAttrCount = 0; |
| 629 |
// fCharacterCount = 0; |
| 630 |
// fElementCount = 0; |
| 631 |
// fSpaceCount = 0; |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
// --------------------------------------------------------------------------- |
| 636 |
// Base::XMLReader: Overrides of the SAX ErrorHandler interface |
| 637 |
// --------------------------------------------------------------------------- |
| 638 |
void Base::XMLReader::error(const XERCES_CPP_NAMESPACE_QUALIFIER SAXParseException& e) |
| 639 |
{ |
| 640 |
// print some details to error output and throw an |
| 641 |
// exception to abort the parsing |
| 642 |
cerr << "Error at file " << StrX(e.getSystemId()) << ", line " << e.getLineNumber() << ", char " |
| 643 |
<< e.getColumnNumber() << endl; |
| 644 |
throw e; |
| 645 |
} |
| 646 |
|
| 647 |
void Base::XMLReader::fatalError(const XERCES_CPP_NAMESPACE_QUALIFIER SAXParseException& e) |
| 648 |
{ |
| 649 |
// print some details to error output and throw an |
| 650 |
// exception to abort the parsing |
| 651 |
cerr << "Fatal Error at file " << StrX(e.getSystemId()) << ", line " << e.getLineNumber() |
| 652 |
<< ", char " << e.getColumnNumber() << endl; |
| 653 |
throw e; |
| 654 |
} |
| 655 |
|
| 656 |
void Base::XMLReader::warning(const XERCES_CPP_NAMESPACE_QUALIFIER SAXParseException& e) |
| 657 |
{ |
| 658 |
// print some details to error output and throw an |
| 659 |
// exception to abort the parsing |
| 660 |
cerr << "Warning at file " << StrX(e.getSystemId()) << ", line " << e.getLineNumber() |
| 661 |
<< ", char " << e.getColumnNumber() << endl; |
| 662 |
throw e; |
| 663 |
} |
| 664 |
|
| 665 |
void Base::XMLReader::resetErrors() |
| 666 |
{} |
| 667 |
|
| 668 |
bool Base::XMLReader::testStatus(ReaderStatus pos) const |
| 669 |
{ |
| 670 |
return StatusBits.test(static_cast<size_t>(pos)); |
| 671 |
} |
| 672 |
|
| 673 |
void Base::XMLReader::setStatus(ReaderStatus pos, bool on) |
| 674 |
{ |
| 675 |
StatusBits.set(static_cast<size_t>(pos), on); |
| 676 |
} |
| 677 |
|
| 678 |
void Base::XMLReader::setPartialRestore(bool on) |
| 679 |
{ |
| 680 |
setStatus(PartialRestore, on); |
| 681 |
setStatus(PartialRestoreInDocumentObject, on); |
| 682 |
setStatus(PartialRestoreInProperty, on); |
| 683 |
setStatus(PartialRestoreInObject, on); |
| 684 |
} |
| 685 |
|
| 686 |
void Base::XMLReader::clearPartialRestoreDocumentObject() |
| 687 |
{ |
| 688 |
setStatus(PartialRestoreInDocumentObject, false); |
| 689 |
setStatus(PartialRestoreInProperty, false); |
| 690 |
setStatus(PartialRestoreInObject, false); |
| 691 |
} |
| 692 |
|
| 693 |
void Base::XMLReader::clearPartialRestoreProperty() |
| 694 |
{ |
| 695 |
setStatus(PartialRestoreInProperty, false); |
| 696 |
setStatus(PartialRestoreInObject, false); |
| 697 |
} |
| 698 |
|
| 699 |
void Base::XMLReader::clearPartialRestoreObject() |
| 700 |
{ |
| 701 |
setStatus(PartialRestoreInObject, false); |
| 702 |
} |
| 703 |
|
| 704 |
// ---------------------------------------------------------- |
| 705 |
|
| 706 |
// NOLINTNEXTLINE |
| 707 |
Base::Reader::Reader(std::istream& str, const std::string& name, int version) |
| 708 |
: std::istream(str.rdbuf()) |
| 709 |
, _str(str) |
| 710 |
, _name(name) |
| 711 |
, fileVersion(version) |
| 712 |
{} |
| 713 |
|
| 714 |
std::string Base::Reader::getFileName() const |
| 715 |
{ |
| 716 |
return this->_name; |
| 717 |
} |
| 718 |
|
| 719 |
int Base::Reader::getFileVersion() const |
| 720 |
{ |
| 721 |
return fileVersion; |
| 722 |
} |
| 723 |
|
| 724 |
std::istream& Base::Reader::getStream() |
| 725 |
{ |
| 726 |
return this->_str; |
| 727 |
} |
| 728 |
|
| 729 |
void Base::Reader::initLocalReader(std::shared_ptr<Base::XMLReader> reader) |
| 730 |
{ |
| 731 |
this->localreader = reader; |
| 732 |
} |
| 733 |
|
| 734 |
std::shared_ptr<Base::XMLReader> Base::Reader::getLocalReader() const |
| 735 |
{ |
| 736 |
return (this->localreader); |
| 737 |
} |