| 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 |
#ifndef SRC_BASE_READER_H_ |
| 26 |
#define SRC_BASE_READER_H_ |
| 27 |
|
| 28 |
#include <bitset> |
| 29 |
#include <map> |
| 30 |
#include <memory> |
| 31 |
#include <string> |
| 32 |
#include <vector> |
| 33 |
|
| 34 |
#include <xercesc/framework/XMLPScanToken.hpp> |
| 35 |
#include <xercesc/sax2/DefaultHandler.hpp> |
| 36 |
|
| 37 |
#include <boost/iostreams/categories.hpp> |
| 38 |
|
| 39 |
#include "FileInfo.h" |
| 40 |
|
| 41 |
|
| 42 |
namespace zipios |
| 43 |
{ |
| 44 |
class ZipInputStream; |
| 45 |
} |
| 46 |
#ifndef XERCES_CPP_NAMESPACE_BEGIN |
| 47 |
# define XERCES_CPP_NAMESPACE_QUALIFIER |
| 48 |
namespace XERCES_CPP_NAMESPACE |
| 49 |
{ |
| 50 |
class Attributes; |
| 51 |
class DefaultHandler; |
| 52 |
class SAX2XMLReader; |
| 53 |
} // namespace XERCES_CPP_NAMESPACE |
| 54 |
#else |
| 55 |
XERCES_CPP_NAMESPACE_BEGIN |
| 56 |
class Attributes; |
| 57 |
class DefaultHandler; |
| 58 |
class SAX2XMLReader; |
| 59 |
XERCES_CPP_NAMESPACE_END |
| 60 |
#endif |
| 61 |
|
| 62 |
namespace Base |
| 63 |
{ |
| 64 |
class Persistence; |
| 65 |
|
| 66 |
/** The XML reader class |
| 67 |
* This is an important helper class for the store and retrieval system |
| 68 |
* of objects in FreeCAD. These classes mainly inherit the App::Persitance |
| 69 |
* base class and implement the Restore() method. |
| 70 |
* \par |
| 71 |
* The reader gets mainly initialized by the App::Document on retrieving a |
| 72 |
* document out of a file. From there subsequently the Restore() method will |
| 73 |
* by called on all object stored. |
| 74 |
* \par |
| 75 |
* A simple example is the Restore of App::PropertyString: |
| 76 |
* \code |
| 77 |
void PropertyString::Save (short indent,std::ostream &str) |
| 78 |
{ |
| 79 |
str << "<String value=\"" << _cValue.c_str() <<"\"/>" ; |
| 80 |
} |
| 81 |
|
| 82 |
void PropertyString::Restore(Base::Reader &reader) |
| 83 |
{ |
| 84 |
// read my Element |
| 85 |
reader.readElement("String"); |
| 86 |
// get the value of my Attribute |
| 87 |
_cValue = reader.getAttribute<const char*>("value"); |
| 88 |
} |
| 89 |
|
| 90 |
* \endcode |
| 91 |
* \par |
| 92 |
* An more complicated example is the retrieval of the App::PropertyContainer: |
| 93 |
* \code |
| 94 |
void PropertyContainer::Save (short indent,std::ostream &str) |
| 95 |
{ |
| 96 |
std::map<std::string,Property*> Map; |
| 97 |
getPropertyMap(Map); |
| 98 |
|
| 99 |
str << ind(indent) << "<Properties Count=\"" << Map.size() << "\">" << endl; |
| 100 |
std::map<std::string,Property*>::iterator it; |
| 101 |
for(it = Map.begin(); it != Map.end(); ++it) |
| 102 |
{ |
| 103 |
str << ind(indent+1) << "<Property name=\"" << it->first << "\" type=\"" << |
| 104 |
it->second->getTypeId().getName() << "\">" ; it->second->Save(indent+2,str); str << "</Property>" << |
| 105 |
endl; |
| 106 |
} |
| 107 |
str << ind(indent) << "</Properties>" << endl; |
| 108 |
} |
| 109 |
|
| 110 |
void PropertyContainer::Restore(Base::Reader &reader) |
| 111 |
{ |
| 112 |
reader.readElement("Properties"); |
| 113 |
int Cnt = reader.getAttribute<long>("Count"); |
| 114 |
|
| 115 |
for(int i=0 ;i<Cnt ;i++) |
| 116 |
{ |
| 117 |
reader.readElement("Property"); |
| 118 |
string PropName = reader.getAttribute<const char*>("name"); |
| 119 |
Property* prop = getPropertyByName(PropName.c_str()); |
| 120 |
if(prop) |
| 121 |
prop->Restore(reader); |
| 122 |
|
| 123 |
reader.readEndElement("Property"); |
| 124 |
} |
| 125 |
reader.readEndElement("Properties"); |
| 126 |
} |
| 127 |
* \endcode |
| 128 |
* \see Base::Persistence |
| 129 |
* \author Juergen Riegel |
| 130 |
*/ |
| 131 |
class BaseExport XMLReader: public XERCES_CPP_NAMESPACE_QUALIFIER DefaultHandler |
| 132 |
{ |
| 133 |
public: |
| 134 |
enum ReaderStatus |
| 135 |
{ |
| 136 |
PartialRestore = 0, // This bit indicates that a partial restore took place somewhere in |
| 137 |
// this Document |
| 138 |
PartialRestoreInDocumentObject = 1, // This bit is local to the DocumentObject being read |
| 139 |
// indicating a partial restore therein |
| 140 |
PartialRestoreInProperty = 2, // Local to the Property |
| 141 |
PartialRestoreInObject = 3 // Local to the object partially restored itself |
| 142 |
}; |
| 143 |
/// open the file and read the first element |
| 144 |
XMLReader(const char* FileName, std::istream&); |
| 145 |
~XMLReader() override; |
| 146 |
|
| 147 |
/** @name boost iostream device interface */ |
| 148 |
//@{ |
| 149 |
using category = boost::iostreams::source_tag; |
| 150 |
using char_type = char; |
| 151 |
std::streamsize read(char_type* s, std::streamsize n); |
| 152 |
//@} |
| 153 |
|
| 154 |
bool isValid() const |
| 155 |
{ |
| 156 |
return _valid; |
| 157 |
} |
| 158 |
bool isVerbose() const |
| 159 |
{ |
| 160 |
return _verbose; |
| 161 |
} |
| 162 |
void setVerbose(bool on) |
| 163 |
{ |
| 164 |
_verbose = on; |
| 165 |
} |
| 166 |
|
| 167 |
/** @name Parser handling */ |
| 168 |
//@{ |
| 169 |
/// get the local name of the current Element |
| 170 |
const char* localName() const; |
| 171 |
/// get the current element level |
| 172 |
int level() const; |
| 173 |
|
| 174 |
/// return true if the end of an element is reached, false otherwise |
| 175 |
bool isEndOfElement() const; |
| 176 |
|
| 177 |
/// return true if the on the start of the document, false otherwise |
| 178 |
bool isStartOfDocument() const; |
| 179 |
|
| 180 |
/// return true if the end of the document is reached, false otherwise |
| 181 |
bool isEndOfDocument() const; |
| 182 |
|
| 183 |
/// read until a start element is found (\<name\>) or start-end element (\<name/\>) (with |
| 184 |
/// special name if given) |
| 185 |
void readElement(const char* ElementName = nullptr); |
| 186 |
|
| 187 |
/// Read in the next element. Return true if it succeeded and false otherwise |
| 188 |
bool readNextElement(); |
| 189 |
|
| 190 |
/** read until an end element is found |
| 191 |
* |
| 192 |
* @param ElementName: optional end element name to look for. If given, then |
| 193 |
* the parser will read until this name is found. |
| 194 |
* |
| 195 |
* @param level: optional level to look for. If given, then the parser will |
| 196 |
* read until this level. Note that the parse only increase the level when |
| 197 |
* finding a start element, not start-end element, and decrease the level |
| 198 |
* after finding an end element. So, if you obtain the parser level after |
| 199 |
* calling readElement(), you should specify a level minus one when calling |
| 200 |
* this function. This \c level parameter is only useful if you know the |
| 201 |
* child element may have the same name as its parent, otherwise, using \c |
| 202 |
* ElementName is enough. |
| 203 |
*/ |
| 204 |
void readEndElement(const char* ElementName = nullptr, int level = -1); |
| 205 |
/// read until characters are found |
| 206 |
void readCharacters(const char* filename, CharStreamFormat format = CharStreamFormat::Raw); |
| 207 |
|
| 208 |
/** Obtain an input stream for reading characters |
| 209 |
* |
| 210 |
* @return Return a input stream for reading characters. The stream will be |
| 211 |
* auto destroyed when you call with readElement() or readEndElement(), or |
| 212 |
* you can end it explicitly with endCharStream(). |
| 213 |
*/ |
| 214 |
std::istream& beginCharStream(CharStreamFormat format = CharStreamFormat::Raw); |
| 215 |
/// Manually end the current character stream |
| 216 |
void endCharStream(); |
| 217 |
/// Obtain the current character stream |
| 218 |
std::istream& charStream(); |
| 219 |
//@} |
| 220 |
|
| 221 |
/// read binary file |
| 222 |
void readBinFile(const char*); |
| 223 |
//@} |
| 224 |
|
| 225 |
/** @name Attribute handling */ |
| 226 |
//@{ |
| 227 |
/// get the number of attributes of the current element |
| 228 |
unsigned int getAttributeCount() const; |
| 229 |
/// check if the read element has a special attribute |
| 230 |
bool hasAttribute(const char* AttrName) const; |
| 231 |
|
| 232 |
private: |
| 233 |
// all explicit template instantiations - this is for getting |
| 234 |
// a compile error, rather than linker error. |
| 235 |
template<typename T> |
| 236 |
static constexpr bool instantiated = std::is_same_v<T, bool> || std::is_same_v<T, const char*> |
| 237 |
|| std::is_same_v<T, double> || std::is_same_v<T, int> || std::is_same_v<T, long> |
| 238 |
|| std::is_same_v<T, unsigned long>; |
| 239 |
|
| 240 |
public: |
| 241 |
/// return the named attribute as T (does type checking); if missing return defaultValue. |
| 242 |
/// If defaultValue is not set, it will default to the default initialization of the |
| 243 |
/// corresponding type; bool: false, int: 0, ... as if one had used defaultValue=bool{} |
| 244 |
/// or defaultValue=int{} |
| 245 |
// General template, mark delete as it's not implemented, and should not be used! |
| 246 |
template<typename T> |
| 247 |
requires Base::XMLReader::instantiated<T> |
| 248 |
T getAttribute(const char* AttrName, T defaultValue) const; |
| 249 |
|
| 250 |
/// No default? Will throw exception if not found! |
| 251 |
template<typename T> |
| 252 |
requires Base::XMLReader::instantiated<T> |
| 253 |
T getAttribute(const char* AttrName) const; |
| 254 |
|
| 255 |
/// E.g. std::string, QString |
| 256 |
template<typename T> |
| 257 |
T getAttribute(const char* AttrName) const |
| 258 |
{ |
| 259 |
return T(getAttribute<const char*>(AttrName)); |
| 260 |
} |
| 261 |
/// E.g. std::string, QString |
| 262 |
template<typename T> |
| 263 |
T getAttribute(const char* AttrName, T defaultValue) const |
| 264 |
{ |
| 265 |
return T(getAttribute<const char*>(AttrName, defaultValue)); |
| 266 |
} |
| 267 |
|
| 268 |
/// Enum classes |
| 269 |
template<typename T> |
| 270 |
requires std::is_enum_v<T> |
| 271 |
T getAttribute(const char* AttrName, T defaultValue) const |
| 272 |
{ |
| 273 |
return static_cast<T>( |
| 274 |
getAttribute<unsigned long>(AttrName, static_cast<unsigned long>(defaultValue)) |
| 275 |
); |
| 276 |
} |
| 277 |
/// Enum classes |
| 278 |
template<typename T> |
| 279 |
requires std::is_enum_v<T> |
| 280 |
T getAttribute(const char* AttrName) const |
| 281 |
{ |
| 282 |
return static_cast<T>(getAttribute<unsigned long>(AttrName)); |
| 283 |
} |
| 284 |
|
| 285 |
/** @name additional file reading */ |
| 286 |
//@{ |
| 287 |
/// add a read request of a persistent object |
| 288 |
const char* addFile(const char* Name, Base::Persistence* Object); |
| 289 |
/// process the requested file writes |
| 290 |
void readFiles(zipios::ZipInputStream& zipstream) const; |
| 291 |
/// Returns whether reader has any registered filenames |
| 292 |
bool hasFilenames() const; |
| 293 |
/// returns true if reading the file \a filename has failed |
| 294 |
bool hasReadFailed(const std::string& filename) const; |
| 295 |
bool isRegistered(Base::Persistence* Object) const; |
| 296 |
virtual void addName(const char*, const char*); |
| 297 |
virtual const char* getName(const char*) const; |
| 298 |
virtual bool doNameMapping() const; |
| 299 |
//@} |
| 300 |
|
| 301 |
/// Schema Version of the document |
| 302 |
int DocumentSchema {0}; |
| 303 |
/// Version of FreeCAD that wrote this document |
| 304 |
std::string ProgramVersion; |
| 305 |
/// Version of the file format |
| 306 |
int FileVersion {0}; |
| 307 |
|
| 308 |
/// sets simultaneously the global and local PartialRestore bits |
| 309 |
void setPartialRestore(bool on); |
| 310 |
|
| 311 |
void clearPartialRestoreDocumentObject(); |
| 312 |
void clearPartialRestoreProperty(); |
| 313 |
void clearPartialRestoreObject(); |
| 314 |
|
| 315 |
/// return the status bits |
| 316 |
bool testStatus(ReaderStatus pos) const; |
| 317 |
/// set the status bits |
| 318 |
void setStatus(ReaderStatus pos, bool on); |
| 319 |
|
| 320 |
protected: |
| 321 |
/// read the next element |
| 322 |
bool read(); |
| 323 |
|
| 324 |
// ----------------------------------------------------------------------- |
| 325 |
// Handlers for the SAX ContentHandler interface |
| 326 |
// ----------------------------------------------------------------------- |
| 327 |
/** @name Content handler */ |
| 328 |
//@{ |
| 329 |
void startDocument() override; |
| 330 |
void endDocument() override; |
| 331 |
void startElement( |
| 332 |
const XMLCh* const uri, |
| 333 |
const XMLCh* const localname, |
| 334 |
const XMLCh* const qname, |
| 335 |
const XERCES_CPP_NAMESPACE_QUALIFIER Attributes& attrs |
| 336 |
) override; |
| 337 |
void endElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname) override; |
| 338 |
void characters(const XMLCh* const chars, const XMLSize_t length) override; |
| 339 |
void ignorableWhitespace(const XMLCh* const chars, const XMLSize_t length) override; |
| 340 |
//@} |
| 341 |
|
| 342 |
/** @name Lexical handler */ |
| 343 |
//@{ |
| 344 |
void startCDATA() override; |
| 345 |
void endCDATA() override; |
| 346 |
//@} |
| 347 |
|
| 348 |
/** @name Document handler */ |
| 349 |
//@{ |
| 350 |
void resetDocument() override; |
| 351 |
//@} |
| 352 |
|
| 353 |
|
| 354 |
// ----------------------------------------------------------------------- |
| 355 |
// Handlers for the SAX ErrorHandler interface |
| 356 |
// ----------------------------------------------------------------------- |
| 357 |
/** @name Error handler */ |
| 358 |
//@{ |
| 359 |
void warning(const XERCES_CPP_NAMESPACE_QUALIFIER SAXParseException& exc) override; |
| 360 |
void error(const XERCES_CPP_NAMESPACE_QUALIFIER SAXParseException& exc) override; |
| 361 |
void fatalError(const XERCES_CPP_NAMESPACE_QUALIFIER SAXParseException& exc) override; |
| 362 |
void resetErrors() override; |
| 363 |
//@} |
| 364 |
|
| 365 |
private: |
| 366 |
int Level {0}; |
| 367 |
std::string LocalName; |
| 368 |
std::string Characters; |
| 369 |
unsigned int CharacterCount {0}; |
| 370 |
std::streamsize CharacterOffset {-1}; |
| 371 |
|
| 372 |
std::map<std::string, std::string> AttrMap; |
| 373 |
using AttrMapType = std::map<std::string, std::string>; |
| 374 |
|
| 375 |
enum |
| 376 |
{ |
| 377 |
None = 0, |
| 378 |
Chars, |
| 379 |
StartDocument, |
| 380 |
EndDocument, |
| 381 |
StartElement, |
| 382 |
StartEndElement, |
| 383 |
EndElement, |
| 384 |
StartCDATA, |
| 385 |
EndCDATA |
| 386 |
} ReadType {None}; |
| 387 |
|
| 388 |
|
| 389 |
FileInfo _File; |
| 390 |
XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader* parser; |
| 391 |
XERCES_CPP_NAMESPACE_QUALIFIER XMLPScanToken token; |
| 392 |
bool _valid {false}; |
| 393 |
bool _verbose {true}; |
| 394 |
|
| 395 |
public: |
| 396 |
struct FileEntry |
| 397 |
{ |
| 398 |
std::string FileName; |
| 399 |
Base::Persistence* Object; |
| 400 |
}; |
| 401 |
std::vector<FileEntry> FileList; |
| 402 |
|
| 403 |
private: |
| 404 |
mutable std::vector<std::string> FailedFiles; |
| 405 |
|
| 406 |
std::bitset<32> StatusBits; |
| 407 |
|
| 408 |
std::unique_ptr<std::istream> CharStream; |
| 409 |
}; |
| 410 |
|
| 411 |
class BaseExport Reader: public std::istream |
| 412 |
{ |
| 413 |
public: |
| 414 |
Reader(std::istream&, const std::string&, int version); |
| 415 |
std::istream& getStream(); |
| 416 |
std::string getFileName() const; |
| 417 |
int getFileVersion() const; |
| 418 |
void initLocalReader(std::shared_ptr<Base::XMLReader>); |
| 419 |
std::shared_ptr<Base::XMLReader> getLocalReader() const; |
| 420 |
|
| 421 |
private: |
| 422 |
std::istream& _str; |
| 423 |
std::string _name; |
| 424 |
int fileVersion; |
| 425 |
std::shared_ptr<Base::XMLReader> localreader; |
| 426 |
}; |
| 427 |
|
| 428 |
} // namespace Base |
| 429 |
|
| 430 |
|
| 431 |
#endif // SRC_BASE_READER_H_ |