Implementing agnostic version of ParameterGrp for reading XML, replacing XMLReader from src/Base/reader.cpp with new class DocumentReader

This commit is contained in:
AgCaliva
2023-06-16 15:36:17 -03:00
parent 8db6ab544a
commit 52c55f59a8
18 changed files with 862 additions and 176 deletions

View File

@@ -267,6 +267,7 @@ SET(FreeCADBase_CPP_SRCS
Writer.cpp
XMLTools.cpp
ZipHeader.cpp
DocumentReader.cpp
)
SET(SWIG_HEADERS

314
src/Base/DocumentReader.cpp Normal file
View File

@@ -0,0 +1,314 @@
/***************************************************************************
* Copyright (c) 2011 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
//#include "PreCompiled.h"
//#include <locale>
#include "DocumentReader.h"
//#include "Reader.h"
//#include "Base64.h"
//#include "Console.h"
#include "InputSource.h"
//#include "Persistence.h"
//#include "Sequencer.h"
//#include "Stream.h"
#include "XMLTools.h"
//#ifdef _MSC_VER
//#include <zipios++/zipios-config.h>
//#endif
//#include <zipios++/zipinputstream.h>
#include <Base/Reader.h>
#include <Base/Parameter.h>
#ifndef _PreComp_
//# include <cassert>
//# include <memory>
# include <xercesc/dom/DOM.hpp>
//# include <xercesc/framework/LocalFileFormatTarget.hpp>
//# include <xercesc/framework/LocalFileInputSource.hpp>
//# include <xercesc/framework/MemBufFormatTarget.hpp>
//# include <xercesc/framework/MemBufInputSource.hpp>
# include <xercesc/parsers/XercesDOMParser.hpp>
//# include <xercesc/sax/ErrorHandler.hpp>
//# include <xercesc/sax/SAXParseException.hpp>
//# include <sstream>
//# include <string>
//# include <utility>
#endif
XERCES_CPP_NAMESPACE_USE
//using namespace std;
using namespace Base;
// ---------------------------------------------------------------------------
// DocumentReader: Constructors and Destructor
// ---------------------------------------------------------------------------
static XercesDOMParser::ValSchemes gValScheme = XercesDOMParser::Val_Auto;
DocumentReader::DocumentReader()
{
gDoNamespaces = false;
gDoSchema = false;
gSchemaFullChecking = false;
gDoCreate = true;
/*
gOutputEncoding = nullptr;
gMyEOLSequence = nullptr;
gSplitCdataSections = true;
gDiscardDefaultContent = true;
gUseFilter = true;
gFormatPrettyPrint = true;
*/
}
//DocumentReader::~DocumentReader()
//{
//delete parser;
//}
//int DocumentReader::LoadDocument(const XERCES_CPP_NAMESPACE_QUALIFIER InputSource& inputSource)
//int DocumentReader::LoadDocument(std::istream& Stream,std::string filename)
int DocumentReader::LoadDocument(Base::Reader& reader)
{
FileInfo _File( reader.getFileName() );
StdInputSource inputSource(reader, _File.filePath().c_str());
//
// Create our parser, then attach an error handler to the parser.
// The parser will call back to methods of the ErrorHandler if it
// discovers errors during the course of parsing the XML document.
//
XercesDOMParser *parser = new XercesDOMParser;
parser->setValidationScheme(gValScheme);
parser->setDoNamespaces(gDoNamespaces);
parser->setDoSchema(gDoSchema);
parser->setValidationSchemaFullChecking(gSchemaFullChecking);
parser->setCreateEntityReferenceNodes(gDoCreate);
DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter();
parser->setErrorHandler(errReporter);
//
// Parse the XML file, catching any XML exceptions that might propagate
// out of it.
//
bool errorsOccured = false;
try {
parser->parse(inputSource);
}
catch (const XMLException& e) {
std::cerr << "An error occurred during parsing\n Message: "
<< StrX(e.getMessage()) << std::endl;
errorsOccured = true;
}
catch (const DOMException& e) {
std::cerr << "A DOM error occurred during parsing\n DOMException code: "
<< e.code << std::endl;
errorsOccured = true;
}
catch (...) {
std::cerr << "An error occurred during parsing\n " << std::endl;
errorsOccured = true;
}
if (errorsOccured) {
delete parser;
delete errReporter;
return 0;
}
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* _pDocument = parser->adoptDocument();
delete parser;
delete errReporter;
if (!_pDocument)
throw XMLBaseException("Malformed Parameter document: Invalid document");
DOMElement* rootElem = _pDocument->getDocumentElement();
if (!rootElem)
throw XMLBaseException("Malformed Parameter document: Root group not found");
_pGroupNode = rootElem;
if (!_pGroupNode){
throw XMLBaseException("Malformed document.");
}
return 1;
}
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DocumentReader::GetRootElement() const
{
//if (!_pGroupNode)
//return nullptr;
return _pGroupNode;
}
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DocumentReader::FindElement(const char* Type) const
{
if(!Type)
return nullptr;
for (DOMNode *clChild = _pGroupNode->getFirstChild(); clChild != nullptr; clChild = clChild->getNextSibling()) {
if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) {
if (!strcmp(Type,StrX(clChild->getNodeName()).c_str())) {
if (clChild->getAttributes()->getLength() > 0) {
return static_cast<DOMElement*>(clChild);
}
}
}
}
return nullptr;
}
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DocumentReader::FindElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* Start, const char* Type) const
{
if(!Start || !Type)
return nullptr;
for (DOMNode *clChild = Start->getFirstChild(); clChild != nullptr; clChild = clChild->getNextSibling()) {
if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) {
if (!strcmp(Type,StrX(clChild->getNodeName()).c_str())) {
return static_cast<DOMElement*>(clChild);
//if (clChild->getAttributes()->getLength() > 0) {
//return static_cast<DOMElement*>(clChild);
//}
}
}
}
return nullptr;
}
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DocumentReader::FindNextElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *Prev, const char* Type) const
{
if (!Prev || !Type)
return nullptr;
DOMNode *clChild = Prev;
while ((clChild = clChild->getNextSibling()) != nullptr) {
if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) {
// the right node Type
if (!strcmp(Type,StrX(clChild->getNodeName()).c_str())) {
return static_cast<DOMElement*>(clChild);
}
}
}
return nullptr;
}
/*
//CONTENT:
const char * DocumentReader::GetContent(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DOMEl) const
{
if (!DOMEl)
return nullptr;
return StrX(DOMEl->getAttribute(XStr("Value").unicodeForm())).c_str();
}
std::string DocumentReader::GetContentASCII(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DOMEl) const
{
//return std::string(StrXUTF8(pcElem->getNodeValue()).c_str() );
//maybe its better to use getNodeValue()
if (!DOMEl)
return nullptr;
return std::string( StrXUTF8(DOMEl->getAttribute(XStr("Value").unicodeForm())).c_str() );
}
*/
long DocumentReader::ContentToInt( const char* content ) const
{
return atol( content );
}
unsigned long DocumentReader::ContentToUnsigned(const char* content) const
{
return strtoul(content,nullptr,10);
}
double DocumentReader::ContentToFloat(const char* content) const
{
return atof(content);
}
bool DocumentReader::ContentToBool(const char* content) const
{
if (strcmp(content,"1"))
return false;
else
return true;
}
//ATTRIBUTE:
const char * DocumentReader::GetAttribute(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DOMEl, const char* Attr) const
{
if(!Attr)
return nullptr;
XStr xstr( Attr );
bool hasAttr = DOMEl->hasAttribute(xstr.unicodeForm());
if (!hasAttr){
return nullptr;
}
const XMLCh * attr = DOMEl->getAttribute( xstr.unicodeForm() );
return strdup( StrX( attr ).c_str() );
}
const char * DocumentReader::GetAttribute(const char* Attr) const
{
if(!Attr)
return nullptr;
XStr xstr( Attr );
bool hasAttr = _pGroupNode->hasAttribute(xstr.unicodeForm());
if (!hasAttr){
return nullptr;
}
const XMLCh * attr = _pGroupNode->getAttribute( xstr.unicodeForm() );
//stringLen
return strdup( StrX( attr ).c_str() );//strdup is needed since pointer from strx only exists in context where StrX() is created.
}
/*
unsigned int Base::XMLReader::getAttributeCount() const
{
return static_cast<unsigned int>(AttrMap.size());
}
*/
//Status
void Base::DocumentReader::clearPartialRestoreProperty()
{
setStatus(PartialRestoreInProperty, false);
setStatus(PartialRestoreInObject, false);
}
bool Base::DocumentReader::testStatus(ReaderStatus pos) const
{
return StatusBits.test(static_cast<size_t>(pos));
}
void Base::DocumentReader::setStatus(ReaderStatus pos, bool on)
{
StatusBits.set(static_cast<size_t>(pos), on);
}

98
src/Base/DocumentReader.h Normal file
View File

@@ -0,0 +1,98 @@
/***************************************************************************
* Copyright (c) 2011 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef BASE_DOCUMENTREADER_H
#define BASE_DOCUMENTREADER_H
#include <bitset>
#include <map>
//#include <memory>
//#include <sstream>
#include <string>
#include <vector>
//#include <xercesc/framework/XMLPScanToken.hpp>
//#include <xercesc/sax2/Attributes.hpp>
//#include <xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/util/XercesDefs.hpp>
//#include "FileInfo.h"//reemplazado por:
#include <FCGlobal.h>
//#include <Base/Parameter.h>
XERCES_CPP_NAMESPACE_BEGIN
class DOMNode;
class DOMElement;
// class DefaultHandler;
// class SAX2XMLReader;
XERCES_CPP_NAMESPACE_END
namespace Base
{
class Reader;
class Persistence;
class BaseExport DocumentReader //: public ParameterManager
{
public:
enum ReaderStatus {
PartialRestore = 0, // This bit indicates that a partial restore took place somewhere in this Document
PartialRestoreInDocumentObject = 1, // This bit is local to the DocumentObject being read indicating a partial restore therein
PartialRestoreInProperty = 2, // Local to the Property
PartialRestoreInObject = 3 // Local to the object partially restored itself
};
DocumentReader();
int LoadDocument(Base::Reader& reader);
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *GetRootElement() const;
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *FindElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* Start, const char* Type) const;
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *FindElement(const char* Type) const;
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *FindNextElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *Prev, const char* Type) const;
long ContentToASCII(const char* Content) const;
long ContentToInt(const char* Content) const;
unsigned long ContentToUnsigned(const char* Content) const;
double ContentToFloat(const char* Content) const;
bool ContentToBool(const char* Content) const;
const char* GetAttribute(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* DOMEl, const char* Attr) const;
const char* GetAttribute(const char* Attr) const;
/// return the status bits
bool testStatus(ReaderStatus pos) const;
/// set the status bits
void setStatus(ReaderStatus pos, bool on);
void clearPartialRestoreProperty();
protected:
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *_pGroupNode;
bool gDoNamespaces ;
bool gDoSchema ;
bool gSchemaFullChecking ;
bool gDoCreate ;
std::bitset<32> StatusBits;
};
}
#endif

View File

@@ -75,43 +75,44 @@ using namespace Base;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class DOMTreeErrorReporter : public ErrorHandler
DOMTreeErrorReporter::DOMTreeErrorReporter():
fSawErrors(false) {
}
void DOMTreeErrorReporter::warning(const SAXParseException&)
{
public:
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
DOMTreeErrorReporter() :
fSawErrors(false) {
}
//
// Ignore all warnings.
//
}
~DOMTreeErrorReporter() override = default;
void DOMTreeErrorReporter::error(const SAXParseException& toCatch)
{
fSawErrors = true;
std::cerr << "Error at file \"" << StrX(toCatch.getSystemId())
<< "\", line " << toCatch.getLineNumber()
<< ", column " << toCatch.getColumnNumber()
<< "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
}
void DOMTreeErrorReporter::fatalError(const SAXParseException& toCatch)
{
fSawErrors = true;
std::cerr << "Fatal Error at file \"" << StrX(toCatch.getSystemId())
<< "\", line " << toCatch.getLineNumber()
<< ", column " << toCatch.getColumnNumber()
<< "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
}
// -----------------------------------------------------------------------
// Implementation of the error handler interface
// -----------------------------------------------------------------------
void warning(const SAXParseException& toCatch) override;
void error(const SAXParseException& toCatch) override;
void fatalError(const SAXParseException& toCatch) override;
void resetErrors() override;
// -----------------------------------------------------------------------
// Getter methods
// -----------------------------------------------------------------------
bool getSawErrors() const;
// -----------------------------------------------------------------------
// Private data members
//
// fSawErrors
// This is set if we get any errors, and is queryable via a getter
// method. Its used by the main code to suppress output if there are
// errors.
// -----------------------------------------------------------------------
bool fSawErrors;
};
void DOMTreeErrorReporter::resetErrors()
{
// No-op in this case
}
inline bool DOMTreeErrorReporter::getSawErrors() const
{
return fSawErrors;
}
class DOMPrintFilter : public DOMLSSerializerFilter
{
@@ -157,14 +158,6 @@ private :
void operator=(const DOMErrorHandler&);
};
inline bool DOMTreeErrorReporter::getSawErrors() const
{
return fSawErrors;
}
//**************************************************************************
//**************************************************************************
// ParameterManager
@@ -1865,42 +1858,6 @@ void ParameterManager::CheckDocument() const
}
//**************************************************************************
//**************************************************************************
// DOMTreeErrorReporter
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void DOMTreeErrorReporter::warning(const SAXParseException&)
{
//
// Ignore all warnings.
//
}
void DOMTreeErrorReporter::error(const SAXParseException& toCatch)
{
fSawErrors = true;
std::cerr << "Error at file \"" << StrX(toCatch.getSystemId())
<< "\", line " << toCatch.getLineNumber()
<< ", column " << toCatch.getColumnNumber()
<< "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
}
void DOMTreeErrorReporter::fatalError(const SAXParseException& toCatch)
{
fSawErrors = true;
std::cerr << "Fatal Error at file \"" << StrX(toCatch.getSystemId())
<< "\", line " << toCatch.getLineNumber()
<< ", column " << toCatch.getColumnNumber()
<< "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
}
void DOMTreeErrorReporter::resetErrors()
{
// No-op in this case
}
//**************************************************************************
//**************************************************************************
// DOMPrintFilter

View File

@@ -65,6 +65,10 @@ using PyObject = struct _object;
# pragma warning( disable : 4275 )
#endif
#ifndef _PreComp_
# include <xercesc/sax/ErrorHandler.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
class DOMNode;
@@ -279,6 +283,7 @@ protected:
~ParameterGrp() override;
/// helper function for GetGroup
Base::Reference<ParameterGrp> _GetGroup(const char* Name);
bool ShouldRemove() const;
void _Reset();
@@ -411,16 +416,17 @@ public:
/// Saves an XML document by calling the serializer's save method.
void SaveDocument() const;
//@}
private:
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *_pDocument;
ParameterSerializer * paramSerializer;
bool gDoNamespaces ;
protected:
bool gDoNamespaces ;
bool gDoSchema ;
bool gSchemaFullChecking ;
bool gDoCreate ;
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *_pDocument;
private:
ParameterManager();
~ParameterManager() override;
ParameterSerializer * paramSerializer;
const XMLCh* gOutputEncoding ;
@@ -431,11 +437,42 @@ private:
bool gUseFilter ;
bool gFormatPrettyPrint ;
private:
ParameterManager();
~ParameterManager() override;
};
XERCES_CPP_NAMESPACE_USE
class DOMTreeErrorReporter : public ErrorHandler
{
public:
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
DOMTreeErrorReporter();
~DOMTreeErrorReporter() override = default;
// -----------------------------------------------------------------------
// Implementation of the error handler interface
// -----------------------------------------------------------------------
void warning(const SAXParseException& toCatch) override;
void error(const SAXParseException& toCatch) override;
void fatalError(const SAXParseException& toCatch) override;
void resetErrors() override;
// -----------------------------------------------------------------------
// Getter methods
// -----------------------------------------------------------------------
bool getSawErrors() const;
private:
// -----------------------------------------------------------------------
// Private data members
//
// fSawErrors
// This is set if we get any errors, and is queryable via a getter
// method. Its used by the main code to suppress output if there are
// errors.
// -----------------------------------------------------------------------
bool fSawErrors;
};
/** python wrapper function
*/
BaseExport PyObject* GetPyObject( const Base::Reference<ParameterGrp> &hcParamGrp);

View File

@@ -61,6 +61,12 @@ void Persistence::Save (Writer &/*writer*/) const
assert(0);
}
void Persistence::Restore(DocumentReader &/*reader*/)
{
// you have to implement this method in all descending classes!
assert(0);
}
void Persistence::Restore(XMLReader &/*reader*/)
{
// you have to implement this method in all descending classes!

View File

@@ -31,6 +31,7 @@ namespace Base
class Reader;
class Writer;
class XMLReader;
class DocumentReader;
/// Persistence class and root of the type system
class BaseExport Persistence : public BaseClass
@@ -77,6 +78,7 @@ public:
* \endcode
*/
virtual void Restore(XMLReader &/*reader*/) = 0;
virtual void Restore(DocumentReader &/*reader*/);
/** This method is used to save large amounts of data to a binary file.
* Sometimes it makes no sense to write property data as XML. In case the
* amount of data is too big or the data type has a more effective way to

View File

@@ -25,6 +25,7 @@
#ifndef _PreComp_
# include <xercesc/sax2/XMLReaderFactory.hpp>
# include <xercesc/dom/DOM.hpp>
#endif
#include <locale>
@@ -42,7 +43,7 @@
#include <zipios++/zipios-config.h>
#endif
#include <zipios++/zipinputstream.h>
#include <Gui/Document.h>
XERCES_CPP_NAMESPACE_USE
@@ -330,24 +331,11 @@ void Base::XMLReader::readFiles(zipios::ZipInputStream &zipstream) const
// no file name for the current entry in the zip was registered.
if (jt != FileList.end()) {
try {
Base::Reader reader(zipstream, jt->FileName, FileVersion);
try{
jt->Object->RestoreDocFile(reader);
}catch (const Base::XMLParseException& e) {
//For some reason catching this error in RestoreDocFile(reader) its working but
//still need to catch it here again. Im not sure how its that possible since its from a constructor.
//It comes from trying to read "ProjectUnitSystem" from GuiDocument.xml and reaching EndDocument
//because was not found.
//I dont think EndDocument should throw error anyway.
if(e.getMessage() != "End of document reached"){
throw;
}
}
if (reader.getLocalReader())
reader.getLocalReader()->readFiles(zipstream);
Base::Reader reader(zipstream, jt->FileName, FileVersion);
jt->Object->RestoreDocFile(reader);
if (reader.getLocalReader())
reader.getLocalReader()->readFiles(zipstream);
}catch(...) {
// For any exception we just continue with the next file.
// It doesn't matter if the last reader has read more or